10
|
1 #!/usr/bin/python
|
|
2
|
|
3 import argparse, optparse, os, shutil, subprocess, sys, tempfile, shlex, time
|
|
4
|
|
5
|
|
6 parser = argparse.ArgumentParser(description='')
|
|
7
|
21
|
8 parser.add_argument('-x', dest='binPath')
|
10
|
9 parser.add_argument ( '-i1', dest='input_bam', help='the bam input file' )
|
|
10 parser.add_argument ( '-o1', dest='output_raw', help='the output file' )
|
|
11 parser.add_argument ( '-o2', dest='output_vcf', help='the output file' )
|
|
12
|
|
13
|
|
14 tmp_dir = tempfile.mkdtemp()
|
|
15 errorFile = tmp_dir+"/errorLog"
|
|
16
|
16
|
17 currentDir = os.path.dirname(os.path.abspath(__file__))
|
21
|
18
|
|
19 binPath = args.binPath
|
|
20 bam2cfg = binPath+"/bam2cfg.pl"
|
|
21 breakdancer = binPath+"/breakdancer-max"
|
|
22 breakdancer2vcf = binPath+"/breakdancer2vcf.py"
|
10
|
23
|
|
24
|
|
25 def bam2cfg(args):
|
|
26 config = tmp_dir+"/breakdancer_config"
|
19
|
27 cmd = 'perl %s %s' % (bam2cfg, args.input_bam)
|
10
|
28 execute(cmd, output=config)
|
|
29 print ("\ncmd = %s \n" ) %(cmd)
|
|
30 return config
|
|
31
|
|
32
|
|
33 def breakdancer(args, config):
|
19
|
34 cmd = '%s %s' % (breakdancer, config)
|
10
|
35 execute(cmd, output=args.output_raw)
|
|
36
|
|
37
|
|
38 def breakdancer2vcf(args):
|
19
|
39 cmd = "python %s -i %s -o %s" % ( breakdancer2vcf, args.output_raw, args.output_vcf )
|
10
|
40 execute(cmd)
|
|
41
|
|
42
|
|
43 def execute( cmd, output="" ):
|
|
44 try:
|
|
45 err = open(tmp_dir+"/errorLog", 'a')
|
|
46 if output != "":
|
|
47 out = open(output, 'w')
|
|
48 else:
|
|
49 out = subprocess.PIPE
|
|
50 process = subprocess.Popen( args=shlex.split(cmd), stdout=out, stderr=err )
|
|
51 process.wait()
|
|
52 err.close()
|
|
53 if out != subprocess.PIPE:
|
|
54 out.close()
|
|
55 except Exception, e:
|
|
56 sys.stderr.write("problem doing : %s\n" %(cmd))
|
|
57 sys.stderr.write( '%s\n\n' % str(e) )
|
|
58
|
|
59
|
|
60 def check( output ):
|
|
61 if (os.path.getsize(output)>0):
|
|
62 return True
|
|
63 else:
|
|
64 sys.stderr.write('The output file is empty : %s\n' % (output))
|
|
65
|
|
66
|
|
67 def getLine(file):
|
|
68 try:
|
|
69 f = open(file, 'r')
|
|
70 lignes = f.readlines()
|
|
71 n=0
|
|
72 for ligne in lignes:
|
|
73 if ligne.strip()[0]!="#":
|
|
74 n+=1
|
|
75 # n = len(lignes)
|
|
76 f.close()
|
|
77 except Exception, e:
|
|
78 sys.stderr.write( '%s\n' % str(e) )
|
|
79 return n
|
|
80
|
|
81
|
|
82 def compare( output1, output2 ):
|
|
83 # compare le nombre de ligne entre 2 fichiers
|
|
84 # pour verifier qu'aucune ligne n'a ete saute
|
|
85 num_raw = getLine(output1)
|
|
86 num_vcf = getLine(output2)
|
|
87 if (num_raw==num_vcf):
|
|
88 return True
|
|
89 else:
|
|
90 sys.stderr.write('Not the same number of variant between the raw file and the vcf file : %d vs %d\n' % (num_raw, num_vcf))
|
|
91
|
|
92
|
|
93 def __main__():
|
|
94
|
|
95 time0 = time.time()
|
|
96 args = parser.parse_args()
|
|
97
|
|
98 try:
|
|
99 config = bam2cfg(args)
|
|
100 breakdancer(args, config)
|
|
101 breakdancer2vcf(args)
|
|
102
|
|
103 # quelques tests
|
|
104 # verifier que les fichiers de sorties ne sont pas vides
|
|
105 check(args.output_raw)
|
|
106 check(args.output_vcf)
|
|
107 # comparer le nombre de ligne entre les 2 fichiers
|
|
108 # pour etre sur que toute les variations ont ete prises en compte
|
|
109 compare(args.output_raw, args.output_vcf)
|
|
110
|
|
111 sys.stdout.write( '\nDone in %d seconds\n' %(int(time.time()-time0)))
|
|
112
|
|
113 if ( os.path.getsize(errorFile)>0 ):
|
|
114 sys.stdout.write( 'At least one non fatal error was send, check the file : %s\n' %(errorFile) )
|
|
115 try:
|
|
116 err = open(errorFile, 'r')
|
|
117 errors = err.read()
|
|
118 err.close()
|
|
119 sys.stdout.write( "Errors :\n%s" %(errors))
|
|
120 except Exception, e:
|
|
121 sys.stderr.write( '%s\n' % str(e) )
|
|
122 else:
|
|
123 sys.stdout.write( 'BreakDancer successful' )
|
|
124
|
|
125 except Exception, e:
|
|
126 sys.stdout.write( 'BreakDancer fail\n' )
|
|
127 sys.stderr.write( '%s\n' % str(e) )
|
|
128 sys.exit()
|
|
129
|
|
130 finally:
|
|
131 if os.path.exists( tmp_dir ):
|
|
132 shutil.rmtree( tmp_dir )
|
|
133
|
|
134 if __name__=="__main__":
|
|
135 __main__()
|