From db6df7f531341ba6a7ade77103cf071cfb25e543 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 1 Oct 2015 09:33:02 +0100 Subject: [PATCH] JAL-1499 write position numbers at end of each line (interleaved) --- src/jalview/io/MegaFile.java | 21 +++++++++++++------ test/jalview/io/MegaFileTest.java | 41 ++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/jalview/io/MegaFile.java b/src/jalview/io/MegaFile.java index af9889b..2f48bf5 100644 --- a/src/jalview/io/MegaFile.java +++ b/src/jalview/io/MegaFile.java @@ -143,6 +143,9 @@ public class MegaFile extends AlignFile // this can be True, False or null (meaning we don't know yet) private Boolean interleaved; + // write end of line positions as a comment + private boolean writePositionNumbers = true; + public MegaFile() { } @@ -891,17 +894,17 @@ public class MegaFile extends AlignFile int maxSequenceLength = getMaxSequenceLength(s); int numLines = maxSequenceLength / positionsPerLine + 3; // approx - /* - * Size a buffer to hold the whole output - */ - StringBuilder sb = new StringBuilder(numLines - * (maxIdLength + 2 + positionsPerLine)); - int numDataBlocks = (maxSequenceLength - 1) / positionsPerLine + 1; int spaceEvery = this.nucleotide != null && this.nucleotide ? 3 : 10; int chunksPerLine = (positionsPerLine + spaceEvery - 1) / spaceEvery; /* + * Roughly size a buffer to hold the whole output + */ + StringBuilder sb = new StringBuilder(numLines + * (maxIdLength + positionsPerLine + chunksPerLine + 10)); + + /* * Output as: #Seqid CGT AGC ACT ... or blocks of 10 for peptide */ int from = 0; @@ -936,6 +939,12 @@ public class MegaFile extends AlignFile advancedBy += subSequence.length; } } + // write last position as a comment + if (writePositionNumbers) + { + sb.append(SPACE).append(COMMENT_START).append(from + advancedBy) + .append(COMMENT_END); + } sb.append(newline); first = false; } diff --git a/test/jalview/io/MegaFileTest.java b/test/jalview/io/MegaFileTest.java index fef8337..948efa8 100644 --- a/test/jalview/io/MegaFileTest.java +++ b/test/jalview/io/MegaFileTest.java @@ -231,8 +231,9 @@ public class MegaFileTest // normally output should match input // we cheated here with a number of short input lines // nb don't get Title in output if not calling print(AlignmentI) - String expected = "#MEGA\n\n" + "#U455 ABCDEF\n" - + "#CPZANT MNOPQR\n\n" + "#U455 KLMNOP\n" + "#CPZANT WXYZGC" + String expected = "#MEGA\n\n" + "#U455 ABCDEF [6]\n" + + "#CPZANT MNOPQR [6]\n\n" + "#U455 KLMNOP [12]\n" + + "#CPZANT WXYZGC [12]" + "\n"; assertEquals("Print format wrong", expected, printed); } @@ -250,8 +251,14 @@ public class MegaFileTest String printed = testee.print(); System.out.println(printed); - assertEquals("Print format wrong", "#MEGA\n\n" + INTERLEAVED_NOHEADERS, + //@formatter:off + assertEquals("Print format wrong", + "#MEGA\n\n" + "#U455 ABCDEF [6]\n" + + "#CPZANT MNOPQR [6]\n\n" + + "#U455 KLMNOP [12]\n" + + "#CPZANT WXYZGC [12]\n", printed); + //@formatter:on } /** @@ -297,12 +304,12 @@ public class MegaFileTest //0123456789klmnopqrstABCDEFGHIJ9876543210abcdefghij String expected = "#MEGA\n\n" + - "#U455 0123456789 klmnopqrst\n" + // first 20 - "#CPZANT 9876543210 abcdefghij\n\n" + - "#U455 ABCDEFGHIJ 9876543210\n" + // next 20 - "#CPZANT 0123456789 klmnopqrst\n\n" + - "#U455 abcdefghij\n" + // last 10 - "#CPZANT ABCDEFGHIJ\n"; + "#U455 0123456789 klmnopqrst [20]\n" + // first 20 + "#CPZANT 9876543210 abcdefghij [20]\n\n" + + "#U455 ABCDEFGHIJ 9876543210 [40]\n" + // next 20 + "#CPZANT 0123456789 klmnopqrst [40]\n\n" + + "#U455 abcdefghij [50]\n" + // last 10 + "#CPZANT ABCDEFGHIJ [50]\n"; //@formatter:on assertEquals("Print format wrong", expected, printed); } @@ -426,10 +433,10 @@ public class MegaFileTest " DataType=DNA CodeTable=Standard\n" + " NSeqs=2 NSites=12\n" + " Indel=- Identical=. Missing=?;\n\n" + - "#U455 CGC GTA\n" + - "#CPZANT ATC GGG\n\n" + - "#U455 CGA TTT\n" + - "#CPZANT CAA TGC\n"; + "#U455 CGC GTA [6]\n" + + "#CPZANT ATC GGG [6]\n\n" + + "#U455 CGA TTT [12]\n" + + "#CPZANT CAA TGC [12]\n"; //@formatter:on assertEquals("Roundtrip didn't match", expected, formatted); @@ -476,10 +483,10 @@ public class MegaFileTest " DataType=DNA CodeTable=Standard\n" + " NSeqs=2 NSites=12\n" + " Indel=- Identical=. Missing=?;\n\n" + - "#U455 CGC GTA\n" + - "#CPZANT ATC GGG\n\n" + - "#U455 CGA TTT\n" + - "#CPZANT CAA TGC\n"; + "#U455 CGC GTA [6]\n" + + "#CPZANT ATC GGG [6]\n\n" + + "#U455 CGA TTT [12]\n" + + "#CPZANT CAA TGC [12]\n"; //@formatter:on assertEquals("Roundtrip didn't match", expected, formatted); -- 1.7.10.2