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;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.AlignmentAnnotation;
31 import jalview.datamodel.AlignmentI;
32 import jalview.datamodel.Annotation;
33 import jalview.datamodel.DBRefEntry;
34 import jalview.datamodel.Sequence;
35 import jalview.datamodel.SequenceFeature;
36 import jalview.datamodel.SequenceI;
37 import jalview.gui.JvOptionPane;
38 import jalview.util.DBRefUtils;
41 import java.util.Arrays;
42 import java.util.BitSet;
43 import java.util.HashMap;
44 import java.util.List;
46 import java.util.regex.Matcher;
47 import java.util.regex.Pattern;
49 import org.testng.Assert;
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.Test;
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)
124 "Couldn't locate UNIPROT Accession in generated Stockholm file.");
125 AlignmentI fromStockholm = af.readFile(toStockholm,
126 DataSourceType.PASTE, FileFormat.Stockholm);
127 SequenceI importedSeq = fromStockholm.getSequenceAt(0);
128 assertTrue(importedSeq.getDBRefs().length == 1,
129 "Expected just one database reference to be added to sequence.");
131 importedSeq.getDBRefs()[0].getAccessionId().indexOf(" ") == -1,
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()
146 * should work with it)
148 * - label for IO class used to write and read
149 * back in the data from f
150 * @param ignoreFeatures
151 * @param ignoreRowVisibility
152 * @param allowNullAnnotations
155 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
156 int naliannot, int nminseqann, boolean ignoreFeatures,
157 boolean ignoreRowVisibility, boolean allowNullAnnotations)
159 System.out.println("Reading file: " + f);
160 String ff = f.getPath();
163 AppletFormatAdapter rf = new AppletFormatAdapter();
165 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
166 new IdentifyFile().identify(ff, DataSourceType.FILE));
168 assertNotNull("Couldn't read supplied alignment data.", al);
170 // make sure dataset is initialised ? not sure about this
171 for (int i = 0; i < al.getSequencesArray().length; ++i)
173 al.getSequenceAt(i).createDatasetSequence();
175 String outputfile = rf.formatSequences(ioformat, al, true);
176 System.out.println("Output file in '" + ioformat + "':\n"
177 + outputfile + "\n<<EOF\n");
178 // test for consistency in io
179 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
180 DataSourceType.PASTE, ioformat);
181 assertNotNull("Couldn't parse reimported alignment data.", al_input);
183 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
184 DataSourceType.PASTE);
185 assertNotNull("Identify routine failed for outputformat " + ioformat,
188 "Identify routine could not recognise output generated by '"
189 + ioformat + "' writer",
190 ioformat.equals(identifyoutput));
191 testAlignmentEquivalence(al, al_input, ignoreFeatures,
192 ignoreRowVisibility, allowNullAnnotations);
193 int numaliannot = 0, numsqswithali = 0;
194 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
196 if (ala.sequenceRef == null)
207 assertEquals("Number of alignment annotations", naliannot,
212 "Number of sequence associated annotations wasn't at least "
213 + nminseqann, 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);
267 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
268 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
269 + "x" + al_input.getWidth(),
270 al.getHeight() == al_input.getHeight()
271 && al.getWidth() == al_input.getWidth());
273 // check Alignment annotation
274 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
275 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
277 // note - at moment we do not distinguish between alignment without any
278 // annotation rows and alignment with no annotation row vector
279 // we might want to revise this in future
280 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
281 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
282 Map<Integer, BitSet> orig_groups = new HashMap<>();
283 Map<Integer, BitSet> new_groups = new HashMap<>();
285 if (aa_new != null && aa_original != null)
287 for (int i = 0; i < aa_original.length; i++)
289 if (aa_new.length > i)
291 assertEqualSecondaryStructure(
292 "Different alignment annotation at position " + i,
293 aa_original[i], aa_new[i], allowNullAnnotation);
294 // compare graphGroup or graph properties - needed to verify JAL-1299
295 assertEquals("Graph type not identical.", aa_original[i].graph,
297 if (!ignoreRowVisibility)
299 assertEquals("Visibility not identical.",
300 aa_original[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.getSequencesArray().length];
338 seq_new = al_input.getSequencesArray();
339 List<SequenceFeature> sequenceFeatures_original;
340 List<SequenceFeature> sequenceFeatures_new;
341 AlignmentAnnotation annot_original, annot_new;
343 for (int i = 0; i < al.getSequencesArray().length; i++)
345 String name = seq_original[i].getName();
346 int start = seq_original[i].getStart();
347 int end = seq_original[i].getEnd();
348 System.out.println("Check sequence: " + name + "/" + start + "-"
351 // search equal sequence
352 for (int in = 0; in < al_input.getSequencesArray().length; in++)
354 if (name.equals(seq_new[in].getName())
355 && start == seq_new[in].getStart()
356 && end == seq_new[in].getEnd())
358 String ss_original = seq_original[i].getSequenceAsString();
359 String ss_new = seq_new[in].getSequenceAsString();
360 assertEquals("The sequences " + name + "/" + start + "-" + end
361 + " are not equal", ss_original, ss_new);
364 "Sequence Features were not equivalent"
365 + (ignoreFeatures ? " ignoring." : ""),
367 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
368 .getSequenceFeatures() == null)
369 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
370 .getSequenceFeatures() != null));
371 // compare sequence features
373 && seq_original[i].getSequenceFeatures() != null
374 && seq_new[in].getSequenceFeatures() != null)
376 System.out.println("Checking feature equivalence.");
377 sequenceFeatures_original = seq_original[i]
378 .getSequenceFeatures();
379 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
381 assertEquals("different number of features", seq_original[i]
382 .getSequenceFeatures().size(), seq_new[in]
383 .getSequenceFeatures().size());
385 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
388 assertEquals("Different features",
389 sequenceFeatures_original.get(feat),
390 sequenceFeatures_new.get(feat));
393 // compare alignment annotation
394 if (al.getSequenceAt(i).getAnnotation() != null
395 && al_input.getSequenceAt(in).getAnnotation() != null)
397 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
399 if (al.getSequenceAt(i).getAnnotation()[j] != null
400 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
402 annot_original = al.getSequenceAt(i).getAnnotation()[j];
403 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
404 assertEqualSecondaryStructure(
405 "Different annotation elements", annot_original,
406 annot_new, allowNullAnnotation);
410 else if (al.getSequenceAt(i).getAnnotation() == null
411 && al_input.getSequenceAt(in).getAnnotation() == null)
413 System.out.println("No annotations");
415 else if (al.getSequenceAt(i).getAnnotation() != null
416 && al_input.getSequenceAt(in).getAnnotation() == null)
418 fail("Annotations differed between sequences ("
419 + al.getSequenceAt(i).getName() + ") and ("
420 + al_input.getSequenceAt(i).getName() + ")");
429 * compare two annotation rows, with special support for secondary structure
430 * comparison. With RNA, only the value and the secondaryStructure symbols are
431 * compared, displayCharacter and description are ignored. Annotations where
432 * Annotation.isWhitespace() is true are always considered equal.
435 * - not actually used yet..
437 * - the original annotation
439 * - the one compared to the original annotation
440 * @param allowNullEquivalence
441 * when true, positions in alignment annotation that are null will be
442 * considered equal to non-null positions for which
443 * Annotation.isWhitespace() is true.
445 private static void assertEqualSecondaryStructure(String message,
446 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
447 boolean allowNullEqivalence)
449 // TODO: test to cover this assert behaves correctly for all allowed
450 // variations of secondary structure annotation row equivalence
451 if (annot_or.annotations.length != annot_new.annotations.length)
453 fail("Different lengths for annotation row elements: "
454 + annot_or.annotations.length + "!="
455 + annot_new.annotations.length);
457 boolean isRna = annot_or.isRNA();
458 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
459 + " secondary structure in the row.",
460 isRna == annot_new.isRNA());
461 for (int i = 0; i < annot_or.annotations.length; i++)
463 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
464 if (an_or != null && an_new != null)
469 if (an_or.secondaryStructure != an_new.secondaryStructure
470 || ((Float.isNaN(an_or.value) != Float
471 .isNaN(an_new.value)) || an_or.value != an_new.value))
473 fail("Different RNA secondary structure at column " + i
474 + " expected: [" + annot_or.annotations[i].toString()
475 + "] but got: [" + annot_new.annotations[i].toString()
481 // not RNA secondary structure, so expect all elements to match...
482 if ((an_or.isWhitespace() != an_new.isWhitespace())
483 || !an_or.displayCharacter.trim().equals(
484 an_new.displayCharacter.trim())
485 || !("" + an_or.secondaryStructure).trim().equals(
486 ("" + an_new.secondaryStructure).trim())
487 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
488 .trim().length() == 0)
489 || (an_new.description == null && an_or.description
490 .trim().length() == 0) || an_or.description
491 .trim().equals(an_new.description.trim())))
492 || !((Float.isNaN(an_or.value) && Float
493 .isNaN(an_new.value)) || an_or.value == an_new.value))
495 fail("Annotation Element Mismatch\nElement " + i
496 + " in original: " + annot_or.annotations[i].toString()
497 + "\nElement " + i + " in new: "
498 + annot_new.annotations[i].toString());
502 else if (annot_or.annotations[i] == null
503 && annot_new.annotations[i] == null)
509 if (allowNullEqivalence)
511 if (an_or != null && an_or.isWhitespace())
516 if (an_new != null && an_new.isWhitespace())
521 // need also to test for null in one, non-SS annotation in other...
522 fail("Annotation Element Mismatch\nElement " + i + " in original: "
523 + (an_or == null ? "is null" : an_or.toString())
524 + "\nElement " + i + " in new: "
525 + (an_new == null ? "is null" : an_new.toString()));
531 * @see assertEqualSecondaryStructure - test if two secondary structure
532 * annotations are not equal
536 * @param allowNullEquivalence
538 public static void assertNotEqualSecondaryStructure(String message,
539 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
540 boolean allowNullEquivalence)
542 boolean thrown = false;
545 assertEqualSecondaryStructure("", an_orig, an_new,
546 allowNullEquivalence);
547 } catch (AssertionError af)
553 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
556 private AlignmentAnnotation makeAnnot(Annotation ae)
558 return new AlignmentAnnotation("label", "description", new Annotation[]
562 @Test(groups={"Functional"})
563 public void testAnnotationEquivalence()
565 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
566 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
568 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
569 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
570 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
572 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
574 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
575 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
576 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
578 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
581 // check self equivalence
582 for (boolean allowNull : new boolean[] { true, false })
584 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
586 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
588 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
589 anotherSheetWithLabel, allowNull);
590 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
591 anotherRnaNoDC, allowNull);
592 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
593 anotherRnaWithDC, allowNull);
594 // display character doesn't matter for RNA structure (for 2.10.2)
595 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
597 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
601 // verify others are different
602 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
603 sheetWithLabel, rnaWithDC);
604 for (int p = 0; p < aaSet.size(); p++)
606 for (int q = 0; q < aaSet.size(); q++)
610 assertNotEqualSecondaryStructure("Should be different",
611 aaSet.get(p), aaSet.get(q), false);
615 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
616 aaSet.get(q), false);
617 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
619 assertNotEqualSecondaryStructure(
620 "Should be different to empty anot", aaSet.get(p),
621 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
622 assertNotEqualSecondaryStructure(
623 "Should be different to empty annot",
624 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
626 assertNotEqualSecondaryStructure("Should be different to null",
627 aaSet.get(p), makeAnnot(null), false);
628 assertNotEqualSecondaryStructure("Should be different to null",
629 makeAnnot(null), aaSet.get(q), true);
638 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
639 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
640 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
641 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
642 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
644 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
645 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
646 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
647 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
648 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
649 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
650 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
651 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
653 @Test(groups = { "Functional" })
654 public void secondaryStructureForRNASequence() throws Exception
656 roundTripSSForRNA(aliFile, annFile);
659 @Test(groups = { "Functional" })
660 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
662 roundTripSSForRNA(aliFile, annFileCurlyWuss);
665 @Test(groups = { "Functional" })
666 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
668 roundTripSSForRNA(aliFile, annFileFullWuss);
671 @Test(groups = { "Functional" })
672 public void detectWussBrackets()
674 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
676 Assert.assertTrue(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
677 "Didn't recognise '" + ch + "' as a WUSS bracket");
679 for (char ch : new char[] { '@', '!', '*', ' ', '-', '.' })
681 Assert.assertFalse(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
682 "Shouldn't recognise '" + ch + "' as a WUSS bracket");
685 private static void roundTripSSForRNA(String aliFile, String annFile)
688 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
689 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
690 AnnotationFile aaf = new AnnotationFile();
691 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
692 al.getAlignmentAnnotation()[0].visible = true;
694 // TODO: create a better 'save as <format>' pattern
695 StockholmFile sf = new StockholmFile(al);
697 String stockholmFile = sf.print(al.getSequencesArray(), true);
699 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
700 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
701 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
702 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
703 // .getViewport().getAlignment().getSequences(), true, true);
704 testAlignmentEquivalence(al, newAl, true, true, true);
708 // this is the single sequence alignment and the SS annotations equivalent to
709 // the ones in file RnaSSTestFile
710 String aliFileRnaSS = ">Test.sequence/1-14\n"
712 String annFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
713 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
714 + "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"
716 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
717 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
718 String wrongAnnFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
719 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
720 + "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"
722 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
723 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
724 @Test(groups = { "Functional" })
725 public void stockholmFileRnaSSAlphaChars() throws Exception
727 AppletFormatAdapter af = new AppletFormatAdapter();
728 AlignmentI al = af.readFile(RnaSSTestFile, DataSourceType.FILE,
729 jalview.io.FileFormat.Stockholm);
730 Iterable<AlignmentAnnotation> aai = al.findAnnotations(null, null,
731 "Secondary Structure");
732 AlignmentAnnotation aa = aai.iterator().next();
733 Assert.assertTrue(aa.isRNA(),
734 "'" + RnaSSTestFile + "' not recognised as RNA SS");
735 Assert.assertTrue(aa.isValidStruc(),
736 "'" + RnaSSTestFile + "' not recognised as valid structure");
737 Annotation[] as = aa.annotations;
738 char[] As = new char[as.length];
739 for (int i = 0; i < as.length; i++)
741 As[i] = as[i].secondaryStructure;
743 char[] shouldBe = { '<', '(', 'E', 'H', 'B', 'h', 'e', 'b', '(', 'E',
744 ')', 'e', ')', '>' };
746 Arrays.equals(As, shouldBe),
747 "Annotation is " + new String(As) + " but should be "
748 + new String(shouldBe));
750 // this should result in the same RNA SS Annotations
751 AlignmentI newAl = new AppletFormatAdapter().readFile(
753 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
754 AnnotationFile aaf = new AnnotationFile();
755 aaf.readAnnotationFile(newAl, annFileRnaSSAlphaChars,
756 DataSourceType.PASTE);
759 testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
760 newAl.getAlignmentAnnotation()[0]),
761 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
762 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
763 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
765 // this should NOT result in the same RNA SS Annotations
766 newAl = new AppletFormatAdapter().readFile(
767 aliFileRnaSS, DataSourceType.PASTE,
768 jalview.io.FileFormat.Fasta);
769 aaf = new AnnotationFile();
770 aaf.readAnnotationFile(newAl, wrongAnnFileRnaSSAlphaChars,
771 DataSourceType.PASTE);
773 boolean mismatch = testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
774 newAl.getAlignmentAnnotation()[0]);
775 Assert.assertFalse(mismatch,
776 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
777 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
778 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
781 private static boolean testRnaSSAnnotationsEquivalent(
782 AlignmentAnnotation a1,
783 AlignmentAnnotation a2)
785 return a1.rnaSecondaryStructureEquivalent(a2);
788 String annFileRnaSSWithSpaceChars = "JALVIEW_ANNOTATION\n"
789 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
790 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H| , |B,B|h,h| , |b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
792 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
793 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
794 String annFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
795 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
796 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|.,.|B,B|h,h|.,.|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
798 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
799 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
801 String wrongAnnFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
802 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
803 + "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"
805 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
806 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
808 @Test(groups = { "Functional" })
809 public void stockholmFileRnaSSSpaceChars() throws Exception
811 AlignmentI alWithSpaces = new AppletFormatAdapter().readFile(
812 aliFileRnaSS, DataSourceType.PASTE,
813 jalview.io.FileFormat.Fasta);
814 AnnotationFile afWithSpaces = new AnnotationFile();
815 afWithSpaces.readAnnotationFile(alWithSpaces,
816 annFileRnaSSWithSpaceChars, DataSourceType.PASTE);
818 Iterable<AlignmentAnnotation> aaiWithSpaces = alWithSpaces
819 .findAnnotations(null, null, "Secondary Structure");
820 AlignmentAnnotation aaWithSpaces = aaiWithSpaces.iterator().next();
821 Assert.assertTrue(aaWithSpaces.isRNA(),
822 "'" + aaWithSpaces + "' not recognised as RNA SS");
823 Assert.assertTrue(aaWithSpaces.isValidStruc(),
824 "'" + aaWithSpaces + "' not recognised as valid structure");
825 Annotation[] annWithSpaces = aaWithSpaces.annotations;
826 char[] As = new char[annWithSpaces.length];
827 for (int i = 0; i < annWithSpaces.length; i++)
829 As[i] = annWithSpaces[i].secondaryStructure;
831 // check all spaces and dots are spaces in the internal representation
832 char[] shouldBe = { '<', ' ', 'H', ' ', 'B', 'h', ' ', 'b', '(', 'E',
833 ' ', 'e', ')', '>' };
834 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
835 + new String(As) + " but should be " + new String(shouldBe));
837 // this should result in the same RNA SS Annotations
838 AlignmentI alWithoutSpaces = new AppletFormatAdapter().readFile(
839 aliFileRnaSS, DataSourceType.PASTE,
840 jalview.io.FileFormat.Fasta);
841 AnnotationFile afWithoutSpaces = new AnnotationFile();
842 afWithoutSpaces.readAnnotationFile(alWithoutSpaces,
843 annFileRnaSSWithoutSpaceChars,
844 DataSourceType.PASTE);
847 testRnaSSAnnotationsEquivalent(
848 alWithSpaces.getAlignmentAnnotation()[0],
849 alWithoutSpaces.getAlignmentAnnotation()[0]),
850 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
852 + alWithSpaces.getAlignmentAnnotation()[0]
853 .getRnaSecondaryStructure()
854 + "\n" + "RNA SS A 2:"
855 + alWithoutSpaces.getAlignmentAnnotation()[0]
856 .getRnaSecondaryStructure());
858 // this should NOT result in the same RNA SS Annotations
859 AlignmentI wrongAlWithoutSpaces = new AppletFormatAdapter().readFile(
860 aliFileRnaSS, DataSourceType.PASTE,
861 jalview.io.FileFormat.Fasta);
862 AnnotationFile wrongAfWithoutSpaces = new AnnotationFile();
863 wrongAfWithoutSpaces.readAnnotationFile(wrongAlWithoutSpaces,
864 wrongAnnFileRnaSSWithoutSpaceChars,
865 DataSourceType.PASTE);
868 testRnaSSAnnotationsEquivalent(
869 alWithSpaces.getAlignmentAnnotation()[0],
870 wrongAlWithoutSpaces.getAlignmentAnnotation()[0]),
871 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
873 + alWithSpaces.getAlignmentAnnotation()[0]
874 .getRnaSecondaryStructure()
875 + "\n" + "RNA SS A 2:"
876 + wrongAlWithoutSpaces.getAlignmentAnnotation()[0]
877 .getRnaSecondaryStructure());
879 // check no spaces in the output
880 // TODO: create a better 'save as <format>' pattern
881 alWithSpaces.getAlignmentAnnotation()[0].visible = true;
882 StockholmFile sf = new StockholmFile(alWithSpaces);
884 String stockholmFile = sf.print(alWithSpaces.getSequencesArray(), true);
885 Pattern noSpacesInRnaSSAnnotation = Pattern
886 .compile("\\n#=GC SS_cons\\s+\\S{14}\\n");
887 Matcher m = noSpacesInRnaSSAnnotation.matcher(stockholmFile);
888 boolean matches = m.find();
889 Assert.assertTrue(matches,
890 "StockholmFile output does not contain expected output (may contain spaces):\n"