comparison delly.py @ 13:915e764159df draft default tip

Uploaded
author jeremie
date Tue, 17 Jun 2014 08:46:45 -0400
parents
children
comparison
equal deleted inserted replaced
12:6e04a22d900c 13:915e764159df
1 #!/usr/bin/python
2
3 import argparse, os, shutil, subprocess, sys, tempfile, shlex, vcf
4
5 parser = argparse.ArgumentParser(description='')
6 parser.add_argument ( '-b', dest='bam', help='the bam file', required=True )
7 parser.add_argument ( '-t', dest='types', help='SV analysis type (DEL, DUP, INV, TRA)', nargs='+', required=True )
8 parser.add_argument ( '-q', dest='map_qual', help='min. paired-end mapping quality', default='0' )
9 parser.add_argument ( '-s', dest='mad_cutoff', help='insert size cutoff, median+s*MAD (deletions only)', default=5 )
10 parser.add_argument ( '-g', dest='genome', help='genome fasta file' )
11 parser.add_argument ( '-m', dest='min_flank', help='minimum flanking sequence size', default=13 )
12 parser.add_argument ( '-e', dest='epsilon', help='allowed epsilon deviation of PE vs. SR deletion', default=0.1 )
13 parser.add_argument ( '-o', dest='output', help='output file' )
14
15 dellyPath=os.path.dirname(os.path.realpath(__file__))+"/delly_v0.5.5"
16 os.system("chmod +x "+str(dellyPath))
17
18 def execute( cmd, output="" ):
19 tmp_dir = tempfile.mkdtemp()
20 try:
21 err = open(tmp_dir+"/errorLog", 'a')
22 if output != "":
23 out = open(output, 'w')
24 else:
25 out = subprocess.PIPE
26 process = subprocess.Popen( args=shlex.split(cmd), stdout=out, stderr=err )
27 process.wait()
28 err.close()
29 if out != subprocess.PIPE:
30 out.close()
31 except Exception, e:
32 sys.stderr.write("problem doing : %s\n" %(cmd))
33 sys.stderr.write( '%s\n\n' % str(e) )
34
35
36 def delly(args, tempDir):
37 tempOutputs=[];
38 for typ in args.types:
39 output=str(tempDir)+"/"+str(typ)
40 tempOutputs.append(output)
41 cmd = "%s %s -t %s -o %s -q %s -s %s -m %s -e %s " % (dellyPath, args.bam, typ, output, args.map_qual, args.mad_cutoff, args.min_flank, args.epsilon)
42 if (args.genome!="" and args.genome!=None):
43 cmd += "-g %s" % (args.genome)
44 # print ("commande = "+cmd)
45 execute( cmd )
46 return tempOutputs
47
48
49 def merge(outputs, outputFile):
50 template = vcf.Reader(filename=outputs[0])
51 vcfWriter = vcf.Writer(open(outputFile, 'w'), template)
52 for output in outputs:
53 vcfReader = vcf.Reader(filename=output)
54 for record in vcfReader:
55 vcfWriter.write_record(record)
56 return 0
57
58
59 def getVersion(program):
60 try:
61 tmp = tempfile.NamedTemporaryFile().name
62 tmp_stdout = open( tmp, 'wb' )
63 proc = subprocess.Popen( args=program, shell=True, stdout=tmp_stdout )
64 tmp_stdout.close()
65 returncode = proc.wait()
66 stdout = None
67 for line in open( tmp_stdout.name, 'rb' ):
68 if line.lower().find( 'version' ) >= 0:
69 stdout = line.strip()
70 break
71 if stdout:
72 sys.stdout.write( '%s\n' % stdout )
73 except:
74 sys.stdout.write( 'Could not determine %s version\n' % (program) )
75
76
77 def __main__():
78 print(os.path.dirname(os.path.realpath(__file__)))
79 args = parser.parse_args()
80
81 tempDir = tempfile.mkdtemp();
82 getVersion(dellyPath)
83
84 try:
85 execute("samtools index " + args.bam)
86 except Exception, e:
87 print("problem while indexing bam file " + str(e))
88
89 try:
90 tempOutputs = delly(args, tempDir)
91 except Exception, e:
92 sys.stdout.write("problem while runing delly " + str(e))
93
94 try:
95 if args.output:
96 outputs=[]
97 for output in tempOutputs:
98 if os.path.exists(output):
99 if os.path.getsize(output)>0:
100 outputs.append(output)
101 if len(outputs)>0:
102 merge(outputs, args.output)
103 except Exception, e:
104 sys.stdout.write("problem while merging files " + str(e))
105
106 finally:
107 try:
108 if os.path.exists(tempDir):
109 shutil.rmtree(tempDir)
110 os.system("rm "+str(args.bam)+".bai")
111 except:
112 pass
113
114 if __name__=="__main__":
115 __main__()