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
372 if (seq_original[i].getSequenceFeatures() != null
373 && seq_new[in].getSequenceFeatures() != null)
375 System.out.println("There are feature!!!");
376 sequenceFeatures_original = seq_original[i]
377 .getSequenceFeatures();
378 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
380 assertEquals("different number of features", seq_original[i]
381 .getSequenceFeatures().size(), seq_new[in]
382 .getSequenceFeatures().size());
384 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
387 assertEquals("Different features",
388 sequenceFeatures_original.get(feat),
389 sequenceFeatures_new.get(feat));
392 // compare alignment annotation
393 if (al.getSequenceAt(i).getAnnotation() != null
394 && al_input.getSequenceAt(in).getAnnotation() != null)
396 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
398 if (al.getSequenceAt(i).getAnnotation()[j] != null
399 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
401 annot_original = al.getSequenceAt(i).getAnnotation()[j];
402 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
403 assertEqualSecondaryStructure(
404 "Different annotation elements", annot_original,
405 annot_new, allowNullAnnotation);
409 else if (al.getSequenceAt(i).getAnnotation() == null
410 && al_input.getSequenceAt(in).getAnnotation() == null)
412 System.out.println("No annotations");
414 else if (al.getSequenceAt(i).getAnnotation() != null
415 && al_input.getSequenceAt(in).getAnnotation() == null)
417 fail("Annotations differed between sequences ("
418 + al.getSequenceAt(i).getName() + ") and ("
419 + al_input.getSequenceAt(i).getName() + ")");
428 * compare two annotation rows, with special support for secondary structure
429 * comparison. With RNA, only the value and the secondaryStructure symbols are
430 * compared, displayCharacter and description are ignored. Annotations where
431 * Annotation.isWhitespace() is true are always considered equal.
434 * - not actually used yet..
436 * - the original annotation
438 * - the one compared to the original annotation
439 * @param allowNullEquivalence
440 * when true, positions in alignment annotation that are null will be
441 * considered equal to non-null positions for which
442 * Annotation.isWhitespace() is true.
444 private static void assertEqualSecondaryStructure(String message,
445 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
446 boolean allowNullEqivalence)
448 // TODO: test to cover this assert behaves correctly for all allowed
449 // variations of secondary structure annotation row equivalence
450 if (annot_or.annotations.length != annot_new.annotations.length)
452 fail("Different lengths for annotation row elements: "
453 + annot_or.annotations.length + "!="
454 + annot_new.annotations.length);
456 boolean isRna = annot_or.isRNA();
457 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
458 + " secondary structure in the row.",
459 isRna == annot_new.isRNA());
460 for (int i = 0; i < annot_or.annotations.length; i++)
462 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
463 if (an_or != null && an_new != null)
468 if (an_or.secondaryStructure != an_new.secondaryStructure
469 || ((Float.isNaN(an_or.value) != Float
470 .isNaN(an_new.value)) || an_or.value != an_new.value))
472 fail("Different RNA secondary structure at column " + i
473 + " expected: [" + annot_or.annotations[i].toString()
474 + "] but got: [" + annot_new.annotations[i].toString()
480 // not RNA secondary structure, so expect all elements to match...
481 if ((an_or.isWhitespace() != an_new.isWhitespace())
482 || !an_or.displayCharacter.trim().equals(
483 an_new.displayCharacter.trim())
484 || !("" + an_or.secondaryStructure).trim().equals(
485 ("" + an_new.secondaryStructure).trim())
486 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
487 .trim().length() == 0)
488 || (an_new.description == null && an_or.description
489 .trim().length() == 0) || an_or.description
490 .trim().equals(an_new.description.trim())))
491 || !((Float.isNaN(an_or.value) && Float
492 .isNaN(an_new.value)) || an_or.value == an_new.value))
494 fail("Annotation Element Mismatch\nElement " + i
495 + " in original: " + annot_or.annotations[i].toString()
496 + "\nElement " + i + " in new: "
497 + annot_new.annotations[i].toString());
501 else if (annot_or.annotations[i] == null
502 && annot_new.annotations[i] == null)
508 if (allowNullEqivalence)
510 if (an_or != null && an_or.isWhitespace())
515 if (an_new != null && an_new.isWhitespace())
520 // need also to test for null in one, non-SS annotation in other...
521 fail("Annotation Element Mismatch\nElement " + i + " in original: "
522 + (an_or == null ? "is null" : an_or.toString())
523 + "\nElement " + i + " in new: "
524 + (an_new == null ? "is null" : an_new.toString()));
530 * @see assertEqualSecondaryStructure - test if two secondary structure
531 * annotations are not equal
535 * @param allowNullEquivalence
537 public static void assertNotEqualSecondaryStructure(String message,
538 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
539 boolean allowNullEquivalence)
541 boolean thrown = false;
544 assertEqualSecondaryStructure("", an_orig, an_new,
545 allowNullEquivalence);
546 } catch (AssertionError af)
552 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
555 private AlignmentAnnotation makeAnnot(Annotation ae)
557 return new AlignmentAnnotation("label", "description", new Annotation[]
561 @Test(groups={"Functional"})
562 public void testAnnotationEquivalence()
564 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
565 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
567 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
568 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
569 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
571 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
573 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
574 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
575 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
577 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
580 // check self equivalence
581 for (boolean allowNull : new boolean[] { true, false })
583 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
585 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
587 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
588 anotherSheetWithLabel, allowNull);
589 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
590 anotherRnaNoDC, allowNull);
591 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
592 anotherRnaWithDC, allowNull);
593 // display character doesn't matter for RNA structure (for 2.10.2)
594 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
596 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
600 // verify others are different
601 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
602 sheetWithLabel, rnaWithDC);
603 for (int p = 0; p < aaSet.size(); p++)
605 for (int q = 0; q < aaSet.size(); q++)
609 assertNotEqualSecondaryStructure("Should be different",
610 aaSet.get(p), aaSet.get(q), false);
614 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
615 aaSet.get(q), false);
616 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
618 assertNotEqualSecondaryStructure(
619 "Should be different to empty anot", aaSet.get(p),
620 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
621 assertNotEqualSecondaryStructure(
622 "Should be different to empty annot",
623 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
625 assertNotEqualSecondaryStructure("Should be different to null",
626 aaSet.get(p), makeAnnot(null), false);
627 assertNotEqualSecondaryStructure("Should be different to null",
628 makeAnnot(null), aaSet.get(q), true);
637 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
638 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
639 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
640 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
641 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
643 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
644 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
645 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
646 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
647 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
648 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
649 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
650 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
652 @Test(groups = { "Functional" })
653 public void secondaryStructureForRNASequence() throws Exception
655 roundTripSSForRNA(aliFile, annFile);
658 @Test(groups = { "Functional" })
659 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
661 roundTripSSForRNA(aliFile, annFileCurlyWuss);
664 @Test(groups = { "Functional" })
665 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
667 roundTripSSForRNA(aliFile, annFileFullWuss);
670 @Test(groups = { "Functional" })
671 public void detectWussBrackets()
673 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
675 Assert.assertTrue(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
676 "Didn't recognise '" + ch + "' as a WUSS bracket");
678 for (char ch : new char[] { '@', '!', '*', ' ', '-', '.' })
680 Assert.assertFalse(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
681 "Shouldn't recognise '" + ch + "' as a WUSS bracket");
684 private static void roundTripSSForRNA(String aliFile, String annFile)
687 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
688 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
689 AnnotationFile aaf = new AnnotationFile();
690 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
691 al.getAlignmentAnnotation()[0].visible = true;
693 // TODO: create a better 'save as <format>' pattern
694 StockholmFile sf = new StockholmFile(al);
696 String stockholmFile = sf.print(al.getSequencesArray(), true);
698 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
699 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
700 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
701 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
702 // .getViewport().getAlignment().getSequences(), true, true);
703 testAlignmentEquivalence(al, newAl, true, true, true);
707 // this is the single sequence alignment and the SS annotations equivalent to
708 // the ones in file RnaSSTestFile
709 String aliFileRnaSS = ">Test.sequence/1-14\n"
711 String annFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
712 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
713 + "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"
715 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
716 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
717 String wrongAnnFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
718 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
719 + "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"
721 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
722 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
723 @Test(groups = { "Functional" })
724 public void stockholmFileRnaSSAlphaChars() throws Exception
726 AppletFormatAdapter af = new AppletFormatAdapter();
727 AlignmentI al = af.readFile(RnaSSTestFile, DataSourceType.FILE,
728 jalview.io.FileFormat.Stockholm);
729 Iterable<AlignmentAnnotation> aai = al.findAnnotations(null, null,
730 "Secondary Structure");
731 AlignmentAnnotation aa = aai.iterator().next();
732 Assert.assertTrue(aa.isRNA(),
733 "'" + RnaSSTestFile + "' not recognised as RNA SS");
734 Assert.assertTrue(aa.isValidStruc(),
735 "'" + RnaSSTestFile + "' not recognised as valid structure");
736 Annotation[] as = aa.annotations;
737 char[] As = new char[as.length];
738 for (int i = 0; i < as.length; i++)
740 As[i] = as[i].secondaryStructure;
742 char[] shouldBe = { '<', '(', 'E', 'H', 'B', 'h', 'e', 'b', '(', 'E',
743 ')', 'e', ')', '>' };
745 Arrays.equals(As, shouldBe),
746 "Annotation is " + new String(As) + " but should be "
747 + new String(shouldBe));
749 // this should result in the same RNA SS Annotations
750 AlignmentI newAl = new AppletFormatAdapter().readFile(
752 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
753 AnnotationFile aaf = new AnnotationFile();
754 aaf.readAnnotationFile(newAl, annFileRnaSSAlphaChars,
755 DataSourceType.PASTE);
758 testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
759 newAl.getAlignmentAnnotation()[0]),
760 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
761 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
762 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
764 // this should NOT result in the same RNA SS Annotations
765 newAl = new AppletFormatAdapter().readFile(
766 aliFileRnaSS, DataSourceType.PASTE,
767 jalview.io.FileFormat.Fasta);
768 aaf = new AnnotationFile();
769 aaf.readAnnotationFile(newAl, wrongAnnFileRnaSSAlphaChars,
770 DataSourceType.PASTE);
772 boolean mismatch = testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
773 newAl.getAlignmentAnnotation()[0]);
774 Assert.assertFalse(mismatch,
775 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
776 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
777 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
780 private static boolean testRnaSSAnnotationsEquivalent(
781 AlignmentAnnotation a1,
782 AlignmentAnnotation a2)
784 return a1.rnaSecondaryStructureEquivalent(a2);
787 String annFileRnaSSWithSpaceChars = "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;";
793 String annFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
794 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
795 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|.,.|B,B|h,h|.,.|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
797 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
798 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
800 String wrongAnnFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
801 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
802 + "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"
804 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
805 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
807 @Test(groups = { "Functional" })
808 public void stockholmFileRnaSSSpaceChars() throws Exception
810 AlignmentI alWithSpaces = new AppletFormatAdapter().readFile(
811 aliFileRnaSS, DataSourceType.PASTE,
812 jalview.io.FileFormat.Fasta);
813 AnnotationFile afWithSpaces = new AnnotationFile();
814 afWithSpaces.readAnnotationFile(alWithSpaces,
815 annFileRnaSSWithSpaceChars, DataSourceType.PASTE);
817 Iterable<AlignmentAnnotation> aaiWithSpaces = alWithSpaces
818 .findAnnotations(null, null, "Secondary Structure");
819 AlignmentAnnotation aaWithSpaces = aaiWithSpaces.iterator().next();
820 Assert.assertTrue(aaWithSpaces.isRNA(),
821 "'" + aaWithSpaces + "' not recognised as RNA SS");
822 Assert.assertTrue(aaWithSpaces.isValidStruc(),
823 "'" + aaWithSpaces + "' not recognised as valid structure");
824 Annotation[] annWithSpaces = aaWithSpaces.annotations;
825 char[] As = new char[annWithSpaces.length];
826 for (int i = 0; i < annWithSpaces.length; i++)
828 As[i] = annWithSpaces[i].secondaryStructure;
830 // check all spaces and dots are spaces in the internal representation
831 char[] shouldBe = { '<', ' ', 'H', ' ', 'B', 'h', ' ', 'b', '(', 'E',
832 ' ', 'e', ')', '>' };
833 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
834 + new String(As) + " but should be " + new String(shouldBe));
836 // this should result in the same RNA SS Annotations
837 AlignmentI alWithoutSpaces = new AppletFormatAdapter().readFile(
838 aliFileRnaSS, DataSourceType.PASTE,
839 jalview.io.FileFormat.Fasta);
840 AnnotationFile afWithoutSpaces = new AnnotationFile();
841 afWithoutSpaces.readAnnotationFile(alWithoutSpaces,
842 annFileRnaSSWithoutSpaceChars,
843 DataSourceType.PASTE);
846 testRnaSSAnnotationsEquivalent(
847 alWithSpaces.getAlignmentAnnotation()[0],
848 alWithoutSpaces.getAlignmentAnnotation()[0]),
849 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
851 + alWithSpaces.getAlignmentAnnotation()[0]
852 .getRnaSecondaryStructure()
853 + "\n" + "RNA SS A 2:"
854 + alWithoutSpaces.getAlignmentAnnotation()[0]
855 .getRnaSecondaryStructure());
857 // this should NOT result in the same RNA SS Annotations
858 AlignmentI wrongAlWithoutSpaces = new AppletFormatAdapter().readFile(
859 aliFileRnaSS, DataSourceType.PASTE,
860 jalview.io.FileFormat.Fasta);
861 AnnotationFile wrongAfWithoutSpaces = new AnnotationFile();
862 wrongAfWithoutSpaces.readAnnotationFile(wrongAlWithoutSpaces,
863 wrongAnnFileRnaSSWithoutSpaceChars,
864 DataSourceType.PASTE);
867 testRnaSSAnnotationsEquivalent(
868 alWithSpaces.getAlignmentAnnotation()[0],
869 wrongAlWithoutSpaces.getAlignmentAnnotation()[0]),
870 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
872 + alWithSpaces.getAlignmentAnnotation()[0]
873 .getRnaSecondaryStructure()
874 + "\n" + "RNA SS A 2:"
875 + wrongAlWithoutSpaces.getAlignmentAnnotation()[0]
876 .getRnaSecondaryStructure());
878 // check no spaces in the output
879 // TODO: create a better 'save as <format>' pattern
880 alWithSpaces.getAlignmentAnnotation()[0].visible = true;
881 StockholmFile sf = new StockholmFile(alWithSpaces);
883 String stockholmFile = sf.print(alWithSpaces.getSequencesArray(), true);
884 Pattern noSpacesInRnaSSAnnotation = Pattern
885 .compile("\\n#=GC SS_cons\\s+\\S{14}\\n");
886 Matcher m = noSpacesInRnaSSAnnotation.matcher(stockholmFile);
887 boolean matches = m.find();
888 Assert.assertTrue(matches,
889 "StockholmFile output does not contain expected output (may contain spaces):\n"