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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
25 import static org.testng.AssertJUnit.assertNull;
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;
40 import java.io.IOException;
41 import java.util.Arrays;
42 import java.util.BitSet;
43 import java.util.HashMap;
44 import java.util.List;
47 import org.testng.Assert;
48 import org.testng.annotations.BeforeClass;
49 import org.testng.annotations.Test;
51 public class StockholmFileTest
54 @BeforeClass(alwaysRun = true)
55 public void setUpJvOptionPane()
57 JvOptionPane.setInteractiveMode(false);
58 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
61 static String PfamFile = "examples/PF00111_seed.stk",
62 RfamFile = "examples/RF00031_folded.stk";
64 @Test(groups = { "Functional" })
65 public void pfamFileIO() throws Exception
67 testFileIOwithFormat(new File(PfamFile), FileFormat.Stockholm, -1, 0,
71 @Test(groups = { "Functional" })
72 public void pfamFileDataExtraction() throws Exception
74 AppletFormatAdapter af = new AppletFormatAdapter();
75 AlignmentI al = af.readFile(PfamFile, DataSourceType.FILE,
76 new IdentifyFile().identify(PfamFile, DataSourceType.FILE));
78 for (SequenceI sq : al.getSequences())
80 if (sq.getAllPDBEntries() != null)
82 numpdb += sq.getAllPDBEntries().size();
86 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
90 @Test(groups = { "Functional" })
91 public void rfamFileIO() throws Exception
93 testFileIOwithFormat(new File(RfamFile), FileFormat.Stockholm, 2, 1,
98 * test alignment data in given file can be imported, exported and reimported
102 * - source datafile (IdentifyFile.identify() should work with it)
104 * - label for IO class used to write and read back in the data from
106 * @param ignoreFeatures
107 * @param ignoreRowVisibility
108 * @param allowNullAnnotations
111 public static void testFileIOwithFormat(File f, FileFormatI ioformat,
112 int naliannot, int nminseqann, boolean ignoreFeatures,
113 boolean ignoreRowVisibility, boolean allowNullAnnotations)
115 System.out.println("Reading file: " + f);
116 String ff = f.getPath();
119 AppletFormatAdapter rf = new AppletFormatAdapter();
121 AlignmentI al = rf.readFile(ff, DataSourceType.FILE,
122 new IdentifyFile().identify(ff, DataSourceType.FILE));
124 assertNotNull("Couldn't read supplied alignment data.", al);
126 // make sure dataset is initialised ? not sure about this
127 for (int i = 0; i < al.getSequencesArray().length; ++i)
129 al.getSequenceAt(i).createDatasetSequence();
131 String outputfile = rf.formatSequences(ioformat, al, true);
132 System.out.println("Output file in '" + ioformat + "':\n"
133 + outputfile + "\n<<EOF\n");
134 // test for consistency in io
135 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
136 DataSourceType.PASTE, ioformat);
137 assertNotNull("Couldn't parse reimported alignment data.", al_input);
139 FileFormatI identifyoutput = new IdentifyFile().identify(outputfile,
140 DataSourceType.PASTE);
141 assertNotNull("Identify routine failed for outputformat " + ioformat,
144 "Identify routine could not recognise output generated by '"
145 + ioformat + "' writer",
146 ioformat.equals(identifyoutput));
147 testAlignmentEquivalence(al, al_input, ignoreFeatures,
148 ignoreRowVisibility, allowNullAnnotations);
149 int numaliannot = 0, numsqswithali = 0;
150 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
152 if (ala.sequenceRef == null)
163 assertEquals("Number of alignment annotations", naliannot,
168 "Number of sequence associated annotations wasn't at least "
169 + nminseqann, numsqswithali >= nminseqann);
171 } catch (Exception e)
174 assertTrue("Couln't format the alignment for output file.", false);
179 * assert alignment equivalence
184 * 'secondary' or generated alignment from some datapreserving
186 * @param ignoreFeatures
187 * when true, differences in sequence feature annotation are ignored
189 public static void testAlignmentEquivalence(AlignmentI al,
190 AlignmentI al_input, boolean ignoreFeatures)
192 testAlignmentEquivalence(al, al_input, ignoreFeatures, false, false);
196 * assert alignment equivalence - uses special comparators for RNA structure
202 * 'secondary' or generated alignment from some datapreserving
204 * @param ignoreFeatures
205 * when true, differences in sequence feature annotation are ignored
207 * @param ignoreRowVisibility
208 * when true, do not fail if there are differences in the visibility
210 * @param allowNullAnnotation
211 * when true, positions in alignment annotation that are null will be
212 * considered equal to positions containing annotation where
213 * Annotation.isWhitespace() returns true.
216 public static void testAlignmentEquivalence(AlignmentI al,
217 AlignmentI al_input, boolean ignoreFeatures,
218 boolean ignoreRowVisibility, boolean allowNullAnnotation)
220 assertNotNull("Original alignment was null", al);
221 assertNotNull("Generated alignment was null", al_input);
223 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
224 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
225 + "x" + al_input.getWidth(),
226 al.getHeight() == al_input.getHeight()
227 && al.getWidth() == al_input.getWidth());
229 // check Alignment annotation
230 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
231 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
233 // note - at moment we do not distinguish between alignment without any
234 // annotation rows and alignment with no annotation row vector
235 // we might want to revise this in future
236 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
237 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
238 Map<Integer, BitSet> orig_groups = new HashMap<>();
239 Map<Integer, BitSet> new_groups = new HashMap<>();
241 if (aa_new != null && aa_original != null)
243 for (int i = 0; i < aa_original.length; i++)
245 if (aa_new.length > i)
247 assertEqualSecondaryStructure(
248 "Different alignment annotation at position " + i,
249 aa_original[i], aa_new[i], allowNullAnnotation);
250 // compare graphGroup or graph properties - needed to verify JAL-1299
251 assertEquals("Graph type not identical.", aa_original[i].graph,
253 if (!ignoreRowVisibility)
255 assertEquals("Visibility not identical.",
256 aa_original[i].visible,
259 assertEquals("Threshold line not identical.",
260 aa_original[i].threshold, aa_new[i].threshold);
261 // graphGroup may differ, but pattern should be the same
262 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2);
263 Integer n_ggrp = new Integer(aa_new[i].graphGroup + 2);
264 BitSet orig_g = orig_groups.get(o_ggrp);
265 BitSet new_g = new_groups.get(n_ggrp);
268 orig_groups.put(o_ggrp, orig_g = new BitSet());
272 new_groups.put(n_ggrp, new_g = new BitSet());
274 assertEquals("Graph Group pattern differs at annotation " + i,
281 System.err.println("No matching annotation row for "
282 + aa_original[i].toString());
287 "Generated and imported alignment have different annotation sets",
288 aa_original_size, aa_new_size);
290 // check sequences, annotation and features
291 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
292 seq_original = al.getSequencesArray();
293 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
294 seq_new = al_input.getSequencesArray();
295 List<SequenceFeature> sequenceFeatures_original;
296 List<SequenceFeature> sequenceFeatures_new;
297 AlignmentAnnotation annot_original, annot_new;
299 for (int i = 0; i < al.getSequencesArray().length; i++)
301 String name = seq_original[i].getName();
302 int start = seq_original[i].getStart();
303 int end = seq_original[i].getEnd();
304 System.out.println("Check sequence: " + name + "/" + start + "-"
307 // search equal sequence
308 for (int in = 0; in < al_input.getSequencesArray().length; in++)
310 if (name.equals(seq_new[in].getName())
311 && start == seq_new[in].getStart()
312 && end == seq_new[in].getEnd())
314 String ss_original = seq_original[i].getSequenceAsString();
315 String ss_new = seq_new[in].getSequenceAsString();
316 assertEquals("The sequences " + name + "/" + start + "-" + end
317 + " are not equal", ss_original, ss_new);
320 "Sequence Features were not equivalent"
321 + (ignoreFeatures ? " ignoring." : ""),
323 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
324 .getSequenceFeatures() == null)
325 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
326 .getSequenceFeatures() != null));
327 // compare sequence features
328 if (seq_original[i].getSequenceFeatures() != null
329 && seq_new[in].getSequenceFeatures() != null)
331 System.out.println("There are feature!!!");
332 sequenceFeatures_original = seq_original[i]
333 .getSequenceFeatures();
334 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
336 assertEquals("different number of features", seq_original[i]
337 .getSequenceFeatures().size(), seq_new[in]
338 .getSequenceFeatures().size());
340 for (int feat = 0; feat < seq_original[i].getSequenceFeatures()
343 assertEquals("Different features",
344 sequenceFeatures_original.get(feat),
345 sequenceFeatures_new.get(feat));
348 // compare alignment annotation
349 if (al.getSequenceAt(i).getAnnotation() != null
350 && al_input.getSequenceAt(in).getAnnotation() != null)
352 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
354 if (al.getSequenceAt(i).getAnnotation()[j] != null
355 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
357 annot_original = al.getSequenceAt(i).getAnnotation()[j];
358 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
359 assertEqualSecondaryStructure(
360 "Different annotation elements", annot_original,
361 annot_new, allowNullAnnotation);
365 else if (al.getSequenceAt(i).getAnnotation() == null
366 && al_input.getSequenceAt(in).getAnnotation() == null)
368 System.out.println("No annotations");
370 else if (al.getSequenceAt(i).getAnnotation() != null
371 && al_input.getSequenceAt(in).getAnnotation() == null)
373 fail("Annotations differed between sequences ("
374 + al.getSequenceAt(i).getName() + ") and ("
375 + al_input.getSequenceAt(i).getName() + ")");
384 * compare two annotation rows, with special support for secondary structure
385 * comparison. With RNA, only the value and the secondaryStructure symbols are
386 * compared, displayCharacter and description are ignored. Annotations where
387 * Annotation.isWhitespace() is true are always considered equal.
390 * - not actually used yet..
392 * - the original annotation
394 * - the one compared to the original annotation
395 * @param allowNullEquivalence
396 * when true, positions in alignment annotation that are null will be
397 * considered equal to non-null positions for which
398 * Annotation.isWhitespace() is true.
400 private static void assertEqualSecondaryStructure(String message,
401 AlignmentAnnotation annot_or, AlignmentAnnotation annot_new,
402 boolean allowNullEqivalence)
404 // TODO: test to cover this assert behaves correctly for all allowed
405 // variations of secondary structure annotation row equivalence
406 if (annot_or.annotations.length != annot_new.annotations.length)
408 fail("Different lengths for annotation row elements: "
409 + annot_or.annotations.length + "!="
410 + annot_new.annotations.length);
412 boolean isRna = annot_or.isRNA();
413 assertTrue("Expected " + (isRna ? " valid RNA " : " no RNA ")
414 + " secondary structure in the row.",
415 isRna == annot_new.isRNA());
416 for (int i = 0; i < annot_or.annotations.length; i++)
418 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
419 if (an_or != null && an_new != null)
424 if (an_or.secondaryStructure != an_new.secondaryStructure
425 || ((Float.isNaN(an_or.value) != Float
426 .isNaN(an_new.value)) || an_or.value != an_new.value))
428 fail("Different RNA secondary structure at column " + i
429 + " expected: [" + annot_or.annotations[i].toString()
430 + "] but got: [" + annot_new.annotations[i].toString()
436 // not RNA secondary structure, so expect all elements to match...
437 if ((an_or.isWhitespace() != an_new.isWhitespace())
438 || !an_or.displayCharacter.trim().equals(
439 an_new.displayCharacter.trim())
440 || !("" + an_or.secondaryStructure).trim().equals(
441 ("" + an_new.secondaryStructure).trim())
442 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
443 .trim().length() == 0)
444 || (an_new.description == null && an_or.description
445 .trim().length() == 0) || an_or.description
446 .trim().equals(an_new.description.trim())))
447 || !((Float.isNaN(an_or.value) && Float
448 .isNaN(an_new.value)) || an_or.value == an_new.value))
450 fail("Annotation Element Mismatch\nElement " + i
451 + " in original: " + annot_or.annotations[i].toString()
452 + "\nElement " + i + " in new: "
453 + annot_new.annotations[i].toString());
457 else if (annot_or.annotations[i] == null
458 && annot_new.annotations[i] == null)
464 if (allowNullEqivalence)
466 if (an_or != null && an_or.isWhitespace())
471 if (an_new != null && an_new.isWhitespace())
476 // need also to test for null in one, non-SS annotation in other...
477 fail("Annotation Element Mismatch\nElement " + i + " in original: "
478 + (an_or == null ? "is null" : an_or.toString())
479 + "\nElement " + i + " in new: "
480 + (an_new == null ? "is null" : an_new.toString()));
486 * @see assertEqualSecondaryStructure - test if two secondary structure
487 * annotations are not equal
491 * @param allowNullEquivalence
493 public static void assertNotEqualSecondaryStructure(String message,
494 AlignmentAnnotation an_orig, AlignmentAnnotation an_new,
495 boolean allowNullEquivalence)
497 boolean thrown = false;
500 assertEqualSecondaryStructure("", an_orig, an_new,
501 allowNullEquivalence);
502 } catch (AssertionError af)
508 fail("Expected difference for [" + an_orig + "] and [" + an_new + "]");
511 private AlignmentAnnotation makeAnnot(Annotation ae)
513 return new AlignmentAnnotation("label", "description", new Annotation[]
517 @Test(groups={"Functional"})
518 public void testAnnotationEquivalence()
520 AlignmentAnnotation one = makeAnnot(new Annotation("", "", ' ', 1));
521 AlignmentAnnotation anotherOne = makeAnnot(new Annotation("", "", ' ',
523 AlignmentAnnotation sheet = makeAnnot(new Annotation("","",'E',0f));
524 AlignmentAnnotation anotherSheet = makeAnnot(new Annotation("","",'E',0f));
525 AlignmentAnnotation sheetWithLabel = makeAnnot(new Annotation("1", "",
527 AlignmentAnnotation anotherSheetWithLabel = makeAnnot(new Annotation(
529 AlignmentAnnotation rnaNoDC = makeAnnot(new Annotation("","",'<',0f));
530 AlignmentAnnotation anotherRnaNoDC = makeAnnot(new Annotation("","",'<',0f));
531 AlignmentAnnotation rnaWithDC = makeAnnot(new Annotation("B", "", '<',
533 AlignmentAnnotation anotherRnaWithDC = makeAnnot(new Annotation("B",
536 // check self equivalence
537 for (boolean allowNull : new boolean[] { true, false })
539 assertEqualSecondaryStructure("Should be equal", one, anotherOne,
541 assertEqualSecondaryStructure("Should be equal", sheet, anotherSheet,
543 assertEqualSecondaryStructure("Should be equal", sheetWithLabel,
544 anotherSheetWithLabel, allowNull);
545 assertEqualSecondaryStructure("Should be equal", rnaNoDC,
546 anotherRnaNoDC, allowNull);
547 assertEqualSecondaryStructure("Should be equal", rnaWithDC,
548 anotherRnaWithDC, allowNull);
549 // display character doesn't matter for RNA structure (for 2.10.2)
550 assertEqualSecondaryStructure("Should be equal", rnaWithDC, rnaNoDC,
552 assertEqualSecondaryStructure("Should be equal", rnaNoDC, rnaWithDC,
556 // verify others are different
557 List<AlignmentAnnotation> aaSet = Arrays.asList(one, sheet,
558 sheetWithLabel, rnaWithDC);
559 for (int p = 0; p < aaSet.size(); p++)
561 for (int q = 0; q < aaSet.size(); q++)
565 assertNotEqualSecondaryStructure("Should be different",
566 aaSet.get(p), aaSet.get(q), false);
570 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
571 aaSet.get(q), false);
572 assertEqualSecondaryStructure("Should be same", aaSet.get(p),
574 assertNotEqualSecondaryStructure(
575 "Should be different to empty anot", aaSet.get(p),
576 makeAnnot(Annotation.EMPTY_ANNOTATION), false);
577 assertNotEqualSecondaryStructure(
578 "Should be different to empty annot",
579 makeAnnot(Annotation.EMPTY_ANNOTATION), aaSet.get(q),
581 assertNotEqualSecondaryStructure("Should be different to null",
582 aaSet.get(p), makeAnnot(null), false);
583 assertNotEqualSecondaryStructure("Should be different to null",
584 makeAnnot(null), aaSet.get(q), true);
593 String aliFile = ">Dm\nAAACCCUUUUACACACGGGAAAGGG";
594 String annFile = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
595 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
596 + "(|(|(|(|, .|, .|, .|, .|)|)|)|)|\t0.0\nROWPROPERTIES\t"
597 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
599 String annFileCurlyWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
600 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
601 + "(|(|(|(||{|{||{|{||)|)|)|)||}|}|}|}|\t0.0\nROWPROPERTIES\t"
602 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
603 String annFileFullWuss = "JALVIEW_ANNOTATION\n# Created: Thu May 04 11:16:52 BST 2017\n\n"
604 + "SEQUENCE_REF\tDm\nNO_GRAPH\tsecondary structure\tsecondary structure\t"
605 + "(|(|(|(||{|{||[|[||)|)|)|)||}|}|]|]|\t0.0\nROWPROPERTIES\t"
606 + "secondary structure\tscaletofit=true\tshowalllabs=true\tcentrelabs=false";
608 @Test(groups = { "Functional" })
609 public void secondaryStructureForRNASequence() throws Exception
611 roundTripSSForRNA(aliFile, annFile);
614 @Test(groups = { "Functional" })
615 public void curlyWUSSsecondaryStructureForRNASequence() throws Exception
617 roundTripSSForRNA(aliFile, annFileCurlyWuss);
620 @Test(groups = { "Functional" })
621 public void fullWUSSsecondaryStructureForRNASequence() throws Exception
623 roundTripSSForRNA(aliFile, annFileFullWuss);
626 @Test(groups = { "Functional" })
627 public void detectWussBrackets()
629 for (char ch : new char[] { '{', '}', '[', ']', '(', ')', '<', '>' })
631 Assert.assertTrue(StockholmFile.DETECT_BRACKETS.matchAt("" + ch, 0),
632 "Didn't recognise " + ch + " as a WUSS bracket");
634 for (char ch : new char[] { '@', '!', 'V', 'Q', '*', ' ', '-', '.' })
636 Assert.assertFalse(StockholmFile.DETECT_BRACKETS.matchAt("" + ch, 0),
637 "Shouldn't recognise " + ch + " as a WUSS bracket");
640 private static void roundTripSSForRNA(String aliFile, String annFile)
643 AlignmentI al = new AppletFormatAdapter().readFile(aliFile,
644 DataSourceType.PASTE, jalview.io.FileFormat.Fasta);
645 AnnotationFile aaf = new AnnotationFile();
646 aaf.readAnnotationFile(al, annFile, DataSourceType.PASTE);
647 al.getAlignmentAnnotation()[0].visible = true;
649 // TODO: create a better 'save as <format>' pattern
650 StockholmFile sf = new StockholmFile(al);
652 String stockholmFile = sf.print(al.getSequencesArray(), true);
654 AlignmentI newAl = new AppletFormatAdapter().readFile(stockholmFile,
655 DataSourceType.PASTE, jalview.io.FileFormat.Stockholm);
656 // AlignmentUtils.showOrHideSequenceAnnotations(newAl.getViewport()
657 // .getAlignment(), Arrays.asList("Secondary Structure"), newAl
658 // .getViewport().getAlignment().getSequences(), true, true);
659 testAlignmentEquivalence(al, newAl, true, true, true);
663 @Test(groups = { "Functional" })
664 public void testTypeToDescription()
666 assertEquals("Secondary Structure",
667 StockholmFile.typeToDescription("SS"));
668 assertEquals("Surface Accessibility",
669 StockholmFile.typeToDescription("SA"));
670 assertEquals("transmembrane", StockholmFile.typeToDescription("TM"));
671 assertEquals("Posterior Probability",
672 StockholmFile.typeToDescription("PP"));
673 assertEquals("ligand binding", StockholmFile.typeToDescription("LI"));
674 assertEquals("active site", StockholmFile.typeToDescription("AS"));
675 assertEquals("intron", StockholmFile.typeToDescription("IN"));
676 assertEquals("interacting residue",
677 StockholmFile.typeToDescription("IR"));
678 assertEquals("accession", StockholmFile.typeToDescription("AC"));
679 assertEquals("organism", StockholmFile.typeToDescription("OS"));
680 assertEquals("class", StockholmFile.typeToDescription("CL"));
681 assertEquals("description", StockholmFile.typeToDescription("DE"));
682 assertEquals("reference", StockholmFile.typeToDescription("DR"));
683 assertEquals("look", StockholmFile.typeToDescription("LO"));
684 assertEquals("Reference Positions",
685 StockholmFile.typeToDescription("RF"));
688 assertEquals("Rf", StockholmFile.typeToDescription("Rf"));
689 assertEquals("junk", StockholmFile.typeToDescription("junk"));
690 assertEquals("", StockholmFile.typeToDescription(""));
691 assertNull(StockholmFile.typeToDescription(null));
694 @Test(groups = { "Functional" })
695 public void testDescriptionToType()
698 StockholmFile.descriptionToType("Secondary Structure"));
700 StockholmFile.descriptionToType("Surface Accessibility"));
701 assertEquals("TM", StockholmFile.descriptionToType("transmembrane"));
703 // test is not case-sensitive:
705 StockholmFile.descriptionToType("secondary structure"));
707 // test is white-space sensitive:
708 assertNull(StockholmFile.descriptionToType("secondary structure "));
710 assertNull(StockholmFile.descriptionToType("any old junk"));
711 assertNull(StockholmFile.descriptionToType(""));
712 assertNull(StockholmFile.descriptionToType(null));
715 @Test(groups = { "Functional" })
716 public void testPrint()
718 SequenceI seq1 = new Sequence("seq1", "LKMF-RS-Q");
719 SequenceI seq2 = new Sequence("seq2/10-15", "RRS-LIP-");
720 SequenceI[] seqs = new SequenceI[] { seq1, seq2 };
721 AlignmentI al = new Alignment(seqs);
723 StockholmFile testee = new StockholmFile(al);
726 * basic output (sequences only):
727 * sequence ids are padded with 9 spaces more than the widest id
729 String output = testee.print(seqs, true);
730 String expected = "# STOCKHOLM 1.0\n" + "seq1/1-7 LKMF-RS-Q\n"
731 + "seq2/10-15 RRS-LIP-\n//\n";
732 assertEquals(expected, output);
737 seq1.addDBRef(new DBRefEntry("PFAM", "1", "PF00111"));
738 seq1.addDBRef(new DBRefEntry("UNIPROT", "1", "P83527"));
739 seq2.addDBRef(new DBRefEntry("RFAM", "1", "AY119185.1"));
740 seq2.addDBRef(new DBRefEntry("EMBL", "1", "AF125575"));
741 output = testee.print(seqs, true);
742 // PFAM and RFAM dbrefs should be output as AC, others as DR
743 expected = "# STOCKHOLM 1.0\n" + "#=GS seq1/1-7 AC PF00111\n"
744 + "#=GS seq1/1-7 DR UNIPROT ; P83527\n"
745 + "#=GS seq2/10-15 AC AY119185.1\n"
746 + "#=GS seq2/10-15 DR EMBL ; AF125575\n"
747 + "seq1/1-7 LKMF-RS-Q\n"
748 + "seq2/10-15 RRS-LIP-\n//\n";
749 assertEquals(expected, output);
752 * add some sequence and alignment annotation
754 Annotation[] anns = new Annotation[5];
755 for (int i = 0; i < anns.length; i++)
757 anns[i] = new Annotation(String.valueOf((char) ('B' + i)),
759 (char) ('C' + i), i + 3);
762 // expect "secondary structure" to be output as #=GR seqid SS
763 // using the secondary structure character (CDEFG) not display char (BCDEF)
764 AlignmentAnnotation aa1 = new AlignmentAnnotation("secondary structure",
766 aa1.sequenceRef = seq1;
767 seq1.addAlignmentAnnotation(aa1);
768 al.addAnnotation(aa1);
770 // "sec structure" should not be output as no corresponding feature id
771 AlignmentAnnotation aa2 = new AlignmentAnnotation("sec structure",
773 aa2.sequenceRef = seq2;
774 seq2.addAlignmentAnnotation(aa2);
775 al.addAnnotation(aa2);
777 // alignment annotation for Reference Positions: output as #=GC RF
778 AlignmentAnnotation aa3 = new AlignmentAnnotation("reference positions",
780 al.addAnnotation(aa3);
782 // 'seq' annotation: output as seq_cons
783 AlignmentAnnotation aa4 = new AlignmentAnnotation("seq", "seqdesc",
785 al.addAnnotation(aa4);
787 // 'intron' annotation: output as IN_cons
788 AlignmentAnnotation aa5 = new AlignmentAnnotation("intron",
790 al.addAnnotation(aa5);
792 // 'binding site' annotation: output as binding_site
793 AlignmentAnnotation aa6 = new AlignmentAnnotation("binding site",
794 "bindingdesc", anns);
795 al.addAnnotation(aa6);
797 // 'autocalc' annotation should not be output
798 AlignmentAnnotation aa7 = new AlignmentAnnotation("Consensus",
799 "consensusdesc", anns);
800 aa7.autoCalculated = true;
801 al.addAnnotation(aa7);
803 // hidden annotation should not be output
804 AlignmentAnnotation aa8 = new AlignmentAnnotation("domains",
807 al.addAnnotation(aa8);
809 output = testee.print(seqs, true);
813 + "#=GS seq1/1-7 AC PF00111\n"
814 + "#=GS seq1/1-7 DR UNIPROT ; P83527\n"
815 + "#=GS seq2/10-15 AC AY119185.1\n"
816 + "#=GS seq2/10-15 DR EMBL ; AF125575\n"
817 + "#=GR seq1/1-7 SS CDEFG\n"
818 + "seq1/1-7 LKMF-RS-Q\n"
819 + "seq2/10-15 RRS-LIP-\n"
820 + "#=GC RF BCDEF\n" + "#=GC seq_cons BCDEF\n"
821 + "#=GC IN_cons BCDEF\n" + "#=GC binding_site BCDEF\n"
824 assertEquals(expected, output);
827 @Test(groups = "Functional")
828 public void testOutputCharacter()
830 SequenceI seq = new Sequence("seq", "abc--def-");
832 Annotation[] ann = new Annotation[8];
833 ann[1] = new Annotation("Z", "desc", 'E', 1f);
834 ann[2] = new Annotation("Q", "desc", ' ', 1f);
835 ann[4] = new Annotation("", "desc", 'E', 1f);
836 ann[6] = new Annotation("ZH", "desc", 'E', 1f);
839 * null annotation in column (not Secondary Structure annotation)
840 * should answer sequence character, or '-' if null sequence
843 StockholmFile.getAnnotationCharacter("RF", 0, ann[0], null));
845 StockholmFile.getAnnotationCharacter("RF", 5, ann[5], seq));
847 StockholmFile.getAnnotationCharacter("RF", 8, null, seq));
850 * null annotation in column (SS annotation) should answer underscore
853 StockholmFile.getAnnotationCharacter("SS", 0, ann[0], seq));
856 * SS secondary structure symbol
859 StockholmFile.getAnnotationCharacter("SS", 1, ann[1], seq));
862 * no SS symbol, use label instead
865 StockholmFile.getAnnotationCharacter("SS", 2, ann[2], seq));
868 * SS with 2 character label - second character overrides SS symbol
871 StockholmFile.getAnnotationCharacter("SS", 6, ann[6], seq));
874 * empty display character, not SS - answers '.'
877 StockholmFile.getAnnotationCharacter("RF", 4, ann[4], seq));
881 * Test to verify that gaps are input/output as underscore in STO annotation
883 * @throws IOException
885 @Test(groups = "Functional")
886 public void testRoundtripWithGaps() throws IOException
889 * small extract from RF00031_folded.stk
893 "# STOCKHOLM 1.0\n" +
894 "#=GR B.taurus.4 SS .._((.))_\n" +
895 "B.taurus.4 AC.UGCGU.\n" +
896 "#=GR B.taurus.5 SS ..((_._))\n" +
897 "B.taurus.5 ACUU.G.CG\n" +
900 StockholmFile parser = new StockholmFile(stoData, DataSourceType.PASTE);
901 SequenceI[] seqs = parser.getSeqsAsArray();
902 assertEquals(2, seqs.length);
905 * B.taurus.4 has a trailing gap
906 * rendered as underscore in Stockholm annotation
908 assertEquals("AC.UGCGU.", seqs[0].getSequenceAsString());
909 AlignmentAnnotation[] anns = seqs[0].getAnnotation();
910 assertEquals(1, anns.length);
911 AlignmentAnnotation taurus4SS = anns[0];
912 assertEquals(9, taurus4SS.annotations.length);
913 assertEquals(" .", taurus4SS.annotations[0].displayCharacter);
914 assertNull(taurus4SS.annotations[2]); // gapped position
915 assertNull(taurus4SS.annotations[8]); // gapped position
916 assertEquals('(', taurus4SS.annotations[3].secondaryStructure);
917 assertEquals("(", taurus4SS.annotations[3].displayCharacter);
918 assertEquals(')', taurus4SS.annotations[7].secondaryStructure);
921 * output as Stockholm and verify it matches the original input
922 * (gaps output as underscore in annotation lines)
923 * note: roundtrip test works with the input lines ordered as above;
924 * can also parse in other orders, but then input doesn't match output
926 AlignmentFileWriterI afile = FileFormat.Stockholm
927 .getWriter(new Alignment(seqs));
928 String output = afile.print(seqs, false);
929 assertEquals(stoData, output);