1 Clazz.declarePackage ("jalview.io");
2 Clazz.load (["jalview.io.AlignFile"], "jalview.io.PhylipFile", ["java.io.IOException", "java.lang.StringBuffer"], function () {
3 c$ = Clazz.declareType (jalview.io, "PhylipFile", jalview.io.AlignFile);
4 Clazz.overrideMethod (c$, "parse",
7 var line = this.nextLine ();
8 var lineElements = line.trim ().$plit ("\\s+");
9 if (lineElements.length < 2) {
10 throw new java.io.IOException ("First line must contain the number of specifies and number of characters");
11 }var numberSpecies = Integer.parseInt (lineElements[0]);
12 var numberCharacters = Integer.parseInt (lineElements[1]);
13 if (numberSpecies <= 0) {
15 }var sequenceElements = new Array (numberSpecies);
16 var sequences = new Array (numberSpecies);
17 for (var i = 0; i < numberSpecies; i++) {
18 line = this.nextLine ();
19 var potentialName = line.substring (0, 10);
20 var tabIndex = potentialName.indexOf ('\t');
22 sequenceElements[i] = this.parseId (this.validateName (potentialName));
23 sequences[i] = new StringBuffer (this.removeWhitespace (line.substring (10)));
25 sequenceElements[i] = this.parseId (this.validateName (potentialName.substring (0, tabIndex)));
26 sequences[i] = new StringBuffer (this.removeWhitespace (line.substring (tabIndex)));
28 if ((sequences[0]).length () != numberCharacters) {
30 for (line = this.nextLine (); line != null; line = this.nextLine ()) {
31 if (line.length > 0) {
32 sequences[i++].append (this.removeWhitespace (line));
33 }if (i == sequences.length) {
36 }for (var i = 0; i < numberSpecies; i++) {
37 if (sequences[i].length () != numberCharacters) {
38 throw new java.io.IOException (sequenceElements[i].getName () + " sequence is incorrect length - should be " + numberCharacters + " but is " + sequences[i].length ());
39 }sequenceElements[i].setSequence (sequences[i].toString ());
40 this.seqs.add (sequenceElements[i]);
43 if (Clazz.exceptionOf (e, java.io.IOException)) {
44 System.err.println ("Exception parsing PHYLIP file " + e);
45 e.printStackTrace (System.err);
52 Clazz.defineMethod (c$, "removeWhitespace",
54 return txt.replaceAll ("\\s*", "");
56 Clazz.defineMethod (c$, "validateName",
58 var invalidCharacters = Clazz.newCharArray (-1, ['(', ')', '[', ']', ':', ';', ',']);
59 for (var c, $c = 0, $$c = invalidCharacters; $c < $$c.length && ((c = $$c[$c]) || true); $c++) {
60 if (name.indexOf (c) > -1) {
61 throw new java.io.IOException ("Species name contains illegal character " + c);
65 Clazz.overrideMethod (c$, "print",
67 var sb = new StringBuffer (Integer.toString (this.seqs.size ()));
69 sb.append ((this.seqs.size () > 0) ? Integer.toString (this.seqs.get (0).getSequence ().length) : "0").append (this.newline);
70 var sequential = false;
71 var numInterleavedColumns = 60;
72 var sequenceLength = 0;
73 for (var s, $s = this.seqs.iterator (); $s.hasNext () && ((s = $s.next ()) || true);) {
74 var name = s.getName ();
75 if (name.length > 10) {
76 name = name.substring (0, 10);
78 name = String.format ("%1$-10s", [s.getName ()]);
81 sb.append (s.getSequence ());
83 sequenceLength = s.getSequence ().length;
84 sb.append (s.getSequence (0, Math.min (numInterleavedColumns, sequenceLength)));
85 }sb.append (this.newline);
87 if (!sequential && sequenceLength > numInterleavedColumns) {
88 var numMatrics = Clazz.doubleToInt (sequenceLength / numInterleavedColumns);
89 if ((sequenceLength % numInterleavedColumns) > 0) {
91 }for (var i = 1; i < numMatrics; i++) {
92 sb.append (this.newline);
93 var start = i * numInterleavedColumns;
94 for (var s, $s = this.seqs.iterator (); $s.hasNext () && ((s = $s.next ()) || true);) {
95 sb.append (s.getSequence (start, Math.min (start + numInterleavedColumns, sequenceLength))).append (this.newline);
98 }return sb.toString ();
100 Clazz.defineStatics (c$,
102 "FILE_DESC", "PHYLIP");