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