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 List<DBRefEntry> dbrefs = DBRefUtils.searchRefs(importedSeq.getDBRefs(),
124 assertTrue(dbrefs.size() == 1,
125 "Couldn't find Uniprot DBRef on re-imported sequence.");
130 * test alignment data in given file can be imported, exported and reimported
134 * - source datafile (IdentifyFile.identify()
135 * should work with it)
137 * - label for IO class used to write and read
138 * back in the data from f
139 * @param ignoreFeatures
140 * @param ignoreRowVisibility
141 * @param allowNullAnnotations
144 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
145 int naliannot, int nminseqann, boolean ignoreFeatures,
146 boolean ignoreRowVisibility, boolean allowNullAnnotations)
148 System.out.println("Reading file: " + f);
149 String ff = f.getPath();
152 AppletFormatAdapter rf = new AppletFormatAdapter();
154 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
155 new IdentifyFile().identify(ff, DataSourceType.FILE));
157 assertNotNull("Couldn't read supplied alignment data.", al);
159 // make sure dataset is initialised ? not sure about this
160 for (int i = 0; i < al.getSequencesArray().length; ++i)
162 al.getSequenceAt(i).createDatasetSequence();
164 String outputfile = rf.formatSequences(ioformat, al, true);
165 System.out.println("Output file in '" + ioformat + "':\n"
166 + outputfile + "\n<<EOF\n");
167 // test for consistency in io
168 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
169 DataSourceType.PASTE, ioformat);
170 assertNotNull("Couldn't parse reimported alignment data.", al_input);
172 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
173 DataSourceType.PASTE);
174 assertNotNull("Identify routine failed for outputformat " + ioformat,
177 "Identify routine could not recognise output generated by '"
178 + ioformat + "' writer",
179 ioformat.equals(identifyoutput));
180 testAlignmentEquivalence(al, al_input, ignoreFeatures,
181 ignoreRowVisibility, allowNullAnnotations);
182 int numaliannot = 0, numsqswithali = 0;
183 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
185 if (ala.sequenceRef == null)
196 assertEquals("Number of alignment annotations", naliannot,
201 "Number of sequence associated annotations wasn't at least "
202 + nminseqann, numsqswithali >= nminseqann);
204 } catch (Exception e)
207 assertTrue("Couln't format the alignment for output file.", false);
212 * assert alignment equivalence
217 * 'secondary' or generated alignment from some datapreserving
219 * @param ignoreFeatures
220 * when true, differences in sequence feature annotation are ignored
222 public static void testAlignmentEquivalence(AlignmentI al,
223 AlignmentI al_input, boolean ignoreFeatures)
225 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
229 * assert alignment equivalence - uses special comparators for RNA structure
235 * 'secondary' or generated alignment from some datapreserving
237 * @param ignoreFeatures
238 * when true, differences in sequence feature annotation are ignored
240 * @param ignoreRowVisibility
241 * when true, do not fail if there are differences in the visibility
243 * @param allowNullAnnotation
244 * when true, positions in alignment annotation that are null will be
245 * considered equal to positions containing annotation where
246 * Annotation.isWhitespace() returns true.
249 public static void testAlignmentEquivalence(AlignmentI al,
250 AlignmentI al_input, boolean ignoreFeatures,
251 boolean ignoreRowVisibility, boolean allowNullAnnotation)
253 assertNotNull("Original alignment was null", al);
254 assertNotNull("Generated alignment was null", al_input);
256 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
257 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
258 + "x" + al_input.getWidth(),
259 al.getHeight() == al_input.getHeight()
260 && al.getWidth() == al_input.getWidth());
262 // check Alignment annotation
263 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
264 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
266 // note - at moment we do not distinguish between alignment without any
267 // annotation rows and alignment with no annotation row vector
268 // we might want to revise this in future
269 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
270 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
271 Map<Integer, BitSet> orig_groups = new HashMap<>();
272 Map<Integer, BitSet> new_groups = new HashMap<>();
274 if (aa_new != null && aa_original != null)
276 for (int i = 0; i < aa_original.length; i++)
278 if (aa_new.length > i)
280 assertEqualSecondaryStructure(
281 "Different alignment annotation at position " + i,
282 aa_original[i], aa_new[i], allowNullAnnotation);
283 // compare graphGroup or graph properties - needed to verify JAL-1299
284 assertEquals("Graph type not identical.", aa_original[i].graph,
286 if (!ignoreRowVisibility)
288 assertEquals("Visibility not identical.",
289 aa_original[i].visible,
292 assertEquals("Threshold line not identical.",
293 aa_original[i].threshold, aa_new[i].threshold);
294 // graphGroup may differ, but pattern should be the same
295 Integer o_ggrp = Integer.valueOf(aa_original[i].graphGroup + 2);
296 Integer n_ggrp = Integer.valueOf(aa_new[i].graphGroup + 2);
297 BitSet orig_g = orig_groups.get(o_ggrp);
298 BitSet new_g = new_groups.get(n_ggrp);
301 orig_groups.put(o_ggrp, orig_g = new BitSet());
305 new_groups.put(n_ggrp, new_g = new BitSet());
307 assertEquals("Graph Group pattern differs at annotation " + i,
314 System.err.println("No matching annotation row for "
315 + aa_original[i].toString());
320 "Generated and imported alignment have different annotation sets",
321 aa_original_size, aa_new_size);
323 // check sequences, annotation and features
324 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
325 seq_original = al.getSequencesArray();
326 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
327 seq_new = al_input.getSequencesArray();
328 List<SequenceFeature> sequenceFeatures_original;
329 List<SequenceFeature> sequenceFeatures_new;
330 AlignmentAnnotation annot_original, annot_new;
332 for (int i = 0; i < al.getSequencesArray().length; i++)
334 String name = seq_original[i].getName();
335 int start = seq_original[i].getStart();
336 int end = seq_original[i].getEnd();
337 System.out.println("Check sequence: " + name + "/" + start + "-"
340 // search equal sequence
341 for (int in = 0; in < al_input.getSequencesArray().length; in++)
343 if (name.equals(seq_new[in].getName())
344 && start == seq_new[in].getStart()
345 && end == seq_new[in].getEnd())
347 String ss_original = seq_original[i].getSequenceAsString();
348 String ss_new = seq_new[in].getSequenceAsString();
349 assertEquals("The sequences " + name + "/" + start + "-" + end
350 + " are not equal", ss_original, ss_new);
353 "Sequence Features were not equivalent"
354 + (ignoreFeatures ? " ignoring." : ""),
356 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
357 .getSequenceFeatures() == null)
358 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
359 .getSequenceFeatures() != null));
360 // compare sequence features
361 if (seq_original[i].getSequenceFeatures() != null
362 && seq_new[in].getSequenceFeatures() != null)
364 System.out.println("There are feature!!!");
365 sequenceFeatures_original = seq_original[i]
366 .getSequenceFeatures();
367 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
369 assertEquals("different number of features", seq_original[i]
370 .getSequenceFeatures().size(), seq_new[in]
371 .getSequenceFeatures().size());
373 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
376 assertEquals("Different features",
377 sequenceFeatures_original.get(feat),
378 sequenceFeatures_new.get(feat));
381 // compare alignment annotation
382 if (al.getSequenceAt(i).getAnnotation() != null
383 && al_input.getSequenceAt(in).getAnnotation() != null)
385 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
387 if (al.getSequenceAt(i).getAnnotation()[j] != null
388 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
390 annot_original = al.getSequenceAt(i).getAnnotation()[j];
391 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
392 assertEqualSecondaryStructure(
393 "Different annotation elements", annot_original,
394 annot_new, allowNullAnnotation);
398 else if (al.getSequenceAt(i).getAnnotation() == null
399 && al_input.getSequenceAt(in).getAnnotation() == null)
401 System.out.println("No annotations");
403 else if (al.getSequenceAt(i).getAnnotation() != null
404 && al_input.getSequenceAt(in).getAnnotation() == null)
406 fail("Annotations differed between sequences ("
407 + al.getSequenceAt(i).getName() + ") and ("
408 + al_input.getSequenceAt(i).getName() + ")");
417 * compare two annotation rows, with special support for secondary structure
418 * comparison. With RNA, only the value and the secondaryStructure symbols are
419 * compared, displayCharacter and description are ignored. Annotations where
420 * Annotation.isWhitespace() is true are always considered equal.
423 * - not actually used yet..
425 * - the original annotation
427 * - the one compared to the original annotation
428 * @param allowNullEquivalence
429 * when true, positions in alignment annotation that are null will be
430 * considered equal to non-null positions for which
431 * Annotation.isWhitespace() is true.
433 private static void assertEqualSecondaryStructure(String message,
434 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
435 boolean allowNullEqivalence)
437 // TODO: test to cover this assert behaves correctly for all allowed
438 // variations of secondary structure annotation row equivalence
439 if (annot_or.annotations.length != annot_new.annotations.length)
441 fail("Different lengths for annotation row elements: "
442 + annot_or.annotations.length + "!="
443 + annot_new.annotations.length);
445 boolean isRna = annot_or.isRNA();
446 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
447 + " secondary structure in the row.",
448 isRna == annot_new.isRNA());
449 for (int i = 0; i < annot_or.annotations.length; i++)
451 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
452 if (an_or != null && an_new != null)
457 if (an_or.secondaryStructure != an_new.secondaryStructure
458 || ((Float.isNaN(an_or.value) != Float
459 .isNaN(an_new.value)) || an_or.value != an_new.value))
461 fail("Different RNA secondary structure at column " + i
462 + " expected: [" + annot_or.annotations[i].toString()
463 + "] but got: [" + annot_new.annotations[i].toString()
469 // not RNA secondary structure, so expect all elements to match...
470 if ((an_or.isWhitespace() != an_new.isWhitespace())
471 || !an_or.displayCharacter.trim().equals(
472 an_new.displayCharacter.trim())
473 || !("" + an_or.secondaryStructure).trim().equals(
474 ("" + an_new.secondaryStructure).trim())
475 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
476 .trim().length() == 0)
477 || (an_new.description == null && an_or.description
478 .trim().length() == 0) || an_or.description
479 .trim().equals(an_new.description.trim())))
480 || !((Float.isNaN(an_or.value) && Float
481 .isNaN(an_new.value)) || an_or.value == an_new.value))
483 fail("Annotation Element Mismatch\nElement " + i
484 + " in original: " + annot_or.annotations[i].toString()
485 + "\nElement " + i + " in new: "
486 + annot_new.annotations[i].toString());
490 else if (annot_or.annotations[i] == null
491 && annot_new.annotations[i] == null)
497 if (allowNullEqivalence)
499 if (an_or != null && an_or.isWhitespace())
504 if (an_new != null && an_new.isWhitespace())
509 // need also to test for null in one, non-SS annotation in other...
510 fail("Annotation Element Mismatch\nElement " + i + " in original: "
511 + (an_or == null ? "is null" : an_or.toString())
512 + "\nElement " + i + " in new: "
513 + (an_new == null ? "is null" : an_new.toString()));
519 * @see assertEqualSecondaryStructure - test if two secondary structure
520 * annotations are not equal
524 * @param allowNullEquivalence
526 public static void assertNotEqualSecondaryStructure(String message,
527 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
528 boolean allowNullEquivalence)
530 boolean thrown = false;
533 assertEqualSecondaryStructure("", an_orig, an_new,
534 allowNullEquivalence);
535 } catch (AssertionError af)
541 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
544 private AlignmentAnnotation makeAnnot(Annotation ae)
546 return new AlignmentAnnotation("label", "description", new Annotation[]
550 @Test(groups={"Functional"})
551 public void testAnnotationEquivalence()
553 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
554 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
556 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
557 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
558 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
560 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
562 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
563 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
564 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
566 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
569 // check self equivalence
570 for (boolean allowNull : new boolean[] { true, false })
572 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
574 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
576 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
577 anotherSheetWithLabel, allowNull);
578 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
579 anotherRnaNoDC, allowNull);
580 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
581 anotherRnaWithDC, allowNull);
582 // display character doesn't matter for RNA structure (for 2.10.2)
583 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
585 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
589 // verify others are different
590 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
591 sheetWithLabel, rnaWithDC);
592 for (int p = 0; p < aaSet.size(); p++)
594 for (int q = 0; q < aaSet.size(); q++)
598 assertNotEqualSecondaryStructure("Should be different",
599 aaSet.get(p), aaSet.get(q), false);
603 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
604 aaSet.get(q), false);
605 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
607 assertNotEqualSecondaryStructure(
608 "Should be different to empty anot", aaSet.get(p),
609 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
610 assertNotEqualSecondaryStructure(
611 "Should be different to empty annot",
612 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
614 assertNotEqualSecondaryStructure("Should be different to null",
615 aaSet.get(p), makeAnnot(null), false);
616 assertNotEqualSecondaryStructure("Should be different to null",
617 makeAnnot(null), aaSet.get(q), true);
626 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
627 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
628 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
629 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
630 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
632 String annFileCurlyWuss = "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";
636 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
637 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
638 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
639 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
641 @Test(groups = { "Functional" })
642 public void secondaryStructureForRNASequence() throws Exception
644 roundTripSSForRNA(aliFile, annFile);
647 @Test(groups = { "Functional" })
648 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
650 roundTripSSForRNA(aliFile, annFileCurlyWuss);
653 @Test(groups = { "Functional" })
654 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
656 roundTripSSForRNA(aliFile, annFileFullWuss);
659 @Test(groups = { "Functional" })
660 public void detectWussBrackets()
662 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
664 Assert.assertTrue(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
665 "Didn't recognise '" + ch + "' as a WUSS bracket");
667 for (char ch : new char[] { '@', '!', '*', ' ', '-', '.' })
669 Assert.assertFalse(StockholmFile.RNASS_BRACKETS.indexOf(ch) >= 0,
670 "Shouldn't recognise '" + ch + "' as a WUSS bracket");
673 private static void roundTripSSForRNA(String aliFile, String annFile)
676 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
677 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
678 AnnotationFile aaf = new AnnotationFile();
679 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
680 al.getAlignmentAnnotation()[0].visible = true;
682 // TODO: create a better 'save as <format>' pattern
683 StockholmFile sf = new StockholmFile(al);
685 String stockholmFile = sf.print(al.getSequencesArray(), true);
687 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
688 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
689 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
690 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
691 // .getViewport().getAlignment().getSequences(), true, true);
692 testAlignmentEquivalence(al, newAl, true, true, true);
696 // this is the single sequence alignment and the SS annotations equivalent to
697 // the ones in file RnaSSTestFile
698 String aliFileRnaSS = ">Test.sequence/1-14\n"
700 String annFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
701 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
702 + "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"
704 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
705 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
706 String wrongAnnFileRnaSSAlphaChars = "JALVIEW_ANNOTATION\n"
707 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
708 + "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"
710 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
711 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
712 @Test(groups = { "Functional" })
713 public void stockholmFileRnaSSAlphaChars() throws Exception
715 AppletFormatAdapter af = new AppletFormatAdapter();
716 AlignmentI al = af.readFile(RnaSSTestFile, DataSourceType.FILE,
717 jalview.io.FileFormat.Stockholm);
718 Iterable<AlignmentAnnotation> aai = al.findAnnotations(null, null,
719 "Secondary Structure");
720 AlignmentAnnotation aa = aai.iterator().next();
721 Assert.assertTrue(aa.isRNA(),
722 "'" + RnaSSTestFile + "' not recognised as RNA SS");
723 Assert.assertTrue(aa.isValidStruc(),
724 "'" + RnaSSTestFile + "' not recognised as valid structure");
725 Annotation[] as = aa.annotations;
726 char[] As = new char[as.length];
727 for (int i = 0; i < as.length; i++)
729 As[i] = as[i].secondaryStructure;
731 char[] shouldBe = { '<', '(', 'E', 'H', 'B', 'h', 'e', 'b', '(', 'E',
732 ')', 'e', ')', '>' };
734 Arrays.equals(As, shouldBe),
735 "Annotation is " + new String(As) + " but should be "
736 + new String(shouldBe));
738 // this should result in the same RNA SS Annotations
739 AlignmentI newAl = new AppletFormatAdapter().readFile(
741 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
742 AnnotationFile aaf = new AnnotationFile();
743 aaf.readAnnotationFile(newAl, annFileRnaSSAlphaChars,
744 DataSourceType.PASTE);
747 testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
748 newAl.getAlignmentAnnotation()[0]),
749 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
750 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
751 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
753 // this should NOT result in the same RNA SS Annotations
754 newAl = new AppletFormatAdapter().readFile(
755 aliFileRnaSS, DataSourceType.PASTE,
756 jalview.io.FileFormat.Fasta);
757 aaf = new AnnotationFile();
758 aaf.readAnnotationFile(newAl, wrongAnnFileRnaSSAlphaChars,
759 DataSourceType.PASTE);
761 boolean mismatch = testRnaSSAnnotationsEquivalent(al.getAlignmentAnnotation()[0],
762 newAl.getAlignmentAnnotation()[0]);
763 Assert.assertFalse(mismatch,
764 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
765 + "RNA SS A 1:" + al.getAlignmentAnnotation()[0] + "\n"
766 + "RNA SS A 2:" + newAl.getAlignmentAnnotation()[0]);
769 private static boolean testRnaSSAnnotationsEquivalent(
770 AlignmentAnnotation a1,
771 AlignmentAnnotation a2)
773 return a1.rnaSecondaryStructureEquivalent(a2);
776 String annFileRnaSSWithSpaceChars = "JALVIEW_ANNOTATION\n"
777 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
778 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H| , |B,B|h,h| , |b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
780 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
781 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
782 String annFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
783 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
784 + "NO_GRAPH\tSecondary Structure\tSecondary Structure\t<,<|.,.|H,H|.,.|B,B|h,h|.,.|b,b|(,(|E,E|.,.|e,e|),)|>,>|\t2.0\n"
786 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
787 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
789 String wrongAnnFileRnaSSWithoutSpaceChars = "JALVIEW_ANNOTATION\n"
790 + "# Created: Thu Aug 02 14:54:57 BST 2018\n" + "\n"
791 + "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"
793 + "ROWPROPERTIES\tSecondary Structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false\n"
794 + "\n" + "\n" + "ALIGNMENT\tID=RNA.SS.TEST\tTP=RNA;";
796 @Test(groups = { "Functional" })
797 public void stockholmFileRnaSSSpaceChars() throws Exception
799 AlignmentI alWithSpaces = new AppletFormatAdapter().readFile(
800 aliFileRnaSS, DataSourceType.PASTE,
801 jalview.io.FileFormat.Fasta);
802 AnnotationFile afWithSpaces = new AnnotationFile();
803 afWithSpaces.readAnnotationFile(alWithSpaces,
804 annFileRnaSSWithSpaceChars, DataSourceType.PASTE);
806 Iterable<AlignmentAnnotation> aaiWithSpaces = alWithSpaces
807 .findAnnotations(null, null, "Secondary Structure");
808 AlignmentAnnotation aaWithSpaces = aaiWithSpaces.iterator().next();
809 Assert.assertTrue(aaWithSpaces.isRNA(),
810 "'" + aaWithSpaces + "' not recognised as RNA SS");
811 Assert.assertTrue(aaWithSpaces.isValidStruc(),
812 "'" + aaWithSpaces + "' not recognised as valid structure");
813 Annotation[] annWithSpaces = aaWithSpaces.annotations;
814 char[] As = new char[annWithSpaces.length];
815 for (int i = 0; i < annWithSpaces.length; i++)
817 As[i] = annWithSpaces[i].secondaryStructure;
819 // check all spaces and dots are spaces in the internal representation
820 char[] shouldBe = { '<', ' ', 'H', ' ', 'B', 'h', ' ', 'b', '(', 'E',
821 ' ', 'e', ')', '>' };
822 Assert.assertTrue(Arrays.equals(As, shouldBe), "Annotation is "
823 + new String(As) + " but should be " + new String(shouldBe));
825 // this should result in the same RNA SS Annotations
826 AlignmentI alWithoutSpaces = new AppletFormatAdapter().readFile(
827 aliFileRnaSS, DataSourceType.PASTE,
828 jalview.io.FileFormat.Fasta);
829 AnnotationFile afWithoutSpaces = new AnnotationFile();
830 afWithoutSpaces.readAnnotationFile(alWithoutSpaces,
831 annFileRnaSSWithoutSpaceChars,
832 DataSourceType.PASTE);
835 testRnaSSAnnotationsEquivalent(
836 alWithSpaces.getAlignmentAnnotation()[0],
837 alWithoutSpaces.getAlignmentAnnotation()[0]),
838 "RNA SS Annotations SHOULD be pair-wise equivalent (but apparently aren't): \n"
840 + alWithSpaces.getAlignmentAnnotation()[0]
841 .getRnaSecondaryStructure()
842 + "\n" + "RNA SS A 2:"
843 + alWithoutSpaces.getAlignmentAnnotation()[0]
844 .getRnaSecondaryStructure());
846 // this should NOT result in the same RNA SS Annotations
847 AlignmentI wrongAlWithoutSpaces = new AppletFormatAdapter().readFile(
848 aliFileRnaSS, DataSourceType.PASTE,
849 jalview.io.FileFormat.Fasta);
850 AnnotationFile wrongAfWithoutSpaces = new AnnotationFile();
851 wrongAfWithoutSpaces.readAnnotationFile(wrongAlWithoutSpaces,
852 wrongAnnFileRnaSSWithoutSpaceChars,
853 DataSourceType.PASTE);
856 testRnaSSAnnotationsEquivalent(
857 alWithSpaces.getAlignmentAnnotation()[0],
858 wrongAlWithoutSpaces.getAlignmentAnnotation()[0]),
859 "RNA SS Annotations SHOULD NOT be pair-wise equivalent (but apparently are): \n"
861 + alWithSpaces.getAlignmentAnnotation()[0]
862 .getRnaSecondaryStructure()
863 + "\n" + "RNA SS A 2:"
864 + wrongAlWithoutSpaces.getAlignmentAnnotation()[0]
865 .getRnaSecondaryStructure());
867 // check no spaces in the output
868 // TODO: create a better 'save as <format>' pattern
869 alWithSpaces.getAlignmentAnnotation()[0].visible = true;
870 StockholmFile sf = new StockholmFile(alWithSpaces);
872 String stockholmFile = sf.print(alWithSpaces.getSequencesArray(), true);
873 Pattern noSpacesInRnaSSAnnotation = Pattern
874 .compile("\\n#=GC SS_cons\\s+\\S{14}\\n");
875 Matcher m = noSpacesInRnaSSAnnotation.matcher(stockholmFile);
876 boolean matches = m.find();
877 Assert.assertTrue(matches,
878 "StockholmFile output does not contain expected output (may contain spaces):\n"