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 ignoreFeatures
102 * @param ignoreRowVisibility
103 * @param allowNullAnnotations
106 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
107 int naliannot, int nminseqann, boolean ignoreFeatures,
108 boolean ignoreRowVisibility, boolean allowNullAnnotations)
110 System.out.println("Reading file: " + f);
111 String ff = f.getPath();
114 AppletFormatAdapter rf = new AppletFormatAdapter();
116 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
117 new IdentifyFile().identify(ff, DataSourceType.FILE));
119 assertNotNull("Couldn't read supplied alignment data.", al);
121 // make sure dataset is initialised ? not sure about this
122 for (int i = 0; i < al.getSequencesArray().length; ++i)
124 al.getSequenceAt(i).createDatasetSequence();
126 String outputfile = rf.formatSequences(ioformat, al, true);
127 System.out.println("Output file in '" + ioformat + "':\n"
128 + outputfile + "\n<<EOF\n");
129 // test for consistency in io
130 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
131 DataSourceType.PASTE, ioformat);
132 assertNotNull("Couldn't parse reimported alignment data.", al_input);
134 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
135 DataSourceType.PASTE);
136 assertNotNull("Identify routine failed for outputformat " + ioformat,
139 "Identify routine could not recognise output generated by '"
140 + ioformat + "' writer",
141 ioformat.equals(identifyoutput));
142 testAlignmentEquivalence(al, al_input, ignoreFeatures,
143 ignoreRowVisibility, allowNullAnnotations);
144 int numaliannot = 0, numsqswithali = 0;
145 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
147 if (ala.sequenceRef == null)
158 assertEquals("Number of alignment annotations", naliannot,
163 "Number of sequence associated annotations wasn't at least "
164 + nminseqann, numsqswithali >= nminseqann);
166 } catch (Exception e)
169 assertTrue("Couln't format the alignment for output file.", false);
174 * assert alignment equivalence
179 * 'secondary' or generated alignment from some datapreserving
181 * @param ignoreFeatures
182 * when true, differences in sequence feature annotation are ignored
184 public static void testAlignmentEquivalence(AlignmentI al,
185 AlignmentI al_input, boolean ignoreFeatures)
187 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
191 * assert alignment equivalence - uses special comparators for RNA structure
197 * 'secondary' or generated alignment from some datapreserving
199 * @param ignoreFeatures
200 * when true, differences in sequence feature annotation are ignored
202 * @param ignoreRowVisibility
203 * when true, do not fail if there are differences in the visibility
205 * @param allowNullAnnotation
206 * when true, positions in alignment annotation that are null will be
207 * considered equal to positions containing annotation where
208 * Annotation.isWhitespace() returns true.
211 public static void testAlignmentEquivalence(AlignmentI al,
212 AlignmentI al_input, boolean ignoreFeatures,
213 boolean ignoreRowVisibility, boolean allowNullAnnotation)
215 assertNotNull("Original alignment was null", al);
216 assertNotNull("Generated alignment was null", al_input);
218 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
219 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
220 + "x" + al_input.getWidth(),
221 al.getHeight() == al_input.getHeight()
222 && al.getWidth() == al_input.getWidth());
224 // check Alignment annotation
225 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
226 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
228 // note - at moment we do not distinguish between alignment without any
229 // annotation rows and alignment with no annotation row vector
230 // we might want to revise this in future
231 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
232 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
233 Map<Integer, BitSet> orig_groups = new HashMap<Integer, BitSet>();
234 Map<Integer, BitSet> new_groups = new HashMap<Integer, BitSet>();
236 if (aa_new != null && aa_original != null)
238 for (int i = 0; i < aa_original.length; i++)
240 if (aa_new.length > i)
242 assertEqualSecondaryStructure(
243 "Different alignment annotation at position " + i,
244 aa_original[i], aa_new[i], allowNullAnnotation);
245 // compare graphGroup or graph properties - needed to verify JAL-1299
246 assertEquals("Graph type not identical.", aa_original[i].graph,
248 if (!ignoreRowVisibility)
250 assertEquals("Visibility not identical.",
251 aa_original[i].visible,
254 assertEquals("Threshold line not identical.",
255 aa_original[i].threshold, aa_new[i].threshold);
256 // graphGroup may differ, but pattern should be the same
257 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2);
258 Integer n_ggrp = new Integer(aa_new[i].graphGroup + 2);
259 BitSet orig_g = orig_groups.get(o_ggrp);
260 BitSet new_g = new_groups.get(n_ggrp);
263 orig_groups.put(o_ggrp, orig_g = new BitSet());
267 new_groups.put(n_ggrp, new_g = new BitSet());
269 assertEquals("Graph Group pattern differs at annotation " + i,
276 System.err.println("No matching annotation row for "
277 + aa_original[i].toString());
282 "Generated and imported alignment have different annotation sets",
283 aa_original_size, aa_new_size);
285 // check sequences, annotation and features
286 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
287 seq_original = al.getSequencesArray();
288 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
289 seq_new = al_input.getSequencesArray();
290 List<SequenceFeature> sequenceFeatures_original;
291 List<SequenceFeature> sequenceFeatures_new;
292 AlignmentAnnotation annot_original, annot_new;
294 for (int i = 0; i < al.getSequencesArray().length; i++)
296 String name = seq_original[i].getName();
297 int start = seq_original[i].getStart();
298 int end = seq_original[i].getEnd();
299 System.out.println("Check sequence: " + name + "/" + start + "-"
302 // search equal sequence
303 for (int in = 0; in < al_input.getSequencesArray().length; in++)
305 if (name.equals(seq_new[in].getName())
306 && start == seq_new[in].getStart()
307 && end == seq_new[in].getEnd())
309 String ss_original = seq_original[i].getSequenceAsString();
310 String ss_new = seq_new[in].getSequenceAsString();
311 assertEquals("The sequences " + name + "/" + start + "-" + end
312 + " are not equal", ss_original, ss_new);
315 "Sequence Features were not equivalent"
316 + (ignoreFeatures ? " ignoring." : ""),
318 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
319 .getSequenceFeatures() == null)
320 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
321 .getSequenceFeatures() != null));
322 // compare sequence features
323 if (seq_original[i].getSequenceFeatures() != null
324 && seq_new[in].getSequenceFeatures() != null)
326 System.out.println("There are feature!!!");
327 sequenceFeatures_original = seq_original[i]
328 .getSequenceFeatures();
329 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
331 assertEquals("different number of features", seq_original[i]
332 .getSequenceFeatures().size(), seq_new[in]
333 .getSequenceFeatures().size());
335 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
338 assertEquals("Different features",
339 sequenceFeatures_original.get(feat),
340 sequenceFeatures_new.get(feat));
343 // compare alignment annotation
344 if (al.getSequenceAt(i).getAnnotation() != null
345 && al_input.getSequenceAt(in).getAnnotation() != null)
347 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
349 if (al.getSequenceAt(i).getAnnotation()[j] != null
350 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
352 annot_original = al.getSequenceAt(i).getAnnotation()[j];
353 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
354 assertEqualSecondaryStructure(
355 "Different annotation elements", annot_original,
356 annot_new, allowNullAnnotation);
360 else if (al.getSequenceAt(i).getAnnotation() == null
361 && al_input.getSequenceAt(in).getAnnotation() == null)
363 System.out.println("No annotations");
365 else if (al.getSequenceAt(i).getAnnotation() != null
366 && al_input.getSequenceAt(in).getAnnotation() == null)
368 fail("Annotations differed between sequences ("
369 + al.getSequenceAt(i).getName() + ") and ("
370 + al_input.getSequenceAt(i).getName() + ")");
379 * compare two annotation rows, with special support for secondary structure
380 * comparison. With RNA, only the value and the secondaryStructure symbols are
381 * compared, displayCharacter and description are ignored. Annotations where
382 * Annotation.isWhitespace() is true are always considered equal.
385 * - not actually used yet..
387 * - the original annotation
389 * - the one compared to the original annotation
390 * @param allowNullEquivalence
391 * when true, positions in alignment annotation that are null will be
392 * considered equal to non-null positions for which
393 * Annotation.isWhitespace() is true.
395 private static void assertEqualSecondaryStructure(String message,
396 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
397 boolean allowNullEqivalence)
399 // TODO: test to cover this assert behaves correctly for all allowed
400 // variations of secondary structure annotation row equivalence
401 if (annot_or.annotations.length != annot_new.annotations.length)
403 fail("Different lengths for annotation row elements: "
404 + annot_or.annotations.length + "!="
405 + annot_new.annotations.length);
407 boolean isRna = annot_or.isRNA();
408 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
409 + " secondary structure in the row.",
410 isRna == annot_new.isRNA());
411 for (int i = 0; i < annot_or.annotations.length; i++)
413 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
414 if (an_or != null && an_new != null)
419 if (an_or.secondaryStructure != an_new.secondaryStructure
420 || ((Float.isNaN(an_or.value) != Float
421 .isNaN(an_new.value)) || an_or.value != an_new.value))
423 fail("Different RNA secondary structure at column " + i
424 + " expected: [" + annot_or.annotations[i].toString()
425 + "] but got: [" + annot_new.annotations[i].toString()
431 // not RNA secondary structure, so expect all elements to match...
432 if ((an_or.isWhitespace() != an_new.isWhitespace())
433 || !an_or.displayCharacter.trim().equals(
434 an_new.displayCharacter.trim())
435 || !("" + an_or.secondaryStructure).trim().equals(
436 ("" + an_new.secondaryStructure).trim())
437 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
438 .trim().length() == 0)
439 || (an_new.description == null && an_or.description
440 .trim().length() == 0) || an_or.description
441 .trim().equals(an_new.description.trim())))
442 || !((Float.isNaN(an_or.value) && Float
443 .isNaN(an_new.value)) || an_or.value == an_new.value))
445 fail("Annotation Element Mismatch\nElement " + i
446 + " in original: " + annot_or.annotations[i].toString()
447 + "\nElement " + i + " in new: "
448 + annot_new.annotations[i].toString());
452 else if (annot_or.annotations[i] == null
453 && annot_new.annotations[i] == null)
459 if (allowNullEqivalence)
461 if (an_or != null && an_or.isWhitespace())
466 if (an_new != null && an_new.isWhitespace())
471 // need also to test for null in one, non-SS annotation in other...
472 fail("Annotation Element Mismatch\nElement " + i + " in original: "
473 + (an_or == null ? "is null" : an_or.toString())
474 + "\nElement " + i + " in new: "
475 + (an_new == null ? "is null" : an_new.toString()));
481 * @see assertEqualSecondaryStructure - test if two secondary structure
482 * annotations are not equal
486 * @param allowNullEquivalence
488 public static void assertNotEqualSecondaryStructure(String message,
489 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
490 boolean allowNullEquivalence)
492 boolean thrown = false;
495 assertEqualSecondaryStructure("", an_orig, an_new,
496 allowNullEquivalence);
497 } catch (AssertionError af)
503 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
506 private AlignmentAnnotation makeAnnot(Annotation ae)
508 return new AlignmentAnnotation("label", "description", new Annotation[]
512 @Test(groups={"Functional"})
513 public void testAnnotationEquivalence()
515 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
516 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
518 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
519 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
520 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
522 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
524 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
525 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
526 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
528 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
531 // check self equivalence
532 for (boolean allowNull : new boolean[] { true, false })
534 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
536 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
538 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
539 anotherSheetWithLabel, allowNull);
540 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
541 anotherRnaNoDC, allowNull);
542 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
543 anotherRnaWithDC, allowNull);
544 // display character doesn't matter for RNA structure (for 2.10.2)
545 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
547 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
551 // verify others are different
552 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
553 sheetWithLabel, rnaWithDC);
554 for (int p = 0; p < aaSet.size(); p++)
556 for (int q = 0; q < aaSet.size(); q++)
560 assertNotEqualSecondaryStructure("Should be different",
561 aaSet.get(p), aaSet.get(q), false);
565 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
566 aaSet.get(q), false);
567 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
569 assertNotEqualSecondaryStructure(
570 "Should be different to empty anot", aaSet.get(p),
571 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
572 assertNotEqualSecondaryStructure(
573 "Should be different to empty annot",
574 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
576 assertNotEqualSecondaryStructure("Should be different to null",
577 aaSet.get(p), makeAnnot(null), false);
578 assertNotEqualSecondaryStructure("Should be different to null",
579 makeAnnot(null), aaSet.get(q), true);
588 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
589 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
590 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
591 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
592 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
594 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
595 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
596 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
597 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
598 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
599 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
600 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
601 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
603 @Test(groups = { "Functional" })
604 public void secondaryStructureForRNASequence() throws Exception
606 roundTripSSForRNA(aliFile, annFile);
609 @Test(groups = { "Functional" })
610 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
612 roundTripSSForRNA(aliFile, annFileCurlyWuss);
615 @Test(groups = { "Functional" })
616 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
618 roundTripSSForRNA(aliFile, annFileFullWuss);
621 @Test(groups = { "Functional" })
622 public void detectWussBrackets()
624 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
626 Assert.assertTrue(StockholmFile.DETECT_BRACKETS.matchAt("" + ch, 0),
627 "Didn't recognise " + ch + " as a WUSS bracket");
629 for (char ch : new char[] { '@', '!', 'V', 'Q', '*', ' ', '-', '.' })
631 Assert.assertFalse(StockholmFile.DETECT_BRACKETS.matchAt("" + ch, 0),
632 "Shouldn't recognise " + ch + " as a WUSS bracket");
635 private static void roundTripSSForRNA(String aliFile, String annFile)
638 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
639 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
640 AnnotationFile aaf = new AnnotationFile();
641 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
642 al.getAlignmentAnnotation()[0].visible = true;
644 // TODO: create a better 'save as <format>' pattern
645 StockholmFile sf = new StockholmFile(al);
647 String stockholmFile = sf.print(al.getSequencesArray(), true);
649 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
650 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
651 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
652 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
653 // .getViewport().getAlignment().getSequences(), true, true);
654 testAlignmentEquivalence(al, newAl, true, true, true);