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

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