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.Arrays;
37 import java.util.BitSet;
38 import java.util.HashMap;
39 import java.util.List;
42 import org.testng.Assert;
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.Test;
46 public class StockholmFileTest
49 @BeforeClass(alwaysRun = true)
50 public void setUpJvOptionPane()
52 JvOptionPane.setInteractiveMode(false);
53 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56 static String PfamFile = "examples/PF00111_seed.stk",
57 RfamFile = "examples/RF00031_folded.stk";
59 @Test(groups = { "Functional" })
60 public void pfamFileIO() throws Exception
62 testFileIOwithFormat(new File(PfamFile), FileFormat.Stockholm, -1, 0,
66 @Test(groups = { "Functional" })
67 public void pfamFileDataExtraction() throws Exception
69 AppletFormatAdapter af = new AppletFormatAdapter();
70 AlignmentI al = af.readFile(PfamFile, DataSourceType.FILE,
71 new IdentifyFile().identify(PfamFile, DataSourceType.FILE));
73 for (SequenceI sq : al.getSequences())
75 if (sq.getAllPDBEntries() != null)
77 numpdb += sq.getAllPDBEntries().size();
81 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
85 @Test(groups = { "Functional" })
86 public void rfamFileIO() throws Exception
88 testFileIOwithFormat(new File(RfamFile), FileFormat.Stockholm, 2, 1,
93 * test alignment data in given file can be imported, exported and reimported
97 * - source datafile (IdentifyFile.identify() should work with it)
99 * - label for IO class used to write and read back in the data from
101 * @param ignoreRowVisibility
104 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
105 int naliannot, int nminseqann, boolean ignoreRowVisibility)
107 System.out.println("Reading file: " + f);
108 String ff = f.getPath();
111 AppletFormatAdapter rf = new AppletFormatAdapter();
113 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
114 new IdentifyFile().identify(ff, DataSourceType.FILE));
116 assertNotNull("Couldn't read supplied alignment data.", al);
118 // make sure dataset is initialised ? not sure about this
119 for (int i = 0; i < al.getSequencesArray().length; ++i)
121 al.getSequenceAt(i).createDatasetSequence();
123 String outputfile = rf.formatSequences(ioformat, al, true);
124 System.out.println("Output file in '" + ioformat + "':\n"
125 + outputfile + "\n<<EOF\n");
126 // test for consistency in io
127 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
128 DataSourceType.PASTE, ioformat);
129 assertNotNull("Couldn't parse reimported alignment data.", al_input);
131 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
132 DataSourceType.PASTE);
133 assertNotNull("Identify routine failed for outputformat " + ioformat,
136 "Identify routine could not recognise output generated by '"
137 + ioformat + "' writer",
138 ioformat.equals(identifyoutput));
139 testAlignmentEquivalence(al, al_input, false);
140 int numaliannot = 0, numsqswithali = 0;
141 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
143 if (ala.sequenceRef == null)
154 assertEquals("Number of alignment annotations", naliannot,
159 "Number of sequence associated annotations wasn't at least "
160 + nminseqann, numsqswithali >= nminseqann);
162 } catch (Exception e)
165 assertTrue("Couln't format the alignment for output file.", false);
170 * assert alignment equivalence
175 * 'secondary' or generated alignment from some datapreserving
177 * @param ignoreFeatures
178 * when true, differences in sequence feature annotation are ignored
180 public static void testAlignmentEquivalence(AlignmentI al,
181 AlignmentI al_input, boolean ignoreFeatures)
183 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
187 * assert alignment equivalence - uses special comparators for RNA structure
193 * 'secondary' or generated alignment from some datapreserving
195 * @param ignoreFeatures
196 * when true, differences in sequence feature annotation are ignored
198 * @param ignoreRowVisibility
199 * when true, do not fail if there are differences in the visibility
201 * @param allowNullAnnotation
202 * when true, positions in alignment annotation that are null will be
203 * considered equal to positions containing annotation where
204 * Annotation.isWhitespace() returns true.
207 public static void testAlignmentEquivalence(AlignmentI al,
208 AlignmentI al_input, boolean ignoreFeatures,
209 boolean ignoreRowVisibility, boolean allowNullAnnotation)
211 assertNotNull("Original alignment was null", al);
212 assertNotNull("Generated alignment was null", al_input);
214 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
215 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
216 + "x" + al_input.getWidth(),
217 al.getHeight() == al_input.getHeight()
218 && al.getWidth() == al_input.getWidth());
220 // check Alignment annotation
221 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
222 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
224 // note - at moment we do not distinguish between alignment without any
225 // annotation rows and alignment with no annotation row vector
226 // we might want to revise this in future
227 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
228 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
229 Map<Integer, BitSet> orig_groups = new HashMap<Integer, BitSet>();
230 Map<Integer, BitSet> new_groups = new HashMap<Integer, BitSet>();
232 if (aa_new != null && aa_original != null)
234 for (int i = 0; i < aa_original.length; i++)
236 if (aa_new.length > i)
238 assertEqualSecondaryStructure(
239 "Different alignment annotation at position " + i,
240 aa_original[i], aa_new[i], allowNullAnnotation);
241 // compare graphGroup or graph properties - needed to verify JAL-1299
242 assertEquals("Graph type not identical.", aa_original[i].graph,
244 if (!ignoreRowVisibility)
246 assertEquals("Visibility not identical.",
247 aa_original[i].visible,
250 assertEquals("Threshold line not identical.",
251 aa_original[i].threshold, aa_new[i].threshold);
252 // graphGroup may differ, but pattern should be the same
253 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2);
254 Integer n_ggrp = new Integer(aa_new[i].graphGroup + 2);
255 BitSet orig_g = orig_groups.get(o_ggrp);
256 BitSet new_g = new_groups.get(n_ggrp);
259 orig_groups.put(o_ggrp, orig_g = new BitSet());
263 new_groups.put(n_ggrp, new_g = new BitSet());
265 assertEquals("Graph Group pattern differs at annotation " + i,
272 System.err.println("No matching annotation row for "
273 + aa_original[i].toString());
278 "Generated and imported alignment have different annotation sets",
279 aa_original_size, aa_new_size);
281 // check sequences, annotation and features
282 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
283 seq_original = al.getSequencesArray();
284 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
285 seq_new = al_input.getSequencesArray();
286 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
287 AlignmentAnnotation annot_original, annot_new;
289 for (int i = 0; i < al.getSequencesArray().length; i++)
291 String name = seq_original[i].getName();
292 int start = seq_original[i].getStart();
293 int end = seq_original[i].getEnd();
294 System.out.println("Check sequence: " + name + "/" + start + "-"
297 // search equal sequence
298 for (int in = 0; in < al_input.getSequencesArray().length; in++)
300 if (name.equals(seq_new[in].getName())
301 && start == seq_new[in].getStart()
302 && end == seq_new[in].getEnd())
304 String ss_original = seq_original[i].getSequenceAsString();
305 String ss_new = seq_new[in].getSequenceAsString();
306 assertEquals("The sequences " + name + "/" + start + "-" + end
307 + " are not equal", ss_original, ss_new);
310 "Sequence Features were not equivalent"
311 + (ignoreFeatures ? " ignoring." : ""),
313 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
314 .getSequenceFeatures() == null)
315 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
316 .getSequenceFeatures() != null));
317 // compare sequence features
318 if (seq_original[i].getSequenceFeatures() != null
319 && seq_new[in].getSequenceFeatures() != null)
321 System.out.println("There are feature!!!");
322 sequenceFeatures_original = new SequenceFeature[seq_original[i]
323 .getSequenceFeatures().length];
324 sequenceFeatures_original = seq_original[i]
325 .getSequenceFeatures();
326 sequenceFeatures_new = new SequenceFeature[seq_new[in]
327 .getSequenceFeatures().length];
328 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
330 assertEquals("different number of features",
331 seq_original[i].getSequenceFeatures().length,
332 seq_new[in].getSequenceFeatures().length);
334 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
336 assertEquals("Different features",
337 sequenceFeatures_original[feat],
338 sequenceFeatures_new[feat]);
341 // compare alignment annotation
342 if (al.getSequenceAt(i).getAnnotation() != null
343 && al_input.getSequenceAt(in).getAnnotation() != null)
345 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
347 if (al.getSequenceAt(i).getAnnotation()[j] != null
348 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
350 annot_original = al.getSequenceAt(i).getAnnotation()[j];
351 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
352 assertEqualSecondaryStructure(
353 "Different annotation elements", annot_original,
354 annot_new, allowNullAnnotation);
358 else if (al.getSequenceAt(i).getAnnotation() == null
359 && al_input.getSequenceAt(in).getAnnotation() == null)
361 System.out.println("No annotations");
363 else if (al.getSequenceAt(i).getAnnotation() != null
364 && al_input.getSequenceAt(in).getAnnotation() == null)
366 fail("Annotations differed between sequences ("
367 + al.getSequenceAt(i).getName() + ") and ("
368 + al_input.getSequenceAt(i).getName() + ")");
377 * compare two annotation rows, with special support for secondary structure
378 * comparison. With RNA, only the value and the secondaryStructure symbols are
379 * compared, displayCharacter and description are ignored. Annotations where
380 * Annotation.isWhitespace() is true are always considered equal.
383 * - not actually used yet..
385 * - the original annotation
387 * - the one compared to the original annotation
388 * @param allowNullEquivalence
389 * when true, positions in alignment annotation that are null will be
390 * considered equal to non-null positions for which
391 * Annotation.isWhitespace() is true.
393 private static void assertEqualSecondaryStructure(String message,
394 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
395 boolean allowNullEqivalence)
397 // TODO: test to cover this assert behaves correctly for all allowed
398 // variations of secondary structure annotation row equivalence
399 if (annot_or.annotations.length != annot_new.annotations.length)
401 fail("Different lengths for annotation row elements: "
402 + annot_or.annotations.length + "!="
403 + annot_new.annotations.length);
405 boolean isRna = annot_or.isRNA();
406 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
407 + " secondary structure in the row.",
408 isRna == annot_new.isRNA());
409 for (int i = 0; i < annot_or.annotations.length; i++)
411 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
412 if (an_or != null && an_new != null)
417 if (an_or.secondaryStructure != an_new.secondaryStructure
418 || ((Float.isNaN(an_or.value) != Float
419 .isNaN(an_new.value)) || an_or.value != an_new.value))
421 fail("Different RNA secondary structure at column " + i
422 + " expected: [" + annot_or.annotations[i].toString()
423 + "] but got: [" + annot_new.annotations[i].toString()
429 // not RNA secondary structure, so expect all elements to match...
430 if ((an_or.isWhitespace() != an_new.isWhitespace())
431 || !an_or.displayCharacter.trim().equals(
432 an_new.displayCharacter.trim())
433 || !("" + an_or.secondaryStructure).trim().equals(
434 ("" + an_new.secondaryStructure).trim())
435 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
436 .trim().length() == 0)
437 || (an_new.description == null && an_or.description
438 .trim().length() == 0) || an_or.description
439 .trim().equals(an_new.description.trim())))
440 || !((Float.isNaN(an_or.value) && Float
441 .isNaN(an_new.value)) || an_or.value == an_new.value))
443 fail("Annotation Element Mismatch\nElement " + i
444 + " in original: " + annot_or.annotations[i].toString()
445 + "\nElement " + i + " in new: "
446 + annot_new.annotations[i].toString());
450 else if (annot_or.annotations[i] == null
451 && annot_new.annotations[i] == null)
457 if (allowNullEqivalence)
459 if (an_or != null && an_or.isWhitespace())
464 if (an_new != null && an_new.isWhitespace())
469 // need also to test for null in one, non-SS annotation in other...
470 fail("Annotation Element Mismatch\nElement " + i + " in original: "
471 + (an_or == null ? "is null" : an_or.toString())
472 + "\nElement " + i + " in new: "
473 + (an_new == null ? "is null" : an_new.toString()));
479 * @see assertEqualSecondaryStructure - test if two secondary structure
480 * annotations are not equal
484 * @param allowNullEquivalence
486 public static void assertNotEqualSecondaryStructure(String message,
487 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
488 boolean allowNullEquivalence)
490 boolean thrown = false;
493 assertEqualSecondaryStructure("", an_orig, an_new,
494 allowNullEquivalence);
495 } catch (AssertionError af)
501 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
504 private AlignmentAnnotation makeAnnot(Annotation ae)
506 return new AlignmentAnnotation("label", "description", new Annotation[]
510 @Test(groups={"Functional"})
511 public void testAnnotationEquivalence()
513 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
514 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
516 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
517 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
518 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
520 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
522 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
523 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
524 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
526 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
529 // check self equivalence
530 for (boolean allowNull : new boolean[] { true, false })
532 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
534 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
536 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
537 anotherSheetWithLabel, allowNull);
538 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
539 anotherRnaNoDC, allowNull);
540 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
541 anotherRnaWithDC, allowNull);
542 // display character doesn't matter for RNA structure (for 2.10.2)
543 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
545 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
549 // verify others are different
550 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
551 sheetWithLabel, rnaWithDC);
552 for (int p = 0; p < aaSet.size(); p++)
554 for (int q = 0; q < aaSet.size(); q++)
558 assertNotEqualSecondaryStructure("Should be different",
559 aaSet.get(p), aaSet.get(q), false);
563 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
564 aaSet.get(q), false);
565 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
567 assertNotEqualSecondaryStructure(
568 "Should be different to empty anot", aaSet.get(p),
569 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
570 assertNotEqualSecondaryStructure(
571 "Should be different to empty annot",
572 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
574 assertNotEqualSecondaryStructure("Should be different to null",
575 aaSet.get(p), makeAnnot(null), false);
576 assertNotEqualSecondaryStructure("Should be different to null",
577 makeAnnot(null), aaSet.get(q), true);
586 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
587 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
588 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
589 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
590 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
592 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
593 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
594 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
595 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
596 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
597 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
598 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
599 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
601 @Test(groups = { "Functional" })
602 public void secondaryStructureForRNASequence() throws Exception
604 roundTripSSForRNA(aliFile, annFile);
607 @Test(groups = { "Functional" })
608 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
610 roundTripSSForRNA(aliFile, annFileCurlyWuss);
613 @Test(groups = { "Functional" })
614 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
616 roundTripSSForRNA(aliFile, annFileFullWuss);
619 @Test(groups = { "Functional" })
620 public void detectWussBrackets()
622 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
624 Assert.assertTrue(StockholmFile.DETECT_BRACKETS.matchAt("" + ch, 0),
625 "Didn't recognise " + ch + " as a WUSS bracket");
627 for (char ch : new char[] { '@', '!', 'V', 'Q', '*', ' ', '-', '.' })
629 Assert.assertFalse(StockholmFile.DETECT_BRACKETS.matchAt("" + ch, 0),
630 "Shouldn't recognise " + ch + " as a WUSS bracket");
633 private static void roundTripSSForRNA(String aliFile, String annFile)
636 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
637 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
638 AnnotationFile aaf = new AnnotationFile();
639 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
640 al.getAlignmentAnnotation()[0].visible = true;
642 // TODO: create a better 'save as <format>' pattern
643 StockholmFile sf = new StockholmFile(al);
645 String stockholmFile = sf.print(al.getSequencesArray(), true);
647 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
648 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
649 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
650 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
651 // .getViewport().getAlignment().getSequences(), true, true);
652 testAlignmentEquivalence(al, newAl, true, true, true);