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";
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 private void test(File f)
50 System.out.println("Reading file: " + f);
51 String ff = f.getPath();
54 Alignment al = new AppletFormatAdapter().readFile(ff, AppletFormatAdapter.FILE,
55 new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
57 for (int i = 0; i < al.getSequencesArray().length; ++i)
59 al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
61 AlignFile stFile = new StockholmFile(al);
62 stFile.setSeqs(al.getSequencesArray());
64 String stockholmoutput = stFile.print();
65 Alignment al_input = new AppletFormatAdapter().readFile(stockholmoutput,
66 AppletFormatAdapter.PASTE, "STH");
67 if (al != null && al_input != null)
69 System.out.println("Alignment contains: " + al.getHeight()
70 + " and " + al_input.getHeight() + " sequences; "
71 + al.getWidth() + " and " + al_input.getWidth()
73 AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
74 AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
76 // check Alignment annotation
77 if (aa_new != null && aa_original != null)
79 System.out.println("Alignment contains: " + aa_new.length
80 + " and " + aa_original.length
81 + " alignment annotation(s)");
82 for (int i = 0; i < aa_original.length; i++)
84 assertTrue("Different alignment annotation",equalss(aa_original[i], aa_new[i]));
89 // check sequences, annotation and features
90 SequenceI[] seq_original = new SequenceI[al.getSequencesArray().length];
91 seq_original = al.getSequencesArray();
92 SequenceI[] seq_new = new SequenceI[al_input.getSequencesArray().length];
93 seq_new = al_input.getSequencesArray();
94 SequenceFeature[] sequenceFeatures_original, sequenceFeatures_new;
95 AlignmentAnnotation annot_original, annot_new;
97 for (int i = 0; i < al.getSequencesArray().length; i++)
99 String name = seq_original[i].getName();
100 int start = seq_original[i].getStart();
101 int end = seq_original[i].getEnd();
102 System.out.println("Check sequence: " + name + "/" + start + "-"
105 // search equal sequence
106 for (int in = 0; in < al_input.getSequencesArray().length; in++)
108 if (name.equals(seq_new[in].getName())
109 && start == seq_new[in].getStart()
110 && end == seq_new[in].getEnd())
112 String ss_original = seq_original[i].getSequenceAsString();
113 String ss_new = seq_new[in].getSequenceAsString();
114 assertTrue("The sequences " + name + "/" + start
115 + "-" + end + " are not equal", ss_original.equals(ss_new));
117 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));
118 // compare sequence features
119 if (seq_original[i].getSequenceFeatures() != null
120 && seq_new[in].getSequenceFeatures() != null)
122 System.out.println("There are feature!!!");
123 sequenceFeatures_original = new SequenceFeature[seq_original[i]
124 .getSequenceFeatures().length];
125 sequenceFeatures_original = seq_original[i]
126 .getSequenceFeatures();
127 sequenceFeatures_new = new SequenceFeature[seq_new[in]
128 .getSequenceFeatures().length];
129 sequenceFeatures_new = seq_new[in].getSequenceFeatures();
131 assertTrue("different number of features", seq_original[i].getSequenceFeatures().length == seq_new[in]
132 .getSequenceFeatures().length);
134 for (int feat = 0; feat < seq_original[i]
135 .getSequenceFeatures().length; feat++)
137 assertTrue("Different features",sequenceFeatures_original[feat]
138 .equals(sequenceFeatures_new[feat]));
142 // compare alignment annotation
143 if (al.getSequenceAt(i).getAnnotation() != null
144 && al_input.getSequenceAt(in).getAnnotation() != null)
146 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
148 if (al.getSequenceAt(i).getAnnotation()[j] != null
149 && al_input.getSequenceAt(in).getAnnotation()[j] != null)
151 annot_original = al.getSequenceAt(i).getAnnotation()[j];
152 annot_new = al_input.getSequenceAt(in).getAnnotation()[j];
153 assertTrue("Different annotation", equalss(annot_original, annot_new));
157 else if (al.getSequenceAt(i).getAnnotation() == null
158 && al_input.getSequenceAt(in).getAnnotation() == null)
160 System.out.println("No annotations");
162 else if (al.getSequenceAt(i).getAnnotation() != null
163 && al_input.getSequenceAt(in).getAnnotation() == null)
165 assertTrue("Annotations differed between sequences ("+al.getSequenceAt(i).getName()+") and ("+al_input.getSequenceAt(i).getName()+")", false);
174 assertTrue("Couldn't read alignment", false);
176 } catch (Exception e)
179 assertTrue("Couln't format the alignment for output file.", false);
184 * compare annotations
186 private boolean equalss(AlignmentAnnotation annot_or,
187 AlignmentAnnotation annot_new)
189 if (annot_or.annotations.length != annot_new.annotations.length)
193 for (int i = 0; i < annot_or.annotations.length; i++)
195 if (annot_or.annotations[i] != null
196 && annot_new.annotations[i] != null)
198 // Jim's comment - shouldn't the conditional here be using || not && for all these clauses ?
199 if (!annot_or.annotations[i].displayCharacter
200 .equals(annot_new.annotations[i].displayCharacter)
201 && annot_or.annotations[i].secondaryStructure != annot_new.annotations[i].secondaryStructure
202 && !annot_or.annotations[i].description
203 .equals(annot_new.annotations[i].description))
208 else if (annot_or.annotations[i] == null
209 && annot_new.annotations[i] == null)