/lib/galaxy/datatypes/converters/pbed_ldreduced_converter.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 123 lines · 93 code · 15 blank · 15 comment · 8 complexity · 1edc8ea713b989f572d2d04a7ce2249e MD5 · raw file

  1. # converter for ldreduced rgenetics datatype
  2. # used for grr and eigenstrat - shellfish if we get around to it
  3. #
  4. import os,sys,tempfile,subprocess,time
  5. from galaxy import eggs
  6. prog="pbed_ldreduced_converter.py"
  7. galhtmlprefix = """<?xml version="1.0" encoding="utf-8" ?>
  8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  9. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  10. <head>
  11. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  12. <meta name="generator" content="Galaxy %s tool output - see http://getgalaxy.org" />
  13. <title></title>
  14. <link rel="stylesheet" href="/static/style/base.css" type="text/css" />
  15. </head>
  16. <body>
  17. <div class="document">
  18. """
  19. plinke = 'plink'
  20. def timenow():
  21. """return current time as a string
  22. """
  23. return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
  24. def pruneLD(plinktasks=[],cd='./',vclbase = []):
  25. """
  26. """
  27. fplog,plog = tempfile.mkstemp()
  28. alog = []
  29. alog.append('## Rgenetics: http://rgenetics.org Galaxy Tools rgQC.py Plink pruneLD runner\n')
  30. for task in plinktasks: # each is a list
  31. vcl = vclbase + task
  32. sto = file(plog,'w')
  33. x = subprocess.Popen(' '.join(vcl),shell=True,stdout=sto,stderr=sto,cwd=cd)
  34. retval = x.wait()
  35. sto.close()
  36. try:
  37. lplog = file(plog,'r').readlines()
  38. lplog = [x for x in lplog if x.find('Pruning SNP') == -1]
  39. alog += lplog
  40. alog.append('\n')
  41. os.unlink(plog) # no longer needed
  42. except:
  43. alog.append('### %s Strange - no std out from plink when running command line\n%s\n' % (timenow(),' '.join(vcl)))
  44. return alog
  45. def makeLDreduced(basename,infpath=None,outfpath=None,plinke='plink',forcerebuild=False,returnFname=False,
  46. winsize="60", winmove="40", r2thresh="0.1" ):
  47. """ not there so make and leave in output dir for post job hook to copy back into input extra files path for next time
  48. """
  49. ldr = basename # we store ld reduced and thinned data
  50. ldreduced = os.path.join(outfpath,ldr) # note where this is going
  51. outbase = os.path.join(outfpath,basename)
  52. inbase = os.path.join(infpath)
  53. loglines = []
  54. ldbedname = '%s.bed' % ldreduced
  55. bedname = '%s.bed' % basename
  56. ldbedfn = os.path.join(infpath,ldbedname)
  57. bedfn = os.path.join(infpath,bedname)
  58. bmap = os.path.join(infpath,'%s.bim' % basename)
  59. plinktasks = []
  60. vclbase = [plinke,'--noweb']
  61. plinktasks += [['--bfile',inbase,'--indep-pairwise %s %s %s' % (winsize,winmove,r2thresh),'--out %s' % outbase],
  62. ['--bfile',inbase,'--extract %s.prune.in --make-bed --out %s' % (outbase, outbase)]]
  63. vclbase = [plinke,'--noweb']
  64. loglines = pruneLD(plinktasks=plinktasks,cd=outfpath,vclbase = vclbase)
  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. .. raw:: xml
  70. <command interpreter="python">
  71. pbed_ldreduced_converter.py '$input1.extra_files_path/$input1.metadata.base_name' '$winsize' '$winmove' '$r2thresh'
  72. '$output1' '$output1.files_path' 'plink'
  73. </command>
  74. """
  75. nparm = 7
  76. if len(sys.argv) < nparm:
  77. sys.stderr.write('## %s called with %s - needs %d parameters \n' % (prog,sys.argv,nparm))
  78. sys.exit(1)
  79. inpedfilepath = sys.argv[1]
  80. base_name = os.path.split(inpedfilepath)[-1]
  81. winsize = sys.argv[2]
  82. winmove = sys.argv[3]
  83. r2thresh = sys.argv[4]
  84. outhtmlname = sys.argv[5]
  85. outfilepath = sys.argv[6]
  86. try:
  87. os.makedirs(outfilepath)
  88. except:
  89. pass
  90. plink = sys.argv[7]
  91. makeLDreduced(base_name,infpath=inpedfilepath,outfpath=outfilepath,plinke=plink,forcerebuild=False,returnFname=False,
  92. winsize=winsize,winmove=winmove,r2thresh=r2thresh)
  93. f = file(outhtmlname,'w')
  94. f.write(galhtmlprefix % prog)
  95. flist = os.listdir(outfilepath)
  96. s1 = '## Rgenetics: http://rgenetics.org Galaxy Tools %s %s' % (prog,timenow()) # becomes info
  97. s2 = 'Input %s, winsize=%s, winmove=%s, r2thresh=%s' % (base_name,winsize,winmove,r2thresh)
  98. print '%s %s' % (s1,s2)
  99. f.write('<div>%s\n%s\n<ol>' % (s1,s2))
  100. for i, data in enumerate( flist ):
  101. f.write('<li><a href="%s">%s</a></li>\n' % (os.path.split(data)[-1],os.path.split(data)[-1]))
  102. f.write("</div></body></html>")
  103. f.close()
  104. if __name__ == "__main__":
  105. main()