PageRenderTime 43ms CodeModel.GetById 15ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 1ms

/tools/expression/go_analysis_code.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 130 lines | 109 code | 10 blank | 11 comment | 22 complexity | fcae4b4ba932daa2ec251af1650b84b4 MD5 | raw file
  1#build list of available data
  2import string, os, sys, glob, shutil, re
  3import galaxy.util
  4from galaxy import datatypes, config
  5
  6from galaxy.model import Dataset
  7
  8states = Dataset.states
  9
 10repository = "/usr/local/galaxy/data/rg/library"
 11
 12def getSampleNames(sampleName):
 13    fileName = sampleName.file_name
 14    f = file(fileName, 'r')
 15    sampleData = f.readlines()
 16    f.close()
 17    header = string.strip(sampleData[0], '\n')
 18    i = 0
 19    samples = []
 20
 21    x_re = re.compile(r'" "')
 22    headerItems = x_re.split(header)
 23
 24    for (sample) in headerItems:
 25        sample = string.strip(sample, '"')
 26        i += 1
 27        samples.append((sample,str(i),False))
 28
 29    return samples
 30
 31def exec_after_process(app, inp_data, out_data, param_dict, tool, stdout, stderr):
 32    """tricky. We wrote
 33    outlist = ['%s\t%s\t%s' % (mng,newfiles[i],newnames[i]) for i in range(length(newfiles))]
 34    to the end of the log
 35    containing tab separated filepaths and types which we now need to create in
 36    the history
 37    This code was written for the security branch
 38    """
 39    mng = '### makenewgalaxy'
 40    tmpString = '### tmp'
 41    killme = string.punctuation + string.whitespace
 42    trantab = string.maketrans(killme,'_'*len(killme))
 43    job_name = param_dict.get( 'title', 'makeAffyBatch' ).translate(trantab)
 44    dbkey = param_dict.get('dbkey','hg18')
 45    if dbkey == "fly.db0.db":
 46        dbkey = "fly.db0"
 47        return 
 48    base_dataset = out_data.items()[0][1]
 49    history = base_dataset.history
 50    if history == None:
 51        print "unknown history!"
 52        return
 53    logpath = out_data['logmeta'].file_name
 54    loglist = file(logpath,'r').readlines()
 55    newfiles = [x for x in loglist if x.split('\t')[0] == mng]
 56    # parse out the encoded new datasets for galaxy
 57    newfiles = [x.strip().split('\t')[1:] for x in newfiles] # get rid of #ymakenewgalaxy
 58    for (file_path,file_name,file_type) in newfiles:
 59        # note, this gets created - pass extra args?
 60        #class DatasetInstance( object ):
 61        #    """A base class for all 'dataset instances', HDAs, LDAs, etc"""
 62        #    states = Dataset.states
 63        #    permitted_actions = Dataset.permitted_actions
 64        #    def __init__( self, id=None, hid=None, name=None, info=None, blurb=None, peek=None, extension=None,
 65        #                  dbkey=None, metadata=None, history=None, dataset=None, deleted=False, designation=None,
 66        #                  parent_id=None, validation_errors=None, visible=True, create_dataset = False ):
 67        file_path = file_path.strip()
 68        newname = file_name.strip()
 69        info = '%s, %s' % (job_name, newname)
 70        iphenofile = file_path
 71        pk = '' #file(iphenofile,'r').read(512)
 72        newdata = app.model.HistoryDatasetAssociation(extension=file_type,dbkey=dbkey,info=info,
 73            name=newname,peek = pk, create_dataset = True, sa_session = app.model.context)
 74        # as noted in encode_import_code.py on which this was based :)
 75        # This import should become a library
 76        #newdata.metadata.base_name = geoid
 77        efp = newdata.extra_files_path
 78        try:
 79            os.makedirs(efp)
 80        except:
 81            pass
 82        phenoname = os.path.split(iphenofile)[-1] # name
 83        iid = os.path.splitext(phenoname)[0]
 84        newppath = os.path.join(efp,phenoname)
 85        shutil.copy(iphenofile,newppath) # save pheno for metadata
 86        newdata.metadata.pheno_path = newppath
 87        newdata.metadata.base_name = iid
 88        #newdata.metadata.pheno='Name\tGroup\na.cel\t1\nb.cel\t1\nc.cel\t0\nd.cel\t0\n'
 89        try:
 90            app.security_agent.set_dataset_permissions( newdata.dataset, base_dataset.dataset.groups )
 91        except:
 92            pass # old pre-security?
 93        app.model.context.add(newdata)
 94        app.model.context.flush()
 95        try:
 96            shutil.copyfile(file_path,newdata.file_name)
 97            newdata.set_dataset_state ( states.OK )
 98        except:
 99            s = "The requested file %s is missing from the system." % file_path
100            lf = file(logpath,'a')
101            lf.write(s)
102            lf.write('\n')
103            lf.write('Trying to write to %s\n' % (newdata.file_name))
104            lf.close()
105            newdata.info = s
106            newdata.set_dataset_state ( states.ERROR )
107        newdata.dbkey = dbkey
108        newdata.set_peek()
109        newdata.set_meta() # must set peek first
110        lf = file(logpath,'a')
111        lf.write('## saving %s as %s\n' % (newname, newdata.file_name))
112        s = '# newdata %s peek = %s\n' % (newname,newdata.peek)
113        lf.write(s)
114        s = '# newdata %s metadata pheno_path = %s\n' % (newname,newdata.metadata.pheno_path)
115        lf.write(s)
116        lf.write('\n')
117        lf.close()
118        newdata.set_size()
119        history.add_dataset( newdata )
120        app.model.context.flush()
121
122    tmpDir = [x for x in loglist if x.split('\t')[0] == tmpString]
123    if len(tmpDir) > 0:
124        tmpDir = [x.strip().split('\t')[1:] for x in tmpDir]
125        if len(tmpDir) > 0:
126            for (tdir) in tmpDir[0]:
127                for (f) in os.listdir(tdir):
128                    os.unlink(tdir + '/' + f)
129                os.rmdir(tdir)
130