From 4be0c06f705f53162b4658e382ead85a40a0cffe Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 1 Oct 2015 08:57:52 +0100 Subject: [PATCH 1/1] JAL-1499 tweak output format for Description --- src/jalview/io/MegaFile.java | 38 +++++++++++--------------- test/jalview/io/MegaFileTest.java | 53 ++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/jalview/io/MegaFile.java b/src/jalview/io/MegaFile.java index 238061a..af9889b 100644 --- a/src/jalview/io/MegaFile.java +++ b/src/jalview/io/MegaFile.java @@ -955,8 +955,21 @@ public class MegaFile extends AlignFile { StringBuilder sb = new StringBuilder(128); sb.append(MEGA_ID).append(newline); - printProperty(al, sb, PROP_TITLE, TITLE); - printProperty(al, sb, PROP_DESCRIPTION, DESCRIPTION); + String propertyValue = (String) al.getProperty(PROP_TITLE); + if (propertyValue != null) + { + sb.append(BANG).append(TITLE).append(SPACE) +.append(propertyValue) + .append(SEMICOLON) + .append(newline); + } + propertyValue = (String) al.getProperty(PROP_DESCRIPTION); + if (propertyValue != null) + { + sb.append(BANG).append(DESCRIPTION).append(newline) + .append(propertyValue).append(SEMICOLON) + .append(newline); + } /* * !Format DataType CodeTable @@ -1166,27 +1179,6 @@ public class MegaFile extends AlignFile } /** - * Helper method to append a property e.g. !Title to the output buffer, if the - * property is set on the alignment. - * - * @param al - * @param headers - * @param propertyName - * @param propertyKeyword - */ - protected void printProperty(AlignmentI al, StringBuilder headers, - String propertyName, String propertyKeyword) - { - String propertyValue = (String) al.getProperty(propertyName); - if (propertyValue != null) - { - headers.append(BANG).append(propertyKeyword).append(SPACE) - .append(propertyValue).append(SEMICOLON) - .append(newline); - } - } - - /** * Returns the number of sequence positions output per line * * @return diff --git a/test/jalview/io/MegaFileTest.java b/test/jalview/io/MegaFileTest.java index 5caa50e..fef8337 100644 --- a/test/jalview/io/MegaFileTest.java +++ b/test/jalview/io/MegaFileTest.java @@ -419,7 +419,58 @@ public class MegaFileTest //@formatter:off String expected = "#MEGA\n!Title Data with description;\n" + - "!Description Line one of description\n" + + "!Description\n" + + " Line one of description\n" + + " Line two of description;\n" + + "!Format\n" + + " 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"; + //@formatter:on + assertEquals("Roundtrip didn't match", expected, + formatted); + } + + /** + * Test reading a MEGA file to an alignment then writing it out in MEGA + * format. Verify the output is (functionally) the same as the input. + * + * @throws IOException + */ + @Test(groups = "Functional") + public void testRoundTrip_multilineFormatWithComments() + throws IOException + { + AppletFormatAdapter fa = new AppletFormatAdapter(); + //@formatter:off + AlignmentI al = fa.readFile("#MEGA\n" + + "!Title Data with description;\n" + + "[ this comment should be ignored\n" + + "including [this nested comment]\n" + + "]\n" + + "!Format \n" + + "DataType=DNA CodeTable=Standard\n" + + "indel=- Missing=? MatchChar=.;\n\n" + + "!Description\n" + + " Line one of description\n" + + " Line two of description;\n\n" + + "#U455 CGC GTA\n" + + "#CPZANT ATC GGG\n\n" + + "#U455 CGA TTT\n" + + "#CPZANT CAA TGC\n", + AppletFormatAdapter.PASTE, "MEGA"); + //@formatter:on + MegaFile output = new MegaFile(); + String formatted = output.print(al); + //@formatter:off + String expected = + "#MEGA\n!Title Data with description;\n" + + "!Description\n" + + " Line one of description\n" + " Line two of description;\n" + "!Format\n" + " DataType=DNA CodeTable=Standard\n" + -- 1.7.10.2