2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
33 import java.util.BitSet;
34 import java.util.HashMap;
37 import org.junit.Test;
39 public class StockholmFileTest
42 static String PfamFile = "examples/PF00111_seed.stk",
43 RfamFile = "examples/RF00031_folded.stk";
46 public void pfamFileIO() throws Exception
48 testFileIOwithFormat(new File(PfamFile), "STH");
52 public void pfamFileDataExtraction() throws Exception
54 AppletFormatAdapter af = new AppletFormatAdapter();
55 AlignmentI al = af.readFile(PfamFile, af.FILE,
56 new IdentifyFile().Identify(PfamFile, af.FILE));
58 for (SequenceI sq : al.getSequences())
60 if (sq.getPDBId() != null)
62 numpdb += sq.getPDBId().size();
66 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
71 public void rfamFileIO() throws Exception
73 testFileIOwithFormat(new File(RfamFile), "STH");
77 * test alignment data in given file can be imported, exported and reimported
81 * - source datafile (IdentifyFile.identify() should work with it)
83 * - label for IO class used to write and read back in the data from
86 public static void testFileIOwithFormat(File f, String ioformat)
88 System.out.println("Reading file: " + f);
89 String ff = f.getPath();
92 AppletFormatAdapter rf = new AppletFormatAdapter();
94 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
95 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
97 assertNotNull("Couldn't read supplied alignment data.", al);
99 // make sure dataset is initialised ? not sure about this
100 for (int i = 0; i < al.getSequencesArray().length; ++i)
102 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
104 String outputfile = rf.formatSequences(ioformat, al, true);
105 System.out.println("Output file in '" + ioformat + "':\n"
106 + outputfile + "\n<<EOF\n");
107 // test for consistency in io
108 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
109 AppletFormatAdapter.PASTE, ioformat);
110 assertNotNull("Couldn't parse reimported alignment data.", al_input);
112 String identifyoutput = new IdentifyFile().Identify(outputfile,
113 AppletFormatAdapter.PASTE);
114 assertNotNull("Identify routine failed for outputformat " + ioformat,
117 "Identify routine could not recognise output generated by '"
118 + ioformat + "' writer",
119 ioformat.equals(identifyoutput));
120 testAlignmentEquivalence(al, al_input);
121 } catch (Exception e)
124 assertTrue("Couln't format the alignment for output file.", false);
129 * assert alignment equivalence
134 * 'secondary' or generated alignment from some datapreserving
137 public static void testAlignmentEquivalence(AlignmentI al,
140 assertNotNull("Original alignment was null", al);
141 assertNotNull("Generated alignment was null", al_input);
144 "Alignment dimension mismatch: originl contains "
145 + al.getHeight() + " and generated has "
146 + al_input.getHeight() + " sequences; original has "
147 + al.getWidth() + " and generated has "
148 + al_input.getWidth() + " columns.",
149 al.getHeight() == al_input.getHeight()
150 && al.getWidth() == al_input.getWidth());
152 // check Alignment annotation
153 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
154 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
156 // note - at moment we do not distinguish between alignment without any
157 // annotation rows and alignment with no annotation row vector
158 // we might want to revise this in future
159 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
160 : aa_original.length);
161 Map<Integer, java.util.BitSet> orig_groups = new HashMap<Integer, java.util.BitSet>(), new_groups = new HashMap<Integer, java.util.BitSet>();
163 if (aa_new != null && aa_original != null)
165 for (int i = 0; i < aa_original.length; i++)
167 if (aa_new.length > i)
169 assertTrue("Different alignment annotation at position " + i,
170 equalss(aa_original[i], aa_new[i]));
171 // compare graphGroup or graph properties - needed to verify JAL-1299
172 assertTrue("Graph type not identical.",
173 aa_original[i].graph == aa_new[i].graph);
174 assertTrue("Visibility not identical.",
175 aa_original[i].visible == aa_new[i].visible);
177 "Threshold line not identical.",
178 aa_original[i].threshold == null ? aa_new[i].threshold == null
179 : aa_original[i].threshold
180 .equals(aa_new[i].threshold));
181 // graphGroup may differ, but pattern should be the same
182 Integer o_ggrp = new Integer(aa_original[i].graphGroup + 2), n_ggrp = new Integer(
183 aa_new[i].graphGroup + 2);
184 BitSet orig_g = orig_groups.get(o_ggrp), new_g = new_groups
188 orig_groups.put(o_ggrp, orig_g = new BitSet());
192 new_groups.put(n_ggrp, new_g = new BitSet());
194 assertTrue("Graph Group pattern differs at annotation " + i,
195 orig_g.equals(new_g));
201 System.err.println("No matching annotation row for "
202 + aa_original[i].toString());
207 "Generated and imported alignment have different annotation sets ("
208 + aa_new_size + " != " + aa_original_size + ")",
209 aa_new_size == aa_original_size);
211 // check sequences, annotation and features
212 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
213 seq_original = al.getSequencesArray();
214 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
215 seq_new = al_input.getSequencesArray();
216 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
217 AlignmentAnnotation annot_original, annot_new;
219 for (int i = 0; i < al.getSequencesArray().length; i++)
221 String name = seq_original[i].getName();
222 int start = seq_original[i].getStart();
223 int end = seq_original[i].getEnd();
224 System.out.println("Check sequence: " + name + "/" + start + "-"
227 // search equal sequence
228 for (int in = 0; in < al_input.getSequencesArray().length; in++)
230 if (name.equals(seq_new[in].getName())
231 && start == seq_new[in].getStart()
232 && end == seq_new[in].getEnd())
234 String ss_original = seq_original[i].getSequenceAsString();
235 String ss_new = seq_new[in].getSequenceAsString();
236 assertTrue("The sequences " + name + "/" + start + "-" + end
237 + " are not equal", ss_original.equals(ss_new));
240 "Sequence Features were not equivalent",
241 (seq_original[i].getSequenceFeatures() == null && seq_new[in]
242 .getSequenceFeatures() == null)
243 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
244 .getSequenceFeatures() != null));
245 // compare sequence features
246 if (seq_original[i].getSequenceFeatures() != null
247 && seq_new[in].getSequenceFeatures() != null)
249 System.out.println("There are feature!!!");
250 sequenceFeatures_original = new SequenceFeature[seq_original[i]
251 .getSequenceFeatures().length];
252 sequenceFeatures_original = seq_original[i]
253 .getSequenceFeatures();
254 sequenceFeatures_new = new SequenceFeature[seq_new[in]
255 .getSequenceFeatures().length];
256 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
258 assertTrue("different number of features", seq_original[i]
259 .getSequenceFeatures().length == seq_new[in]
260 .getSequenceFeatures().length);
262 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
264 assertTrue("Different features",
265 sequenceFeatures_original[feat]
266 .equals(sequenceFeatures_new[feat]));
269 // compare alignment annotation
270 if (al.getSequenceAt(i).getAnnotation() != null
271 && al_input.getSequenceAt(in).getAnnotation() != null)
273 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
275 if (al.getSequenceAt(i).getAnnotation()[j] != null
276 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
278 annot_original = al.getSequenceAt(i).getAnnotation()[j];
279 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
280 assertTrue("Different annotation elements",
281 equalss(annot_original, annot_new));
285 else if (al.getSequenceAt(i).getAnnotation() == null
286 && al_input.getSequenceAt(in).getAnnotation() == null)
288 System.out.println("No annotations");
290 else if (al.getSequenceAt(i).getAnnotation() != null
291 && al_input.getSequenceAt(in).getAnnotation() == null)
293 assertTrue("Annotations differed between sequences ("
294 + al.getSequenceAt(i).getName() + ") and ("
295 + al_input.getSequenceAt(i).getName() + ")", false);
304 * compare annotations
306 private static boolean equalss(AlignmentAnnotation annot_or,
307 AlignmentAnnotation annot_new)
309 if (annot_or.annotations.length != annot_new.annotations.length)
311 System.err.println("Different lengths for annotation row elements: "
312 + annot_or.annotations.length + "!="
313 + annot_new.annotations.length);
316 for (int i = 0; i < annot_or.annotations.length; i++)
318 Annotation an_or = annot_or.annotations[i], an_new = annot_new.annotations[i];
319 if (an_or != null && an_new != null)
321 if (!an_or.displayCharacter.trim().equals(
322 an_new.displayCharacter.trim())
323 || !("" + an_or.secondaryStructure).trim().equals(
324 ("" + an_new.secondaryStructure).trim())
325 || (an_or.description != an_new.description && (an_or.description == null
326 || an_new.description == null || !an_or.description
327 .equals(an_new.description))))
329 System.err.println("Annotation Element Mismatch\nElement " + i
330 + " in original: " + annot_or.annotations[i].toString()
331 + "\nElement " + i + " in new: "
332 + annot_new.annotations[i].toString());
336 else if (annot_or.annotations[i] == null
337 && annot_new.annotations[i] == null)
343 System.err.println("Annotation Element Mismatch\nElement "
346 + (annot_or.annotations[i] == null ? "is null"
347 : annot_or.annotations[i].toString())
351 + (annot_new.annotations[i] == null ? "is null"
352 : annot_new.annotations[i].toString()));