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