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.List;
14 import org.testng.annotations.DataProvider;
15 import org.testng.annotations.Test;
17 public class FormatAdapterTest
21 * Test saving and re-reading in a specified format
25 @Test(groups = { "Functional" }, dataProvider = "formats")
26 public void testRoundTrip(FileFormatI format) throws IOException
30 AlignmentI al = new FormatAdapter().readFile("examples/uniref50.fa",
31 DataSourceType.FILE, FileFormat.Fasta);
34 * 'gap' is the gap character used in the alignment data file here,
35 * not the user preferred gap character
37 char gap = al.getGapCharacter();
40 SequenceI[] seqs = al.getSequencesArray();
41 String formatted = new FormatAdapter().formatSequences(format, al,
44 AlignmentI reloaded = new FormatAdapter().readFile(formatted,
45 DataSourceType.PASTE, format);
46 List<SequenceI> reread = reloaded.getSequences();
47 assertEquals("Wrong number of reloaded sequences", seqs.length,
51 for (SequenceI seq : reread)
53 String sequenceString = seq.getSequenceAsString();
56 * special case: MSF always uses '.' as gap character
58 sequenceString = adjustForGapTreatment(sequenceString, gap, format);
60 String.format("Sequence %d: %s", i,
61 seqs[i].getName()), seqs[i].getSequenceAsString(),
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 (format == FileFormat.MSF)
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<FileFormatI> both = new ArrayList<FileFormatI>();
107 for (FileFormat format : FileFormat.values())
109 if (format.isReadable() && format.isWritable())
115 Object[][] formats = new Object[both.size()][];
117 for (FileFormatI format : both)
119 formats[i] = new Object[] { format };
126 * Enable this to isolate testing to a single file format
128 * @throws IOException
130 @Test(groups = { "Functional" }, enabled = false)
131 public void testOneFormatRoundTrip() throws IOException
133 testRoundTrip(FileFormat.Json);