2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.fail;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SequenceI;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
37 public class FormatAdapterTest
41 * Test saving and re-reading in a specified format
45 @Test(groups = { "Functional" }, dataProvider = "formats")
46 public void testRoundTrip(FileFormatI format) throws IOException
50 AlignmentI al = new FormatAdapter().readFile("examples/uniref50.fa",
51 DataSourceType.FILE, FileFormat.Fasta);
54 * 'gap' is the gap character used in the alignment data file here,
55 * not the user preferred gap character
57 char gap = al.getGapCharacter();
60 SequenceI[] seqs = al.getSequencesArray();
61 String formatted = new FormatAdapter().formatSequences(format, al,
64 AlignmentI reloaded = new FormatAdapter().readFile(formatted,
65 DataSourceType.PASTE, format);
66 List<SequenceI> reread = reloaded.getSequences();
67 assertEquals("Wrong number of reloaded sequences", seqs.length,
71 for (SequenceI seq : reread)
73 String sequenceString = seq.getSequenceAsString();
76 * special case: MSF always uses '.' as gap character
78 sequenceString = adjustForGapTreatment(sequenceString, gap, format);
80 String.format("Sequence %d: %s", i, seqs[i].getName()),
81 seqs[i].getSequenceAsString(), sequenceString);
84 } catch (IOException e)
87 .format("Format %s failed with %s", format, e.getMessage()));
92 * Optionally change the gap character in the string to the given character,
93 * depending on the sequence file format
95 * @param sequenceString
96 * a sequence (as written in 'format' format)
98 * the sequence's original gap character
102 String adjustForGapTreatment(String sequenceString, char gap,
105 if (FileFormat.MSF.equals(format))
108 * MSF forces gap character to '.', so change it back
109 * for comparison purposes
111 sequenceString = sequenceString.replace('.', gap);
113 return sequenceString;
117 * Data provider that serves alignment formats that are both readable and
122 @DataProvider(name = "formats")
123 static Object[][] getFormats()
125 List<FileFormatI> both = new ArrayList<FileFormatI>();
126 for (FileFormat format : FileFormat.values())
128 if (format.isReadable() && format.isWritable()
129 && format.isTextFormat())
135 Object[][] formats = new Object[both.size()][];
137 for (FileFormatI format : both)
139 formats[i] = new Object[] { format };
146 * Enable this to isolate testing to a single file format
148 * @throws IOException
150 @Test(groups = { "Functional" }, enabled = false)
151 public void testOneFormatRoundTrip() throws IOException
153 testRoundTrip(FileFormat.Json);