2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import jalview.datamodel.Alignment;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.Annotation;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
31 import java.util.BitSet;
32 import java.util.HashMap;
35 import org.junit.Test;
37 public class StockholmFileTest
40 static String PfamFile = "examples/PF00111_seed.stk",
41 RfamFile = "examples/RF00031_folded.stk";
44 public void pfamFileIO() throws Exception
46 testFileIOwithFormat(new File(PfamFile), "STH");
49 public void pfamFileDataExtraction() throws Exception
50 { AppletFormatAdapter af = new AppletFormatAdapter();
51 AlignmentI al = af.readFile(PfamFile, af.FILE,
52 new IdentifyFile().Identify(PfamFile, af.FILE));
54 for (SequenceI sq : al.getSequences())
56 if (sq.getPDBId() != null)
58 numpdb += sq.getPDBId().size();
62 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
67 public void rfamFileIO() throws Exception
69 testFileIOwithFormat(new File(RfamFile), "STH");
73 * test alignment data in given file can be imported, exported and reimported
77 * - source datafile (IdentifyFile.identify() should work with it)
79 * - label for IO class used to write and read back in the data from
82 public static void testFileIOwithFormat(File f, String ioformat)
84 System.out.println("Reading file: " + f);
85 String ff = f.getPath();
88 AppletFormatAdapter rf = new AppletFormatAdapter();
90 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
91 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
93 assertNotNull("Couldn't read supplied alignment data.", al);
95 // make sure dataset is initialised ? not sure about this
96 for (int i = 0; i < al.getSequencesArray().length; ++i)
98 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
100 String outputfile = rf.formatSequences(ioformat, al, true);
101 System.out.println("Output file in '"+ioformat+"':\n"+outputfile+"\n<<EOF\n");
102 // test for consistency in io
103 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
104 AppletFormatAdapter.PASTE, ioformat);
105 assertNotNull("Couldn't parse reimported alignment data.", al_input);
107 String identifyoutput = new IdentifyFile().Identify(outputfile,
108 AppletFormatAdapter.PASTE);
109 assertNotNull("Identify routine failed for outputformat " + ioformat,
112 "Identify routine could not recognise output generated by '"
113 + ioformat + "' writer",
114 ioformat.equals(identifyoutput));
115 testAlignmentEquivalence(al, al_input);
116 } catch (Exception e)
119 assertTrue("Couln't format the alignment for output file.", false);
124 * assert alignment equivalence
129 * 'secondary' or generated alignment from some datapreserving
132 public static void testAlignmentEquivalence(AlignmentI al,
135 assertNotNull("Original alignment was null", al);
136 assertNotNull("Generated alignment was null", al_input);
139 "Alignment dimension mismatch: originl contains "
140 + al.getHeight() + " and generated has "
141 + al_input.getHeight() + " sequences; original has "
142 + al.getWidth() + " and generated has "
143 + al_input.getWidth() + " columns.",
144 al.getHeight() == al_input.getHeight()
145 && al.getWidth() == al_input.getWidth());
147 // check Alignment annotation
148 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
149 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
151 // note - at moment we do not distinguish between alignment without any
152 // annotation rows and alignment with no annotation row vector
153 // we might want to revise this in future
154 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
155 : aa_original.length);
156 Map<Integer,java.util.BitSet> orig_groups=new HashMap<Integer,java.util.BitSet>(),new_groups=new HashMap<Integer,java.util.BitSet>();
158 if (aa_new != null && aa_original != null)
160 for (int i = 0; i < aa_original.length; i++)
162 if (aa_new.length>i) {
163 assertTrue("Different alignment annotation at position "+i,
164 equalss(aa_original[i], aa_new[i]));
165 // compare graphGroup or graph properties - needed to verify JAL-1299
166 assertTrue("Graph type not identical.",aa_original[i].graph==aa_new[i].graph);
167 assertTrue("Visibility not identical.", aa_original[i].visible==aa_new[i].visible);
169 "Threshold line not identical.",
170 aa_original[i].threshold == null ? aa_new[i].threshold == null
171 : aa_original[i].threshold
172 .equals(aa_new[i].threshold));
173 // graphGroup may differ, but pattern should be the same
174 Integer o_ggrp=new Integer(aa_original[i].graphGroup+2),n_ggrp=new Integer(aa_new[i].graphGroup+2);
175 BitSet orig_g=orig_groups.get(o_ggrp),new_g=new_groups.get(n_ggrp);
177 orig_groups.put(o_ggrp,orig_g= new BitSet());
180 new_groups.put(n_ggrp, new_g=new BitSet());
182 assertTrue("Graph Group pattern differs at annotation "+i, orig_g.equals(new_g));
183 orig_g.set(i); new_g.set(i);
185 System.err.println("No matching annotation row for "+aa_original[i].toString());
190 "Generated and imported alignment have different annotation sets ("
191 + aa_new_size + " != " + aa_original_size + ")",
192 aa_new_size == aa_original_size);
194 // check sequences, annotation and features
195 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
196 seq_original = al.getSequencesArray();
197 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
198 seq_new = al_input.getSequencesArray();
199 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
200 AlignmentAnnotation annot_original, annot_new;
202 for (int i = 0; i < al.getSequencesArray().length; i++)
204 String name = seq_original[i].getName();
205 int start = seq_original[i].getStart();
206 int end = seq_original[i].getEnd();
207 System.out.println("Check sequence: " + name + "/" + start + "-"
210 // search equal sequence
211 for (int in = 0; in < al_input.getSequencesArray().length; in++)
213 if (name.equals(seq_new[in].getName())
214 && start == seq_new[in].getStart()
215 && end == seq_new[in].getEnd())
217 String ss_original = seq_original[i].getSequenceAsString();
218 String ss_new = seq_new[in].getSequenceAsString();
219 assertTrue("The sequences " + name + "/" + start + "-" + end
220 + " are not equal", ss_original.equals(ss_new));
223 "Sequence Features were not equivalent",
224 (seq_original[i].getSequenceFeatures() == null && seq_new[in]
225 .getSequenceFeatures() == null)
226 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
227 .getSequenceFeatures() != null));
228 // compare sequence features
229 if (seq_original[i].getSequenceFeatures() != null
230 && seq_new[in].getSequenceFeatures() != null)
232 System.out.println("There are feature!!!");
233 sequenceFeatures_original = new SequenceFeature[seq_original[i]
234 .getSequenceFeatures().length];
235 sequenceFeatures_original = seq_original[i]
236 .getSequenceFeatures();
237 sequenceFeatures_new = new SequenceFeature[seq_new[in]
238 .getSequenceFeatures().length];
239 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
241 assertTrue("different number of features", seq_original[i]
242 .getSequenceFeatures().length == seq_new[in]
243 .getSequenceFeatures().length);
245 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
247 assertTrue("Different features",
248 sequenceFeatures_original[feat]
249 .equals(sequenceFeatures_new[feat]));
252 // compare alignment annotation
253 if (al.getSequenceAt(i).getAnnotation() != null
254 && al_input.getSequenceAt(in).getAnnotation() != null)
256 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
258 if (al.getSequenceAt(i).getAnnotation()[j] != null
259 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
261 annot_original = al.getSequenceAt(i).getAnnotation()[j];
262 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
263 assertTrue("Different annotation elements",
264 equalss(annot_original, annot_new));
268 else if (al.getSequenceAt(i).getAnnotation() == null
269 && al_input.getSequenceAt(in).getAnnotation() == null)
271 System.out.println("No annotations");
273 else if (al.getSequenceAt(i).getAnnotation() != null
274 && al_input.getSequenceAt(in).getAnnotation() == null)
276 assertTrue("Annotations differed between sequences ("
277 + al.getSequenceAt(i).getName() + ") and ("
278 + al_input.getSequenceAt(i).getName() + ")", false);
287 * compare annotations
289 private static boolean equalss(AlignmentAnnotation annot_or,
290 AlignmentAnnotation annot_new)
292 if (annot_or.annotations.length != annot_new.annotations.length)
294 System.err.println("Different lengths for annotation row elements: "+annot_or.annotations.length +"!="+ annot_new.annotations.length);
297 for (int i = 0; i < annot_or.annotations.length; i++)
299 Annotation an_or=annot_or.annotations[i],an_new=annot_new.annotations[i];
303 if (!an_or.displayCharacter.trim()
304 .equals(an_new.displayCharacter.trim())
305 || !(""+an_or.secondaryStructure).trim().equals((""+an_new.secondaryStructure).trim())
306 || (an_or.description != an_new.description && (an_or.description == null
307 || an_new.description == null || !an_or.description
308 .equals(an_new.description))))
310 System.err.println("Annotation Element Mismatch\nElement "+i+" in original: "+annot_or.annotations[i].toString()+"\nElement "+i+" in new: "+annot_new.annotations[i].toString());
314 else if (annot_or.annotations[i] == null
315 && annot_new.annotations[i] == null)
321 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()));