/tools/expression/go_analysis_code.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 130 lines · 105 code · 10 blank · 15 comment · 17 complexity · fcae4b4ba932daa2ec251af1650b84b4 MD5 · raw file

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