/tools/rgenetics/rgGTOOL.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 58 lines · 40 code · 8 blank · 10 comment · 2 complexity · 4d60178859b8cf76f16bdce4d35848f1 MD5 · raw file

  1. #!/usr/local/bin/python
  2. # hack to run and process a linkage format file into
  3. # the format used by Marchini's SNPTEST imputed case control association
  4. # expects args as
  5. # rgGTOOL.py $i $o $discrete $logf $outdir
  6. # ross lazarus
  7. import sys,math,shutil,subprocess,os,time
  8. from os.path import abspath
  9. imagedir = '/static/rg' # if needed for images
  10. myversion = 'V000.1 August 2007'
  11. def timenow():
  12. """return current time as a string
  13. """
  14. return time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(time.time()))
  15. if __name__ == "__main__":
  16. if len(sys.argv) < 6:
  17. s = 'rgGTOOL.py needs 5 params - got %s \n' % (sys.argv)
  18. sys.stderr.write(s) # print >>,s would probably also work?
  19. sys.exit(0)
  20. print 'Rgenetics %s http://rgenetics.org SNPTEST Tools, rgGTOOL.py starting at %s' % (myversion,timenow())
  21. pname = sys.argv[1]
  22. lpedname = pname.split('.ped')[0] # get file name part
  23. outname = sys.argv[2]
  24. discrete = sys.argv[3]
  25. logf = sys.argv[4]
  26. outdir = sys.argv[5]
  27. cdir = os.getcwd()
  28. me = sys.argv[0]
  29. mypath = abspath(os.path.join(cdir,me)) # get abs path to this python script
  30. shpath = abspath(os.path.sep.join(mypath.split(os.path.sep)[:-1]))
  31. alogf = abspath(os.path.join(cdir,logf)) # absolute paths
  32. apedf = abspath(os.path.join(cdir,'%s.ped' % lpedname)) # absolute paths
  33. amapf = abspath(os.path.join(cdir,'%s.map' % lpedname)) # absolute paths
  34. outg = abspath(os.path.join(outdir,'%s.gen' % outname)) # absolute paths
  35. outs = abspath(os.path.join(outdir,'%s.sample' % outname)) # absolute paths
  36. workdir = abspath(os.path.sep.join(mypath.split(os.path.sep)[:-1])) # trim end off './database/files/foo.dat'
  37. os.chdir(workdir)
  38. tlogname = '%s.logtemp' % outname
  39. sto = file(tlogname,'w')
  40. sto.write('rgGTOOL.py: called with %s\n' % (sys.argv))
  41. exme = 'gtool'
  42. vcl = [exme,'-P','--ped',apedf,'--map',amapf,'--discrete_phenotype',discrete,'--og',outg,'--os',outs]
  43. #'/usr/local/bin/plink','/usr/local/bin/plink',pc1,pc2,pc3)
  44. #os.spawnv(os.P_WAIT,plink,vcl)
  45. p=subprocess.Popen(' '.join(vcl),shell=True,stdout=sto)
  46. retval = p.wait()
  47. sto.write('rgGTOOL.py after calling %s: vcl=%s\n' % (exme,vcl))
  48. sto.close()
  49. shutil.move(tlogname,alogf)
  50. os.chdir(cdir)