JAL-1807 test
[jalviewjs.git] / bin / jalview / io / PhylipFile.js
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", 
5 function () {
6 try {
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) {
14 return;
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');
21 if (tabIndex == -1) {
22 sequenceElements[i] = this.parseId (this.validateName (potentialName));
23 sequences[i] =  new StringBuffer (this.removeWhitespace (line.substring (10)));
24 } else {
25 sequenceElements[i] = this.parseId (this.validateName (potentialName.substring (0, tabIndex)));
26 sequences[i] =  new StringBuffer (this.removeWhitespace (line.substring (tabIndex)));
27 }}
28 if ((sequences[0]).length () != numberCharacters) {
29 var i = 0;
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) {
34 i = 0;
35 }}
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]);
41 }
42 } catch (e) {
43 if (Clazz.exceptionOf (e, java.io.IOException)) {
44 System.err.println ("Exception parsing PHYLIP file " + e);
45 e.printStackTrace (System.err);
46 throw e;
47 } else {
48 throw e;
49 }
50 }
51 });
52 Clazz.defineMethod (c$, "removeWhitespace", 
53 ($fz = function (txt) {
54 return txt.replaceAll ("\\s*", "");
55 }, $fz.isPrivate = true, $fz), "~S");
56 Clazz.defineMethod (c$, "validateName", 
57 ($fz = function (name) {
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);
62 }}
63 return name;
64 }, $fz.isPrivate = true, $fz), "~S");
65 Clazz.overrideMethod (c$, "print", 
66 function () {
67 var sb =  new StringBuffer (Integer.toString (this.seqs.size ()));
68 sb.append (" ");
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);
77 } else {
78 name = String.format ("%1$-10s", [s.getName ()]);
79 }sb.append (name);
80 if (sequential) {
81 sb.append (s.getSequence ());
82 } else {
83 sequenceLength = s.getSequence ().length;
84 sb.append (s.getSequence (0, Math.min (numInterleavedColumns, sequenceLength)));
85 }sb.append (this.newline);
86 }
87 if (!sequential && sequenceLength > numInterleavedColumns) {
88 var numMatrics = Clazz.doubleToInt (sequenceLength / numInterleavedColumns);
89 if ((sequenceLength % numInterleavedColumns) > 0) {
90 numMatrics++;
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);
96 }
97 }
98 }return sb.toString ();
99 });
100 Clazz.defineStatics (c$,
101 "FILE_EXT", "phy",
102 "FILE_DESC", "PHYLIP");
103 });