comparison blast2html.py @ 96:02b795b784e1

fix bug; add comments
author Jan Kanis <jan.code@jankanis.nl>
date Mon, 30 Jun 2014 17:31:10 +0200
parents 4378d11f0ed7
children e780606b7c25
comparison
equal deleted inserted replaced
95:4378d11f0ed7 96:02b795b784e1
68 s += '0' 68 s += '0'
69 return s 69 return s
70 70
71 @filter 71 @filter
72 def firsttitle(hit): 72 def firsttitle(hit):
73 return hit.Hit_def.text.split('>')[0] 73 return str(hit.Hit_def).split('>')[0]
74 74
75 @filter 75 @filter
76 def othertitles(hit): 76 def othertitles(hit):
77 """Split a hit.Hit_def that contains multiple titles up, splitting out the hit ids from the titles.""" 77 """Split a hit.Hit_def that contains multiple titles up, splitting out the hit ids from the titles."""
78 id_titles = hit.Hit_def.text.split('>') 78 id_titles = str(hit.Hit_def).split('>')
79 79
80 titles = [] 80 titles = []
81 for t in id_titles[1:]: 81 for t in id_titles[1:]:
82 id, title = t.split(' ', 1) 82 id, title = t.split(' ', 1)
83 titles.append(argparse.Namespace(Hit_id = id, 83 titles.append(argparse.Namespace(Hit_id = id,
344 # float(...) because non-flooring division doesn't work with lxml elements in python 2.6 344 # float(...) because non-flooring division doesn't work with lxml elements in python 2.6
345 ident = "{0:.0%}".format(float(min(float(hsp.Hsp_identity) / blastxml_len(hsp) for hsp in hsps)))) 345 ident = "{0:.0%}".format(float(min(float(hsp.Hsp_identity) / blastxml_len(hsp) for hsp in hsps))))
346 346
347 @filter 347 @filter
348 def genelink(self, hit, text=None, clas=None, display_nolink=True): 348 def genelink(self, hit, text=None, clas=None, display_nolink=True):
349 """Create a html link from a hit node to a configured gene bank webpage.
350 text: The text of the link, defaults to the hit_id
351 clas: extra css classes that will be added to the <a> element
352 display_nolink: boolean, if false don't display anything if no link can be created. Default True.
353 """
354
349 if text is None: 355 if text is None:
350 text = hitid(hit) 356 text = hitid(hit)
357
351 db = hit.getroottree().getroot().BlastOutput_db 358 db = hit.getroottree().getroot().BlastOutput_db
359
352 if isinstance(self.genelinks, six.string_types): 360 if isinstance(self.genelinks, six.string_types):
353 template = self.genelinks 361 template = self.genelinks
354 else: 362 else:
355 template = self.genelinks.get(db) 363 template = self.genelinks.get(db)
356 if template is None: 364 if template is None:
363 try: 371 try:
364 link = template.format(**args) 372 link = template.format(**args)
365 except Exception as e: 373 except Exception as e:
366 warnings.warn('Error in formatting gene bank link {} with {}: {}'.format(template, args, e)) 374 warnings.warn('Error in formatting gene bank link {} with {}: {}'.format(template, args, e))
367 return text if display_nolink else '' 375 return text if display_nolink else ''
376
368 classattr = 'class="{}" '.format(jinja2.escape(clas)) if clas is not None else '' 377 classattr = 'class="{}" '.format(jinja2.escape(clas)) if clas is not None else ''
369 return jinja2.Markup("<a {}href=\"{}\">{}</a>".format(classattr, jinja2.escape(link), jinja2.escape(text))) 378 return jinja2.Markup("<a {}href=\"{}\">{}</a>".format(classattr, jinja2.escape(link), jinja2.escape(text)))
370 379
371 380
372 def read_genelinks(dir): 381 def read_genelinks(dir):