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