PageRenderTime 10ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/datatypes/converters/lped_to_pbed_converter.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 110 lines | 101 code | 3 blank | 6 comment | 0 complexity | 0dae61f8006158e0e806407861418d15 MD5 | raw file
  1. # for rgenetics - lped to pbed
  2. # where to stop with converters
  3. # pbed might be central
  4. # eg lped/eigen/fbat/snpmatrix all to pbed
  5. # and pbed to lped/eigen/fbat/snpmatrix ?
  6. # that's a lot of converters
  7. import sys,os,time,subprocess
  8. prog = os.path.split(sys.argv[0])[-1]
  9. myversion = 'Oct 10 2009'
  10. galhtmlprefix = """<?xml version="1.0" encoding="utf-8" ?>
  11. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  12. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  13. <head>
  14. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  15. <meta name="generator" content="Galaxy %s tool output - see http://getgalaxy.org" />
  16. <title></title>
  17. <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
  18. </head>
  19. <body>
  20. <div class="document">
  21. """
  22. def timenow():
  23. """return current time as a string
  24. """
  25. return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
  26. def getMissval(inped=''):
  27. """
  28. read some lines...ugly hack - try to guess missing value
  29. should be N or 0 but might be . or -
  30. """
  31. commonmissvals = {'N':'N','0':'0','n':'n','9':'9','-':'-','.':'.'}
  32. try:
  33. f = file(inped,'r')
  34. except:
  35. return None # signal no in file
  36. missval = None
  37. while missval == None: # doggedly continue until we solve the mystery
  38. try:
  39. l = f.readline()
  40. except:
  41. break
  42. ll = l.split()[6:] # ignore pedigree stuff
  43. for c in ll:
  44. if commonmissvals.get(c,None):
  45. missval = c
  46. f.close()
  47. return missval
  48. if not missval:
  49. missval = 'N' # punt
  50. close(f)
  51. return missval
  52. def rgConv(inpedfilepath,outhtmlname,outfilepath,plink):
  53. """
  54. """
  55. pedf = '%s.ped' % inpedfilepath
  56. basename = os.path.split(inpedfilepath)[-1] # get basename
  57. outroot = os.path.join(outfilepath,basename)
  58. missval = getMissval(inped = pedf)
  59. if not missval:
  60. print '### lped_to_pbed_converter.py cannot identify missing value in %s' % pedf
  61. missval = '0'
  62. cl = '%s --noweb --file %s --make-bed --out %s --missing-genotype %s' % (plink,inpedfilepath,outroot,missval)
  63. p = subprocess.Popen(cl,shell=True,cwd=outfilepath)
  64. retval = p.wait() # run plink
  65. def main():
  66. """
  67. need to work with rgenetics composite datatypes
  68. so in and out are html files with data in extrafiles path
  69. <command interpreter="python">lped_to_pbed_converter.py '$input1/$input1.metadata.base_name'
  70. '$output1' '$output1.extra_files_path' '${GALAXY_DATA_INDEX_DIR}/rg/bin/plink'
  71. </command>
  72. """
  73. nparm = 4
  74. if len(sys.argv) < nparm:
  75. sys.stderr.write('## %s called with %s - needs %d parameters \n' % (prog,sys.argv,nparm))
  76. sys.exit(1)
  77. inpedfilepath = sys.argv[1]
  78. outhtmlname = sys.argv[2]
  79. outfilepath = sys.argv[3]
  80. try:
  81. os.makedirs(outfilepath)
  82. except:
  83. pass
  84. plink = sys.argv[4]
  85. rgConv(inpedfilepath,outhtmlname,outfilepath,plink)
  86. f = file(outhtmlname,'w')
  87. f.write(galhtmlprefix % prog)
  88. flist = os.listdir(outfilepath)
  89. s = '## Rgenetics: http://rgenetics.org Galaxy Tools %s %s' % (prog,timenow()) # becomes info
  90. print s
  91. f.write('<div>%s\n<ol>' % (s))
  92. for i, data in enumerate( flist ):
  93. f.write('<li><a href="%s">%s</a></li>\n' % (os.path.split(data)[-1],os.path.split(data)[-1]))
  94. f.write("</div></body></html>")
  95. f.close()
  96. if __name__ == "__main__":
  97. main()