comparison blast2html.py @ 71:371cd585e459

refactor
author Jan Kanis <jan.code@jankanis.nl>
date Wed, 18 Jun 2014 14:23:15 +0200
parents 0c4ac210068b
children 6ecbfebb9dd9
comparison
equal deleted inserted replaced
70:0ef071bba164 71:371cd585e459
82 82
83 83
84 @filter 84 @filter
85 def alignment_pre(hsp): 85 def alignment_pre(hsp):
86 """Create the preformatted alignment blocks""" 86 """Create the preformatted alignment blocks"""
87 87
88 step = 60 88 # line break length
89 linewidth = 60
89 90
90 qfrom = int(hsp['Hsp_query-from']) 91 qfrom = int(hsp['Hsp_query-from'])
91 qto = int(hsp['Hsp_query-to']) 92 qto = int(hsp['Hsp_query-to'])
92 qframe = int(hsp['Hsp_query-frame']) 93 qframe = int(hsp['Hsp_query-frame'])
93 hfrom = int(hsp['Hsp_hit-from']) 94 hfrom = int(hsp['Hsp_hit-from'])
94 hto = int(hsp['Hsp_hit-to']) 95 hto = int(hsp['Hsp_hit-to'])
95 hframe = int(hsp['Hsp_hit-frame']) 96 hframe = int(hsp['Hsp_hit-frame'])
97
96 qseq = hsp.Hsp_qseq.text 98 qseq = hsp.Hsp_qseq.text
97 midline = hsp.Hsp_midline.text 99 midline = hsp.Hsp_midline.text
98 hseq = hsp.Hsp_hseq.text 100 hseq = hsp.Hsp_hseq.text
99 101
100 if not qframe in [1, -1]: 102 if not qframe in (1, -1):
101 warnings.warn("Error in BlastXML input: Hsp node {} has a Hsp_query-frame of {}".format(nodeid(hsp), qframe)) 103 warnings.warn("Error in BlastXML input: Hsp node {} has a Hsp_query-frame of {}. (should be 1 or -1)".format(nodeid(hsp), qframe))
102 qframe = -1 if qframe < 0 else 1 104 qframe = -1 if qframe < 0 else 1
103 if not hframe in [1, -1]: 105 if not hframe in (1, -1):
104 warnings.warn("Error in BlastXML input: Hsp node {} has a Hsp_hit-frame of {}".format(nodeid(hsp), hframe)) 106 warnings.warn("Error in BlastXML input: Hsp node {} has a Hsp_hit-frame of {}. (should be 1 or -1)".format(nodeid(hsp), hframe))
105 hframe = -1 if hframe < 0 else 1 107 hframe = -1 if hframe < 0 else 1
106 108
107 def split(txt): 109 def split(txt):
108 return [txt[i:i+step] for i in range(0, len(txt), step)] 110 return [txt[i:i+linewidth] for i in range(0, len(txt), linewidth)]
109 111
110 for qs, mid, hs, offset in zip(split(qseq), split(midline), split(hseq), range(0, len(qseq), step)): 112 for qs, mid, hs, offset in zip(split(qseq), split(midline), split(hseq), range(0, len(qseq), linewidth)):
111 yield ( 113 yield (
112 "Query {:>7} {} {}\n".format(qfrom+offset*qframe, qs, qfrom+(offset+len(qs)-1)*qframe) + 114 "Query {:>7} {} {}\n".format(qfrom+offset*qframe, qs, qfrom+(offset+len(qs)-1)*qframe) +
113 " {:7} {}\n".format('', mid) + 115 " {:7} {}\n".format('', mid) +
114 "Subject{:>7} {} {}".format(hfrom+offset*hframe, hs, hfrom+(offset+len(hs)-1)*hframe) 116 "Subject{:>7} {} {}".format(hfrom+offset*hframe, hs, hfrom+(offset+len(hs)-1)*hframe)
115 ) 117 )