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