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.assertTrue;
26 import static org.testng.AssertJUnit.fail;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.Annotation;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceI;
35 import java.util.BitSet;
36 import java.util.HashMap;
39 import org.testng.annotations.Test;
41 public class StockholmFileTest
44 static String PfamFile = "examples/PF00111_seed.stk",
45 RfamFile = "examples/RF00031_folded.stk";
47 @Test(groups = { "Functional" })
48 public void pfamFileIO() throws Exception
50 testFileIOwithFormat(new File(PfamFile), "STH", -1, 0);
53 @Test(groups = { "Functional" })
54 public void pfamFileDataExtraction() throws Exception
56 AppletFormatAdapter af = new AppletFormatAdapter();
57 AlignmentI al = af.readFile(PfamFile, af.FILE,
58 new IdentifyFile().identify(PfamFile, af.FILE));
60 for (SequenceI sq : al.getSequences())
62 if (sq.getAllPDBEntries() != null)
64 numpdb += sq.getAllPDBEntries().size();
68 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
72 @Test(groups = { "Functional" })
73 public void rfamFileIO() throws Exception
75 testFileIOwithFormat(new File(RfamFile), "STH", 2, 1);
79 * test alignment data in given file can be imported, exported and reimported
83 * - source datafile (IdentifyFile.identify() should work with it)
85 * - label for IO class used to write and read back in the data from
89 public static void testFileIOwithFormat(File f, String ioformat,
90 int naliannot, int nminseqann)
92 System.out.println("Reading file: " + f);
93 String ff = f.getPath();
96 AppletFormatAdapter rf = new AppletFormatAdapter();
98 AlignmentI al = rf.readFile(ff, AppletFormatAdapter.FILE,
99 new IdentifyFile().identify(ff, AppletFormatAdapter.FILE));
101 assertNotNull("Couldn't read supplied alignment data.", al);
103 // make sure dataset is initialised ? not sure about this
104 for (int i = 0; i < al.getSequencesArray().length; ++i)
106 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
108 String outputfile = rf.formatSequences(ioformat, al, true);
109 System.out.println("Output file in '" + ioformat + "':\n"
110 + outputfile + "\n<<EOF\n");
111 // test for consistency in io
112 AlignmentI al_input = new AppletFormatAdapter().readFile(outputfile,
113 AppletFormatAdapter.PASTE, ioformat);
114 assertNotNull("Couldn't parse reimported alignment data.", al_input);
116 String identifyoutput = new IdentifyFile().identify(outputfile,
117 AppletFormatAdapter.PASTE);
118 assertNotNull("Identify routine failed for outputformat " + ioformat,
121 "Identify routine could not recognise output generated by '"
122 + ioformat + "' writer",
123 ioformat.equals(identifyoutput));
124 testAlignmentEquivalence(al, al_input, false);
125 int numaliannot = 0, numsqswithali = 0;
126 for (AlignmentAnnotation ala : al_input.getAlignmentAnnotation())
128 if (ala.sequenceRef == null)
139 assertEquals("Number of alignment annotations", naliannot,
144 "Number of sequence associated annotations wasn't at least "
145 + nminseqann, numsqswithali >= nminseqann);
147 } catch (Exception e)
150 assertTrue("Couln't format the alignment for output file.", false);
155 * assert alignment equivalence
160 * 'secondary' or generated alignment from some datapreserving
162 * @param ignoreFeatures
163 * when true, differences in sequence feature annotation are ignored
165 public static void testAlignmentEquivalence(AlignmentI al,
166 AlignmentI al_input, boolean ignoreFeatures)
168 assertNotNull("Original alignment was null", al);
169 assertNotNull("Generated alignment was null", al_input);
171 assertTrue("Alignment dimension mismatch: original: " + al.getHeight()
172 + "x" + al.getWidth() + ", generated: " + al_input.getHeight()
173 + "x" + al_input.getWidth(),
174 al.getHeight() == al_input.getHeight()
175 && al.getWidth() == al_input.getWidth());
177 // check Alignment annotation
178 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
179 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
181 // note - at moment we do not distinguish between alignment without any
182 // annotation rows and alignment with no annotation row vector
183 // we might want to revise this in future
184 int aa_new_size = (aa_new == null ? 0 : aa_new.length);
185 int aa_original_size = (aa_original == null ? 0 : aa_original.length);
186 Map<Integer, BitSet> orig_groups = new HashMap<Integer, BitSet>();
187 Map<Integer, BitSet> new_groups = new HashMap<Integer, BitSet>();
189 if (aa_new != null && aa_original != null)
191 for (int i = 0; i < aa_original.length; i++)
193 if (aa_new.length > i)
195 assertTrue("Different alignment annotation at position " + i,
196 equalss(aa_original[i], aa_new[i]));
197 // compare graphGroup or graph properties - needed to verify JAL-1299
198 assertEquals("Graph type not identical.", aa_original[i].graph,
200 assertEquals("Visibility not identical.", aa_original[i].visible,
202 assertEquals("Threshold line not identical.",
203 aa_original[i].threshold, aa_new[i].threshold);
204 // graphGroup may differ, but pattern should be the same
205 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2);
206 Integer n_ggrp = new Integer(aa_new[i].graphGroup + 2);
207 BitSet orig_g = orig_groups.get(o_ggrp);
208 BitSet new_g = new_groups.get(n_ggrp);
211 orig_groups.put(o_ggrp, orig_g = new BitSet());
215 new_groups.put(n_ggrp, new_g = new BitSet());
217 assertEquals("Graph Group pattern differs at annotation " + i,
224 System.err.println("No matching annotation row for "
225 + aa_original[i].toString());
230 "Generated and imported alignment have different annotation sets",
231 aa_new_size, aa_original_size);
233 // check sequences, annotation and features
234 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
235 seq_original = al.getSequencesArray();
236 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
237 seq_new = al_input.getSequencesArray();
238 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
239 AlignmentAnnotation annot_original, annot_new;
241 for (int i = 0; i < al.getSequencesArray().length; i++)
243 String name = seq_original[i].getName();
244 int start = seq_original[i].getStart();
245 int end = seq_original[i].getEnd();
246 System.out.println("Check sequence: " + name + "/" + start + "-"
249 // search equal sequence
250 for (int in = 0; in < al_input.getSequencesArray().length; in++)
252 if (name.equals(seq_new[in].getName())
253 && start == seq_new[in].getStart()
254 && end == seq_new[in].getEnd())
256 String ss_original = seq_original[i].getSequenceAsString();
257 String ss_new = seq_new[in].getSequenceAsString();
258 assertEquals("The sequences " + name + "/" + start + "-" + end
259 + " are not equal", ss_original, ss_new);
262 "Sequence Features were not equivalent"
263 + (ignoreFeatures ? " ignoring." : ""),
265 || (seq_original[i].getSequenceFeatures() == null && seq_new[in]
266 .getSequenceFeatures() == null)
267 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
268 .getSequenceFeatures() != null));
269 // compare sequence features
270 if (seq_original[i].getSequenceFeatures() != null
271 && seq_new[in].getSequenceFeatures() != null)
273 System.out.println("There are feature!!!");
274 sequenceFeatures_original = new SequenceFeature[seq_original[i]
275 .getSequenceFeatures().length];
276 sequenceFeatures_original = seq_original[i]
277 .getSequenceFeatures();
278 sequenceFeatures_new = new SequenceFeature[seq_new[in]
279 .getSequenceFeatures().length];
280 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
282 assertEquals("different number of features",
283 seq_original[i].getSequenceFeatures().length,
285 .getSequenceFeatures().length);
287 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
289 assertEquals("Different features",
290 sequenceFeatures_original[feat],
291 sequenceFeatures_new[feat]);
294 // compare alignment annotation
295 if (al.getSequenceAt(i).getAnnotation() != null
296 && al_input.getSequenceAt(in).getAnnotation() != null)
298 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
300 if (al.getSequenceAt(i).getAnnotation()[j] != null
301 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
303 annot_original = al.getSequenceAt(i).getAnnotation()[j];
304 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
305 assertTrue("Different annotation elements",
306 equalss(annot_original, annot_new));
310 else if (al.getSequenceAt(i).getAnnotation() == null
311 && al_input.getSequenceAt(in).getAnnotation() == null)
313 System.out.println("No annotations");
315 else if (al.getSequenceAt(i).getAnnotation() != null
316 && al_input.getSequenceAt(in).getAnnotation() == null)
318 fail("Annotations differed between sequences ("
319 + al.getSequenceAt(i).getName() + ") and ("
320 + al_input.getSequenceAt(i).getName() + ")");
329 * compare annotations
331 private static boolean equalss(AlignmentAnnotation annot_or,
332 AlignmentAnnotation annot_new)
334 if (annot_or.annotations.length != annot_new.annotations.length)
336 System.err.println("Different lengths for annotation row elements: "
337 + annot_or.annotations.length + "!="
338 + annot_new.annotations.length);
341 for (int i = 0; i < annot_or.annotations.length; i++)
343 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
344 if (an_or != null && an_new != null)
346 if (!an_or.displayCharacter.trim().equals(
347 an_new.displayCharacter.trim())
348 || !("" + an_or.secondaryStructure).trim().equals(
349 ("" + an_new.secondaryStructure).trim())
350 || (an_or.description != an_new.description && !((an_or.description == null && an_new.description
351 .trim().length() == 0)
352 || (an_new.description == null && an_or.description
353 .trim().length() == 0) || an_or.description
354 .trim().equals(an_new.description.trim()))))
356 System.err.println("Annotation Element Mismatch\nElement " + i
357 + " in original: " + annot_or.annotations[i].toString()
358 + "\nElement " + i + " in new: "
359 + annot_new.annotations[i].toString());
363 else if (annot_or.annotations[i] == null
364 && annot_new.annotations[i] == null)
370 System.err.println("Annotation Element Mismatch\nElement "
373 + (annot_or.annotations[i] == null ? "is null"
374 : annot_or.annotations[i].toString())
378 + (annot_new.annotations[i] == null ? "is null"
379 : annot_new.annotations[i].toString()));