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, seqs[i].getName()),
62 seqs[i].getSequenceAsString(), sequenceString);
65 } catch (IOException e)
68 .format("Format %s failed with %s", format, e.getMessage()));
73 * Optionally change the gap character in the string to the given character,
74 * depending on the sequence file format
76 * @param sequenceString
77 * a sequence (as written in 'format' format)
79 * the sequence's original gap character
83 String adjustForGapTreatment(String sequenceString, char gap,
86 if ("MSF".equals(format))
89 * MSF forces gap character to '.', so change it back
90 * for comparison purposes
92 sequenceString = sequenceString.replace('.', gap);
94 return sequenceString;
98 * Data provider that serves alignment formats that are both readable and
103 @DataProvider(name = "formats")
104 static Object[][] getFormats()
106 List<String> both = new ArrayList<String>();
107 String[] readable = FormatAdapter.READABLE_FORMATS;
108 List<String> writeable = Arrays.asList(FormatAdapter.WRITEABLE_FORMATS);
109 for (String r : readable)
111 if (writeable.contains(r))
117 Object[][] formats = new Object[both.size()][];
119 for (String format : both)
121 formats[i] = new Object[] { format };
128 * Enable this to isolate testing to a single file format
130 * @throws IOException
132 @Test(groups = { "Functional" }, enabled = false)
133 public void testOneFormatRoundTrip() throws IOException
135 testRoundTrip("JSON");