| 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 |
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 |
|
| 40 |
|
| 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>>(); |
| 73 |
String[] microsatName = new String[2]; |
| 74 |
microsatName[1] = "unnamed.microsat"; |
| 75 |
String line = reader.readLine(); |
| 76 |
while (line.startsWith("#")) { |
| 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 |
|
| 85 |
String[] names = line.trim().split("[" + delimiter + " ]+"); |
| 86 |
int colLen = names.length; |
| 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++) { |
| 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) { |
| 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 |
|
| 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 |
|
| 123 |
|
| 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 |
|
| 132 |
|
| 133 |
Patterns microsatPat; |
| 134 |
for (int i = 1; i < data.size(); i++) { |
| 135 |
|
| 136 |
List<Integer> pattern; |
| 137 |
Taxa taxa = new Taxa(); |
| 138 |
|
| 139 |
if ((i + 1 < data.size()) && names[i].equalsIgnoreCase(names[i + 1])) { |
| 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 |
|
| 163 |
Taxon t = taxaDiploid.getTaxon(v); |
| 164 |
if (!taxa.contains(t)) { |
| 165 |
taxa.addTaxon(t); |
| 166 |
pattern.add(parseInt(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 |
|
| 177 |
Taxon t = taxaDiploid.getTaxon(v + size); |
| 178 |
if (!taxa.contains(t)) { |
| 179 |
taxa.addTaxon(t); |
| 180 |
pattern.add(parseInt(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 { |
| 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 |
|
| 197 |
Taxon t = taxaHaploid.getTaxon(v); |
| 198 |
if (!taxa.contains(t)) { |
| 199 |
taxa.addTaxon(t); |
| 200 |
pattern.add(parseInt(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; |
| 228 |
} else { |
| 229 |
return Integer.parseInt(s); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
private boolean isUnknownChar(String s) { |
| 234 |
return parseInt(s) == -1; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
} |