Debian Codegraph @

src/dr/evolution/io/MicroSatImporter.java

raw symbols SCIP
1 /*
2 * MicroSatImporter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.io;
27
28 import dr.evolution.alignment.Patterns;
29 import dr.evolution.datatype.Microsatellite;
30 import dr.evolution.util.Taxa;
31 import dr.evolution.util.Taxon;
32
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 /**
39 * @author Alexei Drummond
40 * @author Walter Xie
41 */
42 public class MicroSatImporter implements PatternImporter {
43 protected final BufferedReader reader;
44 protected String delimiter;
45 protected Taxa unionSetTaxonList = new Taxa();
46 protected Microsatellite microsatellite;
47 protected boolean hasDifferentTaxon = false;
48
49 public MicroSatImporter(BufferedReader reader) {
50 this(reader, "\t");
51 }
52
53 public MicroSatImporter(BufferedReader reader, String delimiter) {
54 this.reader = reader;
55 this.delimiter = delimiter;
56 }
57
58 public Taxa getUnionSetTaxonList() throws IOException, Importer.ImportException {
59 return unionSetTaxonList;
60 }
61
62 public Microsatellite getMicrosatellite() {
63 return microsatellite;
64 }
65
66 public boolean isHasDifferentTaxon() {
67 return hasDifferentTaxon;
68 }
69
70 public List<Patterns> importPatterns() throws IOException, Importer.ImportException {
71 List<Patterns> microsatPatList = new ArrayList<Patterns>();
72 List<List<String>> data = new ArrayList<List<String>>(); // 1st List<String> is taxon names
73 String[] microsatName = new String[2]; // microsatName[0] is keyword, microsatName[1] is name
74 microsatName[1] = "unnamed.microsat";
75 String line = reader.readLine();
76 while (line.startsWith("#")) { // comments
77 if (line.toUpperCase().contains("NAME")) {
78 microsatName = line.trim().split("[" + delimiter + " ]+");
79 if (microsatName[1] == null || microsatName[1].length() < 1)
80 throw new Importer.ImportException("Improper microsatellite name : " + microsatName[1]);
81 }
82 line = reader.readLine();
83 }
84 // read locus (microsat pattern) names in the 1st row after comments, where 1st element is id
85 String[] names = line.trim().split("[" + delimiter + " ]+"); // trim trailing whitespace ?
86 int colLen = names.length; // for validation
87
88 if (colLen < 2) throw new Importer.ImportException("Import file must have more than 1 columns : " + colLen);
89
90 for (int i = 0; i < colLen; i++) { // init data
91 List<String> l = new ArrayList<String>();
92 data.add(l);
93 }
94
95 int min = Integer.MAX_VALUE;
96 int max = Integer.MIN_VALUE;
97
98 line = reader.readLine();
99 while (line != null) { // read data
100 String[] dataLine = line.trim().split("[" + delimiter + " ]+");
101
102 if (dataLine.length != colLen)
103 throw new Importer.ImportException("The number of name columns are different with values columns," +
104 "\nplease use only letters or numbers in the name.");
105 // + "\ndataLine.length = " + dataLine.length + ", colLen = " + colLen);
106
107 for (int i = 0; i < dataLine.length; i++) {
108 data.get(i).add(dataLine[i]);
109 if (i > 0) {
110 int v = parseInt(dataLine[i]);
111 if (v != Microsatellite.UNKNOWN_STATE_LENGTH) {
112 if (min > v) min = v;
113 if (max < v) max = v;
114 }
115 }
116 }
117
118 line = reader.readLine();
119 }
120
121 if (max < min) throw new Importer.ImportException("Importing invalid data: max < min !");
122 // if (min - 2 < 0) throw new Importer.ImportException("Importing invaild data: min-2 < 0 where min = " + min);
123 // The min also = 1 and max should be the longest repeat length + 2.
124 microsatellite = new Microsatellite(microsatName[1], 1, max + 2, 1);
125
126 Taxa taxaHaploid = new Taxa();
127 for (String name : data.get(0)) {
128 Taxon t = new Taxon(name);
129 taxaHaploid.addTaxon(t);
130 }
131 // unionSetTaxonList.addTaxa(taxaHaploid);
132
133 Patterns microsatPat;
134 for (int i = 1; i < data.size(); i++) { // create pattern
135 // List<Integer> pattern = new ArrayList<Integer>();
136 List<Integer> pattern;
137 Taxa taxa = new Taxa();
138
139 if ((i + 1 < data.size()) && names[i].equalsIgnoreCase(names[i + 1])) { // diploid: Locus2 Locus2
140 Taxa taxaDiploid = new Taxa();
141 for (String name : data.get(0)) {
142 Taxon t = new Taxon(names[i] + "_1_" + name);
143 taxaDiploid.addTaxon(t);
144 }
145 for (String name : data.get(0)) {
146 Taxon t = new Taxon(names[i] + "_2_" + name);
147 taxaDiploid.addTaxon(t);
148 }
149
150 if (unionSetTaxonList.containsAny(taxaDiploid))
151 throw new Importer.ImportException("Importing invalid data: duplicate taxon name in this locus : " + names[i]);
152
153 unionSetTaxonList.addTaxa(taxaDiploid);
154 hasDifferentTaxon = true;
155
156 pattern = new ArrayList<Integer>();
157 String value;
158 int size = data.get(i).size();
159
160 for (int v = 0; v < size; v++) {
161 value = data.get(i).get(v);
162 // if (!isUnknownChar(value)) {
163 Taxon t = taxaDiploid.getTaxon(v);
164 if (!taxa.contains(t)) {
165 taxa.addTaxon(t);
166 pattern.add(parseInt(value));//microsatellite.getState(value);
167 if (!unionSetTaxonList.contains(t)) {
168 unionSetTaxonList.addTaxon(t);
169 if (i > 1) hasDifferentTaxon = true;
170 }
171 }
172 // }
173 }
174 for (int v = 0; v < data.get(i + 1).size(); v++) {
175 value = data.get(i + 1).get(v);
176 // if (!isUnknownChar(value)) {
177 Taxon t = taxaDiploid.getTaxon(v + size);
178 if (!taxa.contains(t)) {
179 taxa.addTaxon(t);
180 pattern.add(parseInt(value));//microsatellite.getState(value);
181 if (!unionSetTaxonList.contains(t)) {
182 unionSetTaxonList.addTaxon(t);
183 if (i > 1) hasDifferentTaxon = true;
184 }
185 }
186 // }
187 }
188
189 i++;
190
191 } else { // haploid Locus1
192 pattern = new ArrayList<Integer>();
193
194 for (int v = 0; v < data.get(i).size(); v++) {
195 String value = data.get(i).get(v);
196 // if (!isUnknownChar(value)) {
197 Taxon t = taxaHaploid.getTaxon(v);
198 if (!taxa.contains(t)) {
199 taxa.addTaxon(t);
200 pattern.add(parseInt(value));//microsatellite.getState(value);
201 if (!unionSetTaxonList.contains(t)) {
202 unionSetTaxonList.addTaxon(t);
203 if (i > 1) hasDifferentTaxon = true;
204 }
205 }
206 // }
207 }
208
209 }
210 int[] p = new int[pattern.size()];
211 for (int v = 0; v < pattern.size(); v++) {
212 p[v] = pattern.get(v);
213 }
214
215 microsatPat = new Patterns(microsatellite, taxa);
216
217 microsatPat.addPattern(p);
218 microsatPat.setId(names[i]);
219 microsatPatList.add(microsatPat);
220 }
221
222 return microsatPatList;
223 }
224
225 private int parseInt(String s) {
226 if (s.charAt(0) == Microsatellite.UNKNOWN_CHARACTER) {
227 return Microsatellite.UNKNOWN_STATE_LENGTH; // -1
228 } else {
229 return Integer.parseInt(s);
230 }
231 }
232
233 private boolean isUnknownChar(String s) {
234 return parseInt(s) == -1; // -1
235 }
236
237 /*
238 id Locus1 Locus2 Locus2 Locus3 Locus4 Locus4 Locus5 Locus6
239 T1 5 6 ? 20 ? ? ? 11
240 T2 5 6 ? 12 ? ? ? 12
241 T3 8 6 4 16 9 9 ? 13
242 T4 12 ? 6 1 9 12 ? 4
243 T5 17 ? 9 18 7 7 ? 5
244 T6 19 ? 5 14 12 12 ? 6
245 */
246 }