def __add__(self,other):
        """Emulate __add__ method"""
        
        
        selfchroms=self.get_chroms()
        otherchroms=other.get_chroms()
        chroms=list(set(selfchroms).union(otherchroms))
        chroms.sort()
        addwig=Wig()
        
        for chrom in chroms:
        
            # if one of them do not have this chromosome, just simply copy the other's
            if chrom in selfchroms and chrom not in otherchroms:
                addwig.wig[chrom]=self.wig[chrom][:]
                continue
            elif chrom not in selfchroms and chrom in otherchroms:
                addwig.wig[chrom]=other.wig[chrom][:]
                continue
            
            END1=False
            END2=False
            # p--index of f, m--index of f2
            p=0
            m=0
            # signal processing to reduce the effect of unbalanced tag distribution between + and - strands.
            selflen=len(self.wig[chrom][0])
            otherlen=len(other.wig[chrom][0])
            if selflen==0 and otherlen==0:
                END1==True
                END2==True
            
            addwig.wig[chrom]=[array('l',[]), array('d',[])]
            # signal processing loop.
            while END1!=True or END2!=True:      # keep running when the end of either strand is reached.
                if END1!=True and END2!=True:    # not the ends of P and M
                    if self.wig[chrom][0][p]==other.wig[chrom][0][m]:
                        addwig.add_line(chrom,[self.wig[chrom][0][p],self.wig[chrom][1][p]+other.wig[chrom][1][m]])
                        p+=1
                        m+=1
                    elif self.wig[chrom][0][p]<other.wig[chrom][0][m]:     
                        addwig.add_line(chrom,[self.wig[chrom][0][p],self.wig[chrom][1][p]])    
                        p+=1
                    else:
                        addwig.add_line(chrom,[other.wig[chrom][0][m],other.wig[chrom][1][m]])
                        m+=1
                elif END1!=True and END2==True:    # The end of M but not P
                    addwig.add_line(chrom,[self.wig[chrom][0][p],self.wig[chrom][1][p]])
                    p+=1
                elif END1==True and END2!=True:    # the end of P but not M
                    addwig.add_line(chrom,[other.wig[chrom][0][m],other.wig[chrom][1][m]])
                    m+=1
                
                #check the ends.
                if p==selflen: END1=True
                if m==otherlen: END2=True
                
        return addwig
dataset/ETHPy150Open taoliu/taolib/Assoc/inout.py/Wig.__add__/original