diff blast.py @ 8:de11e1a921c4 draft

Uploaded v0.0.18, tweak display_data for running tests
author peterjc
date Tue, 21 Jan 2014 13:33:20 -0500
parents a04cf51612f1
children 2bda64d39931
line wrap: on
line diff
--- a/blast.py	Tue Jan 21 13:32:49 2014 -0500
+++ b/blast.py	Tue Jan 21 13:33:20 2014 -0500
@@ -7,6 +7,11 @@
 from galaxy.datatypes.xml import GenericXml
 from galaxy.datatypes.metadata import MetadataElement
 
+from time import sleep
+import os
+import logging
+
+log = logging.getLogger(__name__)
 
 class BlastXml( GenericXml ):
     """NCBI Blast XML Output data"""
@@ -64,13 +69,26 @@
         out = open(output_file, "w")
         h = None
         for f in split_files:
+            if not os.path.isfile(f):
+                log.warning("BLAST XML file %s missing, retry in 1s..." % f)
+                sleep(1)
+            if not os.path.isfile(f):
+                log.error("BLAST XML file %s missing" % f)
+                raise ValueError("BLAST XML file %s missing" % f)
             h = open(f)
             body = False
             header = h.readline()
             if not header:
                 out.close()
                 h.close()
-                raise ValueError("BLAST XML file %s was empty" % f)
+                #Retry, could be transient error with networked file system...
+                log.warning("BLAST XML file %s empty, retry in 1s..." % f)
+                sleep(1)
+                h = open(f)
+                header = h.readline()
+                if not header:
+                    log.error("BLAST XML file %s was empty" % f)
+                    raise ValueError("BLAST XML file %s was empty" % f)
             if header.strip() != '<?xml version="1.0"?>':
                 out.write(header) #for diagnosis
                 out.close()
@@ -150,15 +168,34 @@
 
     def display_data(self, trans, data, preview=False, filename=None,
                      to_ext=None, size=None, offset=None, **kwd):
-        """Apparently an old display method, but still gets called.
+        """Documented as an old display method, but still gets called via tests etc
 
         This allows us to format the data shown in the central pane via the "eye" icon.
         """
-        return "This is a BLAST database."
-
-    def get_mime(self):
-        """Returns the mime type of the datatype (pretend it is text for peek)"""
-        return 'text/plain'
+        if filename is not None and filename != "index":
+            #Change nothing - important for the unit tests to access child files:
+            return Data.display_data(self, trans, data, preview, filename,
+                                     to_ext, size, offset, **kwd)
+        if self.file_ext == "blastdbn":
+            title = "This is a nucleotide BLAST database"
+        elif self.file_ext =="blastdbp":
+            title = "This is a protein BLAST database"
+        else:
+            #Error?                                                                                                                                                                     
+            title = "This is a BLAST database."
+        msg = ""
+        try:
+            #Try to use any text recorded in the dummy index file:
+            handle = open(data.file_name, "rU")
+            msg = handle.read().strip()
+            handle.close()
+        except Exception, err:
+            #msg = str(err)
+            pass
+        if not msg:
+            msg = title
+        #Galaxy assumes HTML for the display of composite datatypes,
+        return "<html><head><title>%s</title></head><body><pre>%s</pre></body></html>" % (title, msg)
 
     def merge(split_files, output_file):
         """Merge BLAST databases (not implemented for now)."""
@@ -174,7 +211,8 @@
 class BlastNucDb( _BlastDb, Data ):
     """Class for nucleotide BLAST database files."""
     file_ext = 'blastdbn'
-    composite_type ='basic'
+    allow_datatype_change = False
+    composite_type = 'basic'
 
     def __init__(self, **kwd):
         Data.__init__(self, **kwd)
@@ -197,19 +235,12 @@
 #        self.add_composite_file('blastdb.nac', is_binary=True, optional=True) # multiple byte order for a WriteDB column
 # The previous 3 lines should be repeated for each WriteDB column, with filename extensions like ('.nba', '.nbb', '.nbc'), ('.nca', '.ncb', '.ncc'), etc.
 
-    def display_data(self, trans, data, preview=False, filename=None,
-                     to_ext=None, size=None, offset=None, **kwd):
-        """Apparently an old display method, but still gets called.
-
-        This allows us to format the data shown in the central pane via the "eye" icon.
-        """
-        return "This is a BLAST nucleotide database."
-
 
 class BlastProtDb( _BlastDb, Data ):
     """Class for protein BLAST database files."""
     file_ext = 'blastdbp'
-    composite_type ='basic'
+    allow_datatype_change = False
+    composite_type = 'basic'
 
     def __init__(self, **kwd):
         Data.__init__(self, **kwd)
@@ -228,11 +259,3 @@
 #        self.add_composite_file('blastdb.pab', is_binary=True, optional=True)
 #        self.add_composite_file('blastdb.pac', is_binary=True, optional=True)
 # The last 3 lines should be repeated for each WriteDB column, with filename extensions like ('.pba', '.pbb', '.pbc'), ('.pca', '.pcb', '.pcc'), etc.
-
-    def display_data(self, trans, data, preview=False, filename=None,
-                     to_ext=None, size=None, offset=None, **kwd):
-        """Apparently an old display method, but still gets called.
-
-        This allows us to format the data shown in the central pane via the "eye" icon.
-        """
-        return "This is a BLAST protein database."