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