forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCellRangerGexCountStep.java
More file actions
719 lines (615 loc) · 30.4 KB
/
CellRangerGexCountStep.java
File metadata and controls
719 lines (615 loc) · 30.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package org.labkey.singlecell.run;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
import org.labkey.api.collections.CaseInsensitiveHashMap;
import org.labkey.api.data.CompareType;
import org.labkey.api.data.ConvertHelper;
import org.labkey.api.data.DbSchema;
import org.labkey.api.data.DbSchemaType;
import org.labkey.api.data.SimpleFilter;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.query.FieldKey;
import org.labkey.api.reader.Readers;
import org.labkey.api.sequenceanalysis.SequenceAnalysisService;
import org.labkey.api.sequenceanalysis.SequenceOutputFile;
import org.labkey.api.sequenceanalysis.model.AnalysisModel;
import org.labkey.api.sequenceanalysis.model.Readset;
import org.labkey.api.sequenceanalysis.pipeline.AbstractAlignmentStepProvider;
import org.labkey.api.sequenceanalysis.pipeline.AlignerIndexUtil;
import org.labkey.api.sequenceanalysis.pipeline.AlignmentOutputImpl;
import org.labkey.api.sequenceanalysis.pipeline.AlignmentStep;
import org.labkey.api.sequenceanalysis.pipeline.AlignmentStepProvider;
import org.labkey.api.sequenceanalysis.pipeline.AnalysisStep;
import org.labkey.api.sequenceanalysis.pipeline.CommandLineParam;
import org.labkey.api.sequenceanalysis.pipeline.IndexOutputImpl;
import org.labkey.api.sequenceanalysis.pipeline.PipelineContext;
import org.labkey.api.sequenceanalysis.pipeline.PipelineStepProvider;
import org.labkey.api.sequenceanalysis.pipeline.ReferenceGenome;
import org.labkey.api.sequenceanalysis.pipeline.SequenceAnalysisJobSupport;
import org.labkey.api.sequenceanalysis.pipeline.SequencePipelineService;
import org.labkey.api.sequenceanalysis.pipeline.ToolParameterDescriptor;
import org.labkey.api.sequenceanalysis.run.AbstractAlignmentPipelineStep;
import org.labkey.api.sequenceanalysis.run.SimpleScriptWrapper;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.api.writer.PrintWriters;
import org.labkey.singlecell.SingleCellSchema;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CellRangerGexCountStep extends AbstractAlignmentPipelineStep<CellRangerWrapper> implements AlignmentStep
{
public static final String LOUPE_CATEGORY = "10x Loupe File";
public CellRangerGexCountStep(AlignmentStepProvider<?> provider, PipelineContext ctx, CellRangerWrapper wrapper)
{
super(provider, ctx, wrapper);
}
public static class Provider extends AbstractAlignmentStepProvider<AlignmentStep>
{
public Provider()
{
super("CellRanger", "Cell Ranger is an alignment/analysis pipeline specific to 10x genomic data, and this can only be used on fastqs generated by 10x.", getCellRangerGexParams(null), PageFlowUtil.set("sequenceanalysis/field/GenomeFileSelectorField.js"), "https://support.10xgenomics.com/single-cell-gene-expression/software/pipelines/latest/what-is-cell-ranger", true, false, ALIGNMENT_MODE.MERGE_THEN_ALIGN);
}
@Override
public String getName()
{
return "CellRanger";
}
@Override
public String getDescription()
{
return null;
}
@Override
public AlignmentStep create(PipelineContext context)
{
return new CellRangerGexCountStep(this, context, new CellRangerWrapper(context.getLogger()));
}
}
public static List<ToolParameterDescriptor> getCellRangerGexParams(@Nullable List<ToolParameterDescriptor> additionalParams)
{
List<ToolParameterDescriptor> ret = Arrays.asList(
ToolParameterDescriptor.create("id", "Run ID Suffix", "If provided, this will be appended to the ID of this run (readset name will be first).", "textfield", new JSONObject(){{
put("allowBlank", true);
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--nosecondary"), "nosecondary", "Skip Secondary Analysis", "Add this flag to skip secondary analysis of the gene-barcode matrix (dimensionality reduction, clustering and visualization). Set this if you plan to use cellranger reanalyze or your own custom analysis.", "checkbox", new JSONObject(){{
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--r1-length"), "r1-length", "R1 Read Length", "Use this value for the first read length.", "ldk-integerfield", new JSONObject(){{
put("minValue", 0);
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--r2-length"), "r2-length", "R2 Read Length", "Use this value for the second read length.", "ldk-integerfield", new JSONObject(){{
put("minValue", 0);
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--expect-cells"), "expect-cells", "Expect Cells", "Expected number of recovered cells.", "ldk-integerfield", new JSONObject(){{
put("minValue", 0);
}}, 8000),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--force-cells"), "force-cells", "Force Cells", "Force pipeline to use this number of cells, bypassing the cell detection algorithm. Use this if the number of cells estimated by Cell Ranger is not consistent with the barcode rank plot.", "ldk-integerfield", new JSONObject(){{
put("minValue", 0);
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.createSwitch("--disable-ui"), "disable-ui", "Disable UI", "If checked, this will run cellranger with the optional web-based UI disabled.", "checkbox", new JSONObject(){{
put("checked", true);
}}, true),
ToolParameterDescriptor.createExpDataParam("gtfFile", "Gene File", "This is the ID of a GTF file containing genes from this genome.", "sequenceanalysis-genomefileselectorfield", new JSONObject()
{{
put("extensions", List.of("gtf"));
put("width", 400);
put("allowBlank", false);
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--chemistry"), "chemistry", "Chemistry", "This is usually left blank, in which case cellranger will auto-detect. Example values are: SC3Pv1, SC3Pv2, SC3Pv3, SC5P-PE, SC5P-R2, or SC5P-R1", "textfield", new JSONObject(){{
}}, null),
ToolParameterDescriptor.createCommandLineParam(CommandLineParam.create("--include-introns"), "includeIntrons", "Include Introns", "If selected, reads from introns will be included in the counts", "ldk-simplecombo", new JSONObject(){{
put("storeValues", "true;false");
put("value", "true");
}}, "true")
);
if (additionalParams != null)
{
ret = new ArrayList<>(ret);
ret.addAll(additionalParams);
}
return ret;
}
@Override
public boolean supportsGzipFastqs()
{
return true;
}
@Override
public String getAlignmentDescription()
{
return getAlignDescription(getProvider(), getPipelineCtx(), getStepIdx(), true, getWrapper().getVersionString());
}
protected static String getAlignDescription(PipelineStepProvider<?> provider, PipelineContext ctx, int stepIdx, boolean addAligner, String cellrangerVersion)
{
Integer gtfId = provider.getParameterByName("gtfFile").extractValue(ctx.getJob(), provider, stepIdx, Integer.class);
File gtfFile = ctx.getSequenceSupport().getCachedData(gtfId);
if (gtfFile == null)
{
ExpData d = ExperimentService.get().getExpData(gtfId);
if (d != null)
{
gtfFile = d.getFile();
}
}
List<String> lines = new ArrayList<>();
String includeIntrons = provider.getParameterByName("includeIntrons").extractValue(ctx.getJob(), provider, stepIdx, String.class, "false");
lines.add("Include Introns: " + includeIntrons);
if (addAligner)
{
lines.add("Aligner: " + provider.getName());
}
if (gtfFile != null)
{
lines.add("GTF: " + gtfFile.getName());
}
lines.add("Version: " + cellrangerVersion);
return StringUtils.join(lines, '\n');
}
@Override
public String getIndexCachedDirName(PipelineJob job)
{
Integer gtfId = getProvider().getParameterByName("gtfFile").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class);
if (gtfId == null)
{
throw new IllegalArgumentException("Missing gtfFile parameter");
}
return "cellRanger-" + gtfId;
}
@Override
public IndexOutput createIndex(ReferenceGenome referenceGenome, File outputDir) throws PipelineJobException
{
//NOTE: GTF filtering typically only necessary for pseudogenes. Assume this occurs upstream.
//cellranger mkgtf hg19-ensembl.gtf hg19-filtered-ensembl.gtf --attribute=gene_biotype:protein_coding
Integer gtfId = getProvider().getParameterByName("gtfFile").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class);
File gtfFile = getPipelineCtx().getSequenceSupport().getCachedData(gtfId);
IndexOutputImpl output = new IndexOutputImpl(referenceGenome);
File indexDir = new File(outputDir, getIndexCachedDirName(getPipelineCtx().getJob()));
boolean hasCachedIndex = AlignerIndexUtil.hasCachedIndex(this.getPipelineCtx(), getIndexCachedDirName(getPipelineCtx().getJob()), referenceGenome);
if (!hasCachedIndex)
{
getPipelineCtx().getLogger().info("Creating CellRanger Index");
//remove if directory exists
if (indexDir.exists())
{
try
{
FileUtils.deleteDirectory(indexDir);
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
}
output.addInput(gtfFile, "GTF File");
//NOTE: cellranger requires lines to have transcript_id and gene_id.
getPipelineCtx().getLogger().debug("Inspecting GTF for lines without gene_id or transcript_id");
int linesDropped = 0;
int exonsAdded = 0;
File gtfEdit = new File(indexDir.getParentFile(), FileUtil.getBaseName(gtfFile) + ".geneId.gtf");
try (CSVReader reader = new CSVReader(Readers.getReader(gtfFile), '\t', CSVWriter.NO_QUOTE_CHARACTER); CSVWriter writer = new CSVWriter(PrintWriters.getPrintWriter(gtfEdit), '\t', CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER))
{
String[] line;
while ((line = reader.readNext()) != null)
{
if (!line[0].startsWith("#") && line.length < 9)
{
linesDropped++;
continue;
}
//Drop lines lacking gene_id/transcript, or with empty gene_id:
if (!line[0].startsWith("#") && (!line[8].contains("gene_id") || !line[8].contains("transcript_id") || line[8].contains("gene_id \"\"") || line[8].contains("transcript_id \"\"")))
{
linesDropped++;
continue;
}
writer.writeNext(line);
}
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
if (linesDropped > 0)
{
getPipelineCtx().getLogger().info("dropped " + linesDropped + " lines lacking gene_id, transcript_id, or with an empty value for gene_id/transcript_id");
}
boolean useAlternateGtf = linesDropped > 0;
if (useAlternateGtf)
{
gtfFile = gtfEdit;
}
else
{
getPipelineCtx().getLogger().debug("no need to drop lines from GTF");
gtfEdit.delete();
}
List<String> args = new ArrayList<>();
args.add(getWrapper().getExe().getPath());
args.add("mkref");
args.add("--fasta=" + referenceGenome.getWorkingFastaFile().getPath());
args.add("--genes=" + gtfFile.getPath());
args.add("--genome=" + indexDir.getName());
Integer maxThreads = SequencePipelineService.get().getMaxThreads(getPipelineCtx().getLogger());
if (maxThreads != null)
{
args.add("--nthreads=" + maxThreads);
}
Integer maxRam = SequencePipelineService.get().getMaxRam();
if (maxRam != null)
{
args.add("--memgb=" + maxRam);
}
getWrapper().setWorkingDir(indexDir.getParentFile());
getWrapper().execute(args);
output.appendOutputs(referenceGenome.getWorkingFastaFile(), indexDir);
//recache if not already
AlignerIndexUtil.saveCachedIndex(hasCachedIndex, getPipelineCtx(), indexDir, getIndexCachedDirName(getPipelineCtx().getJob()), referenceGenome);
if (useAlternateGtf)
{
gtfEdit.delete();
}
}
return output;
}
@Override
public boolean canAlignMultiplePairsAtOnce()
{
return true;
}
private boolean shouldDiscardBam()
{
// NOTE: if downstream analyses are selected, always keep BAM
if (!SequencePipelineService.get().getSteps(getPipelineCtx().getJob(), AnalysisStep.class).isEmpty())
{
return false;
}
return !_alwaysRetainBam && getProvider().getParameterByName(AbstractAlignmentStepProvider.DISCARD_BAM).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Boolean.class, false);
}
private boolean _alwaysRetainBam = false;
public void setAlwaysRetainBam(boolean alwaysRetainBam)
{
_alwaysRetainBam = alwaysRetainBam;
}
@Override
public AlignmentOutput performAlignment(Readset rs, List<File> inputFastqs1, @Nullable List<File> inputFastqs2, File outputDirectory, ReferenceGenome referenceGenome, String basename, String readGroupId, @Nullable String platformUnit) throws PipelineJobException
{
AlignmentOutputImpl output = new AlignmentOutputImpl();
String idParam = StringUtils.trimToNull(getProvider().getParameterByName("id").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), String.class));
String id = CellRangerWrapper.getId(idParam, rs);
String alignmentMode = getProvider().getParameterByName(AbstractAlignmentStepProvider.ALIGNMENT_MODE_PARAM).extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx());
AbstractAlignmentStepProvider.ALIGNMENT_MODE mode = AbstractAlignmentStepProvider.ALIGNMENT_MODE.valueOf(alignmentMode);
List<Pair<File, File>> inputFastqs = new ArrayList<>();
for (int i = 0; i < inputFastqs1.size(); i++)
{
File inputFastq1 = inputFastqs1.get(i);
File inputFastq2 = inputFastqs2.get(i);
if (!inputFastq1.equals(rs.getReadData().get(0).getFile1()))
{
getPipelineCtx().getLogger().info("FASTQs appear to have been pre-processed, using local copies:");
if (rs.getReadData().size() > 1)
{
if (mode != AbstractAlignmentStepProvider.ALIGNMENT_MODE.MERGE_THEN_ALIGN)
{
throw new PipelineJobException("cellranger cannot be used with pre-processing unless MERGE_THEN_ALIGN is used");
}
}
}
inputFastqs.add(Pair.of(inputFastq1, inputFastq2));
}
List<String> args = new ArrayList<>(getWrapper().prepareCountArgs(output, id, outputDirectory, rs, inputFastqs, getClientCommandArgs("="), true));
Integer gtfId = getProvider().getParameterByName("gtfFile").extractValue(getPipelineCtx().getJob(), getProvider(), getStepIdx(), Integer.class);
File gtfFile = getPipelineCtx().getSequenceSupport().getCachedData(gtfId);
output.addInput(gtfFile, CellRangerWrapper.GTF_FILE);
File indexDir = AlignerIndexUtil.getIndexDir(referenceGenome, getIndexCachedDirName(getPipelineCtx().getJob()));
args.add("--transcriptome=" + indexDir.getPath());
args.add("--create-bam=" + !shouldDiscardBam());
getWrapper().setWorkingDir(outputDirectory);
//Note: we can safely assume only this server is working on these files, so if the _lock file exists, it was from a previous failed job.
File lockFile = new File(outputDirectory, id + "/_lock");
if (lockFile.exists())
{
getPipelineCtx().getLogger().info("Lock file exists, deleting: " + lockFile.getPath());
lockFile.delete();
}
getWrapper().execute(args);
File outdir = new File(outputDirectory, id);
outdir = new File(outdir, "outs");
File bam = new File(outdir, "possorted_genome_bam.bam");
if (!shouldDiscardBam())
{
if (!bam.exists())
{
throw new PipelineJobException("Unable to find file: " + bam.getPath());
}
output.setBAM(bam);
}
getWrapper().deleteSymlinks(getWrapper().getLocalFastqDir(outputDirectory));
try
{
String prefix = FileUtil.makeLegalName(rs.getName() + "_");
File outputHtml = new File(outdir, "web_summary.html");
if (!outputHtml.exists())
{
throw new PipelineJobException("Unable to find file: " + outputHtml.getPath());
}
File outputHtmlRename = new File(outdir, prefix + outputHtml.getName());
if (outputHtmlRename.exists())
{
outputHtmlRename.delete();
}
FileUtils.moveFile(outputHtml, outputHtmlRename);
String description = getAlignDescription(getProvider(), getPipelineCtx(), getStepIdx(), false, getWrapper().getVersionString());
output.addSequenceOutput(outputHtmlRename, rs.getName() + " 10x Count Summary", "10x Run Summary", rs.getRowId(), null, referenceGenome.getGenomeId(), description);
File loupe = new File(outdir, "cloupe.cloupe");
if (loupe.exists())
{
File loupeRename = new File(outdir, prefix + loupe.getName());
if (loupeRename.exists())
{
loupeRename.delete();
}
FileUtils.moveFile(loupe, loupeRename);
output.addSequenceOutput(loupeRename, rs.getName() + " 10x Loupe File", LOUPE_CATEGORY, rs.getRowId(), null, referenceGenome.getGenomeId(), description);
}
else
{
getPipelineCtx().getLogger().info("loupe file not found: " + loupe.getPath());
}
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
//NOTE: this folder has many unnecessary files and symlinks that get corrupted when we rename the main outputs
File directory = new File(outdir.getParentFile(), "SC_RNA_COUNTER_CS");
if (directory.exists())
{
//NOTE: this will have lots of symlinks, including corrupted ones, which java handles badly
new SimpleScriptWrapper(getPipelineCtx().getLogger()).execute(Arrays.asList("rm", "-Rf", directory.getPath()));
}
else
{
getPipelineCtx().getLogger().warn("Unable to find folder: " + directory.getPath());
}
return output;
}
@Override
public boolean doAddReadGroups()
{
return false;
}
@Override
public boolean doSortIndexBam()
{
return true;
}
@Override
public boolean alwaysCopyIndexToWorkingDir()
{
return false;
}
@Override
public void complete(SequenceAnalysisJobSupport support, AnalysisModel model, Collection<SequenceOutputFile> outputFilesCreated) throws PipelineJobException
{
SequenceOutputFile outputForData = outputFilesCreated.stream().filter(x -> LOUPE_CATEGORY.equals(x.getCategory())).findFirst().orElse(null);
if (outputForData == null)
{
outputForData = outputFilesCreated.stream().filter(x -> "10x Run Summary".equals(x.getCategory())).findFirst().orElseThrow();
}
File outsDir = outputForData.getFile().getParentFile();
Long dataId = outputForData.getDataId();
if (dataId == null)
{
throw new PipelineJobException("Unable to find dataId for output file");
}
if (model.getRowId() == null)
{
throw new PipelineJobException("Unable to find rowId for analysis");
}
File metrics = new File(outsDir, "metrics_summary.csv");
if (metrics.exists())
{
getPipelineCtx().getLogger().debug("adding 10x metrics");
try (CSVReader reader = new CSVReader(Readers.getReader(metrics)))
{
String[] line;
String[] header = null;
String[] metricValues = null;
int i = 0;
while ((line = reader.readNext()) != null)
{
if (i == 0)
{
header = line;
}
else
{
metricValues = line;
break;
}
i++;
}
TableInfo ti = DbSchema.get("sequenceanalysis", DbSchemaType.Module).getTable("quality_metrics");
//NOTE: if this job errored and restarted, we may have duplicate records:
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("readset"), model.getReadset());
filter.addCondition(FieldKey.fromString("analysis_id"), model.getRowId(), CompareType.EQUAL);
filter.addCondition(FieldKey.fromString("dataid"), dataId, CompareType.EQUAL);
filter.addCondition(FieldKey.fromString("category"), "Cell Ranger", CompareType.EQUAL);
filter.addCondition(FieldKey.fromString("container"), getPipelineCtx().getJob().getContainer().getId(), CompareType.EQUAL);
TableSelector ts = new TableSelector(ti, PageFlowUtil.set("rowid"), filter, null);
if (ts.exists())
{
getPipelineCtx().getLogger().info("Deleting existing QC metrics (probably from prior restarted job)");
ts.getArrayList(Integer.class).forEach(rowid -> {
Table.delete(ti, rowid);
});
}
for (int j = 0; j < header.length; j++)
{
Map<String, Object> toInsert = new CaseInsensitiveHashMap<>();
toInsert.put("container", getPipelineCtx().getJob().getContainer().getId());
toInsert.put("createdby", getPipelineCtx().getJob().getUser().getUserId());
toInsert.put("created", new Date());
toInsert.put("readset", model.getReadset());
toInsert.put("analysis_id", model.getRowId());
toInsert.put("dataid", dataId);
toInsert.put("category", "Cell Ranger");
toInsert.put("metricname", header[j]);
metricValues[j] = metricValues[j].replaceAll(",", "");
Object val = metricValues[j];
if (metricValues[j].contains("%"))
{
metricValues[j] = metricValues[j].replaceAll("%", "");
Double d = ConvertHelper.convert(metricValues[j], Double.class);
d = d / 100.0;
val = d;
}
toInsert.put("metricvalue", val);
Table.insert(getPipelineCtx().getJob().getUser(), ti, toInsert);
}
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
}
else
{
throw new PipelineJobException("unable to find metrics file: " + metrics.getPath());
}
TableInfo cDNA = SingleCellSchema.getInstance().getSchema().getTable(SingleCellSchema.TABLE_CDNAS);
TableInfo singlecellDatasets = SingleCellSchema.getInstance().getSchema().getTable(SingleCellSchema.TABLE_SINGLECELL_DATASETS);
for (SequenceOutputFile so : outputFilesCreated)
{
if (LOUPE_CATEGORY.equals(so.getCategory()))
{
getPipelineCtx().getLogger().debug("Creating singlecell dataset record");
if (so.getRowid() == null || so.getRowid() == 0)
{
throw new PipelineJobException("The outputfiles have not been created in the database yet");
}
Map<String, Object> toInsert = new HashMap<>();
toInsert.put("loupeFileId", so.getRowid());
toInsert.put("readsetId", so.getReadset());
Readset rs = SequenceAnalysisService.get().getReadset(so.getReadset(), getPipelineCtx().getJob().getUser());
toInsert.put("name", rs.getName());
Integer maxCDNA = new TableSelector(cDNA, PageFlowUtil.set("rowid"), new SimpleFilter(FieldKey.fromString("readsetId"), rs.getReadsetId()), null).getArrayList(Integer.class).stream().max(Integer::compareTo).orElse(null);
toInsert.put("cDNAId", maxCDNA);
toInsert.put("container", so.getContainer());
toInsert.put("created", so.getCreated());
toInsert.put("createdby", so.getCreatedby());
toInsert.put("modified", so.getModified());
toInsert.put("modifiedby", so.getModifiedby());
Table.insert(getPipelineCtx().getJob().getUser(), singlecellDatasets, toInsert);
}
}
}
public enum Chemistry
{
// See: https://kb.10xgenomics.com/s/article/115004506263-What-is-a-barcode-inclusion-list-formerly-barcode-whitelist
// cellranger-x.y.z/lib/python/cellranger/barcodes/
FivePE_V3("Single Cell 5' PE v3", "3M-5pgex-jan-2023.txt.gz"),
FivePE_V2("Single Cell 5' PE v2", "737k-august-2016.txt"),
FivePE_V1("Single Cell 5' PE", "737k-august-2016.txt");
// Single Cell 3' v1: 737K-april-2014_rc.txt
final String _label;
final String _inclusionListFile;
Chemistry(String label, String inclusionListFile)
{
_label = label;
_inclusionListFile = inclusionListFile;
}
public File getInclusionListFile(Logger logger) throws PipelineJobException
{
File exe = new CellRangerWrapper(logger).getExe().getAbsoluteFile();
logger.debug("cellranger executable: " + exe.getPath());
if (Files.isSymbolicLink(exe.toPath()))
{
try
{
Path exePath = Files.readSymbolicLink(exe.toPath());
logger.debug("cellranger symlink target: " + exePath);
if (!exePath.isAbsolute())
{
File parent = exe.getParentFile();
exePath = parent.toPath().resolve(exePath);
logger.debug("resolved symlink target: " + exePath);
}
exe = exePath.toFile();
logger.debug("cellranger resolved symlink target: " + exe.getPath());
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
}
File il = new File(exe.getParentFile(), "../lib/python/cellranger/barcodes/" + _inclusionListFile);
if (!il.exists())
{
throw new PipelineJobException("Unable to find file: " + il.getPath());
}
return il;
}
public static Chemistry getByLabel(String label)
{
for (Chemistry c : Chemistry.values())
{
if (c._label.equals(label))
{
return c;
}
}
throw new IllegalArgumentException("Unknown chemistry: [" + label + "]");
}
}
public static Chemistry inferChemistry(File cloupeFile) throws PipelineJobException
{
File html = new File(cloupeFile.getPath().replaceAll("_cloupe.cloupe$", "_web_summary.html"));
if (!html.exists())
{
throw new IllegalArgumentException("Missing file: " + html.getPath());
}
final Pattern pattern = Pattern.compile("\\[\"Chemistry\",[ ]{0,1}\"(.*?)\"],");
try (BufferedReader reader = Readers.getReader(html))
{
String line;
while ((line = reader.readLine()) != null)
{
Matcher m = pattern.matcher(line);
if (m.find())
{
String chem = m.group(1);
return Chemistry.getByLabel(chem);
}
}
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
throw new IllegalArgumentException("Unable to infer chemistry for file: " + html.getPath());
}
}