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 assertTrue(toStockholm.indexOf("P00224") > -1,
118 "Couldn't locate UNIPROT Accession in generated Stockholm file.");
119 AlignmentI fromStockholm = af.readFile(toStockholm,
120 DataSourceType.PASTE, FileFormat.Stockholm);
121 SequenceI importedSeq = fromStockholm.getSequenceAt(0);
122 assertTrue(importedSeq.getDBRefs().length == 1,
123 "Expected just one database reference to be added to sequence.");
125 importedSeq.getDBRefs()[0].getAccessionId().indexOf(" ") == -1,
126 "Spaces were found in accession ID.");
127 List<DBRefEntry> dbrefs = DBRefUtils.searchRefs(importedSeq.getDBRefs(),
129 assertTrue(dbrefs.size() == 1,
130 "Couldn't find Uniprot DBRef on re-imported sequence.");
135 * test alignment data in given file can be imported, exported and reimported
139 * - source datafile (IdentifyFile.identify()
140 * should work with it)
142 * - label for IO class used to write and read
143 * back in the data from f
144 * @param ignoreFeatures
145 * @param ignoreRowVisibility
146 * @param allowNullAnnotations
149 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
150 int naliannot, int nminseqann, boolean ignoreFeatures,
151 boolean ignoreRowVisibility, boolean allowNullAnnotations)
153 System.out.println("Reading file: " + f);
154 String ff = f.getPath();
157 AppletFormatAdapter rf = new AppletFormatAdapter();
159 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
160 new IdentifyFile().identify(ff, DataSourceType.FILE));
162 assertNotNull("Couldn't read supplied alignment data.", al);
164 // make sure dataset is initialised ? not sure about this
165 for (int i = 0; i < al.getSequencesArray().length; ++i)
167 al.getSequenceAt(i).createDatasetSequence();
169 String outputfile = rf.formatSequences(ioformat, al, true);
170 System.out.println("Output file in '" + ioformat + "':\n"
171 + outputfile + "\n<<EOF\n");
172 // test for consistency in io
173 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
174 DataSourceType.PASTE, ioformat);
175 assertNotNull("Couldn't parse reimported alignment data.", al_input);
177 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
178 DataSourceType.PASTE);
179 assertNotNull("Identify routine failed for outputformat " + ioformat,
182 "Identify routine could not recognise output generated by '"
183 + ioformat + "' writer",
184 ioformat.equals(identifyoutput));
185 testAlignmentEquivalence(al, al_input, ignoreFeatures,
186 ignoreRowVisibility, allowNullAnnotations);
187 int numaliannot = 0, numsqswithali = 0;
188 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
190 if (ala.sequenceRef == null)
201 assertEquals("Number of alignment annotations", naliannot,
206 "Number of sequence associated annotations wasn't at least "
207 + nminseqann, numsqswithali >= nminseqann);
209 } catch (Exception e)
212 assertTrue("Couln't format the alignment for output file.", false);
217 * assert alignment equivalence
222 * 'secondary' or generated alignment from some datapreserving
224 * @param ignoreFeatures
225 * when true, differences in sequence feature annotation are ignored
227 public static void testAlignmentEquivalence(AlignmentI al,
228 AlignmentI al_input, boolean ignoreFeatures)
230 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
234 * assert alignment equivalence - uses special comparators for RNA structure
240 * 'secondary' or generated alignment from some datapreserving
242 * @param ignoreFeatures
243 * when true, differences in sequence feature annotation are ignored
245 * @param ignoreRowVisibility
246 * when true, do not fail if there are differences in the visibility
248 * @param allowNullAnnotation
249 * when true, positions in alignment annotation that are null will be
250 * considered equal to positions containing annotation where
251 * Annotation.isWhitespace() returns true.
254 public static void testAlignmentEquivalence(AlignmentI al,
255 AlignmentI al_input, boolean ignoreFeatures,
256 boolean ignoreRowVisibility, boolean allowNullAnnotation)
258 assertNotNull("Original alignment was null", al);
259 assertNotNull("Generated alignment was null", al_input);
261 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
262 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
263 + "x" + al_input.getWidth(),
264 al.getHeight() == al_input.getHeight()
265 && al.getWidth() == al_input.getWidth());
267 // check Alignment annotation
268 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
269 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
271 // note - at moment we do not distinguish between alignment without any
272 // annotation rows and alignment with no annotation row vector
273 // we might want to revise this in future
274 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
275 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
276 Map<Integer, BitSet> orig_groups = new HashMap<>();
277 Map<Integer, BitSet> new_groups = new HashMap<>();
279 if (aa_new != null && aa_original != null)
281 for (int i = 0; i < aa_original.length; i++)
283 if (aa_new.length > i)
285 assertEqualSecondaryStructure(
286 "Different alignment annotation at position " + i,
287 aa_original[i], aa_new[i], allowNullAnnotation);
288 // compare graphGroup or graph properties - needed to verify JAL-1299
289 assertEquals("Graph type not identical.", aa_original[i].graph,
291 if (!ignoreRowVisibility)
293 assertEquals("Visibility not identical.",
294 aa_original[i].visible,
297 assertEquals("Threshold line not identical.",
298 aa_original[i].threshold, aa_new[i].threshold);
299 // graphGroup may differ, but pattern should be the same
300 Integer o_ggrp = Integer.valueOf(aa_original[i].graphGroup + 2);
301 Integer n_ggrp = Integer.valueOf(aa_new[i].graphGroup + 2);
302 BitSet orig_g = orig_groups.get(o_ggrp);
303 BitSet new_g = new_groups.get(n_ggrp);
306 orig_groups.put(o_ggrp, orig_g = new BitSet());
310 new_groups.put(n_ggrp, new_g = new BitSet());
312 assertEquals("Graph Group pattern differs at annotation " + i,
319 System.err.println("No matching annotation row for "
320 + aa_original[i].toString());
325 "Generated and imported alignment have different annotation sets",
326 aa_original_size, aa_new_size);
328 // check sequences, annotation and features
329 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
330 seq_original = al.getSequencesArray();
331 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
332 seq_new = al_input.getSequencesArray();
333 List<SequenceFeature> sequenceFeatures_original;
334 List<SequenceFeature> sequenceFeatures_new;
335 AlignmentAnnotation annot_original, annot_new;
337 for (int i = 0; i < al.getSequencesArray().length; i++)
339 String name = seq_original[i].getName();
340 int start = seq_original[i].getStart();
341 int end = seq_original[i].getEnd();
342 System.out.println("Check sequence: " + name + "/" + start + "-"
345 // search equal sequence
346 for (int in = 0; in < al_input.getSequencesArray().length; in++)
348 if (name.equals(seq_new[in].getName())
349 && start == seq_new[in].getStart()
350 && end == seq_new[in].getEnd())
352 String ss_original = seq_original[i].getSequenceAsString();
353 String ss_new = seq_new[in].getSequenceAsString();
354 assertEquals("The sequences " + name + "/" + start + "-" + end
355 + " are not equal", ss_original, ss_new);
358 "Sequence Features were not equivalent"
359 + (ignoreFeatures ? " ignoring." : ""),
361 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
362 .getSequenceFeatures() == null)
363 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
364 .getSequenceFeatures() != null));
365 // compare sequence features
366 if (seq_original[i].getSequenceFeatures() != null
367 && seq_new[in].getSequenceFeatures() != null)
369 System.out.println("There are feature!!!");
370 sequenceFeatures_original = seq_original[i]
371 .getSequenceFeatures();
372 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
374 assertEquals("different number of features", seq_original[i]
375 .getSequenceFeatures().size(), seq_new[in]
376 .getSequenceFeatures().size());
378 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
381 assertEquals("Different features",
382 sequenceFeatures_original.get(feat),
383 sequenceFeatures_new.get(feat));
386 // compare alignment annotation
387 if (al.getSequenceAt(i).getAnnotation() != null
388 && al_input.getSequenceAt(in).getAnnotation() != null)
390 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
392 if (al.getSequenceAt(i).getAnnotation()[j] != null
393 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
395 annot_original = al.getSequenceAt(i).getAnnotation()[j];
396 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
397 assertEqualSecondaryStructure(
398 "Different annotation elements", annot_original,
399 annot_new, allowNullAnnotation);
403 else if (al.getSequenceAt(i).getAnnotation() == null
404 && al_input.getSequenceAt(in).getAnnotation() == null)
406 System.out.println("No annotations");
408 else if (al.getSequenceAt(i).getAnnotation() != null
409 && al_input.getSequenceAt(in).getAnnotation() == null)
411 fail("Annotations differed between sequences ("
412 + al.getSequenceAt(i).getName() + ") and ("
413 + al_input.getSequenceAt(i).getName() + ")");
422 * compare two annotation rows, with special support for secondary structure
423 * comparison. With RNA, only the value and the secondaryStructure symbols are
424 * compared, displayCharacter and description are ignored. Annotations where
425 * Annotation.isWhitespace() is true are always considered equal.
428 * - not actually used yet..
430 * - the original annotation
432 * - the one compared to the original annotation
433 * @param allowNullEquivalence
434 * when true, positions in alignment annotation that are null will be
435 * considered equal to non-null positions for which
436 * Annotation.isWhitespace() is true.
438 private static void assertEqualSecondaryStructure(String message,
439 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
440 boolean allowNullEqivalence)
442 // TODO: test to cover this assert behaves correctly for all allowed
443 // variations of secondary structure annotation row equivalence
444 if (annot_or.annotations.length != annot_new.annotations.length)
446 fail("Different lengths for annotation row elements: "
447 + annot_or.annotations.length + "!="
448 + annot_new.annotations.length);
450 boolean isRna = annot_or.isRNA();
451 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
452 + " secondary structure in the row.",
453 isRna == annot_new.isRNA());
454 for (int i = 0; i < annot_or.annotations.length; i++)
456 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
457 if (an_or != null && an_new != null)
462 if (an_or.secondaryStructure != an_new.secondaryStructure
463 || ((Float.isNaN(an_or.value) != Float
464 .isNaN(an_new.value)) || an_or.value != an_new.value))
466 fail("Different RNA secondary structure at column " + i
467 + " expected: [" + annot_or.annotations[i].toString()
468 + "] but got: [" + annot_new.annotations[i].toString()
474 // not RNA secondary structure, so expect all elements to match...
475 if ((an_or.isWhitespace() != an_new.isWhitespace())
476 || !an_or.displayCharacter.trim().equals(
477 an_new.displayCharacter.trim())
478 || !("" + an_or.secondaryStructure).trim().equals(
479 ("" + an_new.secondaryStructure).trim())
480 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
481 .trim().length() == 0)
482 || (an_new.description == null && an_or.description
483 .trim().length() == 0) || an_or.description
484 .trim().equals(an_new.description.trim())))
485 || !((Float.isNaN(an_or.value) && Float
486 .isNaN(an_new.value)) || an_or.value == an_new.value))
488 fail("Annotation Element Mismatch\nElement " + i
489 + " in original: " + annot_or.annotations[i].toString()
490 + "\nElement " + i + " in new: "
491 + annot_new.annotations[i].toString());
495 else if (annot_or.annotations[i] == null
496 && annot_new.annotations[i] == null)
502 if (allowNullEqivalence)
504 if (an_or != null && an_or.isWhitespace())
509 if (an_new != null && an_new.isWhitespace())
514 // need also to test for null in one, non-SS annotation in other...
515 fail("Annotation Element Mismatch\nElement " + i + " in original: "
516 + (an_or == null ? "is null" : an_or.toString())
517 + "\nElement " + i + " in new: "
518 + (an_new == null ? "is null" : an_new.toString()));
524 * @see assertEqualSecondaryStructure - test if two secondary structure
525 * annotations are not equal
529 * @param allowNullEquivalence
531 public static void assertNotEqualSecondaryStructure(String message,
532 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
533 boolean allowNullEquivalence)
535 boolean thrown = false;
538 assertEqualSecondaryStructure("", an_orig, an_new,
539 allowNullEquivalence);
540 } catch (AssertionError af)
546 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
549 private AlignmentAnnotation makeAnnot(Annotation ae)
551 return new AlignmentAnnotation("label", "description", new Annotation[]
555 @Test(groups={"Functional"})
556 public void testAnnotationEquivalence()
558 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
559 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
561 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
562 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
563 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
565 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
567 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
568 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
569 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
571 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
574 // check self equivalence
575 for (boolean allowNull : new boolean[] { true, false })
577 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
579 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
581 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
582 anotherSheetWithLabel, allowNull);
583 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
584 anotherRnaNoDC, allowNull);
585 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
586 anotherRnaWithDC, allowNull);
587 // display character doesn't matter for RNA structure (for 2.10.2)
588 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
590 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
594 // verify others are different
595 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
596 sheetWithLabel, rnaWithDC);
597 for (int p = 0; p < aaSet.size(); p++)
599 for (int q = 0; q < aaSet.size(); q++)
603 assertNotEqualSecondaryStructure("Should be different",
604 aaSet.get(p), aaSet.get(q), false);
608 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
609 aaSet.get(q), false);
610 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
612 assertNotEqualSecondaryStructure(
613 "Should be different to empty anot", aaSet.get(p),
614 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
615 assertNotEqualSecondaryStructure(
616 "Should be different to empty annot",
617 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
619 assertNotEqualSecondaryStructure("Should be different to null",
620 aaSet.get(p), makeAnnot(null), false);
621 assertNotEqualSecondaryStructure("Should be different to null",
622 makeAnnot(null), aaSet.get(q), true);
631 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
632 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
633 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
634 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
635 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
637 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
638 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
639 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
640 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
641 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
642 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
643 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
644 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
646 @Test(groups = { "Functional" })
647 public void secondaryStructureForRNASequence() throws Exception
649 roundTripSSForRNA(aliFile, annFile);
652 @Test(groups = { "Functional" })
653 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
655 roundTripSSForRNA(aliFile, annFileCurlyWuss);
658 @Test(groups = { "Functional" })
659 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
661 roundTripSSForRNA(aliFile, annFileFullWuss);
664 @Test(groups = { "Functional" })
665 public void detectWussBrackets()
667 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
669 Assert.assertTrue(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
670 "Didn't recognise '" + ch + "' as a WUSS bracket");
672 for (char ch : new char[] { '@', '!', '*', ' ', '-', '.' })
674 Assert.assertFalse(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
675 "Shouldn't recognise '" + ch + "' as a WUSS bracket");
678 private static void roundTripSSForRNA(String aliFile, String annFile)
681 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
682 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
683 AnnotationFile aaf = new AnnotationFile();
684 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
685 al.getAlignmentAnnotation()[0].visible = true;
687 // TODO: create a better 'save as <format>' pattern
688 StockholmFile sf = new StockholmFile(al);
690 String stockholmFile = sf.print(al.getSequencesArray(), true);
692 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
693 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
694 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
695 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
696 // .getViewport().getAlignment().getSequences(), true, true);
697 testAlignmentEquivalence(al, newAl, true, true, true);
701 // this is the single sequence alignment and the SS annotations equivalent to
702 // the ones in file RnaSSTestFile
703 String aliFileRnaSS = ">Test.sequence/1-14\n"
705 String annFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
706 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
707 + "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"
709 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
710 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
711 String wrongAnnFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
712 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
713 + "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"
715 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
716 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
717 @Test(groups = { "Functional" })
718 public void stockholmFileRnaSSAlphaChars() throws Exception
720 AppletFormatAdapter af = new AppletFormatAdapter();
721 AlignmentI al = af.readFile(RnaSSTestFile, DataSourceType.FILE,
722 jalview.io.FileFormat.Stockholm);
723 Iterable<AlignmentAnnotation> aai = al.findAnnotations(null, null,
724 "Secondary Structure");
725 AlignmentAnnotation aa = aai.iterator().next();
726 Assert.assertTrue(aa.isRNA(),
727 "'" + RnaSSTestFile + "' not recognised as RNA SS");
728 Assert.assertTrue(aa.isValidStruc(),
729 "'" + RnaSSTestFile + "' not recognised as valid structure");
730 Annotation[] as = aa.annotations;
731 char[] As = new char[as.length];
732 for (int i = 0; i < as.length; i++)
734 As[i] = as[i].secondaryStructure;
736 char[] shouldBe = { '<', '(', 'E', 'H', 'B', 'h', 'e', 'b', '(', 'E',
737 ')', 'e', ')', '>' };
739 Arrays.equals(As, shouldBe),
740 "Annotation is " + new String(As) + " but should be "
741 + new String(shouldBe));
743 // this should result in the same RNA SS Annotations
744 AlignmentI newAl = new AppletFormatAdapter().readFile(
746 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
747 AnnotationFile aaf = new AnnotationFile();
748 aaf.readAnnotationFile(newAl, annFileRnaSSAlphaChars,
749 DataSourceType.PASTE);
752 testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
753 newAl.getAlignmentAnnotation()[0]),
754 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
755 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
756 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
758 // this should NOT result in the same RNA SS Annotations
759 newAl = new AppletFormatAdapter().readFile(
760 aliFileRnaSS, DataSourceType.PASTE,
761 jalview.io.FileFormat.Fasta);
762 aaf = new AnnotationFile();
763 aaf.readAnnotationFile(newAl, wrongAnnFileRnaSSAlphaChars,
764 DataSourceType.PASTE);
766 boolean mismatch = testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
767 newAl.getAlignmentAnnotation()[0]);
768 Assert.assertFalse(mismatch,
769 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
770 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
771 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
774 private static boolean testRnaSSAnnotationsEquivalent(
775 AlignmentAnnotation a1,
776 AlignmentAnnotation a2)
778 return a1.rnaSecondaryStructureEquivalent(a2);
781 String annFileRnaSSWithSpaceChars = "JALVIEW_ANNOTATION\n"
782 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
783 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H| , |B,B|h,h| , |b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
785 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
786 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
787 String annFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
788 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
789 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|.,.|B,B|h,h|.,.|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
791 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
792 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
794 String wrongAnnFileRnaSSWithoutSpaceChars = "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|Z,Z|B,B|h,h|z,z|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 @Test(groups = { "Functional" })
802 public void stockholmFileRnaSSSpaceChars() throws Exception
804 AlignmentI alWithSpaces = new AppletFormatAdapter().readFile(
805 aliFileRnaSS, DataSourceType.PASTE,
806 jalview.io.FileFormat.Fasta);
807 AnnotationFile afWithSpaces = new AnnotationFile();
808 afWithSpaces.readAnnotationFile(alWithSpaces,
809 annFileRnaSSWithSpaceChars, DataSourceType.PASTE);
811 Iterable<AlignmentAnnotation> aaiWithSpaces = alWithSpaces
812 .findAnnotations(null, null, "Secondary Structure");
813 AlignmentAnnotation aaWithSpaces = aaiWithSpaces.iterator().next();
814 Assert.assertTrue(aaWithSpaces.isRNA(),
815 "'" + aaWithSpaces + "' not recognised as RNA SS");
816 Assert.assertTrue(aaWithSpaces.isValidStruc(),
817 "'" + aaWithSpaces + "' not recognised as valid structure");
818 Annotation[] annWithSpaces = aaWithSpaces.annotations;
819 char[] As = new char[annWithSpaces.length];
820 for (int i = 0; i < annWithSpaces.length; i++)
822 As[i] = annWithSpaces[i].secondaryStructure;
824 // check all spaces and dots are spaces in the internal representation
825 char[] shouldBe = { '<', ' ', 'H', ' ', 'B', 'h', ' ', 'b', '(', 'E',
826 ' ', 'e', ')', '>' };
827 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
828 + new String(As) + " but should be " + new String(shouldBe));
830 // this should result in the same RNA SS Annotations
831 AlignmentI alWithoutSpaces = new AppletFormatAdapter().readFile(
832 aliFileRnaSS, DataSourceType.PASTE,
833 jalview.io.FileFormat.Fasta);
834 AnnotationFile afWithoutSpaces = new AnnotationFile();
835 afWithoutSpaces.readAnnotationFile(alWithoutSpaces,
836 annFileRnaSSWithoutSpaceChars,
837 DataSourceType.PASTE);
840 testRnaSSAnnotationsEquivalent(
841 alWithSpaces.getAlignmentAnnotation()[0],
842 alWithoutSpaces.getAlignmentAnnotation()[0]),
843 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
845 + alWithSpaces.getAlignmentAnnotation()[0]
846 .getRnaSecondaryStructure()
847 + "\n" + "RNA SS A 2:"
848 + alWithoutSpaces.getAlignmentAnnotation()[0]
849 .getRnaSecondaryStructure());
851 // this should NOT result in the same RNA SS Annotations
852 AlignmentI wrongAlWithoutSpaces = new AppletFormatAdapter().readFile(
853 aliFileRnaSS, DataSourceType.PASTE,
854 jalview.io.FileFormat.Fasta);
855 AnnotationFile wrongAfWithoutSpaces = new AnnotationFile();
856 wrongAfWithoutSpaces.readAnnotationFile(wrongAlWithoutSpaces,
857 wrongAnnFileRnaSSWithoutSpaceChars,
858 DataSourceType.PASTE);
861 testRnaSSAnnotationsEquivalent(
862 alWithSpaces.getAlignmentAnnotation()[0],
863 wrongAlWithoutSpaces.getAlignmentAnnotation()[0]),
864 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
866 + alWithSpaces.getAlignmentAnnotation()[0]
867 .getRnaSecondaryStructure()
868 + "\n" + "RNA SS A 2:"
869 + wrongAlWithoutSpaces.getAlignmentAnnotation()[0]
870 .getRnaSecondaryStructure());
872 // check no spaces in the output
873 // TODO: create a better 'save as <format>' pattern
874 alWithSpaces.getAlignmentAnnotation()[0].visible = true;
875 StockholmFile sf = new StockholmFile(alWithSpaces);
877 String stockholmFile = sf.print(alWithSpaces.getSequencesArray(), true);
878 Pattern noSpacesInRnaSSAnnotation = Pattern
879 .compile("\\n#=GC SS_cons\\s+\\S{14}\\n");
880 Matcher m = noSpacesInRnaSSAnnotation.matcher(stockholmFile);
881 boolean matches = m.find();
882 Assert.assertTrue(matches,
883 "StockholmFile output does not contain expected output (may contain spaces):\n"