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.");
141 * test alignment data in given file can be imported, exported and reimported
145 * - source datafile (IdentifyFile.identify() should work with it)
147 * - label for IO class used to write and read back in the data from
149 * @param ignoreFeatures
150 * @param ignoreRowVisibility
151 * @param allowNullAnnotations
154 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
155 int naliannot, int nminseqann, boolean ignoreFeatures,
156 boolean ignoreRowVisibility, boolean allowNullAnnotations)
158 System.out.println("Reading file: " + f);
159 String ff = f.getPath();
162 AppletFormatAdapter rf = new AppletFormatAdapter();
164 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
165 new IdentifyFile().identify(ff, DataSourceType.FILE));
167 assertNotNull("Couldn't read supplied alignment data.", al);
169 // make sure dataset is initialised ? not sure about this
170 for (int i = 0; i < al.getSequencesArray().length; ++i)
172 al.getSequenceAt(i).createDatasetSequence();
174 String outputfile = rf.formatSequences(ioformat, al, true);
175 System.out.println("Output file in '" + ioformat + "':\n" + outputfile
177 // test for consistency in io
178 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
179 DataSourceType.PASTE, ioformat);
180 assertNotNull("Couldn't parse reimported alignment data.", al_input);
182 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
183 DataSourceType.PASTE);
184 assertNotNull("Identify routine failed for outputformat " + ioformat,
187 "Identify routine could not recognise output generated by '"
188 + ioformat + "' writer",
189 ioformat.equals(identifyoutput));
190 testAlignmentEquivalence(al, al_input, ignoreFeatures,
191 ignoreRowVisibility, allowNullAnnotations);
192 int numaliannot = 0, numsqswithali = 0;
193 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
195 if (ala.sequenceRef == null)
206 assertEquals("Number of alignment annotations", naliannot,
211 "Number of sequence associated annotations wasn't at least "
213 numsqswithali >= nminseqann);
215 } catch (Exception e)
218 assertTrue("Couln't format the alignment for output file.", false);
223 * assert alignment equivalence
228 * 'secondary' or generated alignment from some datapreserving
230 * @param ignoreFeatures
231 * when true, differences in sequence feature annotation are ignored
233 public static void testAlignmentEquivalence(AlignmentI al,
234 AlignmentI al_input, boolean ignoreFeatures)
236 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
240 * assert alignment equivalence - uses special comparators for RNA structure
246 * 'secondary' or generated alignment from some datapreserving
248 * @param ignoreFeatures
249 * when true, differences in sequence feature annotation are ignored
251 * @param ignoreRowVisibility
252 * when true, do not fail if there are differences in the visibility
254 * @param allowNullAnnotation
255 * when true, positions in alignment annotation that are null will be
256 * considered equal to positions containing annotation where
257 * Annotation.isWhitespace() returns true.
260 public static void testAlignmentEquivalence(AlignmentI al,
261 AlignmentI al_input, boolean ignoreFeatures,
262 boolean ignoreRowVisibility, boolean allowNullAnnotation)
264 assertNotNull("Original alignment was null", al);
265 assertNotNull("Generated alignment was null", al_input);
268 "Alignment dimension mismatch: original: " + al.getHeight()
269 + "x" + al.getWidth() + ", generated: "
270 + al_input.getHeight() + "x" + al_input.getWidth(),
271 al.getHeight() == al_input.getHeight()
272 && al.getWidth() == al_input.getWidth());
274 // check Alignment annotation
275 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
276 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
278 // note - at moment we do not distinguish between alignment without any
279 // annotation rows and alignment with no annotation row vector
280 // we might want to revise this in future
281 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
282 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
283 Map<Integer, BitSet> orig_groups = new HashMap<>();
284 Map<Integer, BitSet> new_groups = new HashMap<>();
286 if (aa_new != null && aa_original != null)
288 for (int i = 0; i < aa_original.length; i++)
290 if (aa_new.length > i)
292 assertEqualSecondaryStructure(
293 "Different alignment annotation at position " + i,
294 aa_original[i], aa_new[i], allowNullAnnotation);
295 // compare graphGroup or graph properties - needed to verify JAL-1299
296 assertEquals("Graph type not identical.", aa_original[i].graph,
298 if (!ignoreRowVisibility)
300 assertEquals("Visibility not identical.",
301 aa_original[i].visible, aa_new[i].visible);
303 assertEquals("Threshold line not identical.",
304 aa_original[i].threshold, aa_new[i].threshold);
305 // graphGroup may differ, but pattern should be the same
306 Integer o_ggrp = Integer.valueOf(aa_original[i].graphGroup + 2);
307 Integer n_ggrp = Integer.valueOf(aa_new[i].graphGroup + 2);
308 BitSet orig_g = orig_groups.get(o_ggrp);
309 BitSet new_g = new_groups.get(n_ggrp);
312 orig_groups.put(o_ggrp, orig_g = new BitSet());
316 new_groups.put(n_ggrp, new_g = new BitSet());
318 assertEquals("Graph Group pattern differs at annotation " + i,
325 System.err.println("No matching annotation row for "
326 + aa_original[i].toString());
331 "Generated and imported alignment have different annotation sets",
332 aa_original_size, aa_new_size);
334 // check sequences, annotation and features
335 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
336 seq_original = al.getSequencesArray();
337 SequenceI[] seq_new = new SequenceI[al_input
338 .getSequencesArray().length];
339 seq_new = al_input.getSequencesArray();
340 List<SequenceFeature> sequenceFeatures_original;
341 List<SequenceFeature> sequenceFeatures_new;
342 AlignmentAnnotation annot_original, annot_new;
344 for (int i = 0; i < al.getSequencesArray().length; i++)
346 String name = seq_original[i].getName();
347 int start = seq_original[i].getStart();
348 int end = seq_original[i].getEnd();
350 .println("Check sequence: " + name + "/" + start + "-" + end);
352 // search equal sequence
353 for (int in = 0; in < al_input.getSequencesArray().length; in++)
355 if (name.equals(seq_new[in].getName())
356 && start == seq_new[in].getStart()
357 && end == seq_new[in].getEnd())
359 String ss_original = seq_original[i].getSequenceAsString();
360 String ss_new = seq_new[in].getSequenceAsString();
361 assertEquals("The sequences " + name + "/" + start + "-" + end
362 + " are not equal", ss_original, ss_new);
365 "Sequence Features were not equivalent"
366 + (ignoreFeatures ? " ignoring." : ""),
368 || (seq_original[i].getSequenceFeatures() == null
370 .getSequenceFeatures() == null)
371 || (seq_original[i].getSequenceFeatures() != null
373 .getSequenceFeatures() != null));
374 // compare sequence features
375 if (seq_original[i].getSequenceFeatures() != null
376 && seq_new[in].getSequenceFeatures() != null)
378 System.out.println("There are feature!!!");
379 sequenceFeatures_original = seq_original[i]
380 .getSequenceFeatures();
381 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
383 assertEquals("different number of features",
384 seq_original[i].getSequenceFeatures().size(),
385 seq_new[in].getSequenceFeatures().size());
387 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
390 assertEquals("Different features",
391 sequenceFeatures_original.get(feat),
392 sequenceFeatures_new.get(feat));
395 // compare alignment annotation
396 if (al.getSequenceAt(i).getAnnotation() != null
397 && al_input.getSequenceAt(in).getAnnotation() != null)
399 for (int j = 0; j < al.getSequenceAt(i)
400 .getAnnotation().length; j++)
402 if (al.getSequenceAt(i).getAnnotation()[j] != null && al_input
403 .getSequenceAt(in).getAnnotation()[j] != null)
405 annot_original = al.getSequenceAt(i).getAnnotation()[j];
406 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
407 assertEqualSecondaryStructure(
408 "Different annotation elements", annot_original,
409 annot_new, allowNullAnnotation);
413 else if (al.getSequenceAt(i).getAnnotation() == null
414 && al_input.getSequenceAt(in).getAnnotation() == null)
416 System.out.println("No annotations");
418 else if (al.getSequenceAt(i).getAnnotation() != null
419 && al_input.getSequenceAt(in).getAnnotation() == null)
421 fail("Annotations differed between sequences ("
422 + al.getSequenceAt(i).getName() + ") and ("
423 + al_input.getSequenceAt(i).getName() + ")");
432 * compare two annotation rows, with special support for secondary structure
433 * comparison. With RNA, only the value and the secondaryStructure symbols are
434 * compared, displayCharacter and description are ignored. Annotations where
435 * Annotation.isWhitespace() is true are always considered equal.
438 * - not actually used yet..
440 * - the original annotation
442 * - the one compared to the original annotation
443 * @param allowNullEquivalence
444 * when true, positions in alignment annotation that are null will be
445 * considered equal to non-null positions for which
446 * Annotation.isWhitespace() is true.
448 private static void assertEqualSecondaryStructure(String message,
449 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
450 boolean allowNullEqivalence)
452 // TODO: test to cover this assert behaves correctly for all allowed
453 // variations of secondary structure annotation row equivalence
454 if (annot_or.annotations.length != annot_new.annotations.length)
456 fail("Different lengths for annotation row elements: "
457 + annot_or.annotations.length + "!="
458 + annot_new.annotations.length);
460 boolean isRna = annot_or.isRNA();
462 "Expected " + (isRna ? " valid RNA " : " no RNA ")
463 + " secondary structure in the row.",
464 isRna == annot_new.isRNA());
465 for (int i = 0; i < annot_or.annotations.length; i++)
467 Annotation an_or = annot_or.annotations[i],
468 an_new = annot_new.annotations[i];
469 if (an_or != null && an_new != null)
474 if (an_or.secondaryStructure != an_new.secondaryStructure
475 || ((Float.isNaN(an_or.value) != Float
476 .isNaN(an_new.value))
477 || an_or.value != an_new.value))
479 fail("Different RNA secondary structure at column " + i
480 + " expected: [" + annot_or.annotations[i].toString()
481 + "] but got: [" + annot_new.annotations[i].toString()
487 // not RNA secondary structure, so expect all elements to match...
488 if ((an_or.isWhitespace() != an_new.isWhitespace())
489 || !an_or.displayCharacter.trim()
490 .equals(an_new.displayCharacter.trim())
491 || !("" + an_or.secondaryStructure).trim()
492 .equals(("" + an_new.secondaryStructure).trim())
493 || (an_or.description != an_new.description
494 && !((an_or.description == null
495 && an_new.description.trim()
497 || (an_new.description == null
498 && an_or.description.trim()
500 || an_or.description.trim().equals(
501 an_new.description.trim())))
502 || !((Float.isNaN(an_or.value)
503 && Float.isNaN(an_new.value))
504 || an_or.value == an_new.value))
506 fail("Annotation Element Mismatch\nElement " + i
507 + " in original: " + annot_or.annotations[i].toString()
508 + "\nElement " + i + " in new: "
509 + annot_new.annotations[i].toString());
513 else if (annot_or.annotations[i] == null
514 && annot_new.annotations[i] == null)
520 if (allowNullEqivalence)
522 if (an_or != null && an_or.isWhitespace())
527 if (an_new != null && an_new.isWhitespace())
532 // need also to test for null in one, non-SS annotation in other...
533 fail("Annotation Element Mismatch\nElement " + i + " in original: "
534 + (an_or == null ? "is null" : an_or.toString())
535 + "\nElement " + i + " in new: "
536 + (an_new == null ? "is null" : an_new.toString()));
542 * @see assertEqualSecondaryStructure - test if two secondary structure
543 * annotations are not equal
547 * @param allowNullEquivalence
549 public static void assertNotEqualSecondaryStructure(String message,
550 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
551 boolean allowNullEquivalence)
553 boolean thrown = false;
556 assertEqualSecondaryStructure("", an_orig, an_new,
557 allowNullEquivalence);
558 } catch (AssertionError af)
564 fail("Expected difference for [" + an_orig + "] and [" + an_new
569 private AlignmentAnnotation makeAnnot(Annotation ae)
571 return new AlignmentAnnotation("label", "description",
576 @Test(groups = { "Functional" })
577 public void testAnnotationEquivalence()
579 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
580 AlignmentAnnotation anotherOne = makeAnnot(
581 new Annotation("", "", ' ', 1));
582 AlignmentAnnotation sheet = makeAnnot(new Annotation("", "", 'E', 0f));
583 AlignmentAnnotation anotherSheet = makeAnnot(
584 new Annotation("", "", 'E', 0f));
585 AlignmentAnnotation sheetWithLabel = makeAnnot(
586 new Annotation("1", "", 'E', 0f));
587 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(
588 new Annotation("1", "", 'E', 0f));
589 AlignmentAnnotation rnaNoDC = makeAnnot(
590 new Annotation("", "", '<', 0f));
591 AlignmentAnnotation anotherRnaNoDC = makeAnnot(
592 new Annotation("", "", '<', 0f));
593 AlignmentAnnotation rnaWithDC = makeAnnot(
594 new Annotation("B", "", '<', 0f));
595 AlignmentAnnotation anotherRnaWithDC = makeAnnot(
596 new Annotation("B", "", '<', 0f));
598 // check self equivalence
599 for (boolean allowNull : new boolean[] { true, false })
601 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
603 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
605 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
606 anotherSheetWithLabel, allowNull);
607 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
608 anotherRnaNoDC, allowNull);
609 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
610 anotherRnaWithDC, allowNull);
611 // display character doesn't matter for RNA structure (for 2.10.2)
612 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
614 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
618 // verify others are different
619 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
620 sheetWithLabel, rnaWithDC);
621 for (int p = 0; p < aaSet.size(); p++)
623 for (int q = 0; q < aaSet.size(); q++)
627 assertNotEqualSecondaryStructure("Should be different",
628 aaSet.get(p), aaSet.get(q), false);
632 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
633 aaSet.get(q), false);
634 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
636 assertNotEqualSecondaryStructure(
637 "Should be different to empty anot", aaSet.get(p),
638 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
639 assertNotEqualSecondaryStructure(
640 "Should be different to empty annot",
641 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
643 assertNotEqualSecondaryStructure("Should be different to null",
644 aaSet.get(p), makeAnnot(null), false);
645 assertNotEqualSecondaryStructure("Should be different to null",
646 makeAnnot(null), aaSet.get(q), true);
655 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
657 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
658 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
659 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
660 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
662 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
663 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
664 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
665 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
667 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
668 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
669 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
670 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
672 @Test(groups = { "Functional" })
673 public void secondaryStructureForRNASequence() throws Exception
675 roundTripSSForRNA(aliFile, annFile);
678 @Test(groups = { "Functional" })
679 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
681 roundTripSSForRNA(aliFile, annFileCurlyWuss);
684 @Test(groups = { "Functional" })
685 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
687 roundTripSSForRNA(aliFile, annFileFullWuss);
690 @Test(groups = { "Functional" })
691 public void detectWussBrackets()
693 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
695 Assert.assertTrue(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
696 "Didn't recognise '" + ch + "' as a WUSS bracket");
698 for (char ch : new char[] { '@', '!', '*', ' ', '-', '.' })
700 Assert.assertFalse(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
701 "Shouldn't recognise '" + ch + "' as a WUSS bracket");
705 private static void roundTripSSForRNA(String aliFile, String annFile)
708 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
709 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
710 AnnotationFile aaf = new AnnotationFile();
711 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
712 al.getAlignmentAnnotation()[0].visible = true;
714 // TODO: create a better 'save as <format>' pattern
715 StockholmFile sf = new StockholmFile(al);
717 String stockholmFile = sf.print(al.getSequencesArray(), true);
719 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
720 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
721 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
722 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
723 // .getViewport().getAlignment().getSequences(), true, true);
724 testAlignmentEquivalence(al, newAl, true, true, true);
728 // this is the single sequence alignment and the SS annotations equivalent to
729 // the ones in file RnaSSTestFile
730 String aliFileRnaSS = ">Test.sequence/1-14\n" + "GUACAAAAAAAAAA";
732 String annFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
733 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
734 + "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"
736 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
737 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
739 String wrongAnnFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
740 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
741 + "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"
743 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
744 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
746 @Test(groups = { "Functional" })
747 public void stockholmFileRnaSSAlphaChars() throws Exception
749 AppletFormatAdapter af = new AppletFormatAdapter();
750 AlignmentI al = af.readFile(RnaSSTestFile, DataSourceType.FILE,
751 jalview.io.FileFormat.Stockholm);
752 Iterable<AlignmentAnnotation> aai = al.findAnnotations(null, null,
753 "Secondary Structure");
754 AlignmentAnnotation aa = aai.iterator().next();
755 Assert.assertTrue(aa.isRNA(),
756 "'" + RnaSSTestFile + "' not recognised as RNA SS");
757 Assert.assertTrue(aa.isValidStruc(),
758 "'" + RnaSSTestFile + "' not recognised as valid structure");
759 Annotation[] as = aa.annotations;
760 char[] As = new char[as.length];
761 for (int i = 0; i < as.length; i++)
763 As[i] = as[i].secondaryStructure;
765 char[] shouldBe = { '<', '(', 'E', 'H', 'B', 'h', 'e', 'b', '(', 'E',
766 ')', 'e', ')', '>' };
767 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
768 + new String(As) + " but should be " + new String(shouldBe));
770 // this should result in the same RNA SS Annotations
771 AlignmentI newAl = new AppletFormatAdapter().readFile(aliFileRnaSS,
772 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
773 AnnotationFile aaf = new AnnotationFile();
774 aaf.readAnnotationFile(newAl, annFileRnaSSAlphaChars,
775 DataSourceType.PASTE);
778 testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
779 newAl.getAlignmentAnnotation()[0]),
780 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
781 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
782 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
784 // this should NOT result in the same RNA SS Annotations
785 newAl = new AppletFormatAdapter().readFile(aliFileRnaSS,
786 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
787 aaf = new AnnotationFile();
788 aaf.readAnnotationFile(newAl, wrongAnnFileRnaSSAlphaChars,
789 DataSourceType.PASTE);
791 boolean mismatch = testRnaSSAnnotationsEquivalent(
792 al.getAlignmentAnnotation()[0],
793 newAl.getAlignmentAnnotation()[0]);
794 Assert.assertFalse(mismatch,
795 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
796 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
797 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
800 private static boolean testRnaSSAnnotationsEquivalent(
801 AlignmentAnnotation a1, AlignmentAnnotation a2)
803 return a1.rnaSecondaryStructureEquivalent(a2);
806 String annFileRnaSSWithSpaceChars = "JALVIEW_ANNOTATION\n"
807 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
808 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H| , |B,B|h,h| , |b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
810 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
811 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
813 String annFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
814 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
815 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|.,.|B,B|h,h|.,.|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
817 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
818 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
820 String wrongAnnFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
821 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
822 + "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"
824 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
825 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
827 @Test(groups = { "Functional" })
828 public void stockholmFileRnaSSSpaceChars() throws Exception
830 AlignmentI alWithSpaces = new AppletFormatAdapter().readFile(
831 aliFileRnaSS, DataSourceType.PASTE,
832 jalview.io.FileFormat.Fasta);
833 AnnotationFile afWithSpaces = new AnnotationFile();
834 afWithSpaces.readAnnotationFile(alWithSpaces,
835 annFileRnaSSWithSpaceChars, DataSourceType.PASTE);
837 Iterable<AlignmentAnnotation> aaiWithSpaces = alWithSpaces
838 .findAnnotations(null, null, "Secondary Structure");
839 AlignmentAnnotation aaWithSpaces = aaiWithSpaces.iterator().next();
840 Assert.assertTrue(aaWithSpaces.isRNA(),
841 "'" + aaWithSpaces + "' not recognised as RNA SS");
842 Assert.assertTrue(aaWithSpaces.isValidStruc(),
843 "'" + aaWithSpaces + "' not recognised as valid structure");
844 Annotation[] annWithSpaces = aaWithSpaces.annotations;
845 char[] As = new char[annWithSpaces.length];
846 for (int i = 0; i < annWithSpaces.length; i++)
848 As[i] = annWithSpaces[i].secondaryStructure;
850 // check all spaces and dots are spaces in the internal representation
851 char[] shouldBe = { '<', ' ', 'H', ' ', 'B', 'h', ' ', 'b', '(', 'E',
852 ' ', 'e', ')', '>' };
853 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
854 + new String(As) + " but should be " + new String(shouldBe));
856 // this should result in the same RNA SS Annotations
857 AlignmentI alWithoutSpaces = new AppletFormatAdapter().readFile(
858 aliFileRnaSS, DataSourceType.PASTE,
859 jalview.io.FileFormat.Fasta);
860 AnnotationFile afWithoutSpaces = new AnnotationFile();
861 afWithoutSpaces.readAnnotationFile(alWithoutSpaces,
862 annFileRnaSSWithoutSpaceChars, DataSourceType.PASTE);
865 testRnaSSAnnotationsEquivalent(
866 alWithSpaces.getAlignmentAnnotation()[0],
867 alWithoutSpaces.getAlignmentAnnotation()[0]),
868 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
870 + alWithSpaces.getAlignmentAnnotation()[0]
871 .getRnaSecondaryStructure()
872 + "\n" + "RNA SS A 2:"
873 + alWithoutSpaces.getAlignmentAnnotation()[0]
874 .getRnaSecondaryStructure());
876 // this should NOT result in the same RNA SS Annotations
877 AlignmentI wrongAlWithoutSpaces = new AppletFormatAdapter().readFile(
878 aliFileRnaSS, DataSourceType.PASTE,
879 jalview.io.FileFormat.Fasta);
880 AnnotationFile wrongAfWithoutSpaces = new AnnotationFile();
881 wrongAfWithoutSpaces.readAnnotationFile(wrongAlWithoutSpaces,
882 wrongAnnFileRnaSSWithoutSpaceChars, DataSourceType.PASTE);
885 testRnaSSAnnotationsEquivalent(
886 alWithSpaces.getAlignmentAnnotation()[0],
887 wrongAlWithoutSpaces.getAlignmentAnnotation()[0]),
888 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
890 + alWithSpaces.getAlignmentAnnotation()[0]
891 .getRnaSecondaryStructure()
892 + "\n" + "RNA SS A 2:"
893 + wrongAlWithoutSpaces.getAlignmentAnnotation()[0]
894 .getRnaSecondaryStructure());
896 // check no spaces in the output
897 // TODO: create a better 'save as <format>' pattern
898 alWithSpaces.getAlignmentAnnotation()[0].visible = true;
899 StockholmFile sf = new StockholmFile(alWithSpaces);
901 String stockholmFile = sf.print(alWithSpaces.getSequencesArray(), true);
902 Pattern noSpacesInRnaSSAnnotation = Pattern
903 .compile("\\n#=GC SS_cons\\s+\\S{14}\\n");
904 Matcher m = noSpacesInRnaSSAnnotation.matcher(stockholmFile);
905 boolean matches = m.find();
906 Assert.assertTrue(matches,
907 "StockholmFile output does not contain expected output (may contain spaces):\n"