changeset 115:0c2a03f9740b

make external gene bank name configurable
author Jan Kanis <jan.code@jankanis.nl>
date Mon, 14 Jul 2014 15:01:32 +0200
parents 4f0ed3b5ae46
children f5066973029a
files blast2html.html.jinja blast2html.py blast2html.xml test-data/blast xml example4b.html test-data/blastdb.loc
diffstat 5 files changed, 41 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/blast2html.html.jinja	Wed Jul 09 15:20:38 2014 +0200
+++ b/blast2html.html.jinja	Mon Jul 14 15:01:32 2014 +0200
@@ -571,7 +571,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description{{hit|nodeid}}">Descriptions</a></div>
-                  {{ hit|genelink('Gene Bank', clas='linkheader', display_nolink=False) }}
+                  {{ hit|genelink(text_from='dbname', cssclass='linkheader', display_nolink=False) }}
                 </div>
 
                 <div class=title>
--- a/blast2html.py	Wed Jul 09 15:20:38 2014 +0200
+++ b/blast2html.py	Mon Jul 14 15:01:32 2014 +0200
@@ -15,7 +15,7 @@
 from six.moves import builtins
 from os import path
 from itertools import repeat
-from collections import defaultdict
+from collections import defaultdict, namedtuple
 import glob
 import argparse
 from lxml import objectify
@@ -238,9 +238,10 @@
 
     max_scale_labels = 10
 
-    def __init__(self, input, templatedir, templatename, genelinks={}):
+    def __init__(self, input, templatedir, templatename, dbname, genelinks={}):
         self.input = input
         self.templatename = templatename
+        self.dbname = dbname
         self.genelinks = genelinks
 
         self.blast = objectify.parse(self.input).getroot()
@@ -348,24 +349,32 @@
                        ident = "{0:.0%}".format(float(min(float(hsp.Hsp_identity) / blastxml_len(hsp) for hsp in hsps))))
 
     @filter
-    def genelink(self, hit, text=None, clas=None, display_nolink=True):
+    def genelink(self, hit, text=None, text_from='hitid', cssclass=None, display_nolink=True):
         """Create a html link from a hit node to a configured gene bank webpage.
-        text: The text of the link, defaults to the hit_id
-        clas: extra css classes that will be added to the <a> element
+        text: The text of the link. If not set applies text_from.
+        text_from: string, if text is not specified, take it from specified source. Either 'hitid' (default) or 'dbname'.
+        cssclass: extra css classes that will be added to the <a> element
         display_nolink: boolean, if false don't display anything if no link can be created. Default True.
         """
         
-        if text is None:
-            text = hitid(hit)
-
         db = hit.getroottree().getroot().BlastOutput_db
 
         if isinstance(self.genelinks, six.string_types):
             template = self.genelinks
         else:
-            template = self.genelinks.get(db)
+            template = self.genelinks[db].template
+
+        if text is None:
+            if text_from == 'hitid':
+                text = hitid(hit)
+            elif text_from == 'dbname':
+                text = self.dbname or self.genelinks[db].dbname or 'Gene Bank'
+            else:
+                raise ValueError("Unknown value for text_from: '{0}'. Use 'hitid' or 'dbname'.".format(text_from))
+
         if template is None:
             return text if display_nolink else ''
+
         args = dict(id=hitid(hit).split('|'),
                     fullid=hitid(hit),
                     defline=str(hit.Hit_def).split(' ', 1)[0].split('|'),
@@ -377,12 +386,13 @@
             warnings.warn('Error in formatting gene bank link {} with {}: {}'.format(template, args, e))
             return text if display_nolink else ''
 
-        classattr = 'class="{0}" '.format(jinja2.escape(clas)) if clas is not None else ''
+        classattr = 'class="{0}" '.format(jinja2.escape(cssclass)) if cssclass is not None else ''
         return jinja2.Markup("<a {0}href=\"{1}\">{2}</a>".format(classattr, jinja2.escape(link), jinja2.escape(text)))
 
 
+genelinks_entry = namedtuple('genelinks_entry', 'dbname template')
 def read_genelinks(dir):
-    links = {}
+    links = defaultdict(lambda: genelinks_entry(None, None))
     # blastdb.loc, blastdb_p.loc, blastdb_d.loc, etc.
     files = sorted(glob.glob(path.join(dir, 'blastdb*.loc')))
     # reversed, so blastdb.loc will take precedence
@@ -394,7 +404,7 @@
                     continue
                 line = l.rstrip('\n').split('\t')
                 try:
-                    links[line[2]] = line[3]
+                    links[line[2]] = genelinks_entry(dbname=line[3], template=line[4])
                 except IndexError:
                     continue
             f.close()
@@ -427,7 +437,9 @@
     # care too much.
     parser.add_argument('--template', type=argparse.FileType(mode='r'), default=default_template,
                         help='The template file to use. Defaults to blast_html.html.jinja')
-    
+
+    parser.add_argument('--dbname', type=str, default=None,
+                        help="The link text to use for external links to a gene bank database. Defaults to 'Gene Bank'")
     dblink_group = parser.add_mutually_exclusive_group()
     dblink_group.add_argument('--genelink-template', metavar='URL_TEMPLATE',
                               default='http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign',
@@ -438,7 +450,7 @@
 
                               The default is 'http://www.ncbi.nlm.nih.gov/nucleotide/{accession}?report=genbank&log$=nuclalign',
                               which is a link to the NCBI nucleotide database.""")
-    
+
     dblink_group.add_argument('--db-config-dir',
                               help="""The directory where databases are configured in blastdb*.loc files. These files
                               are consulted for creating a gene bank link. The files should be tab-separated tables (with lines
@@ -473,20 +485,20 @@
 
         args.output.close()
         args.output = io.open(args.output.name, 'w', encoding='utf-8')
-    
+
     templatedir, templatename = path.split(args.template.name)
     args.template.close()
     if not templatedir:
         templatedir = '.'
 
     if args.db_config_dir is None:
-        genelinks = args.genelink_template
+        genelinks = defaultdict(lambda: genelinks_entry(template=args.genelink_template, dbname=None))
     elif not path.isdir(args.db_config_dir):
         parser.error('db-config-dir does not exist or is not a directory')
     else:
         genelinks = read_genelinks(args.db_config_dir)
 
-    b = BlastVisualize(args.input, templatedir, templatename, genelinks)
+    b = BlastVisualize(args.input, templatedir, templatename, dbname=args.dbname, genelinks=genelinks)
     b.render(args.output)
     args.output.close()
 
--- a/blast2html.xml	Wed Jul 09 15:20:38 2014 +0200
+++ b/blast2html.xml	Mon Jul 14 15:01:32 2014 +0200
@@ -1,4 +1,4 @@
-<tool id="blast2html" name="blast2html" version="0.0.12">
+<tool id="blast2html" name="blast2html" version="0.0.13">
     
     <description>Convert BLAST XML to HTML</description>
     
--- a/test-data/blast xml example4b.html	Wed Jul 09 15:20:38 2014 +0200
+++ b/test-data/blast xml example4b.html	Mon Jul 14 15:01:32 2014 +0200
@@ -605,7 +605,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description1-1">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/AB209952.1/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/AB209952.1/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -647,7 +647,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description1-2">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/DJ437711/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/DJ437711/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -905,7 +905,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description3-1">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/AB209952.1/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/AB209952.1/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -947,7 +947,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description3-2">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/DJ437711/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/DJ437711/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -989,7 +989,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description3-3">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/AJ308515.1/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/AJ308515.1/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -1247,7 +1247,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description6-1">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/EUG/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/EUG/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -1289,7 +1289,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description6-2">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/AY326434/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/AY326434/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -1497,7 +1497,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description7-1">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/EUG/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/EUG/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
@@ -1542,7 +1542,7 @@
 
                 <div class=linkheader>
                   <div class=right><a href="#description7-2">Descriptions</a></div>
-                  <a class="linkheader" href="http://example.com/example-genebank/AY326434/">Gene Bank</a>
+                  <a class="linkheader" href="http://example.com/example-genebank/AY326434/">Example Gene Bank</a>
                 </div>
 
                 <div class=title>
--- a/test-data/blastdb.loc	Wed Jul 09 15:20:38 2014 +0200
+++ b/test-data/blastdb.loc	Mon Jul 14 15:01:32 2014 +0200
@@ -36,4 +36,4 @@
 #See also blastdb_p.loc which is for any protein BLAST database, and
 #blastdb_d.loc which is for any protein domains databases (like CDD).
 
-EUginius_plasmid	Euginius plasmid insert	/opt/galaxy/blastdbs/EUginius_plasmid_insert	http://example.com/example-genebank/{defline[0]}/
+EUginius_plasmid	Euginius plasmid insert	/opt/galaxy/blastdbs/EUginius_plasmid_insert	Example Gene Bank	http://example.com/example-genebank/{defline[0]}/