JAL-1199 added test for Rfam Stockholm file example
[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", RfamFile="examples/RF00031_folded.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   @Test
48   public void rfamFileIO() throws Exception
49   {
50       test(new File(RfamFile));
51   }
52
53   private void test(File f)
54   {
55     System.out.println("Reading file: " + f);
56     String ff = f.getPath();
57     try
58     {
59       Alignment al = new AppletFormatAdapter().readFile(ff, AppletFormatAdapter.FILE,
60               new IdentifyFile().Identify(ff, AppletFormatAdapter.FILE));
61       
62       for (int i = 0; i < al.getSequencesArray().length; ++i)
63       {
64         al.getSequenceAt(i).setDatasetSequence(al.getSequenceAt(i));
65       }
66       AlignFile stFile = new StockholmFile(al);
67       stFile.setSeqs(al.getSequencesArray());
68
69       String stockholmoutput = stFile.print();
70       Alignment al_input = new AppletFormatAdapter().readFile(stockholmoutput,
71               AppletFormatAdapter.PASTE, "STH");
72       if (al != null && al_input != null)
73       {
74         System.out.println("Alignment contains: " + al.getHeight()
75                 + " and " + al_input.getHeight() + " sequences; "
76                 + al.getWidth() + " and " + al_input.getWidth()
77                 + " columns.");
78         AlignmentAnnotation[] aa_new = al_input.getAlignmentAnnotation();
79         AlignmentAnnotation[] aa_original = al.getAlignmentAnnotation();
80
81         // check Alignment annotation
82         if (aa_new != null && aa_original != null)
83         {
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++)
88           {
89             assertTrue("Different alignment annotation",equalss(aa_original[i], aa_new[i]));
90               
91           }
92         }
93
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;
101         //
102         for (int i = 0; i < al.getSequencesArray().length; i++)
103         {
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 + "-"
108                   + end);
109
110           // search equal sequence
111           for (int in = 0; in < al_input.getSequencesArray().length; in++)
112           {
113             if (name.equals(seq_new[in].getName())
114                     && start == seq_new[in].getStart()
115                     && end == seq_new[in].getEnd())
116             {
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));
121               
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)
126               {
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();
135
136                 assertTrue("different number of features", seq_original[i].getSequenceFeatures().length == seq_new[in]
137                         .getSequenceFeatures().length);
138                 
139                 for (int feat = 0; feat < seq_original[i]
140                         .getSequenceFeatures().length; feat++)
141                 {
142                   assertTrue("Different features",sequenceFeatures_original[feat]
143                           .equals(sequenceFeatures_new[feat]));
144                 }
145               }
146
147               // compare alignment annotation
148               if (al.getSequenceAt(i).getAnnotation() != null
149                       && al_input.getSequenceAt(in).getAnnotation() != null)
150               {
151                 for (int j = 0; j < al.getSequenceAt(i).getAnnotation().length; j++)
152                 {
153                   if (al.getSequenceAt(i).getAnnotation()[j] != null
154                           && al_input.getSequenceAt(in).getAnnotation()[j] != null)
155                   {
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));
159                   }
160                 }
161               }
162               else if (al.getSequenceAt(i).getAnnotation() == null
163                       && al_input.getSequenceAt(in).getAnnotation() == null)
164               {
165                 System.out.println("No annotations");
166               }
167               else if (al.getSequenceAt(i).getAnnotation() != null
168                       && al_input.getSequenceAt(in).getAnnotation() == null)
169               {
170                 assertTrue("Annotations differed between sequences ("+al.getSequenceAt(i).getName()+") and ("+al_input.getSequenceAt(i).getName()+")", false);
171               }
172               break;
173             }
174           }
175         }
176       }
177       else
178       {
179         assertTrue("Couldn't read alignment", false);
180       }
181     } catch (Exception e)
182     {
183       e.printStackTrace();
184       assertTrue("Couln't format the alignment for output file.", false);
185     }
186   }
187
188   /*
189    * compare annotations
190    */
191   private boolean equalss(AlignmentAnnotation annot_or,
192           AlignmentAnnotation annot_new)
193   {
194     if (annot_or.annotations.length != annot_new.annotations.length)
195     {
196       return false;
197     }
198     for (int i = 0; i < annot_or.annotations.length; i++)
199     {
200       if (annot_or.annotations[i] != null
201               && annot_new.annotations[i] != null)
202       {
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))
209         {
210           return false;
211         }
212       }
213       else if (annot_or.annotations[i] == null
214               && annot_new.annotations[i] == null)
215       {
216         continue;
217       }
218       else
219       {
220         return false;
221       }
222     }
223     return true;
224   }
225 }