| 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.multisample; |
| 25 |
|
| 26 |
import java.io.File; |
| 27 |
import java.io.PrintStream; |
| 28 |
import java.util.HashSet; |
| 29 |
import java.util.List; |
| 30 |
import java.util.Set; |
| 31 |
|
| 32 |
import htsjdk.samtools.metrics.Header; |
| 33 |
import org.broadinstitute.barclay.argparser.Argument; |
| 34 |
import org.broadinstitute.barclay.argparser.CommandLineProgramProperties; |
| 35 |
import org.broadinstitute.dropseqrna.cmdline.DropSeq; |
| 36 |
|
| 37 |
import htsjdk.samtools.util.IOUtil; |
| 38 |
import htsjdk.samtools.util.Log; |
| 39 |
|
| 40 |
import org.broadinstitute.dropseqrna.utils.FileListParsingUtils; |
| 41 |
import org.broadinstitute.dropseqrna.utils.ReportFileUtil; |
| 42 |
import org.broadinstitute.dropseqrna.utils.io.ErrorCheckingPrintStream; |
| 43 |
import picard.cmdline.CommandLineProgram; |
| 44 |
import picard.cmdline.StandardOptionDefinitions; |
| 45 |
import picard.util.TabbedInputParser; |
| 46 |
|
| 47 |
/** |
| 48 |
* @author skashin |
| 49 |
*/ |
| 50 |
@CommandLineProgramProperties(summary = "Merges doublet detection reports.", |
| 51 |
oneLineSummary = "Merges doublet detection reports.", |
| 52 |
programGroup = DropSeq.class) |
| 53 |
public class MergeDoubletAssignments extends CommandLineProgram { |
| 54 |
|
| 55 |
private static final Log log = Log.getInstance(MergeDoubletAssignments.class); |
| 56 |
|
| 57 |
@Argument(shortName = StandardOptionDefinitions.INPUT_SHORT_NAME, doc = "The input doublet detection reports to be merged.", minElements = 1) |
| 58 |
public List<File> INPUT; |
| 59 |
|
| 60 |
@Argument(shortName = StandardOptionDefinitions.OUTPUT_SHORT_NAME, doc="The merged doublet detection report.") |
| 61 |
public File OUTPUT; |
| 62 |
|
| 63 |
|
| 64 |
@Override |
| 65 |
protected int doWork() { |
| 66 |
IOUtil.assertFileIsWritable(OUTPUT); |
| 67 |
|
| 68 |
INPUT = FileListParsingUtils.expandFileList(INPUT); |
| 69 |
|
| 70 |
PrintStream writer = new ErrorCheckingPrintStream(IOUtil.openFileForWriting(OUTPUT)); |
| 71 |
for (String comment : ReportFileUtil.readComments(INPUT)) |
| 72 |
writer.println(comment); |
| 73 |
for (Header header : getDefaultHeaders()) |
| 74 |
writer.println(ReportFileUtil.COMMENT_PREFIX + header); |
| 75 |
|
| 76 |
String header = null; |
| 77 |
TabbedInputParser parser = null; |
| 78 |
final Set<String> cellBarcodes = new HashSet<>(); |
| 79 |
try { |
| 80 |
for (File assignmentFile : INPUT) { |
| 81 |
parser = new TabbedInputParser(false, assignmentFile); |
| 82 |
|
| 83 |
if (!parser.hasNext()) { |
| 84 |
throw new RuntimeException("Assignments file " + assignmentFile.getAbsolutePath() + " does not contain a header"); |
| 85 |
} |
| 86 |
String[] fields = parser.next(); |
| 87 |
if (!fields[0].equals("cell")) { |
| 88 |
throw new RuntimeException("The first field in the header line of the assignments file " + assignmentFile.getAbsolutePath() + " should be 'cell'"); |
| 89 |
} |
| 90 |
if (header == null) { |
| 91 |
header = parser.getCurrentLine(); |
| 92 |
writer.println(header); |
| 93 |
} |
| 94 |
|
| 95 |
while (parser.hasNext()) { |
| 96 |
fields = parser.next(); |
| 97 |
String cellBarcode = fields[0]; |
| 98 |
if (cellBarcodes.contains(cellBarcode)) { |
| 99 |
throw new RuntimeException("Cell barcode " + cellBarcode + " is present in multiple assignment files"); |
| 100 |
} |
| 101 |
writer.println(parser.getCurrentLine()); |
| 102 |
cellBarcodes.add(cellBarcode); |
| 103 |
} |
| 104 |
} |
| 105 |
} finally { |
| 106 |
parser.close(); |
| 107 |
writer.close(); |
| 108 |
} |
| 109 |
|
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
public static void main(final String[] args) { |
| 114 |
new MergeDoubletAssignments().instanceMainWithExit(args); |
| 115 |
} |
| 116 |
} |