JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / jalview / io / JSONFileTest.java
1 package jalview.io;
2
3
4 import static org.testng.AssertJUnit.assertNotNull;
5 import org.testng.annotations.AfterMethod;
6 import org.testng.annotations.Test;
7 import org.testng.annotations.BeforeMethod;
8 import org.testng.AssertJUnit;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentAnnotation;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.Annotation;
13 import jalview.datamodel.Sequence;
14 import jalview.datamodel.SequenceFeature;
15 import jalview.datamodel.SequenceGroup;
16 import jalview.datamodel.SequenceI;
17 import jalview.gui.AlignFrame;
18 import jalview.gui.AlignmentPanel;
19 import jalview.schemes.ColourSchemeI;
20 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25
26 public class JSONFileTest
27 {
28
29   private int TEST_SEQ_HEIGHT = 0;
30
31   private int TEST_GRP_HEIGHT = 0;
32
33   private int TEST_ANOT_HEIGHT = 0;
34
35   private AlignFrame af;
36
37   AlignmentI alignment;
38
39   AlignmentPanel alignPanel;
40
41   HashMap<String, SequenceI> testSeqs = new HashMap<String, SequenceI>();
42   HashMap<String, AlignmentAnnotation> testAnnots = new HashMap<String, AlignmentAnnotation>();
43   HashMap<String, SequenceGroup> testGrps = new HashMap<String, SequenceGroup>();
44
45   @BeforeMethod
46   public void setup() throws Exception
47   {
48     // create and add sequences
49     Sequence[] seqs = new Sequence[5];
50     seqs[0] = new Sequence("FER_CAPAN",
51             "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34);
52     seqs[1] = new Sequence("FER1_SOLLC",
53             "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34);
54     seqs[2] = new Sequence("Q93XJ9_SOLTU",
55             "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34);
56     seqs[3] = new Sequence("FER1_PEA",
57             "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37);
58     seqs[4] = new Sequence("Q7XA98_TRIPR",
59             "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
60
61     // create and add sequence features
62     SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
63             "desciption", "status", 6, 15, "Jalview");
64     SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
65             "desciption", "status", 9, 18, "Jalview");
66     SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
67             "desciption", "status", 9, 18, "Jalview");
68     seqs[2].addSequenceFeature(seqFeature2);
69     seqs[3].addSequenceFeature(seqFeature3);
70     seqs[4].addSequenceFeature(seqFeature4);
71
72     // add created features to features displayed
73     FeaturesDisplayed fDis = new FeaturesDisplayed();
74     fDis.setVisible("feature_x");
75     // jsonFile.setDisplayedFeatures(fDis);
76     // jsonFile.setShowSeqFeatures(true);
77
78     for (Sequence seq : seqs)
79     {
80       seq.setDatasetSequence(seq);
81       testSeqs.put(seq.getName(), seq);
82       // jsonFile.seqs.add(seq);
83     }
84
85     // create and add sequence groups
86     ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
87     grpSeqs.add(seqs[1]);
88     grpSeqs.add(seqs[2]);
89     grpSeqs.add(seqs[3]);
90     grpSeqs.add(seqs[4]);
91     ColourSchemeI scheme = JSONFile.getJalviewColorScheme("zappo");
92     SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585",
93             scheme, true, true, false, 21, 29);
94     seqGrp.setShowNonconserved(false);
95     seqGrp.setDescription(null);
96     // jsonFile.seqGroups.add(seqGrp);
97     testGrps.put(seqGrp.getName(), seqGrp);
98
99     // create and add annotation
100     Annotation[] annot = new Annotation[35];
101     annot[0] = new Annotation("", "", '\u0000', 0);
102     annot[1] = new Annotation("", "", '\u0000', 0);
103     annot[2] = new Annotation("α", "", 'H', 0);
104     annot[3] = new Annotation("α", "", 'H', 0);
105     annot[4] = new Annotation("α", "", 'H', 0);
106     annot[5] = new Annotation("", "", '\u0000', 0);
107     annot[6] = new Annotation("", "", '\u0000', 0);
108     annot[7] = new Annotation("", "", '\u0000', 0);
109     annot[8] = new Annotation("β", "", 'E', 0);
110     annot[9] = new Annotation("β", "", 'E', 0);
111     annot[10] = new Annotation("β", "", 'E', 0);
112     annot[11] = new Annotation("β", "", 'E', 0);
113     annot[12] = new Annotation("β", "", 'E', 0);
114     annot[13] = new Annotation("β", "", 'E', 0);
115     annot[14] = new Annotation("β", "", 'E', 0);
116     annot[15] = new Annotation("β", "", 'E', 0);
117     annot[16] = new Annotation("", "", '\u0000', 0);
118     annot[17] = new Annotation("", "", '\u0000', 0);
119     annot[18] = new Annotation("", "", '\u0000', 0);
120     annot[19] = new Annotation("", "", '\u0000', 0);
121     annot[20] = new Annotation("", "", '\u0000', 0);
122     annot[21] = new Annotation("", "", '\u0000', 0);
123     annot[22] = new Annotation("", "", '\u0000', 0);
124     annot[23] = new Annotation("", "", '\u0000', 0);
125     annot[24] = new Annotation("", "", '\u0000', 0);
126     annot[25] = new Annotation("", "", '\u0000', 0);
127     annot[26] = new Annotation("α", "", 'H', 0);
128     annot[27] = new Annotation("α", "", 'H', 0);
129     annot[28] = new Annotation("α", "", 'H', 0);
130     annot[29] = new Annotation("α", "", 'H', 0);
131     annot[30] = new Annotation("α", "", 'H', 0);
132     annot[31] = new Annotation("", "", '\u0000', 0);
133     annot[32] = new Annotation("", "", '\u0000', 0);
134     annot[33] = new Annotation("", "", '\u0000', 0);
135     annot[34] = new Annotation("", "", '\u0000', 0);
136
137     AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
138             "Secondary Structure", "New description", annot);
139     // jsonFile.annotations.add(alignAnnot);
140     testAnnots.put(alignAnnot.label, alignAnnot);
141
142     // Alignment al = new Alignment(seqs);
143     TEST_SEQ_HEIGHT = testSeqs.size();
144     TEST_GRP_HEIGHT = testGrps.size();
145     TEST_ANOT_HEIGHT = testAnnots.size();
146   }
147
148   @AfterMethod
149   public void tearDown() throws Exception
150   {
151   }
152
153   @Test
154   public void testParse()
155   {
156     String jsonFile = "examples/example.json";
157     AppletFormatAdapter rf = new AppletFormatAdapter();
158     Alignment al = null;
159     try
160     {
161       al = rf.readFile(jsonFile, AppletFormatAdapter.FILE,
162               JSONFile.FILE_DESC);
163     } catch (IOException e)
164     {
165       e.printStackTrace();
166     }
167     assertNotNull("Couldn't read supplied alignment data.", al);
168
169     int passedCount = 0;
170     for (SequenceI seq : al.getSequences())
171     {
172       SequenceI expectedSeq = testSeqs.get(seq.getName());
173       AssertJUnit.assertTrue("Failed Sequence Test  for >>> " + seq.getName(),
174               isSeqMatched(expectedSeq, seq));
175       passedCount++;
176     }
177     AssertJUnit.assertEquals("Some Sequences did not pass the test",
178             TEST_SEQ_HEIGHT, passedCount);
179
180     passedCount = 0;
181     for (SequenceGroup seqGrp : al.getGroups())
182     {
183       SequenceGroup expectedGrp = testGrps.get(seqGrp.getName());
184       AssertJUnit.assertTrue(
185               "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
186               isGroupMatched(expectedGrp, seqGrp));
187       passedCount++;
188     }
189     AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
190             TEST_GRP_HEIGHT, passedCount);
191
192     passedCount = 0;
193     for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
194     {
195       AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label);
196       AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
197               + annot.label, isAnnotationMatched(expectedAnnot, annot));
198       passedCount++;
199     }
200     AssertJUnit.assertEquals("Some Sequences did not pass the test",
201             TEST_ANOT_HEIGHT, passedCount);
202
203     // af = new AlignFrame(al, 700, 500);
204     // AlignViewport viewport = af.getViewport();
205     // alignPanel = new AlignmentPanel(af, viewport);
206   }
207
208   public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
209           AlignmentAnnotation annot)
210   {
211     if (!eAnnot.label.equals(annot.label)
212             || !eAnnot.description.equals(annot.description)
213             || eAnnot.annotations.length != annot.annotations.length)
214     {
215       return false;
216     }
217
218     for (int x = 0; x < annot.annotations.length; x++)
219     {
220       Annotation y = annot.annotations[x];
221       Annotation z = annot.annotations[x];
222
223       if (!y.displayCharacter.equals(z.displayCharacter)
224               || y.value != z.value
225               || y.secondaryStructure != z.secondaryStructure)
226       {
227         return false;
228       }
229     }
230     return true;
231   }
232
233   public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
234   {
235     System.out.println("Testing >>> " + actualSeq.getName());
236
237     if (expectedSeq.getName().equals(actualSeq.getName())
238             && expectedSeq.getSequenceAsString().equals(
239                     actualSeq.getSequenceAsString())
240             && expectedSeq.getStart() == actualSeq.getStart()
241             && expectedSeq.getEnd() == actualSeq.getEnd()
242             && featuresMatched(expectedSeq, actualSeq))
243     {
244       return true;
245     }
246     return false;
247   }
248
249   public boolean isGroupMatched(SequenceGroup expectedGrp,
250           SequenceGroup actualGrp)
251   {
252
253     System.out.println("Testing >>> " + actualGrp.getName());
254     System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
255     System.out.println(expectedGrp.getColourText() + " | "
256             + actualGrp.getColourText());
257     System.out.println(expectedGrp.getDisplayBoxes() + " | "
258             + actualGrp.getDisplayBoxes());
259     System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
260             + actualGrp.getIgnoreGapsConsensus());
261     System.out.println(expectedGrp.getSequences().size() + " | "
262             + actualGrp.getSequences().size());
263     System.out.println(expectedGrp.getStartRes() + " | "
264             + actualGrp.getStartRes());
265     System.out.println(expectedGrp.getEndRes() + " | "
266             + actualGrp.getEndRes());
267
268     if (expectedGrp.getName().equals(actualGrp.getName())
269             && expectedGrp.getColourText() == actualGrp.getColourText()
270             && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
271             && expectedGrp.getIgnoreGapsConsensus() == actualGrp
272                     .getIgnoreGapsConsensus()
273             && expectedGrp.cs.equals(actualGrp.cs)
274             && expectedGrp.getSequences().size() == actualGrp
275                     .getSequences().size()
276             && expectedGrp.getStartRes() == actualGrp.getStartRes()
277             && expectedGrp.getEndRes() == actualGrp.getEndRes())
278     {
279       return true;
280     }
281     return false;
282   }
283
284   private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
285   {
286     boolean matched = false;
287     try
288     {
289       if (seq1 == null && seq2 == null)
290       {
291         return true;
292       }
293
294       SequenceFeature[] inFeature = seq1.getSequenceFeatures();
295       SequenceFeature[] outFeature = seq2.getSequenceFeatures();
296
297       if (inFeature == null && outFeature == null)
298       {
299         return true;
300       }
301       else if ((inFeature == null && outFeature != null)
302               || (inFeature != null && outFeature == null))
303       {
304         return false;
305       }
306
307       int testSize = inFeature.length;
308       int matchedCount = 0;
309       for (SequenceFeature in : inFeature)
310       {
311         for (SequenceFeature out : outFeature)
312         {
313           System.out.println(out.getType() + " | " + in.getType());
314           System.out.println(out.getBegin() + " | " + in.getBegin());
315           System.out.println(out.getEnd() + " | " + in.getEnd());
316
317           if (inFeature.length == outFeature.length
318                   && in.getBegin() == out.getBegin()
319                   && in.getEnd() == out.getEnd()
320                   && in.getScore() == out.getScore()
321                   && in.getFeatureGroup().equals(out.getFeatureGroup())
322                   && in.getType().equals(out.getType()))
323           {
324
325             ++matchedCount;
326           }
327         }
328       }
329       System.out.println("matched count >>>>>> " + matchedCount);
330       if (testSize == matchedCount)
331       {
332         matched = true;
333       }
334     } catch (Exception e)
335     {
336       e.printStackTrace();
337     }
338     // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);
339     return matched;
340   }
341 }