Debian Codegraph @

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

raw
1 /*
2 * MIT License
3 *
4 * Copyright 2017 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 htsjdk.samtools.util.IOUtil;
27 import org.apache.commons.lang3.StringUtils;
28 import org.broadinstitute.dropseqrna.utils.io.ErrorCheckingPrintStream;
29 import picard.util.BasicInputParser;
30
31 import java.io.File;
32 import java.io.PrintStream;
33 import java.util.*;
34
35 public class MultiCell {
36
37 private final List<String> cellBarcodes;
38 private final String mergedCellName;
39
40 public MultiCell(final List<String> cellBarcodes) {
41 this.cellBarcodes=cellBarcodes;
42 mergedCellName=StringUtils.join(cellBarcodes, ":");
43 }
44
45 public List<String> getCellBarcodes () {
46 return this.cellBarcodes;
47 }
48
49 public String getMultiCellName() {
50 return mergedCellName;
51 }
52
53 public static List<MultiCell> parseFile(final File f) {
54 List<MultiCell> result = new ArrayList<>();
55 BasicInputParser parser = new BasicInputParser(false, 1, f);
56 while (parser.hasNext()) {
57 String[] line = parser.next();
58 String [] cellIDs=line[0].split(":");
59 MultiCell d = new MultiCell(Arrays.asList(cellIDs));
60 result.add(d);
61 }
62 parser.close();
63 return (result);
64 }
65
66 /**
67 * Get a map from each individual cell name to the multi-cell name.
68 * @return A map with the key of a single cell, and a value a list of the multi cell name(s).
69 * Multiple keys will have the same value.
70 */
71 public static Map<String, List<String>> getMap (final Collection<MultiCell> multiCells) {
72 Map<String, List<String>> result = new HashMap<>();
73
74 for (MultiCell m: multiCells)
75 for (String singleCell: m.cellBarcodes) {
76 List<String> l= result.get(singleCell);
77 if (l==null)
78 l= new ArrayList<>();
79 l.add(m.getMultiCellName());
80 result.put(singleCell, l);
81 }
82 return result;
83 }
84
85 public static List<MultiCell> generateMultiCells (final Collection<String> cellBarcodes, final int numMultiCells, final int multiplicity) {
86 if ((numMultiCells*multiplicity)>cellBarcodes.size())
87 throw new IllegalArgumentException("Requested ["+ numMultiCells+" multi-cells with multiplicity ["+ multiplicity +"] "
88 + "from ["+ cellBarcodes.size()+"] You need more cell barcodes or fewer multi-cells / lower multiplicity");
89
90
91 List<MultiCell> result = new ArrayList<>(numMultiCells);
92
93 // make a new list holding the cell barcodes and shuffle it.
94 List <String> cb = new ArrayList<>(cellBarcodes);
95 Collections.shuffle(cb);
96 Iterator<String> iter = cb.iterator();
97
98 for (int i=0; i<numMultiCells; i++) {
99 // generate single multiCell
100 List<String> currentCB = new ArrayList<>(multiplicity);
101 for (int j=0; j<multiplicity; j++)
102 currentCB.add(iter.next());
103 MultiCell mc = new MultiCell(currentCB);
104 result.add(mc);
105 }
106
107 return (result);
108
109 }
110
111 public static void writeToFile (final List<MultiCell> multiCells, final File outFile) {
112 PrintStream writer= new ErrorCheckingPrintStream(IOUtil.openFileForWriting(outFile));
113 for (MultiCell mc: multiCells)
114 writer.println(mc.getMultiCellName());
115 writer.close();
116 }
117
118 @Override
119 public String toString() {
120 return "MultiCell [" + this.mergedCellName +"] individual cell list " + this.cellBarcodes.toString();
121 }
122
123 }