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