11package org .labkey .sequenceanalysis .run .alignment ;
22
33import org .apache .commons .lang3 .StringUtils ;
4- import org .apache .logging .log4j .Level ;
54import org .apache .logging .log4j .Logger ;
65import org .jetbrains .annotations .Nullable ;
76import org .json .JSONObject ;
1413import org .labkey .api .sequenceanalysis .pipeline .AlignmentStepProvider ;
1514import org .labkey .api .sequenceanalysis .pipeline .CommandLineParam ;
1615import org .labkey .api .sequenceanalysis .pipeline .PipelineContext ;
17- import org .labkey .api .sequenceanalysis .pipeline .ProcessUtils ;
1816import org .labkey .api .sequenceanalysis .pipeline .ReferenceGenome ;
1917import org .labkey .api .sequenceanalysis .pipeline .SamtoolsRunner ;
2018import org .labkey .api .sequenceanalysis .pipeline .ToolParameterDescriptor ;
2119import org .labkey .api .util .FileUtil ;
22- import org .labkey .api .util .StringUtilsLabKey ;
2320
24- import java .io .BufferedReader ;
2521import java .io .File ;
26- import java .io .IOException ;
27- import java .io .InputStreamReader ;
2822import java .util .ArrayList ;
2923import java .util .Arrays ;
3024import java .util .List ;
@@ -43,7 +37,7 @@ public BWAMemWrapper(@Nullable Logger logger)
4337
4438 public static class BWAMemAlignmentStep extends BWAAlignmentStep <BWAMemWrapper >
4539 {
46- public BWAMemAlignmentStep (AlignmentStepProvider provider , PipelineContext ctx )
40+ public BWAMemAlignmentStep (AlignmentStepProvider <?> provider , PipelineContext ctx )
4741 {
4842 super (provider , ctx , new BWAMemWrapper (ctx .getLogger ()));
4943 }
@@ -68,7 +62,7 @@ protected void doPerformAlignment(AlignmentOutputImpl output, File inputFastq1,
6862 rg .add ("PL:" + (rs .getPlatform () == null ? "ILLUMINA" : rs .getPlatform ()));
6963 rg .add ("PU:" + (platformUnit == null ? rs .getReadsetId ().toString () : platformUnit ));
7064 rg .add ("SM:" + rs .getName ().replaceAll (" " , "_" ));
71- extraArgs .add (StringUtils .join (rg , "\\ t" ));
65+ extraArgs .add ("'" + StringUtils .join (rg , "\\ t" ) + "'" );
7266
7367 getWrapper ().performMemAlignment (getPipelineCtx ().getJob (), output , inputFastq1 , inputFastq2 , outputDirectory , referenceGenome , basename , extraArgs );
7468 }
@@ -102,93 +96,48 @@ public void performMemAlignment(PipelineJob job, AlignmentOutputImpl output, Fil
10296 {
10397 setOutputDir (outputDirectory );
10498
105- getLogger ().info ("Running BWA-Mem (Piped) " );
99+ getLogger ().info ("Running BWA-Mem" );
106100 getLogger ().debug ("will write BAM to: " + outputDirectory );
107101
108- List <String > args = new ArrayList <>();
109- args .add (getExe ().getPath ());
110- args .add ("mem" );
111- args .add ("-v" );
112- args .add ("1" );
102+ List <String > bwaArgs = new ArrayList <>();
103+ bwaArgs .add (getExe ().getPath ());
104+ bwaArgs .add ("mem" );
105+ bwaArgs .add ("-v" );
106+ bwaArgs .add ("1" );
113107 if (additionalArgs != null )
114- args .addAll (additionalArgs );
115- appendThreads (job , args );
108+ {
109+ bwaArgs .addAll (additionalArgs );
110+ }
111+ appendThreads (job , bwaArgs );
116112
117- args .add (new File (referenceGenome .getAlignerIndexDir ("bwa" ), FileUtil .getBaseName (referenceGenome .getWorkingFastaFile ().getName ()) + ".bwa.index" ).getPath ());
118- args .add (inputFastq1 .getPath ());
113+ bwaArgs .add ("'" + new File (referenceGenome .getAlignerIndexDir ("bwa" ), FileUtil .getBaseName (referenceGenome .getWorkingFastaFile ().getName ()) + ".bwa.index" ).getPath () + "'" );
114+ bwaArgs .add ("'" + inputFastq1 .getPath () + "'" );
119115
120116 if (inputFastq2 != null )
121117 {
122- args .add (inputFastq2 .getPath ());
118+ bwaArgs .add ("'" + inputFastq2 .getPath () + "'" );
123119 }
124120
125- try
126- {
127- //run BWA and pipe directly to samtools to make BAM
128- File bam = new File (outputDirectory , basename + ".bam" );
129-
130- output .addCommandExecuted (StringUtils .join (args , " " ));
131- ProcessBuilder bwaProcessBuilder = getProcessBuilder (args );
132- getLogger ().info (StringUtils .join (args , " " ));
133-
134- SamtoolsRunner sr = new SamtoolsRunner (getLogger ());
135- List <String > samtoolsArgs = Arrays .asList (sr .getSamtoolsPath ().getPath (), "view" , "-b" , "-h" , "-S" , "-T" , referenceGenome .getWorkingFastaFile ().getPath (), "-o" , bam .getPath (), "-" );
136- output .addCommandExecuted (StringUtils .join (samtoolsArgs , " " ));
137- ProcessBuilder samtoolsProcessBuilder = sr .getProcessBuilder (samtoolsArgs );
138- samtoolsProcessBuilder .redirectErrorStream (true );
139- getLogger ().info (StringUtils .join (samtoolsArgs , " " ));
140-
141- Process bwaProcess = null ;
142- Process samtoolsProcess = null ;
143- try
144- {
145- samtoolsProcess = samtoolsProcessBuilder .start ();
146- bwaProcess = bwaProcessBuilder .start ();
147- new ProcessUtils .ProcessReader (getLogger (), true , true ).readProcess (bwaProcess ); //read STDERR in separate thread
148- new ProcessUtils .StreamRedirector (getLogger ()).redirectStreams (bwaProcess , samtoolsProcess );
149-
150- try (BufferedReader procReader = new BufferedReader (new InputStreamReader (samtoolsProcess .getInputStream (), StringUtilsLabKey .DEFAULT_CHARSET )))
151- {
152- String line ;
153- while ((line = procReader .readLine ()) != null )
154- {
155- getLogger ().log (Level .DEBUG , "\t " + line );
156- }
157- }
158-
159- int lastReturnCode = samtoolsProcess .waitFor ();
160- if (lastReturnCode != 0 )
161- {
162- throw new PipelineJobException ("process exited with non-zero value: " + lastReturnCode );
163- }
164- }
165- catch (InterruptedException e )
166- {
167- throw new PipelineJobException (e );
168- }
169- finally
170- {
171- if (bwaProcess != null )
172- {
173- bwaProcess .destroy ();
174- }
175-
176- if (samtoolsProcess != null )
177- {
178- samtoolsProcess .destroy ();
179- }
180- }
181-
182- if (!bam .exists ())
183- {
184- throw new PipelineJobException ("Unable to find output file: " + bam .getPath ());
185- }
186-
187- output .addOutput (bam , AlignmentOutputImpl .BAM_ROLE );
188- }
189- catch (IOException e )
121+ //run BWA and pipe directly to samtools to make BAM
122+ File bam = new File (outputDirectory , basename + ".bam" );
123+ output .addCommandExecuted (StringUtils .join (bwaArgs , " " ));
124+
125+ SamtoolsRunner sr = new SamtoolsRunner (getLogger ());
126+ List <String > samtoolsArgs = Arrays .asList (sr .getSamtoolsPath ().getPath (), "view" , "-b" , "-h" , "-S" , "-T" , "'" + referenceGenome .getWorkingFastaFile ().getPath () + "'" , "-o" , "'" + bam .getPath () + "'" , "-" );
127+ output .addCommandExecuted (StringUtils .join (samtoolsArgs , " " ));
128+
129+ List <String > bashArgs = new ArrayList <>();
130+ bashArgs .add ("/bin/bash" );
131+ bashArgs .add ("-c" );
132+ bashArgs .add (StringUtils .join (bwaArgs , " " ) + " | " + StringUtils .join (samtoolsArgs , " " ));
133+
134+ execute (bashArgs );
135+
136+ if (!bam .exists ())
190137 {
191- throw new PipelineJobException (e );
138+ throw new PipelineJobException ("Unable to find output file: " + bam . getPath () );
192139 }
140+
141+ output .addOutput (bam , AlignmentOutputImpl .BAM_ROLE );
193142 }
194143}
0 commit comments