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 test(new File(RfamFile));
53 private void test(File f)
55 System.out.println("Reading file: " + f);
56 String ff = f.getPath();
59 Alignment al = new AppletFormatAdapter().readFile(ff, AppletFormatAdapter.FILE,
60 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
62 for (int i = 0; i < al.getSequencesArray().length; ++i)
64 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
66 AlignFile stFile = new StockholmFile(al);
67 stFile.setSeqs(al.getSequencesArray());
69 String stockholmoutput = stFile.print();
70 Alignment al_input = new AppletFormatAdapter().readFile(stockholmoutput,
71 AppletFormatAdapter.PASTE, "STH");
72 if (al != null && al_input != null)
74 System.out.println("Alignment contains: " + al.getHeight()
75 + " and " + al_input.getHeight() + " sequences; "
76 + al.getWidth() + " and " + al_input.getWidth()
78 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
79 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
81 // check Alignment annotation
82 if (aa_new != null && aa_original != null)
84 System.out.println("Alignment contains: " + aa_new.length
85 + " and " + aa_original.length
86 + " alignment annotation(s)");
87 for (int i = 0; i < aa_original.length; i++)
89 assertTrue("Different alignment annotation",equalss(aa_original[i], aa_new[i]));
94 // check sequences, annotation and features
95 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
96 seq_original = al.getSequencesArray();
97 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
98 seq_new = al_input.getSequencesArray();
99 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
100 AlignmentAnnotation annot_original, annot_new;
102 for (int i = 0; i < al.getSequencesArray().length; i++)
104 String name = seq_original[i].getName();
105 int start = seq_original[i].getStart();
106 int end = seq_original[i].getEnd();
107 System.out.println("Check sequence: " + name + "/" + start + "-"
110 // search equal sequence
111 for (int in = 0; in < al_input.getSequencesArray().length; in++)
113 if (name.equals(seq_new[in].getName())
114 && start == seq_new[in].getStart()
115 && end == seq_new[in].getEnd())
117 String ss_original = seq_original[i].getSequenceAsString();
118 String ss_new = seq_new[in].getSequenceAsString();
119 assertTrue("The sequences " + name + "/" + start
120 + "-" + end + " are not equal", ss_original.equals(ss_new));
122 assertTrue("Sequence Features were not equivalent", (seq_original[i].getSequenceFeatures()==null && seq_new[in].getSequenceFeatures()==null) || (seq_original[i].getSequenceFeatures()!=null && seq_new[in].getSequenceFeatures()!=null));
123 // compare sequence features
124 if (seq_original[i].getSequenceFeatures() != null
125 && seq_new[in].getSequenceFeatures() != null)
127 System.out.println("There are feature!!!");
128 sequenceFeatures_original = new SequenceFeature[seq_original[i]
129 .getSequenceFeatures().length];
130 sequenceFeatures_original = seq_original[i]
131 .getSequenceFeatures();
132 sequenceFeatures_new = new SequenceFeature[seq_new[in]
133 .getSequenceFeatures().length];
134 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
136 assertTrue("different number of features", seq_original[i].getSequenceFeatures().length == seq_new[in]
137 .getSequenceFeatures().length);
139 for (int feat = 0; feat < seq_original[i]
140 .getSequenceFeatures().length; feat++)
142 assertTrue("Different features",sequenceFeatures_original[feat]
143 .equals(sequenceFeatures_new[feat]));
147 // compare alignment annotation
148 if (al.getSequenceAt(i).getAnnotation() != null
149 && al_input.getSequenceAt(in).getAnnotation() != null)
151 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
153 if (al.getSequenceAt(i).getAnnotation()[j] != null
154 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
156 annot_original = al.getSequenceAt(i).getAnnotation()[j];
157 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
158 assertTrue("Different annotation", equalss(annot_original, annot_new));
162 else if (al.getSequenceAt(i).getAnnotation() == null
163 && al_input.getSequenceAt(in).getAnnotation() == null)
165 System.out.println("No annotations");
167 else if (al.getSequenceAt(i).getAnnotation() != null
168 && al_input.getSequenceAt(in).getAnnotation() == null)
170 assertTrue("Annotations differed between sequences ("+al.getSequenceAt(i).getName()+") and ("+al_input.getSequenceAt(i).getName()+")", false);
179 assertTrue("Couldn't read alignment", false);
181 } catch (Exception e)
184 assertTrue("Couln't format the alignment for output file.", false);
189 * compare annotations
191 private boolean equalss(AlignmentAnnotation annot_or,
192 AlignmentAnnotation annot_new)
194 if (annot_or.annotations.length != annot_new.annotations.length)
198 for (int i = 0; i < annot_or.annotations.length; i++)
200 if (annot_or.annotations[i] != null
201 && annot_new.annotations[i] != null)
203 // Jim's comment - shouldn't the conditional here be using || not && for all these clauses ?
204 if (!annot_or.annotations[i].displayCharacter
205 .equals(annot_new.annotations[i].displayCharacter)
206 && annot_or.annotations[i].secondaryStructure != annot_new.annotations[i].secondaryStructure
207 && !annot_or.annotations[i].description
208 .equals(annot_new.annotations[i].description))
213 else if (annot_or.annotations[i] == null
214 && annot_new.annotations[i] == null)