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.Assert.assertTrue;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertTrue;
27 import static org.testng.AssertJUnit.fail;
30 import java.util.Arrays;
31 import java.util.BitSet;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
42 import jalview.datamodel.Alignment;
43 import jalview.datamodel.AlignmentAnnotation;
44 import jalview.datamodel.AlignmentI;
45 import jalview.datamodel.Annotation;
46 import jalview.datamodel.DBRefEntry;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceFeature;
49 import jalview.datamodel.SequenceI;
50 import jalview.gui.JvOptionPane;
51 import jalview.util.DBRefUtils;
53 public class StockholmFileTest
56 @BeforeClass(alwaysRun = true)
57 public void setUpJvOptionPane()
59 JvOptionPane.setInteractiveMode(false);
60 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
63 static String PfamFile = "examples/PF00111_seed.stk",
64 RfamFile = "examples/RF00031_folded.stk",
65 RnaSSTestFile = "examples/rna_ss_test.stk";
67 @Test(groups = { "Functional" })
68 public void pfamFileIO() throws Exception
70 testFileIOwithFormat(new File(PfamFile), FileFormat.Stockholm, -1, 0,
74 @Test(groups = { "Functional" })
75 public void pfamFileDataExtraction() throws Exception
77 AppletFormatAdapter af = new AppletFormatAdapter();
78 AlignmentI al = af.readFile(PfamFile, DataSourceType.FILE,
79 new IdentifyFile().identify(PfamFile, DataSourceType.FILE));
81 for (SequenceI sq : al.getSequences())
83 if (sq.getAllPDBEntries() != null)
85 numpdb += sq.getAllPDBEntries().size();
89 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
93 @Test(groups = { "Functional" })
94 public void rfamFileIO() throws Exception
96 testFileIOwithFormat(new File(RfamFile), FileFormat.Stockholm, 2, 1,
101 * JAL-3529 - verify uniprot refs for sequences are output for sequences
104 @Test(groups = { "Functional" })
105 public void dbrefOutput() throws Exception
107 // sequences retrieved in a Pfam domain alignment also have a PFAM database
109 SequenceI sq = new Sequence("FER2_SPIOL", "AASSDDDFFF");
110 sq.addDBRef(new DBRefEntry("UNIPROT", "1", "P00224"));
111 sq.addDBRef(new DBRefEntry("PFAM", "1", "P00224.1"));
112 sq.addDBRef(new DBRefEntry("PFAM", "1", "PF00111"));
113 AppletFormatAdapter af = new AppletFormatAdapter();
114 String toStockholm = af.formatSequences(FileFormat.Stockholm,
115 new Alignment(new SequenceI[]
117 System.out.println(toStockholm);
118 // bleh - java.util.Regex sucks
121 "^#=GS\\s+FER2_SPIOL(/\\d+-\\d+)?\\s+AC\\s+P00224$",
122 Pattern.MULTILINE).matcher(toStockholm).find(),
123 "Couldn't locate UNIPROT Accession in generated Stockholm file.");
124 AlignmentI fromStockholm = af.readFile(toStockholm,
125 DataSourceType.PASTE, FileFormat.Stockholm);
126 SequenceI importedSeq = fromStockholm.getSequenceAt(0);
127 assertTrue(importedSeq.getDBRefs().size() == 1,
128 "Expected just one database reference to be added to sequence.");
130 importedSeq.getDBRefs().get(0).getAccessionId()
132 "Spaces were found in accession ID.");
133 List<DBRefEntry> dbrefs = DBRefUtils.searchRefs(importedSeq.getDBRefs(),
135 assertTrue(dbrefs.size() == 1,
136 "Couldn't find Uniprot DBRef on re-imported sequence.");
140 @Test(groups = { "Functional" })
141 public void descriptionLineOutput() throws Exception
143 // quick test that sequence description is exported & reimported
145 SequenceI sq = new Sequence("FER2_SPIOL", "AASSDDDFFF");
146 String expected_descr = "This is a description !@&^%@£@";
147 sq.setDescription(expected_descr);
148 AppletFormatAdapter af = new AppletFormatAdapter();
149 String toStockholm = af.formatSequences(FileFormat.Stockholm,
150 new Alignment(new SequenceI[]
152 System.out.println(toStockholm);
154 // bleh - java.util.Regex sucks
155 assertTrue(toStockholm.contains(expected_descr),
156 "Couldn't locate expected description srting in generated Stockholm file.");
158 AlignmentI fromStockholm = af.readFile(toStockholm,
159 DataSourceType.PASTE, FileFormat.Stockholm);
160 SequenceI importedSeq = fromStockholm.getSequenceAt(0);
161 assertEquals("Description did not get reimported.", expected_descr,
162 importedSeq.getDescription());
166 * test alignment data in given file can be imported, exported and reimported
170 * - source datafile (IdentifyFile.identify() should work with it)
172 * - label for IO class used to write and read back in the data from
174 * @param ignoreFeatures
175 * @param ignoreRowVisibility
176 * @param allowNullAnnotations
179 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
180 int naliannot, int nminseqann, boolean ignoreFeatures,
181 boolean ignoreRowVisibility, boolean allowNullAnnotations)
183 System.out.println("Reading file: " + f);
184 String ff = f.getPath();
187 AppletFormatAdapter rf = new AppletFormatAdapter();
189 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
190 new IdentifyFile().identify(ff, DataSourceType.FILE));
192 assertNotNull("Couldn't read supplied alignment data.", al);
194 // make sure dataset is initialised ? not sure about this
195 for (int i = 0; i < al.getSequencesArray().length; ++i)
197 al.getSequenceAt(i).createDatasetSequence();
199 String outputfile = rf.formatSequences(ioformat, al, true);
200 System.out.println("Output file in '" + ioformat + "':\n" + outputfile
202 // test for consistency in io
203 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
204 DataSourceType.PASTE, ioformat);
205 assertNotNull("Couldn't parse reimported alignment data.", al_input);
207 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
208 DataSourceType.PASTE);
209 assertNotNull("Identify routine failed for outputformat " + ioformat,
212 "Identify routine could not recognise output generated by '"
213 + ioformat + "' writer",
214 ioformat.equals(identifyoutput));
215 testAlignmentEquivalence(al, al_input, ignoreFeatures,
216 ignoreRowVisibility, allowNullAnnotations);
217 int numaliannot = 0, numsqswithali = 0;
218 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
220 if (ala.sequenceRef == null)
231 assertEquals("Number of alignment annotations", naliannot,
236 "Number of sequence associated annotations wasn't at least "
238 numsqswithali >= nminseqann);
240 } catch (Exception e)
243 assertTrue("Couln't format the alignment for output file.", false);
248 * assert alignment equivalence
253 * 'secondary' or generated alignment from some datapreserving
255 * @param ignoreFeatures
256 * when true, differences in sequence feature annotation are ignored
258 public static void testAlignmentEquivalence(AlignmentI al,
259 AlignmentI al_input, boolean ignoreFeatures)
261 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
265 * assert alignment equivalence - uses special comparators for RNA structure
271 * 'secondary' or generated alignment from some datapreserving
273 * @param ignoreFeatures
274 * when true, differences in sequence feature annotation are ignored
276 * @param ignoreRowVisibility
277 * when true, do not fail if there are differences in the visibility
279 * @param allowNullAnnotation
280 * when true, positions in alignment annotation that are null will be
281 * considered equal to positions containing annotation where
282 * Annotation.isWhitespace() returns true.
285 public static void testAlignmentEquivalence(AlignmentI al,
286 AlignmentI al_input, boolean ignoreFeatures,
287 boolean ignoreRowVisibility, boolean allowNullAnnotation)
289 assertNotNull("Original alignment was null", al);
290 assertNotNull("Generated alignment was null", al_input);
293 "Alignment dimension mismatch: original: " + al.getHeight()
294 + "x" + al.getWidth() + ", generated: "
295 + al_input.getHeight() + "x" + al_input.getWidth(),
296 al.getHeight() == al_input.getHeight()
297 && al.getWidth() == al_input.getWidth());
299 // check Alignment annotation
300 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
301 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
302 boolean expectProteinSS = !al.isNucleotide();
304 "Alignments not both "
305 + (al.isNucleotide() ? "nucleotide" : "protein"),
306 al_input.isNucleotide() == al.isNucleotide());
308 // note - at moment we do not distinguish between alignment without any
309 // annotation rows and alignment with no annotation row vector
310 // we might want to revise this in future
311 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
312 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
313 Map<Integer, BitSet> orig_groups = new HashMap<>();
314 Map<Integer, BitSet> new_groups = new HashMap<>();
316 if (aa_new != null && aa_original != null)
318 for (int i = 0; i < aa_original.length; i++)
320 if (aa_new.length > i)
322 assertEqualSecondaryStructure(
323 "Different alignment annotation at position " + i,
324 aa_original[i], aa_new[i], allowNullAnnotation);
325 if (aa_original[i].hasIcons)
328 "Secondary structure expected to be "
329 + (expectProteinSS ? "protein" : "nucleotide"),
330 expectProteinSS == !aa_original[i].isRNA());
332 // compare graphGroup or graph properties - needed to verify JAL-1299
333 assertEquals("Graph type not identical.", aa_original[i].graph,
335 if (!ignoreRowVisibility)
337 assertEquals("Visibility not identical.",
338 aa_original[i].visible, aa_new[i].visible);
340 assertEquals("Threshold line not identical.",
341 aa_original[i].threshold, aa_new[i].threshold);
342 // graphGroup may differ, but pattern should be the same
343 Integer o_ggrp = Integer.valueOf(aa_original[i].graphGroup + 2);
344 Integer n_ggrp = Integer.valueOf(aa_new[i].graphGroup + 2);
345 BitSet orig_g = orig_groups.get(o_ggrp);
346 BitSet new_g = new_groups.get(n_ggrp);
349 orig_groups.put(o_ggrp, orig_g = new BitSet());
353 new_groups.put(n_ggrp, new_g = new BitSet());
355 assertEquals("Graph Group pattern differs at annotation " + i,
362 System.err.println("No matching annotation row for "
363 + aa_original[i].toString());
368 "Generated and imported alignment have different annotation sets",
369 aa_original_size, aa_new_size);
371 // check sequences, annotation and features
372 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
373 seq_original = al.getSequencesArray();
374 SequenceI[] seq_new = new SequenceI[al_input
375 .getSequencesArray().length];
376 seq_new = al_input.getSequencesArray();
377 List<SequenceFeature> sequenceFeatures_original;
378 List<SequenceFeature> sequenceFeatures_new;
379 AlignmentAnnotation annot_original, annot_new;
381 for (int i = 0; i < al.getSequencesArray().length; i++)
383 String name = seq_original[i].getName();
384 int start = seq_original[i].getStart();
385 int end = seq_original[i].getEnd();
387 .println("Check sequence: " + name + "/" + start + "-" + end);
389 // search equal sequence
390 for (int in = 0; in < al_input.getSequencesArray().length; in++)
392 if (name.equals(seq_new[in].getName())
393 && start == seq_new[in].getStart()
394 && end == seq_new[in].getEnd())
396 String ss_original = seq_original[i].getSequenceAsString();
397 String ss_new = seq_new[in].getSequenceAsString();
398 assertEquals("The sequences " + name + "/" + start + "-" + end
399 + " are not equal", ss_original, ss_new);
402 "Sequence Features were not equivalent"
403 + (ignoreFeatures ? " ignoring." : ""),
405 || (seq_original[i].getSequenceFeatures() == null
407 .getSequenceFeatures() == null)
408 || (seq_original[i].getSequenceFeatures() != null
410 .getSequenceFeatures() != null));
411 // compare sequence features
412 if (seq_original[i].getSequenceFeatures() != null
413 && seq_new[in].getSequenceFeatures() != null)
415 System.out.println("There are feature!!!");
416 sequenceFeatures_original = seq_original[i]
417 .getSequenceFeatures();
418 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
420 assertEquals("different number of features",
421 seq_original[i].getSequenceFeatures().size(),
422 seq_new[in].getSequenceFeatures().size());
424 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
427 assertEquals("Different features",
428 sequenceFeatures_original.get(feat),
429 sequenceFeatures_new.get(feat));
432 // compare alignment annotation
433 if (al.getSequenceAt(i).getAnnotation() != null
434 && al_input.getSequenceAt(in).getAnnotation() != null)
436 for (int j = 0; j < al.getSequenceAt(i)
437 .getAnnotation().length; j++)
439 if (al.getSequenceAt(i).getAnnotation()[j] != null && al_input
440 .getSequenceAt(in).getAnnotation()[j] != null)
442 annot_original = al.getSequenceAt(i).getAnnotation()[j];
443 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
444 assertEqualSecondaryStructure(
445 "Different annotation elements", annot_original,
446 annot_new, allowNullAnnotation);
450 else if (al.getSequenceAt(i).getAnnotation() == null
451 && al_input.getSequenceAt(in).getAnnotation() == null)
453 System.out.println("No annotations");
455 else if (al.getSequenceAt(i).getAnnotation() != null
456 && al_input.getSequenceAt(in).getAnnotation() == null)
458 fail("Annotations differed between sequences ("
459 + al.getSequenceAt(i).getName() + ") and ("
460 + al_input.getSequenceAt(i).getName() + ")");
469 * compare two annotation rows, with special support for secondary structure
470 * comparison. With RNA, only the value and the secondaryStructure symbols are
471 * compared, displayCharacter and description are ignored. Annotations where
472 * Annotation.isWhitespace() is true are always considered equal.
475 * - not actually used yet..
477 * - the original annotation
479 * - the one compared to the original annotation
480 * @param allowNullEquivalence
481 * when true, positions in alignment annotation that are null will be
482 * considered equal to non-null positions for which
483 * Annotation.isWhitespace() is true.
485 private static void assertEqualSecondaryStructure(String message,
486 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
487 boolean allowNullEqivalence)
489 // TODO: test to cover this assert behaves correctly for all allowed
490 // variations of secondary structure annotation row equivalence
491 if (annot_or.annotations.length != annot_new.annotations.length)
493 fail("Different lengths for annotation row elements: "
494 + annot_or.annotations.length + "!="
495 + annot_new.annotations.length);
497 boolean isRna = annot_or.isRNA();
499 "Expected " + (isRna ? " valid RNA " : " no RNA ")
500 + " secondary structure in the row.",
501 isRna == annot_new.isRNA());
502 for (int i = 0; i < annot_or.annotations.length; i++)
504 Annotation an_or = annot_or.annotations[i],
505 an_new = annot_new.annotations[i];
506 if (an_or != null && an_new != null)
511 if (an_or.secondaryStructure != an_new.secondaryStructure
512 || ((Float.isNaN(an_or.value) != Float
513 .isNaN(an_new.value))
514 || an_or.value != an_new.value))
516 fail("Different RNA secondary structure at column " + i
517 + " expected: [" + annot_or.annotations[i].toString()
518 + "] but got: [" + annot_new.annotations[i].toString()
524 // not RNA secondary structure, so expect all elements to match...
525 if ((an_or.isWhitespace() != an_new.isWhitespace())
526 || !an_or.displayCharacter.trim()
527 .equals(an_new.displayCharacter.trim())
528 || !("" + an_or.secondaryStructure).trim()
529 .equals(("" + an_new.secondaryStructure).trim())
530 || (an_or.description != an_new.description
531 && !((an_or.description == null
532 && an_new.description.trim()
534 || (an_new.description == null
535 && an_or.description.trim()
537 || an_or.description.trim().equals(
538 an_new.description.trim())))
539 || !((Float.isNaN(an_or.value)
540 && Float.isNaN(an_new.value))
541 || an_or.value == an_new.value))
543 fail("Annotation Element Mismatch\nElement " + i
544 + " in original: " + annot_or.annotations[i].toString()
545 + "\nElement " + i + " in new: "
546 + annot_new.annotations[i].toString());
550 else if (annot_or.annotations[i] == null
551 && annot_new.annotations[i] == null)
557 if (allowNullEqivalence)
559 if (an_or != null && an_or.isWhitespace())
564 if (an_new != null && an_new.isWhitespace())
569 // need also to test for null in one, non-SS annotation in other...
570 fail("Annotation Element Mismatch\nElement " + i + " in original: "
571 + (an_or == null ? "is null" : an_or.toString())
572 + "\nElement " + i + " in new: "
573 + (an_new == null ? "is null" : an_new.toString()));
579 * @see assertEqualSecondaryStructure - test if two secondary structure
580 * annotations are not equal
584 * @param allowNullEquivalence
586 public static void assertNotEqualSecondaryStructure(String message,
587 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
588 boolean allowNullEquivalence)
590 boolean thrown = false;
593 assertEqualSecondaryStructure("", an_orig, an_new,
594 allowNullEquivalence);
595 } catch (AssertionError af)
601 fail("Expected difference for [" + an_orig + "] and [" + an_new
606 private AlignmentAnnotation makeAnnot(Annotation ae)
608 return new AlignmentAnnotation("label", "description",
613 @Test(groups = { "Functional" })
614 public void testAnnotationEquivalence()
616 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
617 AlignmentAnnotation anotherOne = makeAnnot(
618 new Annotation("", "", ' ', 1));
619 AlignmentAnnotation sheet = makeAnnot(new Annotation("", "", 'E', 0f));
620 AlignmentAnnotation anotherSheet = makeAnnot(
621 new Annotation("", "", 'E', 0f));
622 AlignmentAnnotation sheetWithLabel = makeAnnot(
623 new Annotation("1", "", 'E', 0f));
624 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(
625 new Annotation("1", "", 'E', 0f));
626 AlignmentAnnotation rnaNoDC = makeAnnot(
627 new Annotation("", "", '<', 0f));
628 AlignmentAnnotation anotherRnaNoDC = makeAnnot(
629 new Annotation("", "", '<', 0f));
630 AlignmentAnnotation rnaWithDC = makeAnnot(
631 new Annotation("B", "", '<', 0f));
632 AlignmentAnnotation anotherRnaWithDC = makeAnnot(
633 new Annotation("B", "", '<', 0f));
635 // check self equivalence
636 for (boolean allowNull : new boolean[] { true, false })
638 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
640 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
642 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
643 anotherSheetWithLabel, allowNull);
644 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
645 anotherRnaNoDC, allowNull);
646 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
647 anotherRnaWithDC, allowNull);
648 // display character doesn't matter for RNA structure (for 2.10.2)
649 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
651 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
655 // verify others are different
656 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
657 sheetWithLabel, rnaWithDC);
658 for (int p = 0; p < aaSet.size(); p++)
660 for (int q = 0; q < aaSet.size(); q++)
664 assertNotEqualSecondaryStructure("Should be different",
665 aaSet.get(p), aaSet.get(q), false);
669 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
670 aaSet.get(q), false);
671 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
673 assertNotEqualSecondaryStructure(
674 "Should be different to empty anot", aaSet.get(p),
675 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
676 assertNotEqualSecondaryStructure(
677 "Should be different to empty annot",
678 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
680 assertNotEqualSecondaryStructure("Should be different to null",
681 aaSet.get(p), makeAnnot(null), false);
682 assertNotEqualSecondaryStructure("Should be different to null",
683 makeAnnot(null), aaSet.get(q), true);
692 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
694 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
695 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
696 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
697 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
699 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
700 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
701 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
702 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
704 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
705 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
706 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
707 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
709 @Test(groups = { "Functional" })
710 public void secondaryStructureForRNASequence() throws Exception
712 roundTripSSForRNA(aliFile, annFile);
715 @Test(groups = { "Functional" })
716 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
718 roundTripSSForRNA(aliFile, annFileCurlyWuss);
721 @Test(groups = { "Functional" })
722 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
724 roundTripSSForRNA(aliFile, annFileFullWuss);
727 @Test(groups = { "Functional" })
728 public void detectWussBrackets()
730 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
732 Assert.assertTrue(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
733 "Didn't recognise '" + ch + "' as a WUSS bracket");
735 for (char ch : new char[] { '@', '!', '*', ' ', '-', '.' })
737 Assert.assertFalse(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
738 "Shouldn't recognise '" + ch + "' as a WUSS bracket");
742 private static void roundTripSSForRNA(String aliFile, String annFile)
745 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
746 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
747 AnnotationFile aaf = new AnnotationFile();
748 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
749 al.getAlignmentAnnotation()[0].visible = true;
751 // TODO: create a better 'save as <format>' pattern
752 StockholmFile sf = new StockholmFile(al);
754 String stockholmFile = sf.print(al.getSequencesArray(), true);
756 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
757 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
758 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
759 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
760 // .getViewport().getAlignment().getSequences(), true, true);
761 testAlignmentEquivalence(al, newAl, true, true, true);
765 // this is the single sequence alignment and the SS annotations equivalent to
766 // the ones in file RnaSSTestFile
767 String aliFileRnaSS = ">Test.sequence/1-14\n" + "GUACAAAAAAAAAA";
769 String annFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
770 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
771 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|(,(|E,E|H,H|B,B|h,h|e,e|b,b|(,(|E,E|),)|e,e|),)|>,>|\t2.0\n"
773 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
774 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
776 String wrongAnnFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
777 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
778 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|(,(|H,H|E,E|B,B|h,h|e,e|b,b|(,(|E,E|),)|e,e|),)|>,>|\t2.0\n"
780 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
781 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
783 @Test(groups = { "Functional" })
784 public void stockholmFileRnaSSAlphaChars() throws Exception
786 AppletFormatAdapter af = new AppletFormatAdapter();
787 AlignmentI al = af.readFile(RnaSSTestFile, DataSourceType.FILE,
788 jalview.io.FileFormat.Stockholm);
789 Iterable<AlignmentAnnotation> aai = al.findAnnotations(null, null,
790 "Secondary Structure");
791 AlignmentAnnotation aa = aai.iterator().next();
792 Assert.assertTrue(aa.isRNA(),
793 "'" + RnaSSTestFile + "' not recognised as RNA SS");
794 Assert.assertTrue(aa.isValidStruc(),
795 "'" + RnaSSTestFile + "' not recognised as valid structure");
796 Annotation[] as = aa.annotations;
797 char[] As = new char[as.length];
798 for (int i = 0; i < as.length; i++)
800 As[i] = as[i].secondaryStructure;
802 char[] shouldBe = { '<', '(', 'E', 'H', 'B', 'h', 'e', 'b', '(', 'E',
803 ')', 'e', ')', '>' };
804 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
805 + new String(As) + " but should be " + new String(shouldBe));
807 // this should result in the same RNA SS Annotations
808 AlignmentI newAl = new AppletFormatAdapter().readFile(aliFileRnaSS,
809 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
810 AnnotationFile aaf = new AnnotationFile();
811 aaf.readAnnotationFile(newAl, annFileRnaSSAlphaChars,
812 DataSourceType.PASTE);
815 testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
816 newAl.getAlignmentAnnotation()[0]),
817 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
818 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
819 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
821 // this should NOT result in the same RNA SS Annotations
822 newAl = new AppletFormatAdapter().readFile(aliFileRnaSS,
823 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
824 aaf = new AnnotationFile();
825 aaf.readAnnotationFile(newAl, wrongAnnFileRnaSSAlphaChars,
826 DataSourceType.PASTE);
828 boolean mismatch = testRnaSSAnnotationsEquivalent(
829 al.getAlignmentAnnotation()[0],
830 newAl.getAlignmentAnnotation()[0]);
831 Assert.assertFalse(mismatch,
832 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
833 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
834 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
837 private static boolean testRnaSSAnnotationsEquivalent(
838 AlignmentAnnotation a1, AlignmentAnnotation a2)
840 return a1.rnaSecondaryStructureEquivalent(a2);
843 String annFileRnaSSWithSpaceChars = "JALVIEW_ANNOTATION\n"
844 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
845 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H| , |B,B|h,h| , |b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
847 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
848 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
850 String annFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
851 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
852 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|.,.|B,B|h,h|.,.|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
854 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
855 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
857 String wrongAnnFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
858 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
859 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|Z,Z|B,B|h,h|z,z|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
861 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
862 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
864 @Test(groups = { "Functional" })
865 public void stockholmFileRnaSSSpaceChars() throws Exception
867 AlignmentI alWithSpaces = new AppletFormatAdapter().readFile(
868 aliFileRnaSS, DataSourceType.PASTE,
869 jalview.io.FileFormat.Fasta);
870 AnnotationFile afWithSpaces = new AnnotationFile();
871 afWithSpaces.readAnnotationFile(alWithSpaces,
872 annFileRnaSSWithSpaceChars, DataSourceType.PASTE);
874 Iterable<AlignmentAnnotation> aaiWithSpaces = alWithSpaces
875 .findAnnotations(null, null, "Secondary Structure");
876 AlignmentAnnotation aaWithSpaces = aaiWithSpaces.iterator().next();
877 Assert.assertTrue(aaWithSpaces.isRNA(),
878 "'" + aaWithSpaces + "' not recognised as RNA SS");
879 Assert.assertTrue(aaWithSpaces.isValidStruc(),
880 "'" + aaWithSpaces + "' not recognised as valid structure");
881 Annotation[] annWithSpaces = aaWithSpaces.annotations;
882 char[] As = new char[annWithSpaces.length];
883 for (int i = 0; i < annWithSpaces.length; i++)
885 As[i] = annWithSpaces[i].secondaryStructure;
887 // check all spaces and dots are spaces in the internal representation
888 char[] shouldBe = { '<', ' ', 'H', ' ', 'B', 'h', ' ', 'b', '(', 'E',
889 ' ', 'e', ')', '>' };
890 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
891 + new String(As) + " but should be " + new String(shouldBe));
893 // this should result in the same RNA SS Annotations
894 AlignmentI alWithoutSpaces = new AppletFormatAdapter().readFile(
895 aliFileRnaSS, DataSourceType.PASTE,
896 jalview.io.FileFormat.Fasta);
897 AnnotationFile afWithoutSpaces = new AnnotationFile();
898 afWithoutSpaces.readAnnotationFile(alWithoutSpaces,
899 annFileRnaSSWithoutSpaceChars, DataSourceType.PASTE);
902 testRnaSSAnnotationsEquivalent(
903 alWithSpaces.getAlignmentAnnotation()[0],
904 alWithoutSpaces.getAlignmentAnnotation()[0]),
905 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
907 + alWithSpaces.getAlignmentAnnotation()[0]
908 .getRnaSecondaryStructure()
909 + "\n" + "RNA SS A 2:"
910 + alWithoutSpaces.getAlignmentAnnotation()[0]
911 .getRnaSecondaryStructure());
913 // this should NOT result in the same RNA SS Annotations
914 AlignmentI wrongAlWithoutSpaces = new AppletFormatAdapter().readFile(
915 aliFileRnaSS, DataSourceType.PASTE,
916 jalview.io.FileFormat.Fasta);
917 AnnotationFile wrongAfWithoutSpaces = new AnnotationFile();
918 wrongAfWithoutSpaces.readAnnotationFile(wrongAlWithoutSpaces,
919 wrongAnnFileRnaSSWithoutSpaceChars, DataSourceType.PASTE);
922 testRnaSSAnnotationsEquivalent(
923 alWithSpaces.getAlignmentAnnotation()[0],
924 wrongAlWithoutSpaces.getAlignmentAnnotation()[0]),
925 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
927 + alWithSpaces.getAlignmentAnnotation()[0]
928 .getRnaSecondaryStructure()
929 + "\n" + "RNA SS A 2:"
930 + wrongAlWithoutSpaces.getAlignmentAnnotation()[0]
931 .getRnaSecondaryStructure());
933 // check no spaces in the output
934 // TODO: create a better 'save as <format>' pattern
935 alWithSpaces.getAlignmentAnnotation()[0].visible = true;
936 StockholmFile sf = new StockholmFile(alWithSpaces);
938 String stockholmFile = sf.print(alWithSpaces.getSequencesArray(), true);
939 Pattern noSpacesInRnaSSAnnotation = Pattern
940 .compile("\\n#=GC SS_cons\\s+\\S{14}\\n");
941 Matcher m = noSpacesInRnaSSAnnotation.matcher(stockholmFile);
942 boolean matches = m.find();
943 Assert.assertTrue(matches,
944 "StockholmFile output does not contain expected output (may contain spaces):\n"