1+ package org .labkey .sequenceanalysis .analysis ;
2+
3+ import org .apache .commons .lang3 .StringUtils ;
4+ import org .json .JSONObject ;
5+ import org .labkey .api .module .ModuleLoader ;
6+ import org .labkey .api .pipeline .PipelineJob ;
7+ import org .labkey .api .pipeline .PipelineJobException ;
8+ import org .labkey .api .pipeline .RecordedAction ;
9+ import org .labkey .api .sequenceanalysis .SequenceOutputFile ;
10+ import org .labkey .api .sequenceanalysis .pipeline .AbstractParameterizedOutputHandler ;
11+ import org .labkey .api .sequenceanalysis .pipeline .ReferenceGenome ;
12+ import org .labkey .api .sequenceanalysis .pipeline .SequenceAnalysisJobSupport ;
13+ import org .labkey .api .sequenceanalysis .pipeline .SequenceOutputHandler ;
14+ import org .labkey .api .sequenceanalysis .pipeline .ToolParameterDescriptor ;
15+ import org .labkey .api .util .FileType ;
16+ import org .labkey .api .util .FileUtil ;
17+ import org .labkey .sequenceanalysis .SequenceAnalysisModule ;
18+ import org .labkey .sequenceanalysis .run .analysis .DeepVariantAnalysis ;
19+
20+ import java .io .File ;
21+ import java .util .ArrayList ;
22+ import java .util .Arrays ;
23+ import java .util .Date ;
24+ import java .util .List ;
25+
26+ /**
27+ * Created by bimber on 2/3/2016.
28+ */
29+ public class DeepVariantHandler extends AbstractParameterizedOutputHandler <SequenceOutputHandler .SequenceOutputProcessor >
30+ {
31+ private final FileType _bamOrCramFileType = new FileType (Arrays .asList ("bam" , "cram" ), "bam" );
32+
33+ public DeepVariantHandler ()
34+ {
35+ super (ModuleLoader .getInstance ().getModule (SequenceAnalysisModule .class ), "Run DeepVariant" , "This will run DeepVariant on the selected BAMs to generate gVCF files." , null , DeepVariantAnalysis .getToolDescriptors ());
36+ }
37+
38+ @ Override
39+ public boolean canProcess (SequenceOutputFile o )
40+ {
41+ return o .getFile () != null && _bamOrCramFileType .isType (o .getFile ());
42+ }
43+
44+ @ Override
45+ public boolean doRunRemote ()
46+ {
47+ return true ;
48+ }
49+
50+ @ Override
51+ public boolean doRunLocal ()
52+ {
53+ return false ;
54+ }
55+
56+ @ Override
57+ public SequenceOutputProcessor getProcessor ()
58+ {
59+ return new Processor ();
60+ }
61+
62+ @ Override
63+ public boolean doSplitJobs ()
64+ {
65+ return true ;
66+ }
67+
68+ public class Processor implements SequenceOutputProcessor
69+ {
70+ @ Override
71+ public void init (JobContext ctx , List <SequenceOutputFile > inputFiles , List <RecordedAction > actions , List <SequenceOutputFile > outputsToCreate ) throws UnsupportedOperationException , PipelineJobException
72+ {
73+ String modelType = ctx .getParams ().optString ("modelType" );
74+ DeepVariantAnalysis .inferModelType (modelType , ctx );
75+ }
76+
77+ @ Override
78+ public void processFilesRemote (List <SequenceOutputFile > inputFiles , JobContext ctx ) throws UnsupportedOperationException , PipelineJobException
79+ {
80+ PipelineJob job = ctx .getJob ();
81+ if (inputFiles .size () != 1 )
82+ {
83+ throw new PipelineJobException ("Expected a single input file" );
84+ }
85+
86+ SequenceOutputFile so = inputFiles .get (0 );
87+
88+ RecordedAction action = new RecordedAction (getName ());
89+ action .setStartTime (new Date ());
90+
91+ action .addInput (so .getFile (), "Input BAM File" );
92+
93+ File outputFile = new File (ctx .getOutputDir (), FileUtil .getBaseName (so .getFile ()) + ".g.vcf.gz" );
94+
95+ DeepVariantAnalysis .DeepVariantWrapper wrapper = new DeepVariantAnalysis .DeepVariantWrapper (job .getLogger ());
96+ wrapper .setOutputDir (ctx .getOutputDir ());
97+
98+ ReferenceGenome referenceGenome = ctx .getSequenceSupport ().getCachedGenome (so .getLibrary_id ());
99+ if (referenceGenome == null )
100+ {
101+ throw new PipelineJobException ("No reference genome found for output: " + so .getRowid ());
102+ }
103+
104+ String inferredModelType = ctx .getSequenceSupport ().getCachedObject ("modelType" , String .class );
105+ String modelType = inferredModelType == null ? ctx .getParams ().optString ("modelType" ) : inferredModelType ;
106+ if (modelType == null )
107+ {
108+ throw new PipelineJobException ("Missing model type" );
109+ }
110+
111+ List <String > args = new ArrayList <>(getClientCommandArgs (ctx .getParams ()));
112+ args .add ("--model_type=" + modelType );
113+
114+ String binVersion = ctx .getParams ().optString ("binVersion" );
115+ if (binVersion == null )
116+ {
117+ throw new PipelineJobException ("Missing binVersion" );
118+ }
119+
120+ wrapper .execute (so .getFile (), referenceGenome .getWorkingFastaFile (), outputFile , ctx .getFileManager (), binVersion , args );
121+
122+ action .addOutput (outputFile , "gVCF File" , false );
123+
124+ SequenceOutputFile o = new SequenceOutputFile ();
125+ o .setName (outputFile .getName ());
126+ o .setFile (outputFile );
127+ o .setLibrary_id (so .getLibrary_id ());
128+ o .setCategory ("DeepVariant gVCF File" );
129+ o .setReadset (so .getReadset ());
130+ o .setDescription ("DeepVariant Version: " + binVersion );
131+
132+ ctx .addSequenceOutput (o );
133+
134+ ctx .addActions (action );
135+ }
136+
137+ private List <String > getClientCommandArgs (JSONObject params )
138+ {
139+ List <String > ret = new ArrayList <>();
140+
141+ for (ToolParameterDescriptor desc : getParameters ())
142+ {
143+ if (desc .getCommandLineParam () != null )
144+ {
145+ String val = params .optString (desc .getName (), null );
146+ if (StringUtils .trimToNull (val ) != null )
147+ {
148+ ret .addAll (desc .getCommandLineParam ().getArguments (" " , val ));
149+ }
150+ }
151+ }
152+
153+ return ret ;
154+ }
155+
156+ @ Override
157+ public void processFilesOnWebserver (PipelineJob job , SequenceAnalysisJobSupport support , List <SequenceOutputFile > inputFiles , JSONObject params , File outputDir , List <RecordedAction > actions , List <SequenceOutputFile > outputsToCreate ) throws UnsupportedOperationException , PipelineJobException
158+ {
159+
160+ }
161+ }
162+ }
0 commit comments