def alignSeqs(seqs,*rest): distXY,X,Y=alignSeq(seqs[0],seqs[1],*rest) return (distXY,[X,Y])
def formatalign2D(alignment,seq=None): "Formats the alignment for human use" if not alignment: return "No alignment\n" outStrIO=StringIO() try: from editdist import alignSeq except ImportError: print "Using subb alignment" def alignSeq(xseq,yseq,*rest): "Stubb for alignment. Does nothing really" l=max(len(xseq),len(yseq)) return (0,xseq.ljust(l).replace(" ","-"),yseq.rjust(l).replace(" ","-")) def formatAlnSeq(xaln,yaln,xname,yname,xstart,ystart,motifAln="",linelen=60): """Formats the DNA sequence alignment output""" outstr="Sequence 1: %s\nSequence 2: %s\n\n"%(xname,yname) n=len(xaln) xpos,ypos=xstart+1,ystart+1 for i in range(0,n,linelen): xline,yline,mline=xaln[i:i+linelen],yaln[i:i+linelen],motifAln[i:i+linelen] outstr+="%7d : %s\n%7d : %s\n %s\n\n"%(xpos,xline,ypos,yline,mline) xpos+=len(xline)-xline.count("-") ypos+=len(yline)-yline.count("-") return outstr outStrIO.write("### lambda=%g mu=%g nu=%g xi=%g Nucleotides per rotation=%g\n"%(alignment.Lambda,alignment.Mu,alignment.Nu,alignment.Xi,alignment.nuc_per_rotation)) outStrIO.write("### D[%s][%s]\nNote! First nucleotide at position 1 (one) and binding site at zero!\n"%(alignment.x_name,alignment.y_name)) xname,yname=alignment.x_name,alignment.y_name xe,ye=0,0 if seq and not (seq.has_key(xname) and seq.has_key(yname)): seq=None if seq: outStrIO.write("Sequence %s:\n%s\n\nSequence %s:\n%s\n"%(xname,seq.describe(xname),yname,seq.describe(yname))) # goodAlign= [ (x,y,Score,Motif,(startX,endX),(startY,endY),Strand) ] for i,goodAlign in zip(range(1,len(alignment.bestAlignments)+1),alignment.bestAlignments): if len(goodAlign)==0: continue if seq: xstart=max(goodAlign[0].beginX-10,0) ystart=max(goodAlign[0].beginY-10,0) xend=min(goodAlign[-1].endX+10,len(seq[xname])) yend=min(goodAlign[-1].endY+10,len(seq[yname])) xseq=seq[xname][xstart:xend] yseq=seq[yname][ystart:yend] xaln="" yaln="" maln="" xadded=xstart yadded=ystart outStrIO.write("\n### Alignment No %d ###\n"%(i,)) #for (x,y,score,motif,xcoord,ycoord,strand) in goodAlign: for alns in goodAlign: outStrIO.write("D[%d][%d]=%.2f %s (%d,%d) <=> (%d,%d) %s\n"%(alns.seqX,alns.seqY,alns.score,alns.motif,alns.beginX,alns.endX,alns.beginY,alns.endY,alns.strand)) if seq: y2add=yseq[yadded-ystart:alns.beginY-1-ystart].lower() x2add=xseq[xadded-xstart:alns.beginX-1-xstart].lower() #alnFmt="%%s%%-%ds%%s"%(max(len(y2add),len(x2add))) #alnFmt="%s%s%s" siteLen=alns.endX-alns.beginX+1 alnFmt="%%s%%s%%-%ds"%(siteLen) assert(len(xseq[alns.beginX-1-xstart:alns.endX-xstart])==siteLen) distYX,y2add,x2add=alignSeq(y2add,x2add,1,1) yaln=alnFmt%(yaln,y2add,yseq[alns.beginY-1-ystart:alns.endY-ystart].upper()) xaln=alnFmt%(xaln,x2add,xseq[alns.beginX-1-xstart:alns.endX-xstart].upper()) maln=alnFmt%(maln," "*len(y2add),alns.motif[-siteLen:]) xadded,yadded=alns.endX,alns.endY if seq: distYX,y2add,x2add=alignSeq(yseq[yadded-ystart:yend-ystart].lower(),xseq[xadded-xstart:xend-xstart].lower()) yaln+=y2add xaln+=x2add outStrIO.write("\n"+formatAlnSeq(xaln.replace(" ","-"),yaln.replace(" ","-"),xname,yname,xstart,ystart,maln)) #outstr+="%s\n%s\n"%(xaln.replace(" ","-"),yaln.replace(" ","-")) outStrIO.write("### Alignment took %.1f CPU seconds.\n"%(alignment.secs_to_align)) return outStrIO.getvalue()