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