3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertNotNull;
5 import static org.testng.AssertJUnit.fail;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.SequenceI;
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
15 import org.testng.annotations.DataProvider;
16 import org.testng.annotations.Test;
18 public class FormatAdapterTest
22 * Test saving and re-reading in a specified format
26 @Test(groups = { "Functional" }, dataProvider = "formats")
27 public void testRoundTrip(String format) throws IOException
31 AlignmentI al = new FormatAdapter().readFile("examples/uniref50.fa",
32 FormatAdapter.FILE, "FASTA");
35 * 'gap' is the gap character used in the alignment data file here,
36 * not the user preferred gap character
38 char gap = al.getGapCharacter();
41 SequenceI[] seqs = al.getSequencesArray();
42 String formatted = new FormatAdapter().formatSequences(format, al,
45 AlignmentI reloaded = new FormatAdapter().readFile(formatted,
46 FormatAdapter.PASTE, format);
47 List<SequenceI> reread = reloaded.getSequences();
48 assertEquals("Wrong number of reloaded sequences", seqs.length,
52 for (SequenceI seq : reread)
54 String sequenceString = seq.getSequenceAsString();
57 * special case: MSF always uses '.' as gap character
59 sequenceString = adjustForGapTreatment(sequenceString, gap, format);
61 String.format("Sequence %d: %s", i,
62 seqs[i].getName()), seqs[i].getSequenceAsString(),
66 } catch (IOException e)
69 .format("Format %s failed with %s", format, e.getMessage()));
74 * Optionally change the gap character in the string to the given character,
75 * depending on the sequence file format
77 * @param sequenceString
78 * a sequence (as written in 'format' format)
80 * the sequence's original gap character
84 String adjustForGapTreatment(String sequenceString, char gap,
87 if ("MSF".equals(format))
90 * MSF forces gap character to '.', so change it back
91 * for comparison purposes
93 sequenceString = sequenceString.replace('.', gap);
95 return sequenceString;
99 * Data provider that serves alignment formats that are both readable and
104 @DataProvider(name = "formats")
105 static Object[][] getFormats()
107 List<String> both = new ArrayList<String>();
108 String[] readable = FormatAdapter.READABLE_FORMATS;
109 List<String> writeable = Arrays.asList(FormatAdapter.WRITEABLE_FORMATS);
110 for (String r : readable)
112 if (writeable.contains(r))
118 Object[][] formats = new Object[both.size()][];
120 for (String format : both)
122 formats[i] = new Object[] { format };
129 * Enable this to isolate testing to a single file format
131 * @throws IOException
133 @Test(groups = { "Functional" }, enabled = false)
134 public void testOneFormatRoundTrip() throws IOException
136 testRoundTrip("JSON");