annotate strelka_wrapper.py @ 22:1c8dcda28be7

version 27/11/2014, corrected extra arguments bug
author mini
date Thu, 27 Nov 2014 10:31:58 +0100
parents 3c10d88b55ad
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
1 #!/usr/bin/env python
10
f66f23fcc83a Modified layout
mini
parents: 8
diff changeset
2 #Gregoire Seguin-Henry (Engineer IT)
f66f23fcc83a Modified layout
mini
parents: 8
diff changeset
3 #Amine Sbitti (Data Scientist)
f66f23fcc83a Modified layout
mini
parents: 8
diff changeset
4 #Ludovic Marie-Sainte (Project Manager)
f66f23fcc83a Modified layout
mini
parents: 8
diff changeset
5 #For Geviteam 2014
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
7 """
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
8 A wrapper script for running the GenomeAnalysisTK.jar commands.
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
9 """
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
10 from __future__ import print_function
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
11 import sys, argparse, os, tempfile, subprocess, shutil
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
12 from binascii import unhexlify
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
13 from string import Template
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
14 from galaxy import eggs
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
15
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
16 def cleanup_before_exit( tmp_dir ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
17 if tmp_dir and os.path.exists( tmp_dir ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
18 shutil.rmtree( tmp_dir )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
19
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
20 def _create_config(args, config_path):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
21 conf_file = open(config_path, "w")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
22 conf_file.write("[user]\n")
22
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
23 args2 = vars(args)
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
24 for option in args2:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
25 if not option in ["tumorBam", "normalBam", "refFile", "configFile", "scriptPath", "a", "b", "c", "d", "e", "extraStrelkaArguments"] and args2[option]!=None:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
26 conf_file.write("%s=%s\n" % (option, args2[option]))
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
27 if args.extraStrelkaArguments == "yes":
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
28 conf_file.write("extraStrelkaArguments=")
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
29 if args.a:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
30 conf_file.write("--ignore-conflicting-read-names ")
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
31 if args.b != None:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
32 conf_file.write("-used-allele-count-min-qscore %s " % (args.b))
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
33 if args.c != None:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
34 conf_file.write("--candidate-indel-input-vcf %s " % (args.c))
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
35 if args.d != None:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
36 conf_file.write("--force-output-vcf %s " % (args.d))
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
37 if args.e != None:
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
38 conf_file.write("-min-small-candidate-indel-read-frac %s " % (args.e))
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
39 conf_file.write("\n")
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
40 conf_file.close()
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
41
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
42 def my_Popen(cmd, prefix_for_stderr_name, tmp_dir, msg_error):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
43 stderr_name = tempfile.NamedTemporaryFile( prefix = prefix_for_stderr_name ).name
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
44 proc = subprocess.Popen( args=cmd, shell=True, stderr=open( stderr_name, 'wb' ) )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
45 return_code = proc.wait()
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
46 if return_code:
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
47 for line in open( stderr_name ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
48 print(line, file=sys.stderr)
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
49 os.unlink( stderr_name ) #clean up
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
50 cleanup_before_exit( tmp_dir )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
51 raise Exception( msg_error )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
52 else:
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
53 os.unlink( stderr_name )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
54
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
55 def index_bam_files( bam_filenames, tmp_dir ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
56 for bam_filename in bam_filenames:
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
57 bam_index_filename = "%s.bai" % bam_filename
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
58 print("bam_filename is: " + bam_filename + " bam_index_filename is: " + bam_index_filename + " test is: %s" % os.path.exists(bam_index_filename))
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
59 if not os.path.exists( bam_index_filename ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
60 #need to index this bam file
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
61 command = 'samtools index %s %s' % ( bam_filename, bam_index_filename )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
62 my_Popen( command, "bam_index_stderr", tmp_dir, "Error during indexation of fasta file :" + bam_filename)
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
63
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
64 def index_fasta_files( fasta_filenames, tmp_dir ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
65 for fasta_filename in fasta_filenames:
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
66 fasta_index_filename = "%s.fai" % fasta_filename
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
67 print("fasta_filename is: " + fasta_filename + " fasta_index_filename is: " + fasta_index_filename + " test is: %s" % os.path.exists(fasta_index_filename))
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
68 if not os.path.exists( fasta_index_filename ):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
69 #need to index this bam file
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
70 command = 'samtools faidx %s %s' % ( fasta_filename, fasta_index_filename )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
71 my_Popen( command, "fasta_index_stderr", tmp_dir, "Error during indexation of fasta file :" + fasta_filename)
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
72
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
73 def __main__():
10
f66f23fcc83a Modified layout
mini
parents: 8
diff changeset
74 #Manage options
17
8260a4188b08 solved conf_file.ini pb
mini
parents: 14
diff changeset
75 print(os.environ['PATH'])
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
76 parser = argparse.ArgumentParser()
22
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
77 parser.add_argument( '--tumorBam', help='path to tumor bam file', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
78 parser.add_argument( '--normalBam', help='', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
79 parser.add_argument( '--refFile', help='', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
80 parser.add_argument( '--configFile', help='', required = False )
18
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
81 parser.add_argument( '--depthFilterMultiple', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
82 parser.add_argument( '--snvMaxFilteredBasecallFrac', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
83 parser.add_argument( '--snvMaxSpanningDeletionFrac', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
84 parser.add_argument( '--indelMaxRefRepeat', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
85 parser.add_argument( '--indelMaxWindowFilteredBasecallFrac', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
86 parser.add_argument( '--indelMaxIntHpolLength', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
87 parser.add_argument( '--ssnvPrior', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
88 parser.add_argument( '--sindelPrior', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
89 parser.add_argument( '--ssnvNoise', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
90 parser.add_argument( '--sindelNoise', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
91 parser.add_argument( '--ssnvNoiseStrandBiasFrac', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
92 parser.add_argument( '--minTier1Mapq', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
93 parser.add_argument( '--minTier2Mapq', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
94 parser.add_argument( '--ssnvQuality_LowerBound', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
95 parser.add_argument( '--sindelQuality_LowerBound', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
96 parser.add_argument( '--isWriteRealignedBam', help='', required = False )
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
97 parser.add_argument( '--binSize', help='path to tumor bam file', required = False )
18
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
98 parser.add_argument( '--extraStrelkaArguments', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
99 parser.add_argument( '--isSkipDepthFilters', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
100 parser.add_argument( '--maxInputDepth', help='', required = False )
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
101 parser.add_argument( '--scriptPath', help='', required = False )
22
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
102 parser.add_argument( '-a', action="store_true", help='', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
103 parser.add_argument( '-b', help='', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
104 parser.add_argument( '-c', help='', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
105 parser.add_argument( '-d', help='', required = False )
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
106 parser.add_argument( '-e', help='', required = False )
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
107 args = parser.parse_args()
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
108
8
2cee4ed50058 nothing relevant
mini
parents: 7
diff changeset
109 root_dir= args.scriptPath
7
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
110 expected_dir="for_tests"
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
111 job_dir=os.getcwd()
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
112 analysis_dir=job_dir + "/StrelkaAnalysis"
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
113 config_script=root_dir + "/configureStrelkaWorkflow.pl"
9
0e8e6011082b Strelka Workflow 0.0.2
mini
parents: 8
diff changeset
114 tmp_dir = tempfile.mkdtemp( prefix='tmp-strelkaAnalysis-' )
7
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
115 config_ini = "%s/config.ini" % (tmp_dir)
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
116
14
ca84a74ff567 corriging bug in environment variable
mini
parents: 11
diff changeset
117 print("root_dir: " + root_dir + "\njob_dir :" + job_dir + "\nanalysis_dir :" + analysis_dir + "\nconfig_script :" + config_script + "\ntmp_dir :" + tmp_dir + "\nconfig_ini :" + config_ini)
7
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
118
3e8541ece3c7 trying to fix environment variable 0.0.1
mini
parents: 6
diff changeset
119
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
120 #verifying eveything's ok
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
121 if not os.path.isfile(config_script):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
122 sys.exit("ERROR: The strelka workflow must be built prior to running. See installation instructions in '$root_dir/README'")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
123 print("configuring...", file=sys.stdout)
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
124 if os.path.exists(analysis_dir):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
125 sys.exit("'" + analysis_dir + "' already exist, if you are executing this tool from galaxy it should not happen")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
126
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
127
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
128 # creating index if needed
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
129 bam_filenames = [ args.tumorBam, args.normalBam ]
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
130 index_bam_files( bam_filenames, tmp_dir )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
131 fasta_files = [ args.refFile ]
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
132 index_fasta_files( fasta_files, tmp_dir )
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
133
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
134 #creating config file if needed
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
135 if args.configFile == "Custom":
22
1c8dcda28be7 version 27/11/2014, corrected extra arguments bug
mini
parents: 18
diff changeset
136 _create_config(args, config_ini)
18
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
137 elif args.configFile in ["strelka_config_bwa_default.ini", "strelka_config_isaac_default.ini", "strelka_config_eland_default.ini"]:
3c10d88b55ad improved user interface
mini
parents: 17
diff changeset
138 cmdbash="cp %s %s" % (root_dir + "/lib/" + args.configFile, config_ini)
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
139 my_Popen(cmdbash, "copy_default_file_err", tmp_dir, "Error during the copy of default config file, maybe it was removed")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
140 else:
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
141 if not os.path.exists(args.configFile):
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
142 print( "The path to your configuration File seems to be wrong, use another one or custom option", file=sys.stderr)
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
143 cmdbash="cp %s %s" % (args.configFile, config_ini)
9
0e8e6011082b Strelka Workflow 0.0.2
mini
parents: 8
diff changeset
144 my_Popen(cmdbash, "copy_default_file_err", tmp_dir, "Error during the copy of the selected config file")
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
145
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
146
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
147
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
148
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
149 #configuration of workflow
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
150 cmd="%s --tumor=%s --normal=%s --ref=%s --config=%s --output-dir=%s" % (config_script, args.tumorBam, args.normalBam, args.refFile, config_ini, analysis_dir)
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
151 print( "**** Starting configuration.")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
152 print( "**** Configuration cmd: '" + cmd + "'")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
153 my_Popen( cmd, "cinfugation_stderr", tmp_dir, "Error during configuration !")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
154 print("completed configuration")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
155
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
156 #run the workflow !
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
157 cmd="make -C " + analysis_dir
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
158 print("**** starting workflow.")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
159 print("**** workflow cmd: '" + cmd + "'")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
160 my_Popen( cmd, "workflow_stderr", tmp_dir, "Error during workflow execution !")
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
161 print("**** completed workflow execution")
17
8260a4188b08 solved conf_file.ini pb
mini
parents: 14
diff changeset
162
8260a4188b08 solved conf_file.ini pb
mini
parents: 14
diff changeset
163 cmdbash="cp %s %s" % (config_ini, analysis_dir + "/config.ini")
8260a4188b08 solved conf_file.ini pb
mini
parents: 14
diff changeset
164 my_Popen(cmdbash, "copy_final_conf_file_err", tmp_dir, "Error during the copy of conf file after job is done, quite strange...")
8260a4188b08 solved conf_file.ini pb
mini
parents: 14
diff changeset
165
6
87568e5a7d4f Testing strelka version 0.0.1
mini
parents:
diff changeset
166
14
ca84a74ff567 corriging bug in environment variable
mini
parents: 11
diff changeset
167 if __name__=='__main__':
ca84a74ff567 corriging bug in environment variable
mini
parents: 11
diff changeset
168 __main__()