1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.io; |
22 |
|
|
23 |
|
import jalview.datamodel.Sequence; |
24 |
|
import jalview.datamodel.SequenceI; |
25 |
|
|
26 |
|
import java.io.IOException; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
@author |
57 |
|
|
58 |
|
|
59 |
|
|
|
|
| 0% |
Uncovered Elements: 111 (111) |
Complexity: 26 |
Complexity Density: 0.37 |
|
60 |
|
public class PhylipFile extends AlignFile |
61 |
|
{ |
62 |
|
|
63 |
|
public static final String FILE_DESC = "PHYLIP"; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@see@link |
68 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
0 |
public PhylipFile()... |
70 |
|
{ |
71 |
0 |
super(); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
@throws |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
79 |
0 |
public PhylipFile(FileParse source) throws IOException... |
80 |
|
{ |
81 |
0 |
super(source); |
82 |
|
} |
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
@param |
87 |
|
@throws |
88 |
|
@see@link |
89 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
90 |
0 |
public PhylipFile(String inFile, DataSourceType sourceType)... |
91 |
|
throws IOException |
92 |
|
{ |
93 |
0 |
super(inFile, sourceType); |
94 |
|
} |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@see@link |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 54 (54) |
Complexity: 12 |
Complexity Density: 0.35 |
|
101 |
0 |
@Override... |
102 |
|
public void parse() throws IOException |
103 |
|
{ |
104 |
0 |
try |
105 |
|
{ |
106 |
|
|
107 |
|
|
108 |
0 |
String line = nextLine(); |
109 |
0 |
String[] lineElements = line.trim().split("\\s+"); |
110 |
0 |
if (lineElements.length < 2) |
111 |
|
{ |
112 |
0 |
throw new IOException( |
113 |
|
"First line must contain the number of specifies and number of characters"); |
114 |
|
} |
115 |
|
|
116 |
0 |
int numberSpecies = Integer.parseInt(lineElements[0]), |
117 |
|
numberCharacters = Integer.parseInt(lineElements[1]); |
118 |
|
|
119 |
0 |
if (numberSpecies <= 0) |
120 |
|
{ |
121 |
|
|
122 |
|
|
123 |
0 |
return; |
124 |
|
} |
125 |
|
|
126 |
0 |
SequenceI[] sequenceElements = new Sequence[numberSpecies]; |
127 |
0 |
StringBuffer[] sequences = new StringBuffer[numberSpecies]; |
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
0 |
for (int i = 0; i < numberSpecies; i++) |
134 |
|
{ |
135 |
0 |
line = nextLine(); |
136 |
|
|
137 |
|
|
138 |
0 |
String potentialName = line.substring(0, 10); |
139 |
0 |
int tabIndex = potentialName.indexOf('\t'); |
140 |
0 |
if (tabIndex == -1) |
141 |
|
{ |
142 |
0 |
sequenceElements[i] = parseId(validateName(potentialName)); |
143 |
0 |
sequences[i] = new StringBuffer( |
144 |
|
removeWhitespace(line.substring(10))); |
145 |
|
} |
146 |
|
else |
147 |
|
{ |
148 |
0 |
sequenceElements[i] = parseId( |
149 |
|
validateName(potentialName.substring(0, tabIndex))); |
150 |
0 |
sequences[i] = new StringBuffer( |
151 |
|
removeWhitespace(line.substring(tabIndex))); |
152 |
|
} |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
0 |
if ((sequences[0]).length() != numberCharacters) |
157 |
|
{ |
158 |
|
|
159 |
0 |
int i = 0; |
160 |
0 |
for (line = nextLine(); line != null; line = nextLine()) |
161 |
|
{ |
162 |
|
|
163 |
0 |
if (line.length() > 0) |
164 |
|
{ |
165 |
0 |
sequences[i++].append(removeWhitespace(line)); |
166 |
|
} |
167 |
|
|
168 |
0 |
if (i == sequences.length) |
169 |
|
{ |
170 |
0 |
i = 0; |
171 |
|
} |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
0 |
for (int i = 0; i < numberSpecies; i++) |
177 |
|
{ |
178 |
|
|
179 |
0 |
if (sequences[i].length() != numberCharacters) |
180 |
|
{ |
181 |
0 |
throw new IOException(sequenceElements[i].getName() |
182 |
|
+ " sequence is incorrect length - should be " |
183 |
|
+ numberCharacters + " but is " + sequences[i].length()); |
184 |
|
} |
185 |
0 |
sequenceElements[i].setSequence(sequences[i].toString()); |
186 |
0 |
seqs.add(sequenceElements[i]); |
187 |
|
} |
188 |
|
|
189 |
|
} catch (IOException e) |
190 |
|
{ |
191 |
0 |
jalview.bin.Console.errPrintln("Exception parsing PHYLIP file " + e); |
192 |
0 |
e.printStackTrace(System.err); |
193 |
0 |
throw e; |
194 |
|
} |
195 |
|
|
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
@param |
203 |
|
@return |
204 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
205 |
0 |
private String removeWhitespace(String txt)... |
206 |
|
{ |
207 |
0 |
return txt.replaceAll("\\s*", ""); |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
@param |
215 |
|
@return |
216 |
|
@throws |
217 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
218 |
0 |
private String validateName(String name) throws IOException... |
219 |
|
{ |
220 |
0 |
char[] invalidCharacters = new char[] { '(', ')', '[', ']', ':', ';', |
221 |
|
',' }; |
222 |
0 |
for (char c : invalidCharacters) |
223 |
|
{ |
224 |
0 |
if (name.indexOf(c) > -1) |
225 |
|
{ |
226 |
0 |
throw new IOException( |
227 |
|
"Species name contains illegal character " + c); |
228 |
|
} |
229 |
|
} |
230 |
0 |
return name; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@see@link |
242 |
|
|
|
|
| 0% |
Uncovered Elements: 39 (39) |
Complexity: 8 |
Complexity Density: 0.3 |
|
243 |
0 |
@Override... |
244 |
|
public String print(SequenceI[] sqs, boolean jvsuffix) |
245 |
|
{ |
246 |
|
|
247 |
0 |
StringBuffer sb = new StringBuffer(Integer.toString(sqs.length)); |
248 |
0 |
sb.append(" "); |
249 |
|
|
250 |
0 |
sb.append((sqs.length > 0) ? Integer.toString(sqs[0].getLength()) : "0") |
251 |
|
.append(newline); |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
0 |
boolean sequential = false; |
257 |
|
|
258 |
|
|
259 |
0 |
int numInterleavedColumns = 60; |
260 |
|
|
261 |
0 |
int sequenceLength = 0; |
262 |
0 |
for (SequenceI s : sqs) |
263 |
|
{ |
264 |
|
|
265 |
|
|
266 |
0 |
String name = s.getName(); |
267 |
0 |
if (name.length() > 10) |
268 |
|
{ |
269 |
0 |
name = name.substring(0, 10); |
270 |
|
} |
271 |
|
else |
272 |
|
{ |
273 |
|
|
274 |
0 |
name = String.format("%1$-" + 10 + "s", s.getName()); |
275 |
|
} |
276 |
0 |
sb.append(name); |
277 |
|
|
278 |
|
|
279 |
0 |
if (sequential) |
280 |
|
{ |
281 |
0 |
sb.append(s.getSequenceAsString()); |
282 |
|
} |
283 |
|
else |
284 |
|
{ |
285 |
|
|
286 |
|
|
287 |
0 |
sequenceLength = s.getLength(); |
288 |
|
|
289 |
|
|
290 |
0 |
sb.append(s.getSequence(0, |
291 |
|
Math.min(numInterleavedColumns, sequenceLength))); |
292 |
|
} |
293 |
0 |
sb.append(newline); |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
|
298 |
0 |
if (!sequential && sequenceLength > numInterleavedColumns) |
299 |
|
{ |
300 |
|
|
301 |
0 |
int numMatrics = sequenceLength / numInterleavedColumns; |
302 |
0 |
if ((sequenceLength % numInterleavedColumns) > 0) |
303 |
|
{ |
304 |
0 |
numMatrics++; |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
0 |
for (int i = 1; i < numMatrics; i++) |
309 |
|
{ |
310 |
|
|
311 |
0 |
sb.append(newline); |
312 |
0 |
int start = i * numInterleavedColumns; |
313 |
0 |
for (SequenceI s : sqs) |
314 |
|
{ |
315 |
0 |
sb.append(s.getSequence(start, |
316 |
|
Math.min(start + numInterleavedColumns, sequenceLength))) |
317 |
|
.append(newline); |
318 |
|
} |
319 |
|
} |
320 |
|
|
321 |
|
} |
322 |
|
|
323 |
0 |
return sb.toString(); |
324 |
|
} |
325 |
|
} |