6bf954a6fc59d41c1ccaacf7f6bd35bde40cc06a
[jalview.git] / test / jalview / io / FormatAdapterTest.java
1 package jalview.io;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertNotNull;
5 import static org.testng.AssertJUnit.fail;
6
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.SequenceI;
9
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.testng.annotations.DataProvider;
15 import org.testng.annotations.Test;
16
17 public class FormatAdapterTest
18 {
19
20   /**
21    * Test saving and re-reading in a specified format
22    * 
23    * @throws IOException
24    */
25   @Test(groups = { "Functional" }, dataProvider = "formats")
26   public void testRoundTrip(FileFormatI format) throws IOException
27   {
28     try
29     {
30       AlignmentI al = new FormatAdapter().readFile("examples/uniref50.fa",
31               DataSourceType.FILE, FileFormat.Fasta);
32
33       /*
34        * 'gap' is the gap character used in the alignment data file here,
35        * not the user preferred gap character
36        */
37       char gap = al.getGapCharacter();
38       assertNotNull(al);
39
40       SequenceI[] seqs = al.getSequencesArray();
41       String formatted = new FormatAdapter().formatSequences(format, al,
42               false);
43
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,
48               reread.size());
49
50       int i = 0;
51       for (SequenceI seq : reread)
52       {
53         String sequenceString = seq.getSequenceAsString();
54
55         /*
56          * special case: MSF always uses '.' as gap character
57          */
58         sequenceString = adjustForGapTreatment(sequenceString, gap, format);
59         assertEquals(
60                 String.format("Sequence %d: %s", i,
61                         seqs[i].getName()), seqs[i].getSequenceAsString(),
62                 sequenceString);
63         i++;
64       }
65     } catch (IOException e)
66     {
67       fail(String
68               .format("Format %s failed with %s", format, e.getMessage()));
69     }
70   }
71
72   /**
73    * Optionally change the gap character in the string to the given character,
74    * depending on the sequence file format
75    * 
76    * @param sequenceString
77    *          a sequence (as written in 'format' format)
78    * @param gap
79    *          the sequence's original gap character
80    * @param format
81    * @return
82    */
83   String adjustForGapTreatment(String sequenceString, char gap,
84           FileFormatI format)
85   {
86     if (format == FileFormat.MSF)
87     {
88       /*
89        * MSF forces gap character to '.', so change it back
90        * for comparison purposes
91        */
92       sequenceString = sequenceString.replace('.', gap);
93     }
94     return sequenceString;
95   }
96
97   /**
98    * Data provider that serves alignment formats that are both readable and
99    * writable
100    * 
101    * @return
102    */
103   @DataProvider(name = "formats")
104   static Object[][] getFormats()
105   {
106     List<FileFormatI> both = new ArrayList<FileFormatI>();
107     for (FileFormat format : FileFormat.values())
108     {
109       if (format.isReadable() && format.isWritable())
110       {
111         both.add(format);
112       }
113     }
114
115     Object[][] formats = new Object[both.size()][];
116     int i = 0;
117     for (FileFormatI format : both)
118     {
119       formats[i] = new Object[] { format };
120       i++;
121     }
122     return formats;
123   }
124
125   /**
126    * Enable this to isolate testing to a single file format
127    * 
128    * @throws IOException
129    */
130   @Test(groups = { "Functional" }, enabled = false)
131   public void testOneFormatRoundTrip() throws IOException
132   {
133     testRoundTrip(FileFormat.Json);
134   }
135 }