JAL-1199 refactored stockholm test code from main class and added a Pfam Stockholm...
[jalview.git] / test / jalview / io / StockholmFileTest.java
1 package jalview.io;
2
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;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.InputStream;
13
14 import org.junit.Test;
15
16 public class StockholmFileTest
17 {
18
19   public static void main(String argv[])
20   {
21     try {
22       new StockholmFileTest().pfamFileIO();
23     
24     } catch (Exception x)
25     {
26       x.printStackTrace();
27     }
28   }
29   static String PfamFile = "examples/PF00111_seed.stk";
30
31   @Test
32   public void pfamFileIO() throws Exception
33   {
34       test(new File(PfamFile));
35       AppletFormatAdapter af = new AppletFormatAdapter();
36       AlignmentI al = af.readFile(PfamFile, af.FILE, new IdentifyFile().Identify(PfamFile, af.FILE));
37       int numpdb=0;
38       for (SequenceI sq:al.getSequences())
39       {
40         if (sq.getPDBId()!=null)
41         {
42           numpdb+=sq.getPDBId().size();
43         }
44       }
45       assertTrue("PF00111 seed alignment has at least 1 PDB file, but the reader found none.",numpdb>0);
46   }
47
48   private void test(File f)
49   {
50     System.out.println("Reading file: " + f);
51     String ff = f.getPath();
52     try
53     {
54       Alignment al = new AppletFormatAdapter().readFile(ff, AppletFormatAdapter.FILE,
55               new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
56       
57       for (int i = 0; i < al.getSequencesArray().length; ++i)
58       {
59         al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
60       }
61       AlignFile stFile = new StockholmFile(al);
62       stFile.setSeqs(al.getSequencesArray());
63
64       String stockholmoutput = stFile.print();
65       Alignment al_input = new AppletFormatAdapter().readFile(stockholmoutput,
66               AppletFormatAdapter.PASTE, "STH");
67       if (al != null && al_input != null)
68       {
69         System.out.println("Alignment contains: " + al.getHeight()
70                 + " and " + al_input.getHeight() + " sequences; "
71                 + al.getWidth() + " and " + al_input.getWidth()
72                 + " columns.");
73         AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
74         AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
75
76         // check Alignment annotation
77         if (aa_new != null && aa_original != null)
78         {
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++)
83           {
84             assertTrue("Different alignment annotation",equalss(aa_original[i], aa_new[i]));
85               
86           }
87         }
88
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;
96         //
97         for (int i = 0; i < al.getSequencesArray().length; i++)
98         {
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 + "-"
103                   + end);
104
105           // search equal sequence
106           for (int in = 0; in < al_input.getSequencesArray().length; in++)
107           {
108             if (name.equals(seq_new[in].getName())
109                     && start == seq_new[in].getStart()
110                     && end == seq_new[in].getEnd())
111             {
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));
116               
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)
121               {
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();
130
131                 assertTrue("different number of features", seq_original[i].getSequenceFeatures().length == seq_new[in]
132                         .getSequenceFeatures().length);
133                 
134                 for (int feat = 0; feat < seq_original[i]
135                         .getSequenceFeatures().length; feat++)
136                 {
137                   assertTrue("Different features",sequenceFeatures_original[feat]
138                           .equals(sequenceFeatures_new[feat]));
139                 }
140               }
141
142               // compare alignment annotation
143               if (al.getSequenceAt(i).getAnnotation() != null
144                       && al_input.getSequenceAt(in).getAnnotation() != null)
145               {
146                 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
147                 {
148                   if (al.getSequenceAt(i).getAnnotation()[j] != null
149                           && al_input.getSequenceAt(in).getAnnotation()[j] != null)
150                   {
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));
154                   }
155                 }
156               }
157               else if (al.getSequenceAt(i).getAnnotation() == null
158                       && al_input.getSequenceAt(in).getAnnotation() == null)
159               {
160                 System.out.println("No annotations");
161               }
162               else if (al.getSequenceAt(i).getAnnotation() != null
163                       && al_input.getSequenceAt(in).getAnnotation() == null)
164               {
165                 assertTrue("Annotations differed between sequences ("+al.getSequenceAt(i).getName()+") and ("+al_input.getSequenceAt(i).getName()+")", false);
166               }
167               break;
168             }
169           }
170         }
171       }
172       else
173       {
174         assertTrue("Couldn't read alignment", false);
175       }
176     } catch (Exception e)
177     {
178       e.printStackTrace();
179       assertTrue("Couln't format the alignment for output file.", false);
180     }
181   }
182
183   /*
184    * compare annotations
185    */
186   private boolean equalss(AlignmentAnnotation annot_or,
187           AlignmentAnnotation annot_new)
188   {
189     if (annot_or.annotations.length != annot_new.annotations.length)
190     {
191       return false;
192     }
193     for (int i = 0; i < annot_or.annotations.length; i++)
194     {
195       if (annot_or.annotations[i] != null
196               && annot_new.annotations[i] != null)
197       {
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))
204         {
205           return false;
206         }
207       }
208       else if (annot_or.annotations[i] == null
209               && annot_new.annotations[i] == null)
210       {
211         continue;
212       }
213       else
214       {
215         return false;
216       }
217     }
218     return true;
219   }
220 }