/tools/expression/eiplot_code.py

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