view homerDatatypes.py @ 2:7268cbc535e5 draft

findpeaks and maketagdirectory
author kevyin
date Fri, 23 Nov 2012 00:30:27 -0500
parents
children
line wrap: on
line source

"""
homer composite datatypes 
Use at your peril
Kevin Ying
for the homer wrapper

Nov 2012
"""

import logging, os, sys, time, tempfile, shutil, string, glob
import data
from galaxy import util
from cgi import escape
import urllib, binascii
from galaxy.web import url_for
from galaxy.datatypes import metadata
from galaxy.datatypes.metadata import MetadataElement
from galaxy.datatypes.data import Text
from galaxy.datatypes.tabular import Tabular
from galaxy.datatypes.images import Html
from galaxy.datatypes.interval import Interval
from galaxy.util.hash_util import *

gal_Log = logging.getLogger(__name__)
verbose = False

class HomerTagDirectory(Html):
    """
    base class to use for homer tag directory datatypes
    composite datatype elements
    stored in extra files path
    """
   
    #MetadataElement( name="base_name", desc="base name for all transformed versions of this genetic dataset", default='HomerTagDirectory',
    #readonly=True, set_in_upload=False)
    
    composite_type = 'auto_primary_file'
    #allow_datatype_change = False
    file_ext = 'homerTagDirectory'

    def __init__(self, **kwd):
        Html.__init__(self, **kwd)
        self.add_composite_file('tagInfo.txt', description="tagInfo")
        self.add_composite_file('tagAutocorrelation.txt', description="tagAutocorrelation")
        self.add_composite_file('tagCountDistribution.txt', description="tagCountDistribution")
        self.add_composite_file('tagLengthDistribution.txt', description="tagLengthDistribution")

    def generate_primary_file(self, dataset=None):
        rval = ['<html><head><title>Homer Tag Directory Galaxy Composite Dataset </title></head><p/>']
        rval.append('<div>This composite dataset is composed of the following files:<p/><ul>')
        for composite_name, composite_file in self.get_composite_files( dataset = dataset ).iteritems():
            fn = composite_name
            opt_text = ''
            if composite_file.optional:
                opt_text = ' (optional)'
            if composite_file.get('description'):
                rval.append( '<li><a href="%s" type="application/binary">%s (%s)</a>%s</li>' % ( fn, fn, composite_file.get('description'), opt_text ) )
            else:
                rval.append( '<li><a href="%s" type="application/binary">%s</a>%s</li>' % ( fn, fn, opt_text ) )
        rval.append( '</ul></div></html>' )
        return "\n".join( rval )


    def regenerate_primary_file(self,dataset):
        """
        cannot do this until we are setting metadata 
        """
        #bn = dataset.metadata.base_name
        efp = dataset.extra_files_path
        flist = os.listdir(efp)
        rval = ['<html><head><title>Files for Composite Dataset %s</title></head><body><p/>Composite %s contains:<p/><ul>' % (dataset.name,dataset.name)]
        for i,fname in enumerate(flist):
            sfname = os.path.split(fname)[-1] 
            f,e = os.path.splitext(fname)
            rval.append( '<li><a href="%s">%s</a></li>' % ( sfname, sfname) )
        rval.append( '</ul></body></html>' )
        f = file(dataset.file_name,'w')
        f.write("\n".join( rval ))
        f.write('\n')
        f.close()

    def get_mime(self):
        """Returns the mime type of the datatype"""
        return 'text/html'

    def set_meta( self, dataset, **kwd ):

        """
        for homer maketagdirectory eg

        """
        Html.set_meta( self, dataset, **kwd )
        if kwd.get('overwrite') == False:
            if verbose:
                gal_Log.debug('@@@ HomerTagDirectory set_meta called with overwrite = False')
            return True
        try:
            efp = dataset.extra_files_path
        except: 
            if verbose:                
               gal_Log.debug('@@@ HomerTagDirectory set_meta failed %s - dataset %s has no efp ?' % (sys.exc_info()[0], dataset.name))
            return False
        try:
            flist = os.listdir(efp)
        except:
            if verbose: gal_Log.debug('@@@ HomerTagDirectory set_meta failed %s - dataset %s has no efp ?' % (sys.exc_info()[0],dataset.name))
            return False
        if len(flist) == 0:
            if verbose:
                gal_Log.debug('@@@ HomerTagDirectory set_meta failed - %s efp %s is empty?' % (dataset.name,efp))
            return False
        self.regenerate_primary_file(dataset)
        if not dataset.info:           
                dataset.info = 'Galaxy HomerTagDirectory datatype object'
        if not dataset.blurb:
               dataset.blurb = 'Composite file - Homer Galaxy toolkit'
        return True