3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.datamodel.SequenceI;
13 import java.util.BitSet;
14 import java.util.HashMap;
17 import org.junit.Test;
19 public class StockholmFileTest
22 static String PfamFile = "examples/PF00111_seed.stk",
23 RfamFile = "examples/RF00031_folded.stk";
26 public void pfamFileIO() throws Exception
28 testFileIOwithFormat(new File(PfamFile), "STH");
31 public void pfamFileDataExtraction() throws Exception
32 { AppletFormatAdapter af = new AppletFormatAdapter();
33 AlignmentI al = af.readFile(PfamFile, af.FILE,
34 new IdentifyFile().Identify(PfamFile, af.FILE));
36 for (SequenceI sq : al.getSequences())
38 if (sq.getPDBId() != null)
40 numpdb += sq.getPDBId().size();
44 "PF00111 seed alignment has at least 1 PDB file, but the reader found none.",
49 public void rfamFileIO() throws Exception
51 testFileIOwithFormat(new File(RfamFile), "STH");
55 * test alignment data in given file can be imported, exported and reimported
59 * - source datafile (IdentifyFile.identify() should work with it)
61 * - label for IO class used to write and read back in the data from
64 public static void testFileIOwithFormat(File f, String ioformat)
66 System.out.println("Reading file: " + f);
67 String ff = f.getPath();
70 AppletFormatAdapter rf = new AppletFormatAdapter();
72 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
73 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
75 assertNotNull("Couldn't read supplied alignment data.", al);
77 // make sure dataset is initialised ? not sure about this
78 for (int i = 0; i < al.getSequencesArray().length; ++i)
80 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
82 String outputfile = rf.formatSequences(ioformat, al, true);
83 System.out.println("Output file in '"+ioformat+"':\n"+outputfile+"\n<<EOF\n");
84 // test for consistency in io
85 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
86 AppletFormatAdapter.PASTE, ioformat);
87 assertNotNull("Couldn't parse reimported alignment data.", al_input);
89 String identifyoutput = new IdentifyFile().Identify(outputfile,
90 AppletFormatAdapter.PASTE);
91 assertNotNull("Identify routine failed for outputformat " + ioformat,
94 "Identify routine could not recognise output generated by '"
95 + ioformat + "' writer",
96 ioformat.equals(identifyoutput));
97 testAlignmentEquivalence(al, al_input);
101 assertTrue("Couln't format the alignment for output file.", false);
106 * assert alignment equivalence
111 * 'secondary' or generated alignment from some datapreserving
114 public static void testAlignmentEquivalence(AlignmentI al,
117 assertNotNull("Original alignment was null", al);
118 assertNotNull("Generated alignment was null", al_input);
121 "Alignment dimension mismatch: originl contains "
122 + al.getHeight() + " and generated has "
123 + al_input.getHeight() + " sequences; original has "
124 + al.getWidth() + " and generated has "
125 + al_input.getWidth() + " columns.",
126 al.getHeight() == al_input.getHeight()
127 && al.getWidth() == al_input.getWidth());
129 // check Alignment annotation
130 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
131 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
133 // note - at moment we do not distinguish between alignment without any
134 // annotation rows and alignment with no annotation row vector
135 // we might want to revise this in future
136 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
137 : aa_original.length);
138 Map<Integer,java.util.BitSet> orig_groups=new HashMap<Integer,java.util.BitSet>(),new_groups=new HashMap<Integer,java.util.BitSet>();
140 if (aa_new != null && aa_original != null)
142 for (int i = 0; i < aa_original.length; i++)
144 if (aa_new.length>i) {
145 assertTrue("Different alignment annotation at position "+i,
146 equalss(aa_original[i], aa_new[i]));
147 // compare graphGroup or graph properties - needed to verify JAL-1299
148 assertTrue("Graph type not identical.",aa_original[i].graph==aa_new[i].graph);
149 assertTrue("Visibility not identical.", aa_original[i].visible==aa_new[i].visible);
150 // graphGroup may differ, but pattern should be the same
151 Integer o_ggrp=new Integer(aa_original[i].graphGroup+2),n_ggrp=new Integer(aa_new[i].graphGroup+2);
152 BitSet orig_g=orig_groups.get(o_ggrp),new_g=new_groups.get(n_ggrp);
154 orig_groups.put(o_ggrp,orig_g= new BitSet());
157 new_groups.put(n_ggrp, new_g=new BitSet());
159 assertTrue("Graph Group pattern differs at annotation "+i, orig_g.equals(new_g));
160 orig_g.set(i); new_g.set(i);
162 System.err.println("No matching annotation row for "+aa_original[i].toString());
167 "Generated and imported alignment have different annotation sets ("
168 + aa_new_size + " != " + aa_original_size + ")",
169 aa_new_size == aa_original_size);
171 // check sequences, annotation and features
172 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
173 seq_original = al.getSequencesArray();
174 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
175 seq_new = al_input.getSequencesArray();
176 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
177 AlignmentAnnotation annot_original, annot_new;
179 for (int i = 0; i < al.getSequencesArray().length; i++)
181 String name = seq_original[i].getName();
182 int start = seq_original[i].getStart();
183 int end = seq_original[i].getEnd();
184 System.out.println("Check sequence: " + name + "/" + start + "-"
187 // search equal sequence
188 for (int in = 0; in < al_input.getSequencesArray().length; in++)
190 if (name.equals(seq_new[in].getName())
191 && start == seq_new[in].getStart()
192 && end == seq_new[in].getEnd())
194 String ss_original = seq_original[i].getSequenceAsString();
195 String ss_new = seq_new[in].getSequenceAsString();
196 assertTrue("The sequences " + name + "/" + start + "-" + end
197 + " are not equal", ss_original.equals(ss_new));
200 "Sequence Features were not equivalent",
201 (seq_original[i].getSequenceFeatures() == null && seq_new[in]
202 .getSequenceFeatures() == null)
203 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
204 .getSequenceFeatures() != null));
205 // compare sequence features
206 if (seq_original[i].getSequenceFeatures() != null
207 && seq_new[in].getSequenceFeatures() != null)
209 System.out.println("There are feature!!!");
210 sequenceFeatures_original = new SequenceFeature[seq_original[i]
211 .getSequenceFeatures().length];
212 sequenceFeatures_original = seq_original[i]
213 .getSequenceFeatures();
214 sequenceFeatures_new = new SequenceFeature[seq_new[in]
215 .getSequenceFeatures().length];
216 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
218 assertTrue("different number of features", seq_original[i]
219 .getSequenceFeatures().length == seq_new[in]
220 .getSequenceFeatures().length);
222 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
224 assertTrue("Different features",
225 sequenceFeatures_original[feat]
226 .equals(sequenceFeatures_new[feat]));
229 // compare alignment annotation
230 if (al.getSequenceAt(i).getAnnotation() != null
231 && al_input.getSequenceAt(in).getAnnotation() != null)
233 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
235 if (al.getSequenceAt(i).getAnnotation()[j] != null
236 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
238 annot_original = al.getSequenceAt(i).getAnnotation()[j];
239 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
240 assertTrue("Different annotation elements",
241 equalss(annot_original, annot_new));
245 else if (al.getSequenceAt(i).getAnnotation() == null
246 && al_input.getSequenceAt(in).getAnnotation() == null)
248 System.out.println("No annotations");
250 else if (al.getSequenceAt(i).getAnnotation() != null
251 && al_input.getSequenceAt(in).getAnnotation() == null)
253 assertTrue("Annotations differed between sequences ("
254 + al.getSequenceAt(i).getName() + ") and ("
255 + al_input.getSequenceAt(i).getName() + ")", false);
264 * compare annotations
266 private static boolean equalss(AlignmentAnnotation annot_or,
267 AlignmentAnnotation annot_new)
269 if (annot_or.annotations.length != annot_new.annotations.length)
271 System.err.println("Different lengths for annotation row elements: "+annot_or.annotations.length +"!="+ annot_new.annotations.length);
274 for (int i = 0; i < annot_or.annotations.length; i++)
276 Annotation an_or=annot_or.annotations[i],an_new=annot_new.annotations[i];
280 if (!an_or.displayCharacter
281 .equals(an_new.displayCharacter)
282 || an_or.secondaryStructure != an_new.secondaryStructure
283 || (an_or.description != an_new.description && (an_or.description == null
284 || an_new.description == null || !an_or.description
285 .equals(an_new.description))))
287 System.err.println("Annotation Element Mismatch\nElement "+i+" in original: "+annot_or.annotations[i].toString()+"\nElement "+i+" in new: "+annot_new.annotations[i].toString());
291 else if (annot_or.annotations[i] == null
292 && annot_new.annotations[i] == null)
298 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()));