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.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertTrue;
26 import static org.testng.AssertJUnit.fail;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.Annotation;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.JvOptionPane;
36 import java.util.BitSet;
37 import java.util.HashMap;
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.Test;
43 public class StockholmFileTest
46 @BeforeClass(alwaysRun = true)
47 public void setUpJvOptionPane()
49 JvOptionPane.setInteractiveMode(false);
50 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53 static String PfamFile = "examples/PF00111_seed.stk",
54 RfamFile = "examples/RF00031_folded.stk";
56 @Test(groups = { "Functional" })
57 public void pfamFileIO() throws Exception
59 testFileIOwithFormat(new File(PfamFile), FileFormat.Stockholm, -1, 0);
62 @Test(groups = { "Functional" })
63 public void pfamFileDataExtraction() throws Exception
65 AppletFormatAdapter af = new AppletFormatAdapter();
66 AlignmentI al = af.readFile(PfamFile, DataSourceType.FILE,
67 new IdentifyFile().identify(PfamFile, DataSourceType.FILE));
69 for (SequenceI sq : al.getSequences())
71 if (sq.getAllPDBEntries() != null)
73 numpdb += sq.getAllPDBEntries().size();
77 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
81 @Test(groups = { "Functional" })
82 public void rfamFileIO() throws Exception
84 testFileIOwithFormat(new File(RfamFile), FileFormat.Stockholm, 2, 1);
88 * test alignment data in given file can be imported, exported and reimported
92 * - source datafile (IdentifyFile.identify() should work with it)
94 * - label for IO class used to write and read back in the data from
98 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
99 int naliannot, int nminseqann)
101 System.out.println("Reading file: " + f);
102 String ff = f.getPath();
105 AppletFormatAdapter rf = new AppletFormatAdapter();
107 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
108 new IdentifyFile().identify(ff, DataSourceType.FILE));
110 assertNotNull("Couldn't read supplied alignment data.", al);
112 // make sure dataset is initialised ? not sure about this
113 for (int i = 0; i < al.getSequencesArray().length; ++i)
115 al.getSequenceAt(i).createDatasetSequence();
117 String outputfile = rf.formatSequences(ioformat, al, true);
118 System.out.println("Output file in '" + ioformat + "':\n"
119 + outputfile + "\n<<EOF\n");
120 // test for consistency in io
121 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
122 DataSourceType.PASTE, ioformat);
123 assertNotNull("Couldn't parse reimported alignment data.", al_input);
125 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
126 DataSourceType.PASTE);
127 assertNotNull("Identify routine failed for outputformat " + ioformat,
130 "Identify routine could not recognise output generated by '"
131 + ioformat + "' writer",
132 ioformat.equals(identifyoutput));
133 testAlignmentEquivalence(al, al_input, false);
134 int numaliannot = 0, numsqswithali = 0;
135 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
137 if (ala.sequenceRef == null)
148 assertEquals("Number of alignment annotations", naliannot,
153 "Number of sequence associated annotations wasn't at least "
154 + nminseqann, numsqswithali >= nminseqann);
156 } catch (Exception e)
159 assertTrue("Couln't format the alignment for output file.", false);
164 * assert alignment equivalence
169 * 'secondary' or generated alignment from some datapreserving
171 * @param ignoreFeatures
172 * when true, differences in sequence feature annotation are ignored
174 public static void testAlignmentEquivalence(AlignmentI al,
175 AlignmentI al_input, boolean ignoreFeatures)
177 assertNotNull("Original alignment was null", al);
178 assertNotNull("Generated alignment was null", al_input);
180 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
181 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
182 + "x" + al_input.getWidth(),
183 al.getHeight() == al_input.getHeight()
184 && al.getWidth() == al_input.getWidth());
186 // check Alignment annotation
187 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
188 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
190 // note - at moment we do not distinguish between alignment without any
191 // annotation rows and alignment with no annotation row vector
192 // we might want to revise this in future
193 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
194 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
195 Map<Integer, BitSet> orig_groups = new HashMap<Integer, BitSet>();
196 Map<Integer, BitSet> new_groups = new HashMap<Integer, BitSet>();
198 if (aa_new != null && aa_original != null)
200 for (int i = 0; i < aa_original.length; i++)
202 if (aa_new.length > i)
204 assertTrue("Different alignment annotation at position " + i,
205 equalss(aa_original[i], aa_new[i]));
206 // compare graphGroup or graph properties - needed to verify JAL-1299
207 assertEquals("Graph type not identical.", aa_original[i].graph,
209 assertEquals("Visibility not identical.", aa_original[i].visible,
211 assertEquals("Threshold line not identical.",
212 aa_original[i].threshold, aa_new[i].threshold);
213 // graphGroup may differ, but pattern should be the same
214 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2);
215 Integer n_ggrp = new Integer(aa_new[i].graphGroup + 2);
216 BitSet orig_g = orig_groups.get(o_ggrp);
217 BitSet new_g = new_groups.get(n_ggrp);
220 orig_groups.put(o_ggrp, orig_g = new BitSet());
224 new_groups.put(n_ggrp, new_g = new BitSet());
226 assertEquals("Graph Group pattern differs at annotation " + i,
233 System.err.println("No matching annotation row for "
234 + aa_original[i].toString());
239 "Generated and imported alignment have different annotation sets",
240 aa_original_size, aa_new_size);
242 // check sequences, annotation and features
243 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
244 seq_original = al.getSequencesArray();
245 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
246 seq_new = al_input.getSequencesArray();
247 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
248 AlignmentAnnotation annot_original, annot_new;
250 for (int i = 0; i < al.getSequencesArray().length; i++)
252 String name = seq_original[i].getName();
253 int start = seq_original[i].getStart();
254 int end = seq_original[i].getEnd();
255 System.out.println("Check sequence: " + name + "/" + start + "-"
258 // search equal sequence
259 for (int in = 0; in < al_input.getSequencesArray().length; in++)
261 if (name.equals(seq_new[in].getName())
262 && start == seq_new[in].getStart()
263 && end == seq_new[in].getEnd())
265 String ss_original = seq_original[i].getSequenceAsString();
266 String ss_new = seq_new[in].getSequenceAsString();
267 assertEquals("The sequences " + name + "/" + start + "-" + end
268 + " are not equal", ss_original, ss_new);
271 "Sequence Features were not equivalent"
272 + (ignoreFeatures ? " ignoring." : ""),
274 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
275 .getSequenceFeatures() == null)
276 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
277 .getSequenceFeatures() != null));
278 // compare sequence features
279 if (seq_original[i].getSequenceFeatures() != null
280 && seq_new[in].getSequenceFeatures() != null)
282 System.out.println("There are feature!!!");
283 sequenceFeatures_original = new SequenceFeature[seq_original[i]
284 .getSequenceFeatures().length];
285 sequenceFeatures_original = seq_original[i]
286 .getSequenceFeatures();
287 sequenceFeatures_new = new SequenceFeature[seq_new[in]
288 .getSequenceFeatures().length];
289 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
291 assertEquals("different number of features",
292 seq_original[i].getSequenceFeatures().length,
293 seq_new[in].getSequenceFeatures().length);
295 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
297 assertEquals("Different features",
298 sequenceFeatures_original[feat],
299 sequenceFeatures_new[feat]);
302 // compare alignment annotation
303 if (al.getSequenceAt(i).getAnnotation() != null
304 && al_input.getSequenceAt(in).getAnnotation() != null)
306 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
308 if (al.getSequenceAt(i).getAnnotation()[j] != null
309 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
311 annot_original = al.getSequenceAt(i).getAnnotation()[j];
312 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
313 assertTrue("Different annotation elements",
314 equalss(annot_original, annot_new));
318 else if (al.getSequenceAt(i).getAnnotation() == null
319 && al_input.getSequenceAt(in).getAnnotation() == null)
321 System.out.println("No annotations");
323 else if (al.getSequenceAt(i).getAnnotation() != null
324 && al_input.getSequenceAt(in).getAnnotation() == null)
326 fail("Annotations differed between sequences ("
327 + al.getSequenceAt(i).getName() + ") and ("
328 + al_input.getSequenceAt(i).getName() + ")");
337 * compare annotations
339 private static boolean equalss(AlignmentAnnotation annot_or,
340 AlignmentAnnotation annot_new)
342 if (annot_or.annotations.length != annot_new.annotations.length)
344 System.err.println("Different lengths for annotation row elements: "
345 + annot_or.annotations.length + "!="
346 + annot_new.annotations.length);
349 for (int i = 0; i < annot_or.annotations.length; i++)
351 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
352 if (an_or != null && an_new != null)
354 if (!an_or.displayCharacter.trim().equals(
355 an_new.displayCharacter.trim())
356 || !("" + an_or.secondaryStructure).trim().equals(
357 ("" + an_new.secondaryStructure).trim())
358 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
359 .trim().length() == 0)
360 || (an_new.description == null && an_or.description
361 .trim().length() == 0) || an_or.description
362 .trim().equals(an_new.description.trim()))))
364 System.err.println("Annotation Element Mismatch\nElement " + i
365 + " in original: " + annot_or.annotations[i].toString()
366 + "\nElement " + i + " in new: "
367 + annot_new.annotations[i].toString());
371 else if (annot_or.annotations[i] == null
372 && annot_new.annotations[i] == null)
378 System.err.println("Annotation Element Mismatch\nElement "
381 + (annot_or.annotations[i] == null ? "is null"
382 : annot_or.annotations[i].toString())
386 + (annot_new.annotations[i] == null ? "is null"
387 : annot_new.annotations[i].toString()));