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();
30 static String PfamFile = "examples/PF00111_seed.stk",
31 RfamFile = "examples/RF00031_folded.stk";
34 public void pfamFileIO() throws Exception
36 test(new File(PfamFile));
37 AppletFormatAdapter af = new AppletFormatAdapter();
38 AlignmentI al = af.readFile(PfamFile, af.FILE, new IdentifyFile().Identify(PfamFile, af.FILE));
40 for (SequenceI sq:al.getSequences())
42 if (sq.getPDBId()!=null)
44 numpdb+=sq.getPDBId().size();
47 assertTrue("PF00111 seed alignment has at least 1 PDB file, but the reader found none.",numpdb>0);
50 public void rfamFileIO() throws Exception
52 testFileIOwithFormat(new File(RfamFile), "STH");
56 * test alignment data in given file can be imported, exported and reimported
60 * - source datafile (IdentifyFile.identify() should work with it)
62 * - label for IO class used to write and read back in the data from
65 public static void testFileIOwithFormat(File f, String ioformat)
67 System.out.println("Reading file: " + f);
68 String ff = f.getPath();
71 AppletFormatAdapter rf = new AppletFormatAdapter();
73 Alignment al = rf.readFile(ff, AppletFormatAdapter.FILE,
74 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
76 assertNotNull("Couldn't read supplied alignment data.", al);
78 // make sure dataset is initialised ? not sure about this
79 for (int i = 0; i < al.getSequencesArray().length; ++i)
81 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
83 String outputfile = rf.formatSequences(ioformat, al, true);
84 System.out.println("Output file in '"+ioformat+"':\n"+outputfile+"\n<<EOF\n");
85 // test for consistency in io
86 Alignment al_input = new AppletFormatAdapter().readFile(outputfile,
87 AppletFormatAdapter.PASTE, ioformat);
88 assertNotNull("Couldn't parse reimported alignment data.", al_input);
90 String identifyoutput = new IdentifyFile().Identify(outputfile,
91 AppletFormatAdapter.PASTE);
92 assertNotNull("Identify routine failed for outputformat " + ioformat,
95 "Identify routine could not recognise output generated by '"
96 + ioformat + "' writer",
97 ioformat.equals(identifyoutput));
98 testAlignmentEquivalence(al, al_input);
102 assertTrue("Couln't format the alignment for output file.", false);
107 * assert alignment equivalence
112 * 'secondary' or generated alignment from some datapreserving
115 private static void testAlignmentEquivalence(AlignmentI al,
118 assertNotNull("Original alignment was null", al);
119 assertNotNull("Generated alignment was null", al_input);
122 "Alignment dimension mismatch: originl contains "
123 + al.getHeight() + " and generated has "
124 + al_input.getHeight() + " sequences; original has "
125 + al.getWidth() + " and generated has "
126 + al_input.getWidth() + " columns.",
127 al.getHeight() == al_input.getHeight()
128 && al.getWidth() == al_input.getWidth());
130 // check Alignment annotation
131 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
132 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
134 // note - at moment we do not distinguish between alignment without any
135 // annotation rows and alignment with no annotation row vector
136 // we might want to revise this in future
137 int aa_new_size = (aa_new == null ? 0 : aa_new.length), aa_original_size = (aa_original == null ? 0
138 : aa_original.length);
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 ordering",
146 equalss(aa_original[i], aa_new[i]));
148 System.err.println("No matching annotation row for "+aa_original[i].toString());
153 "Generated and imported alignment have different annotation sets ("
154 + aa_new_size + " != " + aa_original_size + ")",
155 aa_new_size == aa_original_size);
157 // check sequences, annotation and features
158 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
159 seq_original = al.getSequencesArray();
160 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
161 seq_new = al_input.getSequencesArray();
162 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
163 AlignmentAnnotation annot_original, annot_new;
165 for (int i = 0; i < al.getSequencesArray().length; i++)
167 String name = seq_original[i].getName();
168 int start = seq_original[i].getStart();
169 int end = seq_original[i].getEnd();
170 System.out.println("Check sequence: " + name + "/" + start + "-"
173 // search equal sequence
174 for (int in = 0; in < al_input.getSequencesArray().length; in++)
176 if (name.equals(seq_new[in].getName())
177 && start == seq_new[in].getStart()
178 && end == seq_new[in].getEnd())
180 String ss_original = seq_original[i].getSequenceAsString();
181 String ss_new = seq_new[in].getSequenceAsString();
182 assertTrue("The sequences " + name + "/" + start + "-" + end
183 + " are not equal", ss_original.equals(ss_new));
186 "Sequence Features were not equivalent",
187 (seq_original[i].getSequenceFeatures() == null && seq_new[in]
188 .getSequenceFeatures() == null)
189 || (seq_original[i].getSequenceFeatures() != null && seq_new[in]
190 .getSequenceFeatures() != null));
191 // compare sequence features
192 if (seq_original[i].getSequenceFeatures() != null
193 && seq_new[in].getSequenceFeatures() != null)
195 System.out.println("There are feature!!!");
196 sequenceFeatures_original = new SequenceFeature[seq_original[i]
197 .getSequenceFeatures().length];
198 sequenceFeatures_original = seq_original[i]
199 .getSequenceFeatures();
200 sequenceFeatures_new = new SequenceFeature[seq_new[in]
201 .getSequenceFeatures().length];
202 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
204 assertTrue("different number of features", seq_original[i]
205 .getSequenceFeatures().length == seq_new[in]
206 .getSequenceFeatures().length);
208 for (int feat = 0; feat < seq_original[i].getSequenceFeatures().length; feat++)
210 assertTrue("Different features",
211 sequenceFeatures_original[feat]
212 .equals(sequenceFeatures_new[feat]));
216 // compare alignment annotation
217 if (al.getSequenceAt(i).getAnnotation() != null
218 && al_input.getSequenceAt(in).getAnnotation() != null)
220 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
222 if (al.getSequenceAt(i).getAnnotation()[j] != null
223 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
225 annot_original = al.getSequenceAt(i).getAnnotation()[j];
226 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
227 assertTrue("Different annotation",
228 equalss(annot_original, annot_new));
232 else if (al.getSequenceAt(i).getAnnotation() == null
233 && al_input.getSequenceAt(in).getAnnotation() == null)
235 System.out.println("No annotations");
237 else if (al.getSequenceAt(i).getAnnotation() != null
238 && al_input.getSequenceAt(in).getAnnotation() == null)
240 assertTrue("Annotations differed between sequences ("
241 + al.getSequenceAt(i).getName() + ") and ("
242 + al_input.getSequenceAt(i).getName() + ")", false);
251 * compare annotations
253 private static boolean equalss(AlignmentAnnotation annot_or,
254 AlignmentAnnotation annot_new)
256 if (annot_or.annotations.length != annot_new.annotations.length)
258 System.err.println("Different lengths for annotation row elements: "+annot_or.annotations.length +"!="+ annot_new.annotations.length);
261 for (int i = 0; i < annot_or.annotations.length; i++)
263 if (annot_or.annotations[i] != null
264 && annot_new.annotations[i] != null)
266 // Jim's comment - shouldn't the conditional here be using || not && for
267 // all these clauses ?
268 if (!annot_or.annotations[i].displayCharacter
269 .equals(annot_new.annotations[i].displayCharacter)
270 && annot_or.annotations[i].secondaryStructure != annot_new.annotations[i].secondaryStructure
271 && !annot_or.annotations[i].description
272 .equals(annot_new.annotations[i].description))
274 System.err.println("Annotation Element Mismatch\nElement "+i+" in original: "+annot_or.annotations[i].toString()+"\nElement "+i+" in new: "+annot_new.annotations[i].toString());
278 else if (annot_or.annotations[i] == null
279 && annot_new.annotations[i] == null)
285 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()));