JAL-2189 apply license
[jalview.git] / test / jalview / io / FormatAdapterTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.fail;
26
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SequenceI;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.testng.annotations.DataProvider;
36 import org.testng.annotations.Test;
37
38 public class FormatAdapterTest
39 {
40
41   /**
42    * Test saving and re-reading in a specified format
43    * 
44    * @throws IOException
45    */
46   @Test(groups = { "Functional" }, dataProvider = "formats")
47   public void testRoundTrip(String format) throws IOException
48   {
49     try
50     {
51       AlignmentI al = new FormatAdapter().readFile("examples/uniref50.fa",
52               FormatAdapter.FILE, "FASTA");
53
54       /*
55        * 'gap' is the gap character used in the alignment data file here,
56        * not the user preferred gap character
57        */
58       char gap = al.getGapCharacter();
59       assertNotNull(al);
60
61       SequenceI[] seqs = al.getSequencesArray();
62       String formatted = new FormatAdapter().formatSequences(format, al,
63               false);
64
65       AlignmentI reloaded = new FormatAdapter().readFile(formatted,
66               FormatAdapter.PASTE, format);
67       List<SequenceI> reread = reloaded.getSequences();
68       assertEquals("Wrong number of reloaded sequences", seqs.length,
69               reread.size());
70
71       int i = 0;
72       for (SequenceI seq : reread)
73       {
74         String sequenceString = seq.getSequenceAsString();
75
76         /*
77          * special case: MSF always uses '.' as gap character
78          */
79         sequenceString = adjustForGapTreatment(sequenceString, gap, format);
80         assertEquals(
81                 String.format("Sequence %d: %s", i, seqs[i].getName()),
82                 seqs[i].getSequenceAsString(), sequenceString);
83         i++;
84       }
85     } catch (IOException e)
86     {
87       fail(String
88               .format("Format %s failed with %s", format, e.getMessage()));
89     }
90   }
91
92   /**
93    * Optionally change the gap character in the string to the given character,
94    * depending on the sequence file format
95    * 
96    * @param sequenceString
97    *          a sequence (as written in 'format' format)
98    * @param gap
99    *          the sequence's original gap character
100    * @param format
101    * @return
102    */
103   String adjustForGapTreatment(String sequenceString, char gap,
104           String format)
105   {
106     if ("MSF".equals(format))
107     {
108       /*
109        * MSF forces gap character to '.', so change it back
110        * for comparison purposes
111        */
112       sequenceString = sequenceString.replace('.', gap);
113     }
114     return sequenceString;
115   }
116
117   /**
118    * Data provider that serves alignment formats that are both readable and
119    * writable
120    * 
121    * @return
122    */
123   @DataProvider(name = "formats")
124   static Object[][] getFormats()
125   {
126     List<String> both = new ArrayList<String>();
127     String[] readable = FormatAdapter.READABLE_FORMATS;
128     List<String> writeable = Arrays.asList(FormatAdapter.WRITEABLE_FORMATS);
129     for (String r : readable)
130     {
131       if (writeable.contains(r))
132       {
133         both.add(r);
134       }
135     }
136
137     Object[][] formats = new Object[both.size()][];
138     int i = 0;
139     for (String format : both)
140     {
141       formats[i] = new Object[] { format };
142       i++;
143     }
144     return formats;
145   }
146
147   /**
148    * Enable this to isolate testing to a single file format
149    * 
150    * @throws IOException
151    */
152   @Test(groups = { "Functional" }, enabled = false)
153   public void testOneFormatRoundTrip() throws IOException
154   {
155     testRoundTrip("JSON");
156   }
157 }