PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/rgenetics/listFiles.py

https://bitbucket.org/galaxy/galaxy-central/
Python | 227 lines | 191 code | 10 blank | 26 comment | 11 complexity | ff852a733df2e0a4f8be35f132adc89d MD5 | raw file
Possible License(s): CC-BY-3.0
  1. #Provides Upload tool with access to list of available files
  2. import glob,sys
  3. import galaxy.app as thisapp
  4. import galaxy.util
  5. from elementtree.ElementTree import XML
  6. librepos = '/usr/local/galaxy/data/rg'
  7. myrepos = '/home/rerla/galaxy'
  8. marchinirepos = '/usr/local/galaxy/data/rg/snptest'
  9. from galaxy.tools.parameters import DataToolParameter
  10. #Provides Upload tool with access to list of available builds
  11. builds = []
  12. #Read build names and keys from galaxy.util
  13. for dbkey, build_name in galaxy.util.dbnames:
  14. builds.append((build_name,dbkey,False))
  15. #Return available builds
  16. def get_available_builds(defval='hg18'):
  17. for i,x in enumerate(builds):
  18. if x[1] == defval:
  19. x = list(x)
  20. x[2] = True
  21. builds[i] = tuple(x)
  22. return builds
  23. def get_tabular_cols( input, outformat='gg' ):
  24. """numeric only other than rs for strict genome graphs
  25. otherwise tabular. Derived from galaxy tool source around August 2007 by Ross"""
  26. columns = []
  27. seenCnames = {}
  28. elems = []
  29. colnames = ['Col%d' % x for x in range(input.metadata.columns+1)]
  30. strict = (outformat=='gg')
  31. for i, line in enumerate( file ( input.file_name ) ):
  32. if line and not line.startswith( '#' ):
  33. line = line.rstrip('\r\n')
  34. elems = line.split( '\t' )
  35. """
  36. Strict gg note:
  37. Since this tool requires users to select only those columns
  38. that contain numerical values, we'll restrict the column select
  39. list appropriately other than the first column which must be a marker
  40. """
  41. if len(elems) > 0:
  42. for col in range(1, input.metadata.columns+1):
  43. isFloat = False # short circuit common result
  44. try:
  45. val = float(elems[col-1])
  46. isFloat = True
  47. except:
  48. val = elems[col-1]
  49. if val:
  50. if i == 0: # header row
  51. colnames[col] = val
  52. if isFloat or (not strict) or (col == 1): # all in if not GG
  53. option = colnames[col]
  54. if not seenCnames.get(option,None): # new
  55. columns.append((option,str(col),False))
  56. seenCnames[option] = option
  57. #print 'get_tab: %d=%s. Columns=%s' % (i,line,str(columns))
  58. if len(columns) > 0 and i > 10:
  59. """
  60. We have our select list built, so we can break out of the outer most for loop
  61. """
  62. break
  63. if i == 30:
  64. break # Hopefully we never get here...
  65. for option in range(min(5,len(columns))):
  66. (x,y,z) = columns[option]
  67. columns[option] = (x,y,True)
  68. return columns # sorted select options
  69. def get_marchini_dir():
  70. """return the filesystem directory for snptest style files"""
  71. return marchinirepos
  72. def get_lib_SNPTESTCaCofiles():
  73. """return a list of file names - without extensions - available for caco studies
  74. These have a common file name with both _1 and _2 suffixes"""
  75. d = get_marchini_dir()
  76. testsuffix = '.gen_1' # glob these
  77. flist = glob.glob('%s/*%s' % (d,testsuffix))
  78. flist = [x.split(testsuffix)[0] for x in flist] # leaves with a list of file set names
  79. if len(flist) > 0:
  80. dat = [(flist[0],flist[0],True),]
  81. dat += [(x,x,False) for x in flist[1:]]
  82. else:
  83. dat = [('No Marchini CaCo files found in %s - convert some using the Marchini converter tool' % d,'None',True),]
  84. return dat
  85. def getChropt():
  86. """return dynamic chromosome select options
  87. """
  88. c = ['X','Y']
  89. c += ['%d' % x for x in range(1,23)]
  90. dat = [(x,x,False) for x in c]
  91. x,y,z = dat[3]
  92. dat[3] = (x,y,True)
  93. return dat
  94. def get_phecols(fname=''):
  95. """ return a list of phenotype columns for a multi-select list
  96. prototype:
  97. foo = ('fake - not yet implemented','not implemented','False')
  98. dat = [foo for x in range(5)]
  99. return dat
  100. """
  101. try:
  102. header = file(fname,'r').next().split()
  103. except:
  104. return [('get_phecols unable to open file %s' % fname,'None',False),]
  105. dat = [(x,x,False) for x in header]
  106. return dat
  107. #Return various kinds of files
  108. def get_lib_pedfiles():
  109. dat = glob.glob('%s/ped/*.ped' % librepos)
  110. dat += glob.glob('%s/ped/*.ped' % myrepos)
  111. dat.sort()
  112. if len(dat) > 0:
  113. dat = [x.split('.ped')[0] for x in dat]
  114. dat = [(x,x,'True') for x in dat]
  115. else:
  116. dat = [('No ped files - add some to %s/ped or %s/ped' % (librepos,myrepos),'None',True),]
  117. return dat
  118. def get_lib_phefiles():
  119. ext = 'phe'
  120. dat = glob.glob('%s/pheno/*.%s' % (librepos,ext))
  121. dat += glob.glob('%s/pheno/*.%s' % (myrepos,ext))
  122. dat.sort()
  123. if len(dat) > 0:
  124. dat = [(x,x,'False') for x in dat]
  125. else:
  126. dat = [('No %s files - add some to %s/pheno or %s/pheno' % (ext,librepos,myrepos),'None',True),]
  127. return dat
  128. def get_lib_bedfiles():
  129. dat = glob.glob('%s/plinkbed/*.bed' % librepos)
  130. dat += glob.glob('%s/plinkbed/*.bed' % myrepos)
  131. dat.sort()
  132. if len(dat) > 0:
  133. dat = [x.split('.bed')[0] for x in dat]
  134. dat = [(x,x,False) for x in dat]
  135. else:
  136. dat = [('No bed files - Please import some to %s/plinkbed or %s/plinkbed' % (librepos,myrepos),'None',True),]
  137. return dat
  138. def get_lib_fbatfiles():
  139. dat = glob.glob('%s/plinkfbat/*.ped' % librepos)
  140. dat += glob.glob('%s/plinkfbat/*.ped' % myrepos)
  141. dat.sort()
  142. if len(dat) > 0:
  143. dat = [(x,x,False) for x in dat]
  144. else:
  145. dat = [('No fbat bed files - Please import some to %s/plinkfbat or %s/plinkfbat' % (librepos,myrepos),'None',True),]
  146. return dat
  147. def get_lib_mapfiles():
  148. dat = glob.glob('%s/ped/*.map' % librepos)
  149. dat += glob.glob('%s/ped/*.map' % myrepos)
  150. dat.sort()
  151. if len(dat) > 0:
  152. dat = [(x,x,False) for x in dat]
  153. else:
  154. dat = [('No map files - add some to %s/ped' % librepos,'None',True),]
  155. return dat
  156. def get_my_pedfiles():
  157. dat = glob.glob('%s/*.ped' % myrepos)
  158. if len(dat) > 0:
  159. dat = [(x,x,False) for x in dat]
  160. else:
  161. dat = [('No ped files - add some to %s' % librepos,'None',True),]
  162. return dat
  163. def get_my_mapfiles():
  164. dat = glob.glob('%s/*.map' % myrepos)
  165. if len(dat) > 0:
  166. dat = [(x,x,'True') for x in dat]
  167. else:
  168. dat = [('No ped files - add some to %s' % librepos,'None',True),]
  169. return dat
  170. def get_lib_xlsfiles():
  171. dat = glob.glob('%s/*.xls' % librepos)
  172. if len(dat) > 0:
  173. dat = [(x,x,False) for x in dat]
  174. else:
  175. dat = [('No ped files - add some to %s' % librepos,'None',True),]
  176. return dat
  177. def get_lib_htmlfiles():
  178. dat = glob.glob('%s/*.html' % librepos)
  179. if len(dat) > 0:
  180. dat = [(x,x,False) for x in dat]
  181. else:
  182. dat = [('No ped files - add some to %s' % librepos,'None',True),]
  183. return dat
  184. def get_my_xlsfiles():
  185. dat = glob.glob('%s/*.xls' % myrepos)
  186. if len(dat) > 0:
  187. dat = [(x,x,False) for x in dat]
  188. else:
  189. dat = [('No ped files - add some to %s' % librepos,'None',True),]
  190. return dat
  191. def get_my_htmlfiles():
  192. dat = glob.glob('%s/*.html' % myrepos)
  193. if len(dat) > 0:
  194. dat = [(x,x,False) for x in dat]
  195. else:
  196. dat = [('No ped files - add some to %s' % librepos,'None',True),]
  197. return dat