Debian Codegraph @

src/java/org/broadinstitute/dropseqrna/barnyard/digitalallelecounts/sampleassignment/MergeCellToSampleAssignments.java

raw
1 /*
2 * MIT License
3 *
4 * Copyright 2020 Broad Institute
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 package org.broadinstitute.dropseqrna.barnyard.digitalallelecounts.sampleassignment;
25
26 import java.io.File;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import htsjdk.samtools.metrics.Header;
31 import htsjdk.samtools.util.IOUtil;
32 import htsjdk.samtools.util.Log;
33
34 import org.broadinstitute.barclay.argparser.Argument;
35 import org.broadinstitute.barclay.argparser.CommandLineProgramProperties;
36 import org.broadinstitute.dropseqrna.cmdline.DropSeq;
37 import org.broadinstitute.dropseqrna.utils.FileListParsingUtils;
38 import org.broadinstitute.dropseqrna.utils.ReportFileUtil;
39 import org.broadinstitute.dropseqrna.utils.io.ErrorCheckingPrintStream;
40
41 import picard.cmdline.CommandLineProgram;
42 import picard.cmdline.StandardOptionDefinitions;
43
44 /**
45 * @author skashin
46 */
47 @CommandLineProgramProperties(summary = "Merges cells to samples assignment files.",
48 oneLineSummary = "Merges cells to samples assignment files.",
49 programGroup = DropSeq.class)
50 public class MergeCellToSampleAssignments extends CommandLineProgram {
51
52 private static final Log log = Log.getInstance(MergeCellToSampleAssignments.class);
53
54 @Argument(shortName = StandardOptionDefinitions.INPUT_SHORT_NAME, doc = "The input assignment files to be merged.", minElements = 1)
55 public List<File> INPUT;
56
57 @Argument(shortName = StandardOptionDefinitions.OUTPUT_SHORT_NAME, doc="The merged assignment file.")
58 public File OUTPUT;
59
60 @Argument (doc="Set to true to emit a column that contains a population average likelihood score aggregated across all SNPs. Setting this to false is purely for backwards compatability", optional=true)
61 public Boolean EMIT_POPULATION_LIKELIHOOD=true;
62
63 @Override
64 protected int doWork() {
65 IOUtil.assertFileIsWritable(OUTPUT);
66
67 INPUT = FileListParsingUtils.expandFileList(INPUT);
68
69 List < String > samples = null;
70 CellCollectionSampleLikelihoodCollection mergedCollection = null;
71 for (File assignmentFile : INPUT) {
72 CellCollectionSampleLikelihoodCollection collection = CellCollectionSampleLikelihoodCollection.parseFile(assignmentFile);
73 if (mergedCollection == null) {
74 mergedCollection = collection;
75 samples = collection.getSamples();
76 } else {
77 if (!collection.getSamples().equals(samples)) {
78 throw new RuntimeException("Make sure all assignment files contain the same list of samples");
79 }
80
81 for (String cellBarcode : collection.getCellBarcodes()) {
82 if (mergedCollection.getCellBarcodes().contains(cellBarcode)) {
83 throw new RuntimeException("Cell barcode " + cellBarcode + " is in multiple assignment files");
84 }
85 mergedCollection.add(collection.getLikelihoodCollection(cellBarcode));
86 }
87 }
88 }
89
90 ErrorCheckingPrintStream out = new ErrorCheckingPrintStream(IOUtil.openFileForWriting(this.OUTPUT));
91 for (String comment : ReportFileUtil.readComments(INPUT))
92 out.println(comment);
93 for (Header header : getDefaultHeaders())
94 out.println(ReportFileUtil.COMMENT_PREFIX + header);
95
96 AssignCellsToSamples.writeBestLikelihoods(samples, mergedCollection, null, out, this.EMIT_POPULATION_LIKELIHOOD);
97 out.close();
98
99 return 0;
100 }
101 }