2
|
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
|
|
16 def execute( cmd, output="" ):
|
|
17 tmp_dir = tempfile.mkdtemp()
|
|
18 try:
|
|
19 err = open(tmp_dir+"/errorLog", 'a')
|
|
20 if output != "":
|
|
21 out = open(output, 'w')
|
|
22 else:
|
|
23 out = subprocess.PIPE
|
|
24 process = subprocess.Popen( args=shlex.split(cmd), stdout=out, stderr=err )
|
|
25 process.wait()
|
|
26 err.close()
|
|
27 if out != subprocess.PIPE:
|
|
28 out.close()
|
|
29 except Exception, e:
|
|
30 sys.stderr.write("problem doing : %s\n" %(cmd))
|
|
31 sys.stderr.write( '%s\n\n' % str(e) )
|
|
32
|
|
33
|
|
34 def delly(args, tempDir):
|
|
35 tempOutputs=[];
|
|
36 for typ in args.types:
|
|
37 output=str(tempDir)+"/"+str(typ)
|
|
38 tempOutputs.append(output)
|
|
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)
|
|
40 if (args.genome!="" and args.genome!=None):
|
|
41 cmd += "-g %s" % (args.genome)
|
|
42 # print ("commande = "+cmd)
|
|
43 execute( cmd )
|
|
44 return tempOutputs
|
|
45
|
|
46
|
|
47 def merge(outputs, outputFile):
|
|
48 template = vcf.Reader(filename=outputs[0])
|
|
49 vcfWriter = vcf.Writer(open(outputFile, 'w'), template)
|
|
50 for output in outputs:
|
|
51 vcfReader = vcf.Reader(filename=output)
|
|
52 for record in vcfReader:
|
|
53 vcfWriter.write_record(record)
|
|
54 return 0
|
|
55
|
|
56
|
|
57 def getVersion(program):
|
|
58 try:
|
|
59 tmp = tempfile.NamedTemporaryFile().name
|
|
60 tmp_stdout = open( tmp, 'wb' )
|
|
61 proc = subprocess.Popen( args=program, shell=True, stdout=tmp_stdout )
|
|
62 tmp_stdout.close()
|
|
63 returncode = proc.wait()
|
|
64 stdout = None
|
|
65 for line in open( tmp_stdout.name, 'rb' ):
|
|
66 if line.lower().find( 'version' ) >= 0:
|
|
67 stdout = line.strip()
|
|
68 break
|
|
69 if stdout:
|
|
70 sys.stdout.write( '%s\n' % stdout )
|
|
71 except:
|
|
72 sys.stdout.write( 'Could not determine %s version\n' % (program) )
|
|
73
|
|
74
|
|
75 def __main__():
|
|
76
|
|
77 args = parser.parse_args()
|
|
78
|
|
79 tempDir = tempfile.mkdtemp();
|
|
80 getVersion("delly")
|
|
81
|
|
82 try:
|
|
83 execute("samtools index " + args.bam)
|
|
84 except Exception, e:
|
|
85 print("problem while indexing bam file " + str(e))
|
|
86
|
|
87 try:
|
|
88 tempOutputs = delly(args, tempDir)
|
|
89 except Exception, e:
|
|
90 sys.stdout.write("problem while runing delly " + str(e))
|
|
91
|
|
92 try:
|
|
93 if args.output:
|
|
94 outputs=[]
|
|
95 for output in tempOutputs:
|
|
96 if os.path.exists(output):
|
|
97 if os.path.getsize(output)>0:
|
|
98 outputs.append(output)
|
|
99 if len(outputs)>0:
|
|
100 merge(outputs, args.output)
|
|
101 except Exception, e:
|
|
102 sys.stdout.write("problem while merging files " + str(e))
|
|
103
|
|
104 finally:
|
|
105 try:
|
|
106 if os.path.exists(tempDir):
|
|
107 shutil.rmtree(tempDir)
|
|
108 os.system("rm "+str(args.bam)+".bai")
|
|
109 except:
|
|
110 pass
|
|
111
|
|
112 if __name__=="__main__":
|
|
113 __main__()
|