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");
51 public void pfamFileDataExtraction() throws Exception
52 { AppletFormatAdapter af = new AppletFormatAdapter();
53 AlignmentI al = af.readFile(PfamFile, af.FILE,
54 new IdentifyFile().Identify(PfamFile, af.FILE));
56 for (SequenceI sq : al.getSequences())
58 if (sq.getPDBId() != null)
60 numpdb += sq.getPDBId().size();
64 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
69 public void rfamFileIO() throws Exception
71 testFileIOwithFormat(new File(RfamFile), "STH");
75 * test alignment data in given file can be imported, exported and reimported
79 * - source datafile (IdentifyFile.identify() should work with it)
81 * - label for IO class used to write and read back in the data from
84 public static void testFileIOwithFormat(File f, String ioformat)
86 System.out.println("Reading file: " + f);
87 String ff = f.getPath();
90 AppletFormatAdapter rf = new AppletFormatAdapter();
92 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
93 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
95 assertNotNull("Couldn't read supplied alignment data.", al);
97 // make sure dataset is initialised ? not sure about this
98 for (int i = 0; i < al.getSequencesArray().length; ++i)
100 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
102 String outputfile = rf.formatSequences(ioformat, al, true);
103 System.out.println("Output file in '"+ioformat+"':\n"+outputfile+"\n<<EOF\n");
104 // test for consistency in io
105 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
106 AppletFormatAdapter.PASTE, ioformat);
107 assertNotNull("Couldn't parse reimported alignment data.", al_input);
109 String identifyoutput = new IdentifyFile().Identify(outputfile,
110 AppletFormatAdapter.PASTE);
111 assertNotNull("Identify routine failed for outputformat " + ioformat,
114 "Identify routine could not recognise output generated by '"
115 + ioformat + "' writer",
116 ioformat.equals(identifyoutput));
117 testAlignmentEquivalence(al, al_input);
118 } catch (Exception e)
121 assertTrue("Couln't format the alignment for output file.", false);
126 * assert alignment equivalence
131 * 'secondary' or generated alignment from some datapreserving
134 public static void testAlignmentEquivalence(AlignmentI al,
137 assertNotNull("Original alignment was null", al);
138 assertNotNull("Generated alignment was null", al_input);
141 "Alignment dimension mismatch: originl contains "
142 + al.getHeight() + " and generated has "
143 + al_input.getHeight() + " sequences; original has "
144 + al.getWidth() + " and generated has "
145 + al_input.getWidth() + " columns.",
146 al.getHeight() == al_input.getHeight()
147 && al.getWidth() == al_input.getWidth());
149 // check Alignment annotation
150 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
151 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
153 // note - at moment we do not distinguish between alignment without any
154 // annotation rows and alignment with no annotation row vector
155 // we might want to revise this in future
156 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
157 : aa_original.length);
158 Map<Integer,java.util.BitSet> orig_groups=new HashMap<Integer,java.util.BitSet>(),new_groups=new HashMap<Integer,java.util.BitSet>();
160 if (aa_new != null && aa_original != null)
162 for (int i = 0; i < aa_original.length; i++)
164 if (aa_new.length>i) {
165 assertTrue("Different alignment annotation at position "+i,
166 equalss(aa_original[i], aa_new[i]));
167 // compare graphGroup or graph properties - needed to verify JAL-1299
168 assertTrue("Graph type not identical.",aa_original[i].graph==aa_new[i].graph);
169 assertTrue("Visibility not identical.", aa_original[i].visible==aa_new[i].visible);
171 "Threshold line not identical.",
172 aa_original[i].threshold == null ? aa_new[i].threshold == null
173 : aa_original[i].threshold
174 .equals(aa_new[i].threshold));
175 // graphGroup may differ, but pattern should be the same
176 Integer o_ggrp=new Integer(aa_original[i].graphGroup+2),n_ggrp=new Integer(aa_new[i].graphGroup+2);
177 BitSet orig_g=orig_groups.get(o_ggrp),new_g=new_groups.get(n_ggrp);
179 orig_groups.put(o_ggrp,orig_g= new BitSet());
182 new_groups.put(n_ggrp, new_g=new BitSet());
184 assertTrue("Graph Group pattern differs at annotation "+i, orig_g.equals(new_g));
185 orig_g.set(i); new_g.set(i);
187 System.err.println("No matching annotation row for "+aa_original[i].toString());
192 "Generated and imported alignment have different annotation sets ("
193 + aa_new_size + " != " + aa_original_size + ")",
194 aa_new_size == aa_original_size);
196 // check sequences, annotation and features
197 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
198 seq_original = al.getSequencesArray();
199 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
200 seq_new = al_input.getSequencesArray();
201 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
202 AlignmentAnnotation annot_original, annot_new;
204 for (int i = 0; i < al.getSequencesArray().length; i++)
206 String name = seq_original[i].getName();
207 int start = seq_original[i].getStart();
208 int end = seq_original[i].getEnd();
209 System.out.println("Check sequence: " + name + "/" + start + "-"
212 // search equal sequence
213 for (int in = 0; in < al_input.getSequencesArray().length; in++)
215 if (name.equals(seq_new[in].getName())
216 && start == seq_new[in].getStart()
217 && end == seq_new[in].getEnd())
219 String ss_original = seq_original[i].getSequenceAsString();
220 String ss_new = seq_new[in].getSequenceAsString();
221 assertTrue("The sequences " + name + "/" + start + "-" + end
222 + " are not equal", ss_original.equals(ss_new));
225 "Sequence Features were not equivalent",
226 (seq_original[i].getSequenceFeatures() == null && seq_new[in]
227 .getSequenceFeatures() == null)
228 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
229 .getSequenceFeatures() != null));
230 // compare sequence features
231 if (seq_original[i].getSequenceFeatures() != null
232 && seq_new[in].getSequenceFeatures() != null)
234 System.out.println("There are feature!!!");
235 sequenceFeatures_original = new SequenceFeature[seq_original[i]
236 .getSequenceFeatures().length];
237 sequenceFeatures_original = seq_original[i]
238 .getSequenceFeatures();
239 sequenceFeatures_new = new SequenceFeature[seq_new[in]
240 .getSequenceFeatures().length];
241 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
243 assertTrue("different number of features", seq_original[i]
244 .getSequenceFeatures().length == seq_new[in]
245 .getSequenceFeatures().length);
247 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
249 assertTrue("Different features",
250 sequenceFeatures_original[feat]
251 .equals(sequenceFeatures_new[feat]));
254 // compare alignment annotation
255 if (al.getSequenceAt(i).getAnnotation() != null
256 && al_input.getSequenceAt(in).getAnnotation() != null)
258 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
260 if (al.getSequenceAt(i).getAnnotation()[j] != null
261 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
263 annot_original = al.getSequenceAt(i).getAnnotation()[j];
264 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
265 assertTrue("Different annotation elements",
266 equalss(annot_original, annot_new));
270 else if (al.getSequenceAt(i).getAnnotation() == null
271 && al_input.getSequenceAt(in).getAnnotation() == null)
273 System.out.println("No annotations");
275 else if (al.getSequenceAt(i).getAnnotation() != null
276 && al_input.getSequenceAt(in).getAnnotation() == null)
278 assertTrue("Annotations differed between sequences ("
279 + al.getSequenceAt(i).getName() + ") and ("
280 + al_input.getSequenceAt(i).getName() + ")", false);
289 * compare annotations
291 private static boolean equalss(AlignmentAnnotation annot_or,
292 AlignmentAnnotation annot_new)
294 if (annot_or.annotations.length != annot_new.annotations.length)
296 System.err.println("Different lengths for annotation row elements: "+annot_or.annotations.length +"!="+ annot_new.annotations.length);
299 for (int i = 0; i < annot_or.annotations.length; i++)
301 Annotation an_or=annot_or.annotations[i],an_new=annot_new.annotations[i];
305 if (!an_or.displayCharacter.trim()
306 .equals(an_new.displayCharacter.trim())
307 || !(""+an_or.secondaryStructure).trim().equals((""+an_new.secondaryStructure).trim())
308 || (an_or.description != an_new.description && (an_or.description == null
309 || an_new.description == null || !an_or.description
310 .equals(an_new.description))))
312 System.err.println("Annotation Element Mismatch\nElement "+i+" in original: "+annot_or.annotations[i].toString()+"\nElement "+i+" in new: "+annot_new.annotations[i].toString());
316 else if (annot_or.annotations[i] == null
317 && annot_new.annotations[i] == null)
323 System.err.println("Annotation Element Mismatch\nElement "+i+" in original: "+(annot_or.annotations[i]==null ? "is null" : annot_or.annotations[i].toString())+"\nElement "+i+" in new: "+(annot_new.annotations[i] == null ? "is null" : annot_new.annotations[i].toString()));