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