PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/expression/eiplot_code.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 128 lines | 107 code | 10 blank | 11 comment | 21 complexity | 51cc018fd0a59b84a5e5f87d3e640a91 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])
 18    i = 0
 19    samples = []
 20
 21    x_re = re.compile(r'\t')
 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    base_dataset = out_data.items()[0][1]
 46    history = base_dataset.history
 47    if history == None:
 48        print "unknown history!"
 49        return
 50    logpath = out_data['logmeta'].file_name
 51    loglist = file(logpath,'r').readlines()
 52    newfiles = [x for x in loglist if x.split('\t')[0] == mng]
 53    # parse out the encoded new datasets for galaxy
 54    newfiles = [x.strip().split('\t')[1:] for x in newfiles] # get rid of #ymakenewgalaxy
 55    for (file_path,file_name,file_type) in newfiles:
 56        # note, this gets created - pass extra args?
 57        #class DatasetInstance( object ):
 58        #    """A base class for all 'dataset instances', HDAs, LDAs, etc"""
 59        #    states = Dataset.states
 60        #    permitted_actions = Dataset.permitted_actions
 61        #    def __init__( self, id=None, hid=None, name=None, info=None, blurb=None, peek=None, extension=None,
 62        #                  dbkey=None, metadata=None, history=None, dataset=None, deleted=False, designation=None,
 63        #                  parent_id=None, validation_errors=None, visible=True, create_dataset = False ):
 64        file_path = file_path.strip()
 65        newname = file_name.strip()
 66        info = '%s, %s' % (job_name, newname)
 67        iphenofile = file_path
 68        pk = '' # file(iphenofile,'r').read()
 69        newdata = app.model.HistoryDatasetAssociation(extension=file_type,dbkey=dbkey,info=info,
 70            name=newname,peek = pk, create_dataset = True, sa_session = app.model.context)
 71        # as noted in encode_import_code.py on which this was based :)
 72        # This import should become a library
 73        #newdata.metadata.base_name = geoid
 74        efp = newdata.extra_files_path
 75        try:
 76            os.makedirs(efp)
 77        except:
 78            pass
 79        phenoname = os.path.split(iphenofile)[-1] # name
 80        iid = os.path.splitext(phenoname)[0]
 81        newppath = os.path.join(efp,phenoname)
 82        shutil.copy(iphenofile,newppath) # save pheno for metadata
 83        newdata.metadata.pheno_path = newppath
 84        newdata.metadata.base_name = iid
 85        #kevin
 86        #newdata.metadata.pheno='Name\tGroup\na.cel\t1\nb.cel\t1\nc.cel\t0\nd.cel\t0\n'
 87        try:
 88            app.security_agent.set_dataset_permissions( newdata.dataset, base_dataset.dataset.groups )
 89        except:
 90            pass # old pre-security?
 91        app.model.context.add(newdata)
 92        app.model.context.flush()
 93        try:
 94            shutil.copyfile(file_path,newdata.file_name)
 95            newdata.set_dataset_state ( states.OK )
 96        except:
 97            s = "The requested file %s is missing from the system." % file_path
 98            lf = file(logpath,'a')
 99            lf.write(s)
100            lf.write('\n')
101            lf.write('Trying to write to %s\n' % (newdata.file_name))
102            lf.close()
103            newdata.info = s
104            newdata.set_dataset_state ( states.ERROR )
105        newdata.dbkey = dbkey
106        newdata.set_peek()
107        newdata.set_meta() # must set peek first
108        lf = file(logpath,'a')
109        lf.write('## saving %s as %s\n' % (newname, newdata.file_name))
110        s = '# newdata %s peek = %s\n' % (newname,newdata.peek)
111        lf.write(s)
112        s = '# newdata %s metadata pheno_path = %s\n' % (newname,newdata.metadata.pheno_path)
113        lf.write(s)
114        lf.write('\n')
115        lf.close()
116        newdata.set_size()
117        history.add_dataset( newdata )
118        app.model.context.flush()
119
120    tmpDir = [x for x in loglist if x.split('\t')[0] == tmpString]
121    if len(tmpDir) > 0:
122        tmpDir = [x.strip().split('\t')[1:] for x in tmpDir]
123        if len(tmpDir) > 0:
124            for (tdir) in tmpDir[0]:
125                for (f) in os.listdir(tdir):
126                    os.unlink(tdir + '/' + f)
127                os.rmdir(tdir)
128