JAL-1517 source formatting
[jalview.git] / test / jalview / io / StockholmFileTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 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.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31
32 import java.io.File;
33 import java.util.BitSet;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import org.junit.Test;
38
39 public class StockholmFileTest
40 {
41
42   static String PfamFile = "examples/PF00111_seed.stk",
43           RfamFile = "examples/RF00031_folded.stk";
44
45   @Test
46   public void pfamFileIO() throws Exception
47   {
48     testFileIOwithFormat(new File(PfamFile), "STH");
49   }
50
51   @Test
52   public void pfamFileDataExtraction() throws Exception
53   {
54     AppletFormatAdapter af = new AppletFormatAdapter();
55     AlignmentI al = af.readFile(PfamFile, af.FILE,
56             new IdentifyFile().Identify(PfamFile, af.FILE));
57     int numpdb = 0;
58     for (SequenceI sq : al.getSequences())
59     {
60       if (sq.getPDBId() != null)
61       {
62         numpdb += sq.getPDBId().size();
63       }
64     }
65     assertTrue(
66             "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
67             numpdb > 0);
68   }
69
70   @Test
71   public void rfamFileIO() throws Exception
72   {
73     testFileIOwithFormat(new File(RfamFile), "STH");
74   }
75
76   /**
77    * test alignment data in given file can be imported, exported and reimported
78    * with no dataloss
79    * 
80    * @param f
81    *          - source datafile (IdentifyFile.identify() should work with it)
82    * @param ioformat
83    *          - label for IO class used to write and read back in the data from
84    *          f
85    */
86   public static void testFileIOwithFormat(File f, String ioformat)
87   {
88     System.out.println("Reading file: " + f);
89     String ff = f.getPath();
90     try
91     {
92       AppletFormatAdapter rf = new AppletFormatAdapter();
93
94       Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
95               new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
96
97       assertNotNull("Couldn't read supplied alignment data.", al);
98
99       // make sure dataset is initialised ? not sure about this
100       for (int i = 0; i < al.getSequencesArray().length; ++i)
101       {
102         al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
103       }
104       String outputfile = rf.formatSequences(ioformat, al, true);
105       System.out.println("Output file in '" + ioformat + "':\n"
106               + outputfile + "\n<<EOF\n");
107       // test for consistency in io
108       Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
109               AppletFormatAdapter.PASTE, ioformat);
110       assertNotNull("Couldn't parse reimported alignment data.", al_input);
111
112       String identifyoutput = new IdentifyFile().Identify(outputfile,
113               AppletFormatAdapter.PASTE);
114       assertNotNull("Identify routine failed for outputformat " + ioformat,
115               identifyoutput);
116       assertTrue(
117               "Identify routine could not recognise output generated by '"
118                       + ioformat + "' writer",
119               ioformat.equals(identifyoutput));
120       testAlignmentEquivalence(al, al_input);
121     } catch (Exception e)
122     {
123       e.printStackTrace();
124       assertTrue("Couln't format the alignment for output file.", false);
125     }
126   }
127
128   /**
129    * assert alignment equivalence
130    * 
131    * @param al
132    *          'original'
133    * @param al_input
134    *          'secondary' or generated alignment from some datapreserving
135    *          transformation
136    */
137   public static void testAlignmentEquivalence(AlignmentI al,
138           AlignmentI al_input)
139   {
140     assertNotNull("Original alignment was null", al);
141     assertNotNull("Generated alignment was null", al_input);
142
143     assertTrue(
144             "Alignment dimension mismatch: originl contains "
145                     + al.getHeight() + " and generated has "
146                     + al_input.getHeight() + " sequences; original has "
147                     + al.getWidth() + " and generated has "
148                     + al_input.getWidth() + " columns.",
149             al.getHeight() == al_input.getHeight()
150                     && al.getWidth() == al_input.getWidth());
151
152     // check Alignment annotation
153     AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
154     AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
155
156     // note - at moment we do not distinguish between alignment without any
157     // annotation rows and alignment with no annotation row vector
158     // we might want to revise this in future
159     int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
160             : aa_original.length);
161     Map<Integer, java.util.BitSet> orig_groups = new HashMap<Integer, java.util.BitSet>(), new_groups = new HashMap<Integer, java.util.BitSet>();
162
163     if (aa_new != null && aa_original != null)
164     {
165       for (int i = 0; i < aa_original.length; i++)
166       {
167         if (aa_new.length > i)
168         {
169           assertTrue("Different alignment annotation at position " + i,
170                   equalss(aa_original[i], aa_new[i]));
171           // compare graphGroup or graph properties - needed to verify JAL-1299
172           assertTrue("Graph type not identical.",
173                   aa_original[i].graph == aa_new[i].graph);
174           assertTrue("Visibility not identical.",
175                   aa_original[i].visible == aa_new[i].visible);
176           assertTrue(
177                   "Threshold line not identical.",
178                   aa_original[i].threshold == null ? aa_new[i].threshold == null
179                           : aa_original[i].threshold
180                                   .equals(aa_new[i].threshold));
181           // graphGroup may differ, but pattern should be the same
182           Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2), n_ggrp = new Integer(
183                   aa_new[i].graphGroup + 2);
184           BitSet orig_g = orig_groups.get(o_ggrp), new_g = new_groups
185                   .get(n_ggrp);
186           if (orig_g == null)
187           {
188             orig_groups.put(o_ggrp, orig_g = new BitSet());
189           }
190           if (new_g == null)
191           {
192             new_groups.put(n_ggrp, new_g = new BitSet());
193           }
194           assertTrue("Graph Group pattern differs at annotation " + i,
195                   orig_g.equals(new_g));
196           orig_g.set(i);
197           new_g.set(i);
198         }
199         else
200         {
201           System.err.println("No matching annotation row for "
202                   + aa_original[i].toString());
203         }
204       }
205     }
206     assertTrue(
207             "Generated and imported alignment have different annotation sets ("
208                     + aa_new_size + " != " + aa_original_size + ")",
209             aa_new_size == aa_original_size);
210
211     // check sequences, annotation and features
212     SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
213     seq_original = al.getSequencesArray();
214     SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
215     seq_new = al_input.getSequencesArray();
216     SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
217     AlignmentAnnotation annot_original, annot_new;
218     //
219     for (int i = 0; i < al.getSequencesArray().length; i++)
220     {
221       String name = seq_original[i].getName();
222       int start = seq_original[i].getStart();
223       int end = seq_original[i].getEnd();
224       System.out.println("Check sequence: " + name + "/" + start + "-"
225               + end);
226
227       // search equal sequence
228       for (int in = 0; in < al_input.getSequencesArray().length; in++)
229       {
230         if (name.equals(seq_new[in].getName())
231                 && start == seq_new[in].getStart()
232                 && end == seq_new[in].getEnd())
233         {
234           String ss_original = seq_original[i].getSequenceAsString();
235           String ss_new = seq_new[in].getSequenceAsString();
236           assertTrue("The sequences " + name + "/" + start + "-" + end
237                   + " are not equal", ss_original.equals(ss_new));
238
239           assertTrue(
240                   "Sequence Features were not equivalent",
241                   (seq_original[i].getSequenceFeatures() == null && seq_new[in]
242                           .getSequenceFeatures() == null)
243                           || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
244                                   .getSequenceFeatures() != null));
245           // compare sequence features
246           if (seq_original[i].getSequenceFeatures() != null
247                   && seq_new[in].getSequenceFeatures() != null)
248           {
249             System.out.println("There are feature!!!");
250             sequenceFeatures_original = new SequenceFeature[seq_original[i]
251                     .getSequenceFeatures().length];
252             sequenceFeatures_original = seq_original[i]
253                     .getSequenceFeatures();
254             sequenceFeatures_new = new SequenceFeature[seq_new[in]
255                     .getSequenceFeatures().length];
256             sequenceFeatures_new = seq_new[in].getSequenceFeatures();
257
258             assertTrue("different number of features", seq_original[i]
259                     .getSequenceFeatures().length == seq_new[in]
260                     .getSequenceFeatures().length);
261
262             for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
263             {
264               assertTrue("Different features",
265                       sequenceFeatures_original[feat]
266                               .equals(sequenceFeatures_new[feat]));
267             }
268           }
269           // compare alignment annotation
270           if (al.getSequenceAt(i).getAnnotation() != null
271                   && al_input.getSequenceAt(in).getAnnotation() != null)
272           {
273             for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
274             {
275               if (al.getSequenceAt(i).getAnnotation()[j] != null
276                       && al_input.getSequenceAt(in).getAnnotation()[j] != null)
277               {
278                 annot_original = al.getSequenceAt(i).getAnnotation()[j];
279                 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
280                 assertTrue("Different annotation elements",
281                         equalss(annot_original, annot_new));
282               }
283             }
284           }
285           else if (al.getSequenceAt(i).getAnnotation() == null
286                   && al_input.getSequenceAt(in).getAnnotation() == null)
287           {
288             System.out.println("No annotations");
289           }
290           else if (al.getSequenceAt(i).getAnnotation() != null
291                   && al_input.getSequenceAt(in).getAnnotation() == null)
292           {
293             assertTrue("Annotations differed between sequences ("
294                     + al.getSequenceAt(i).getName() + ") and ("
295                     + al_input.getSequenceAt(i).getName() + ")", false);
296           }
297           break;
298         }
299       }
300     }
301   }
302
303   /*
304    * compare annotations
305    */
306   private static boolean equalss(AlignmentAnnotation annot_or,
307           AlignmentAnnotation annot_new)
308   {
309     if (annot_or.annotations.length != annot_new.annotations.length)
310     {
311       System.err.println("Different lengths for annotation row elements: "
312               + annot_or.annotations.length + "!="
313               + annot_new.annotations.length);
314       return false;
315     }
316     for (int i = 0; i < annot_or.annotations.length; i++)
317     {
318       Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
319       if (an_or != null && an_new != null)
320       {
321         if (!an_or.displayCharacter.trim().equals(
322                 an_new.displayCharacter.trim())
323                 || !("" + an_or.secondaryStructure).trim().equals(
324                         ("" + an_new.secondaryStructure).trim())
325                 || (an_or.description != an_new.description && (an_or.description == null
326                         || an_new.description == null || !an_or.description
327                           .equals(an_new.description))))
328         {
329           System.err.println("Annotation Element Mismatch\nElement " + i
330                   + " in original: " + annot_or.annotations[i].toString()
331                   + "\nElement " + i + " in new: "
332                   + annot_new.annotations[i].toString());
333           return false;
334         }
335       }
336       else if (annot_or.annotations[i] == null
337               && annot_new.annotations[i] == null)
338       {
339         continue;
340       }
341       else
342       {
343         System.err.println("Annotation Element Mismatch\nElement "
344                 + i
345                 + " in original: "
346                 + (annot_or.annotations[i] == null ? "is null"
347                         : annot_or.annotations[i].toString())
348                 + "\nElement "
349                 + i
350                 + " in new: "
351                 + (annot_new.annotations[i] == null ? "is null"
352                         : annot_new.annotations[i].toString()));
353         return false;
354       }
355     }
356     return true;
357   }
358 }