X-Git-Url: http://source.jalview.org/gitweb/?p=jalviewjs.git;a=blobdiff_plain;f=site%2Fj2s%2Fjalview%2Fio%2FPhylipFile.js;h=b4218f72ad4d48d447e11ea2e6d51555e0353e25;hp=d5782bc3e95b35633cfa59b8e29a5b89f05b3153;hb=b9b7a352eee79b7764c3b09c9d19663075061d8c;hpb=7301a2415adab88038b291fc54caeeb3a5a47a44 diff --git a/site/j2s/jalview/io/PhylipFile.js b/site/j2s/jalview/io/PhylipFile.js index d5782bc..b4218f7 100644 --- a/site/j2s/jalview/io/PhylipFile.js +++ b/site/j2s/jalview/io/PhylipFile.js @@ -1,103 +1,103 @@ -Clazz.declarePackage ("jalview.io"); -Clazz.load (["jalview.io.AlignFile"], "jalview.io.PhylipFile", ["java.io.IOException", "java.lang.StringBuffer"], function () { -c$ = Clazz.declareType (jalview.io, "PhylipFile", jalview.io.AlignFile); -Clazz.overrideMethod (c$, "parse", -function () { -try { -var line = this.nextLine (); -var lineElements = line.trim ().$plit ("\\s+"); -if (lineElements.length < 2) { -throw new java.io.IOException ("First line must contain the number of specifies and number of characters"); -}var numberSpecies = Integer.parseInt (lineElements[0]); -var numberCharacters = Integer.parseInt (lineElements[1]); -if (numberSpecies <= 0) { -return; -}var sequenceElements = new Array (numberSpecies); -var sequences = new Array (numberSpecies); -for (var i = 0; i < numberSpecies; i++) { -line = this.nextLine (); -var potentialName = line.substring (0, 10); -var tabIndex = potentialName.indexOf ('\t'); -if (tabIndex == -1) { -sequenceElements[i] = this.parseId (this.validateName (potentialName)); -sequences[i] = new StringBuffer (this.removeWhitespace (line.substring (10))); -} else { -sequenceElements[i] = this.parseId (this.validateName (potentialName.substring (0, tabIndex))); -sequences[i] = new StringBuffer (this.removeWhitespace (line.substring (tabIndex))); -}} -if ((sequences[0]).length () != numberCharacters) { -var i = 0; -for (line = this.nextLine (); line != null; line = this.nextLine ()) { -if (line.length > 0) { -sequences[i++].append (this.removeWhitespace (line)); -}if (i == sequences.length) { -i = 0; -}} -}for (var i = 0; i < numberSpecies; i++) { -if (sequences[i].length () != numberCharacters) { -throw new java.io.IOException (sequenceElements[i].getName () + " sequence is incorrect length - should be " + numberCharacters + " but is " + sequences[i].length ()); -}sequenceElements[i].setSequence (sequences[i].toString ()); -this.seqs.add (sequenceElements[i]); -} -} catch (e) { -if (Clazz.exceptionOf (e, java.io.IOException)) { -System.err.println ("Exception parsing PHYLIP file " + e); -e.printStackTrace (System.err); -throw e; -} else { -throw e; -} -} -}); -Clazz.defineMethod (c$, "removeWhitespace", - function (txt) { -return txt.replaceAll ("\\s*", ""); -}, "~S"); -Clazz.defineMethod (c$, "validateName", - function (name) { -var invalidCharacters = Clazz.newCharArray (-1, ['(', ')', '[', ']', ':', ';', ',']); -for (var c, $c = 0, $$c = invalidCharacters; $c < $$c.length && ((c = $$c[$c]) || true); $c++) { -if (name.indexOf (c) > -1) { -throw new java.io.IOException ("Species name contains illegal character " + c); -}} -return name; -}, "~S"); -Clazz.overrideMethod (c$, "print", -function () { -var sb = new StringBuffer (Integer.toString (this.seqs.size ())); -sb.append (" "); -sb.append ((this.seqs.size () > 0) ? Integer.toString (this.seqs.get (0).getSequence ().length) : "0").append (this.newline); -var sequential = false; -var numInterleavedColumns = 60; -var sequenceLength = 0; -for (var s, $s = this.seqs.iterator (); $s.hasNext () && ((s = $s.next ()) || true);) { -var name = s.getName (); -if (name.length > 10) { -name = name.substring (0, 10); -} else { -name = String.format ("%1$-10s", [s.getName ()]); -}sb.append (name); -if (sequential) { -sb.append (s.getSequence ()); -} else { -sequenceLength = s.getSequence ().length; -sb.append (s.getSequence (0, Math.min (numInterleavedColumns, sequenceLength))); -}sb.append (this.newline); -} -if (!sequential && sequenceLength > numInterleavedColumns) { -var numMatrics = Clazz.doubleToInt (sequenceLength / numInterleavedColumns); -if ((sequenceLength % numInterleavedColumns) > 0) { -numMatrics++; -}for (var i = 1; i < numMatrics; i++) { -sb.append (this.newline); -var start = i * numInterleavedColumns; -for (var s, $s = this.seqs.iterator (); $s.hasNext () && ((s = $s.next ()) || true);) { -sb.append (s.getSequence (start, Math.min (start + numInterleavedColumns, sequenceLength))).append (this.newline); -} -} -}return sb.toString (); -}); -Clazz.defineStatics (c$, -"FILE_EXT", "phy", -"FILE_DESC", "PHYLIP"); -}); +Clazz.declarePackage ("jalview.io"); +Clazz.load (["jalview.io.AlignFile"], "jalview.io.PhylipFile", ["java.io.IOException", "java.lang.StringBuffer"], function () { +c$ = Clazz.declareType (jalview.io, "PhylipFile", jalview.io.AlignFile); +Clazz.overrideMethod (c$, "parse", +function () { +try { +var line = this.nextLine (); +var lineElements = line.trim ().$plit ("\\s+"); +if (lineElements.length < 2) { +throw new java.io.IOException ("First line must contain the number of specifies and number of characters"); +}var numberSpecies = Integer.parseInt (lineElements[0]); +var numberCharacters = Integer.parseInt (lineElements[1]); +if (numberSpecies <= 0) { +return; +}var sequenceElements = new Array (numberSpecies); +var sequences = new Array (numberSpecies); +for (var i = 0; i < numberSpecies; i++) { +line = this.nextLine (); +var potentialName = line.substring (0, 10); +var tabIndex = potentialName.indexOf ('\t'); +if (tabIndex == -1) { +sequenceElements[i] = this.parseId (this.validateName (potentialName)); +sequences[i] = new StringBuffer (this.removeWhitespace (line.substring (10))); +} else { +sequenceElements[i] = this.parseId (this.validateName (potentialName.substring (0, tabIndex))); +sequences[i] = new StringBuffer (this.removeWhitespace (line.substring (tabIndex))); +}} +if ((sequences[0]).length () != numberCharacters) { +var i = 0; +for (line = this.nextLine (); line != null; line = this.nextLine ()) { +if (line.length > 0) { +sequences[i++].append (this.removeWhitespace (line)); +}if (i == sequences.length) { +i = 0; +}} +}for (var i = 0; i < numberSpecies; i++) { +if (sequences[i].length () != numberCharacters) { +throw new java.io.IOException (sequenceElements[i].getName () + " sequence is incorrect length - should be " + numberCharacters + " but is " + sequences[i].length ()); +}sequenceElements[i].setSequence (sequences[i].toString ()); +this.seqs.add (sequenceElements[i]); +} +} catch (e) { +if (Clazz.exceptionOf (e, java.io.IOException)) { +System.err.println ("Exception parsing PHYLIP file " + e); +e.printStackTrace (System.err); +throw e; +} else { +throw e; +} +} +}); +Clazz.defineMethod (c$, "removeWhitespace", + function (txt) { +return txt.replaceAll ("\\s*", ""); +}, "~S"); +Clazz.defineMethod (c$, "validateName", + function (name) { +var invalidCharacters = Clazz.newCharArray (-1, ['(', ')', '[', ']', ':', ';', ',']); +for (var c, $c = 0, $$c = invalidCharacters; $c < $$c.length && ((c = $$c[$c]) || true); $c++) { +if (name.indexOf (c) > -1) { +throw new java.io.IOException ("Species name contains illegal character " + c); +}} +return name; +}, "~S"); +Clazz.overrideMethod (c$, "print", +function () { +var sb = new StringBuffer (Integer.toString (this.seqs.size ())); +sb.append (" "); +sb.append ((this.seqs.size () > 0) ? Integer.toString (this.seqs.get (0).getSequence ().length) : "0").append (this.newline); +var sequential = false; +var numInterleavedColumns = 60; +var sequenceLength = 0; +for (var s, $s = this.seqs.iterator (); $s.hasNext () && ((s = $s.next ()) || true);) { +var name = s.getName (); +if (name.length > 10) { +name = name.substring (0, 10); +} else { +name = String.format ("%1$-10s", [s.getName ()]); +}sb.append (name); +if (sequential) { +sb.append (s.getSequence ()); +} else { +sequenceLength = s.getSequence ().length; +sb.append (s.getSequence (0, Math.min (numInterleavedColumns, sequenceLength))); +}sb.append (this.newline); +} +if (!sequential && sequenceLength > numInterleavedColumns) { +var numMatrics = Clazz.doubleToInt (sequenceLength / numInterleavedColumns); +if ((sequenceLength % numInterleavedColumns) > 0) { +numMatrics++; +}for (var i = 1; i < numMatrics; i++) { +sb.append (this.newline); +var start = i * numInterleavedColumns; +for (var s, $s = this.seqs.iterator (); $s.hasNext () && ((s = $s.next ()) || true);) { +sb.append (s.getSequence (start, Math.min (start + numInterleavedColumns, sequenceLength))).append (this.newline); +} +} +}return sb.toString (); +}); +Clazz.defineStatics (c$, +"FILE_EXT", "phy", +"FILE_DESC", "PHYLIP"); +});