3 import static org.junit.Assert.*;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentAnnotation;
6 import jalview.datamodel.AlignmentI;
7 import jalview.datamodel.SequenceFeature;
8 import jalview.datamodel.SequenceI;
11 import java.io.IOException;
12 import java.io.InputStream;
14 import org.junit.Test;
16 public class StockholmFileTest
19 public static void main(String argv[])
22 new StockholmFileTest().pfamFileIO();
29 static String PfamFile = "examples/PF00111_seed.stk", RfamFile="examples/RF00031_folded.stk";
32 public void pfamFileIO() throws Exception
34 test(new File(PfamFile));
35 AppletFormatAdapter af = new AppletFormatAdapter();
36 AlignmentI al = af.readFile(PfamFile, af.FILE, new IdentifyFile().Identify(PfamFile, af.FILE));
38 for (SequenceI sq:al.getSequences())
40 if (sq.getPDBId()!=null)
42 numpdb+=sq.getPDBId().size();
45 assertTrue("PF00111 seed alignment has at least 1 PDB file, but the reader found none.",numpdb>0);
48 public void rfamFileIO() throws Exception
50 testFileIOwithFormat(new File(RfamFile), "STH");
54 * test alignment data in given file can be imported, exported and reimported
58 * - source datafile (IdentifyFile.identify() should work with it)
60 * - label for IO class used to write and read back in the data from
63 public static void testFileIOwithFormat(File f, String ioformat)
65 System.out.println("Reading file: " + f);
66 String ff = f.getPath();
69 AppletFormatAdapter rf = new AppletFormatAdapter();
71 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
72 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
74 assertNotNull("Couldn't read supplied alignment data.", al);
76 // make sure dataset is initialised ? not sure about this
77 for (int i = 0; i < al.getSequencesArray().length; ++i)
79 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
81 String outputfile = rf.formatSequences(ioformat, al, true);
82 System.out.println("Output file in '"+ioformat+"':\n"+outputfile+"\n<<EOF\n");
83 // test for consistency in io
84 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
85 AppletFormatAdapter.PASTE, ioformat);
86 assertNotNull("Couldn't parse reimported alignment data.", al_input);
88 String identifyoutput = new IdentifyFile().Identify(outputfile,
89 AppletFormatAdapter.PASTE);
90 assertNotNull("Identify routine failed for outputformat " + ioformat,
93 "Identify routine could not recognise output generated by '"
94 + ioformat + "' writer",
95 ioformat.equals(identifyoutput));
96 testAlignmentEquivalence(al, al_input);
100 assertTrue("Couln't format the alignment for output file.", false);
105 * assert alignment equivalence
110 * 'secondary' or generated alignment from some datapreserving
113 private static void testAlignmentEquivalence(AlignmentI al,
116 assertNotNull("Original alignment was null", al);
117 assertNotNull("Generated alignment was null", al_input);
120 "Alignment dimension mismatch: originl contains "
121 + al.getHeight() + " and generated has "
122 + al_input.getHeight() + " sequences; original has "
123 + al.getWidth() + " and generated has "
124 + al_input.getWidth() + " columns.",
125 al.getHeight() == al_input.getHeight()
126 && al.getWidth() == al_input.getWidth());
128 // check Alignment annotation
129 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
130 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
132 // note - at moment we do not distinguish between alignment without any
133 // annotation rows and alignment with no annotation row vector
134 // we might want to revise this in future
135 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
136 : aa_original.length);
138 if (aa_new != null && aa_original != null)
140 for (int i = 0; i < aa_original.length; i++)
142 if (aa_new.length>i) {
143 assertTrue("Different alignment annotation ordering",
144 equalss(aa_original[i], aa_new[i]));
146 System.err.println("No matching annotation row for "+aa_original[i].toString());
151 "Generated and imported alignment have different annotation sets ("
152 + aa_new_size + " != " + aa_original_size + ")",
153 aa_new_size == aa_original_size);
155 // check sequences, annotation and features
156 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
157 seq_original = al.getSequencesArray();
158 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
159 seq_new = al_input.getSequencesArray();
160 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
161 AlignmentAnnotation annot_original, annot_new;
163 for (int i = 0; i < al.getSequencesArray().length; i++)
165 String name = seq_original[i].getName();
166 int start = seq_original[i].getStart();
167 int end = seq_original[i].getEnd();
168 System.out.println("Check sequence: " + name + "/" + start + "-"
171 // search equal sequence
172 for (int in = 0; in < al_input.getSequencesArray().length; in++)
174 if (name.equals(seq_new[in].getName())
175 && start == seq_new[in].getStart()
176 && end == seq_new[in].getEnd())
178 String ss_original = seq_original[i].getSequenceAsString();
179 String ss_new = seq_new[in].getSequenceAsString();
180 assertTrue("The sequences " + name + "/" + start + "-" + end
181 + " are not equal", ss_original.equals(ss_new));
184 "Sequence Features were not equivalent",
185 (seq_original[i].getSequenceFeatures() == null && seq_new[in]
186 .getSequenceFeatures() == null)
187 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
188 .getSequenceFeatures() != null));
189 // compare sequence features
190 if (seq_original[i].getSequenceFeatures() != null
191 && seq_new[in].getSequenceFeatures() != null)
193 System.out.println("There are feature!!!");
194 sequenceFeatures_original = new SequenceFeature[seq_original[i]
195 .getSequenceFeatures().length];
196 sequenceFeatures_original = seq_original[i]
197 .getSequenceFeatures();
198 sequenceFeatures_new = new SequenceFeature[seq_new[in]
199 .getSequenceFeatures().length];
200 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
202 assertTrue("different number of features", seq_original[i]
203 .getSequenceFeatures().length == seq_new[in]
204 .getSequenceFeatures().length);
206 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
208 assertTrue("Different features",
209 sequenceFeatures_original[feat]
210 .equals(sequenceFeatures_new[feat]));
214 // compare alignment annotation
215 if (al.getSequenceAt(i).getAnnotation() != null
216 && al_input.getSequenceAt(in).getAnnotation() != null)
218 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
220 if (al.getSequenceAt(i).getAnnotation()[j] != null
221 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
223 annot_original = al.getSequenceAt(i).getAnnotation()[j];
224 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
225 assertTrue("Different annotation",
226 equalss(annot_original, annot_new));
230 else if (al.getSequenceAt(i).getAnnotation() == null
231 && al_input.getSequenceAt(in).getAnnotation() == null)
233 System.out.println("No annotations");
235 else if (al.getSequenceAt(i).getAnnotation() != null
236 && al_input.getSequenceAt(in).getAnnotation() == null)
238 assertTrue("Annotations differed between sequences ("
239 + al.getSequenceAt(i).getName() + ") and ("
240 + al_input.getSequenceAt(i).getName() + ")", false);
249 * compare annotations
251 private static boolean equalss(AlignmentAnnotation annot_or,
252 AlignmentAnnotation annot_new)
254 if (annot_or.annotations.length != annot_new.annotations.length)
256 System.err.println("Different lengths for annotation row elements: "+annot_or.annotations.length +"!="+ annot_new.annotations.length);
259 for (int i = 0; i < annot_or.annotations.length; i++)
261 if (annot_or.annotations[i] != null
262 && annot_new.annotations[i] != null)
264 // Jim's comment - shouldn't the conditional here be using || not && for
265 // all these clauses ?
266 if (!annot_or.annotations[i].displayCharacter
267 .equals(annot_new.annotations[i].displayCharacter)
268 && annot_or.annotations[i].secondaryStructure != annot_new.annotations[i].secondaryStructure
269 && !annot_or.annotations[i].description
270 .equals(annot_new.annotations[i].description))
272 System.err.println("Annotation Element Mismatch\nElement "+i+" in original: "+annot_or.annotations[i].toString()+"\nElement "+i+" in new: "+annot_new.annotations[i].toString());
276 else if (annot_or.annotations[i] == null
277 && annot_new.annotations[i] == null)
283 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()));