2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceI;
34 import java.util.BitSet;
35 import java.util.HashMap;
38 import org.junit.Test;
40 public class StockholmFileTest
43 static String PfamFile = "examples/PF00111_seed.stk",
44 RfamFile = "examples/RF00031_folded.stk";
47 public void pfamFileIO() throws Exception
49 testFileIOwithFormat(new File(PfamFile), "STH", -1, 0);
53 public void pfamFileDataExtraction() throws Exception
55 AppletFormatAdapter af = new AppletFormatAdapter();
56 AlignmentI al = af.readFile(PfamFile, af.FILE,
57 new IdentifyFile().Identify(PfamFile, af.FILE));
59 for (SequenceI sq : al.getSequences())
61 if (sq.getPDBId() != null)
63 numpdb += sq.getPDBId().size();
67 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
72 public void rfamFileIO() throws Exception
74 testFileIOwithFormat(new File(RfamFile), "STH", 2, 1);
78 * test alignment data in given file can be imported, exported and reimported
82 * - source datafile (IdentifyFile.identify() should work with it)
84 * - label for IO class used to write and read back in the data from
87 public static void testFileIOwithFormat(File f, String ioformat,
88 int naliannot, int nminseqann)
90 System.out.println("Reading file: " + f);
91 String ff = f.getPath();
94 AppletFormatAdapter rf = new AppletFormatAdapter();
96 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
97 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
99 assertNotNull("Couldn't read supplied alignment data.", al);
101 // make sure dataset is initialised ? not sure about this
102 for (int i = 0; i < al.getSequencesArray().length; ++i)
104 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
106 String outputfile = rf.formatSequences(ioformat, al, true);
107 System.out.println("Output file in '" + ioformat + "':\n"
108 + outputfile + "\n<<EOF\n");
109 // test for consistency in io
110 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
111 AppletFormatAdapter.PASTE, ioformat);
112 assertNotNull("Couldn't parse reimported alignment data.", al_input);
114 String identifyoutput = new IdentifyFile().Identify(outputfile,
115 AppletFormatAdapter.PASTE);
116 assertNotNull("Identify routine failed for outputformat " + ioformat,
119 "Identify routine could not recognise output generated by '"
120 + ioformat + "' writer",
121 ioformat.equals(identifyoutput));
122 testAlignmentEquivalence(al, al_input, false);
123 int numaliannot = 0, numsqswithali = 0;
124 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
126 if (ala.sequenceRef == null)
137 assertEquals("Number of alignment annotations", naliannot,
142 "Number of sequence associated annotations wasn't at least "
143 + nminseqann, numsqswithali >= nminseqann);
145 } catch (Exception e)
148 assertTrue("Couln't format the alignment for output file.", false);
153 * assert alignment equivalence
158 * 'secondary' or generated alignment from some datapreserving
160 * @param ignoreFeatures
161 * when true, differences in seuqence feature annotation are ignored.
163 public static void testAlignmentEquivalence(AlignmentI al,
164 AlignmentI al_input, boolean ignoreFeatures)
166 assertNotNull("Original alignment was null", al);
167 assertNotNull("Generated alignment was null", al_input);
170 "Alignment dimension mismatch: originl contains "
171 + al.getHeight() + " and generated has "
172 + al_input.getHeight() + " sequences; original has "
173 + al.getWidth() + " and generated has "
174 + al_input.getWidth() + " columns.",
175 al.getHeight() == al_input.getHeight()
176 && al.getWidth() == al_input.getWidth());
178 // check Alignment annotation
179 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
180 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
182 // note - at moment we do not distinguish between alignment without any
183 // annotation rows and alignment with no annotation row vector
184 // we might want to revise this in future
185 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
186 : aa_original.length);
187 Map<Integer, java.util.BitSet> orig_groups = new HashMap<Integer, java.util.BitSet>(), new_groups = new HashMap<Integer, java.util.BitSet>();
189 if (aa_new != null && aa_original != null)
191 for (int i = 0; i < aa_original.length; i++)
193 if (aa_new.length > i)
195 assertTrue("Different alignment annotation at position " + i,
196 equalss(aa_original[i], aa_new[i]));
197 // compare graphGroup or graph properties - needed to verify JAL-1299
198 assertTrue("Graph type not identical.",
199 aa_original[i].graph == aa_new[i].graph);
200 assertTrue("Visibility not identical.",
201 aa_original[i].visible == aa_new[i].visible);
203 "Threshold line not identical.",
204 aa_original[i].threshold == null ? aa_new[i].threshold == null
205 : aa_original[i].threshold
206 .equals(aa_new[i].threshold));
207 // graphGroup may differ, but pattern should be the same
208 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2), n_ggrp = new Integer(
209 aa_new[i].graphGroup + 2);
210 BitSet orig_g = orig_groups.get(o_ggrp), new_g = new_groups
214 orig_groups.put(o_ggrp, orig_g = new BitSet());
218 new_groups.put(n_ggrp, new_g = new BitSet());
220 assertTrue("Graph Group pattern differs at annotation " + i,
221 orig_g.equals(new_g));
227 System.err.println("No matching annotation row for "
228 + aa_original[i].toString());
233 "Generated and imported alignment have different annotation sets ("
234 + aa_new_size + " != " + aa_original_size + ")",
235 aa_new_size == aa_original_size);
237 // check sequences, annotation and features
238 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
239 seq_original = al.getSequencesArray();
240 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
241 seq_new = al_input.getSequencesArray();
242 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
243 AlignmentAnnotation annot_original, annot_new;
245 for (int i = 0; i < al.getSequencesArray().length; i++)
247 String name = seq_original[i].getName();
248 int start = seq_original[i].getStart();
249 int end = seq_original[i].getEnd();
250 System.out.println("Check sequence: " + name + "/" + start + "-"
253 // search equal sequence
254 for (int in = 0; in < al_input.getSequencesArray().length; in++)
256 if (name.equals(seq_new[in].getName())
257 && start == seq_new[in].getStart()
258 && end == seq_new[in].getEnd())
260 String ss_original = seq_original[i].getSequenceAsString();
261 String ss_new = seq_new[in].getSequenceAsString();
262 assertTrue("The sequences " + name + "/" + start + "-" + end
263 + " are not equal", ss_original.equals(ss_new));
266 "Sequence Features were not equivalent"
267 + (ignoreFeatures ? " ignoring." : ""),
269 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
270 .getSequenceFeatures() == null)
271 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
272 .getSequenceFeatures() != null));
273 // compare sequence features
274 if (seq_original[i].getSequenceFeatures() != null
275 && seq_new[in].getSequenceFeatures() != null)
277 System.out.println("There are feature!!!");
278 sequenceFeatures_original = new SequenceFeature[seq_original[i]
279 .getSequenceFeatures().length];
280 sequenceFeatures_original = seq_original[i]
281 .getSequenceFeatures();
282 sequenceFeatures_new = new SequenceFeature[seq_new[in]
283 .getSequenceFeatures().length];
284 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
286 assertTrue("different number of features", seq_original[i]
287 .getSequenceFeatures().length == seq_new[in]
288 .getSequenceFeatures().length);
290 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
292 assertTrue("Different features",
293 sequenceFeatures_original[feat]
294 .equals(sequenceFeatures_new[feat]));
297 // compare alignment annotation
298 if (al.getSequenceAt(i).getAnnotation() != null
299 && al_input.getSequenceAt(in).getAnnotation() != null)
301 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
303 if (al.getSequenceAt(i).getAnnotation()[j] != null
304 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
306 annot_original = al.getSequenceAt(i).getAnnotation()[j];
307 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
308 assertTrue("Different annotation elements",
309 equalss(annot_original, annot_new));
313 else if (al.getSequenceAt(i).getAnnotation() == null
314 && al_input.getSequenceAt(in).getAnnotation() == null)
316 System.out.println("No annotations");
318 else if (al.getSequenceAt(i).getAnnotation() != null
319 && al_input.getSequenceAt(in).getAnnotation() == null)
321 assertTrue("Annotations differed between sequences ("
322 + al.getSequenceAt(i).getName() + ") and ("
323 + al_input.getSequenceAt(i).getName() + ")", false);
332 * compare annotations
334 private static boolean equalss(AlignmentAnnotation annot_or,
335 AlignmentAnnotation annot_new)
337 if (annot_or.annotations.length != annot_new.annotations.length)
339 System.err.println("Different lengths for annotation row elements: "
340 + annot_or.annotations.length + "!="
341 + annot_new.annotations.length);
344 for (int i = 0; i < annot_or.annotations.length; i++)
346 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
347 if (an_or != null && an_new != null)
349 if (!an_or.displayCharacter.trim().equals(
350 an_new.displayCharacter.trim())
351 || !("" + an_or.secondaryStructure).trim().equals(
352 ("" + an_new.secondaryStructure).trim())
353 || (an_or.description != an_new.description && (an_or.description == null
354 || an_new.description == null || !an_or.description
355 .equals(an_new.description))))
357 System.err.println("Annotation Element Mismatch\nElement " + i
358 + " in original: " + annot_or.annotations[i].toString()
359 + "\nElement " + i + " in new: "
360 + annot_new.annotations[i].toString());
364 else if (annot_or.annotations[i] == null
365 && annot_new.annotations[i] == null)
371 System.err.println("Annotation Element Mismatch\nElement "
374 + (annot_or.annotations[i] == null ? "is null"
375 : annot_or.annotations[i].toString())
379 + (annot_new.annotations[i] == null ? "is null"
380 : annot_new.annotations[i].toString()));