1313import org .labkey .api .data .SimpleFilter ;
1414import org .labkey .api .data .Table ;
1515import org .labkey .api .data .TableInfo ;
16+ import org .labkey .api .data .TableSelector ;
1617import org .labkey .api .exp .api .DataType ;
1718import org .labkey .api .exp .api .ExpData ;
1819import org .labkey .api .exp .api .ExperimentService ;
3738import org .labkey .api .sequenceanalysis .run .SimpleScriptWrapper ;
3839import org .labkey .api .util .Compress ;
3940import org .labkey .api .util .FileUtil ;
41+ import org .labkey .api .util .PageFlowUtil ;
4042import org .labkey .api .util .Pair ;
4143import org .labkey .api .writer .PrintWriters ;
4244import org .labkey .sequenceanalysis .ReadDataImpl ;
43- import org .labkey .sequenceanalysis .SequenceAnalysisManager ;
4445import org .labkey .sequenceanalysis .SequenceAnalysisModule ;
4546import org .labkey .sequenceanalysis .SequenceAnalysisSchema ;
4647import org .labkey .sequenceanalysis .pipeline .ReadsetCreationTask ;
4748import org .labkey .sequenceanalysis .pipeline .SequenceNormalizationTask ;
48- import org .labkey .sequenceanalysis .pipeline .SequenceReadsetHandlerJob ;
4949import org .labkey .sequenceanalysis .util .SequenceUtil ;
5050
5151import java .io .File ;
@@ -167,19 +167,19 @@ public void init(PipelineJob job, SequenceAnalysisJobSupport support, List<Reads
167167 {
168168 for (String accession : readdataToSra .keySet ())
169169 {
170- if (readdataToSra .get (accession ).size () > 1 )
170+ List <ReadData > toMerge = readdataToSra .get (accession );
171+ if (toMerge .stream ().map (ReadData ::isArchived ).collect (Collectors .toSet ()).size () > 1 )
171172 {
172- List <ReadData > toMerge = readdataToSra .get (accession );
173- if (toMerge .stream ().map (ReadData ::isArchived ).collect (Collectors .toSet ()).size () > 1 )
174- {
175- throw new PipelineJobException ("SRA group contains a mix of archived and non-archived readdata: " + accession );
176- }
173+ throw new PipelineJobException ("SRA group contains a mix of archived and non-archived readdata: " + accession );
174+ }
177175
178- if (!toMerge .get (0 ).isArchived ())
179- {
180- continue ;
181- }
176+ if (!toMerge .get (0 ).isArchived ())
177+ {
178+ continue ;
179+ }
182180
181+ if (readdataToSra .get (accession ).size () > 1 )
182+ {
183183 job .getLogger ().debug ("Consolidating multiple readdata for: " + accession );
184184
185185 ReadDataImpl rd = new ReadDataImpl ();
@@ -221,9 +221,6 @@ public void init(PipelineJob job, SequenceAnalysisJobSupport support, List<Reads
221221 rd .setCreatedBy (job .getUser ().getUserId ());
222222 rd .setModifiedBy (job .getUser ().getUserId ());
223223 rd .setPlatformUnit (accession );
224- int totalReads = toMerge .stream ().map (ReadData ::getTotalReads ).reduce (0 , Integer ::sum );
225- accessionToReads .put (accession , totalReads );
226- job .getLogger ().debug ("Total reads from prior data: " + totalReads );
227224
228225 job .getLogger ().debug ("Merging readdata for accession: " + accession );
229226 File sraLog = new File (data1 .getFile ().getParentFile (), FileUtil .makeLegalName ("sraDownload.txt" ));
@@ -248,6 +245,10 @@ public void init(PipelineJob job, SequenceAnalysisJobSupport support, List<Reads
248245 }
249246 updatedAccessions .add (accession );
250247 }
248+
249+ int totalReads = toMerge .stream ().map (ReadData ::getTotalReads ).reduce (0 , Integer ::sum );
250+ job .getLogger ().debug ("Total reads from prior data: " + totalReads );
251+ accessionToReads .put (accession , totalReads );
251252 }
252253
253254 transaction .commit ();
@@ -298,23 +299,28 @@ public void complete(PipelineJob job, List<Readset> readsets, List<SequenceOutpu
298299
299300 rows .add (toUpdate );
300301
301- List <Integer > toAdd = new ArrayList <>(rd .getFileId1 ());
302- if (rd .getFileId2 () != null )
302+ SimpleFilter filter = new SimpleFilter (FieldKey .fromString ("readset" ), rs .getRowId ());
303+ filter .addCondition (FieldKey .fromString ("category" ), "Readset" );
304+ filter .addCondition (FieldKey .fromString ("container" ), rs .getContainer ());
305+ filter .addCondition (FieldKey .fromString ("dataId" ), rd .getFileId1 ());
306+ boolean hasMetrics = new TableSelector (SequenceAnalysisSchema .getTable (SequenceAnalysisSchema .TABLE_QUALITY_METRICS ), PageFlowUtil .set ("RowId" ), filter , null ).exists ();
307+ if (!hasMetrics )
303308 {
304- toAdd .add (rd .getFileId2 ());
305- }
309+ List <Integer > toAdd = new ArrayList <>(rd .getFileId1 ());
310+ if (rd .getFileId2 () != null )
311+ {
312+ toAdd .add (rd .getFileId2 ());
313+ }
306314
307- for (int dataId : toAdd )
315+ for (int dataId : toAdd )
316+ {
317+ //then delete/add:
318+ ReadsetCreationTask .addQualityMetricsForReadset (rs , dataId , job , true );
319+ }
320+ }
321+ else
308322 {
309- //update metrics. first delete existing:
310- SimpleFilter filter = new SimpleFilter (FieldKey .fromString ("readset" ), rs .getRowId ());
311- filter .addCondition (FieldKey .fromString ("container" ), rs .getContainer ());
312- filter .addCondition (FieldKey .fromString ("dataId" ), dataId );
313- int deleted = Table .delete (SequenceAnalysisManager .get ().getTable (SequenceAnalysisSchema .TABLE_QUALITY_METRICS ), filter );
314- job .getLogger ().debug ("existing metrics deleted: " + deleted );
315-
316- //then add:
317- ReadsetCreationTask .addQualityMetricsForReadset (rs , dataId , job );
323+ job .getLogger ().info ("Existing metrics found, will not re-import" );
318324 }
319325
320326 Map <String , Object > rsUpdate = new HashMap <>();
0 commit comments