annotate delly.py @ 2:aac59860a454 draft

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