JAL_1641 updated test -- added test for annotations
[jalview.git] / test / jalview / io / JSONFileTest.java
1 package jalview.io;
2
3 import jalview.datamodel.AlignmentAnnotation;
4 import jalview.datamodel.Annotation;
5 import jalview.datamodel.Sequence;
6 import jalview.datamodel.SequenceFeature;
7 import jalview.datamodel.SequenceGroup;
8 import jalview.datamodel.SequenceI;
9 import jalview.schemes.ColourSchemeI;
10 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
11
12 import java.util.ArrayList;
13
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18
19 public class JSONFileTest
20 {
21   private JSONFile jsonFile;
22
23   private int TEST_SEQ_HEIGHT = 0;
24
25   private int TEST_GRP_HEIGHT = 0;
26
27   private int TEST_ANOT_HEIGHT = 0;
28
29   @Before
30   public void setUp() throws Exception
31   {
32     jsonFile = new JSONFile();
33
34     // create and add sequences
35     Sequence[] seqs = new Sequence[5];
36     seqs[0] = new Sequence("FER_CAPAN",
37             "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34);
38     seqs[1] = new Sequence("FER1_SOLLC",
39             "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34);
40     seqs[2] = new Sequence("Q93XJ9_SOLTU",
41             "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34);
42     seqs[3] = new Sequence("FER1_PEA",
43             "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37);
44     seqs[4] = new Sequence("Q7XA98_TRIPR",
45             "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
46
47     // create and add sequence features
48     SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
49             "desciption", "status", 22, 29, "jalview");
50     SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
51             "desciption", "status", 25, 32, "jalview");
52     SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
53             "desciption", "status", 25, 32, "jalview");
54     seqs[2].addSequenceFeature(seqFeature2);
55     seqs[3].addSequenceFeature(seqFeature3);
56     seqs[4].addSequenceFeature(seqFeature4);
57
58     // add created features to features displayed
59     FeaturesDisplayed fDis = new FeaturesDisplayed();
60     fDis.setVisible("feature_x");
61     jsonFile.setDisplayedFeatures(fDis);
62     JSONFile.setSeqFeaturesEnabled(true);
63
64     for (Sequence seq : seqs)
65     {
66       seq.setDatasetSequence(seq);
67       jsonFile.seqs.add(seq);
68     }
69
70     // create and add sequence groups
71     ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
72     grpSeqs.add(seqs[0]);
73     grpSeqs.add(seqs[1]);
74     grpSeqs.add(seqs[2]);
75     ColourSchemeI scheme = jsonFile.getJalviewColorScheme("zappo");
76     SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1114606272",
77             scheme, true, true, false, 2, 9);
78     seqGrp.setShowNonconserved(false);
79     seqGrp.setDescription(null);
80     jsonFile.seqGroups.add(seqGrp);
81
82     // create and add annotation
83     Annotation[] annot = new Annotation[35];
84     annot[0] = new Annotation("", "", '\u0000', 0);
85     annot[1] = new Annotation("", "", '\u0000', 0);
86     annot[2] = new Annotation("α", "", 'H', 0);
87     annot[3] = new Annotation("α", "", 'H', 0);
88     annot[4] = new Annotation("α", "", 'H', 0);
89     annot[5] = new Annotation("α", "", 'H', 0);
90     annot[6] = new Annotation("", "", '\u0000', 0);
91     annot[7] = new Annotation("", "", '\u0000', 0);
92     annot[8] = new Annotation("", "", '\u0000', 0);
93     annot[9] = new Annotation("", "", '\u0000', 0);
94     annot[10] = new Annotation("β", "", 'E', 0);
95     annot[11] = new Annotation("β", "", 'E', 0);
96     annot[12] = new Annotation("", "", '\u0000', 0);
97     annot[13] = new Annotation("", "", '\u0000', 0);
98     annot[14] = new Annotation("", "", '\u0000', 0);
99     annot[15] = new Annotation("", "", '\u0000', 0);
100     annot[16] = new Annotation("α", "", 'H', 0);
101     annot[17] = new Annotation("α", "", 'H', 0);
102     annot[18] = new Annotation("α", "", 'H', 0);
103     annot[19] = new Annotation("α", "", 'H', 0);
104     annot[20] = new Annotation("α", "", 'H', 0);
105
106     annot[21] = new Annotation("", "", '\u0000', 0);
107     annot[22] = new Annotation("", "", '\u0000', 0);
108     annot[23] = new Annotation("", "", '\u0000', 0);
109     annot[24] = new Annotation("", "", '\u0000', 0);
110     annot[25] = new Annotation("", "", '\u0000', 0);
111     annot[26] = new Annotation("", "", '\u0000', 0);
112     annot[27] = new Annotation("", "", '\u0000', 0);
113     annot[28] = new Annotation("", "", '\u0000', 0);
114     annot[29] = new Annotation("", "", '\u0000', 0);
115     annot[30] = new Annotation("", "", '\u0000', 0);
116     annot[31] = new Annotation("", "", '\u0000', 0);
117     annot[32] = new Annotation("β", "", 'E', 0);
118     annot[33] = new Annotation("β", "", 'E', 0);
119     annot[34] = new Annotation("β", "", 'E', 0);
120
121     AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
122             "Secondary Structure", "New description", annot);
123     jsonFile.annotations.add(alignAnnot);
124
125     // Alignment al = new Alignment(seqs);
126     TEST_SEQ_HEIGHT = jsonFile.seqs.size();
127     TEST_GRP_HEIGHT = jsonFile.seqGroups.size();
128     TEST_ANOT_HEIGHT = jsonFile.annotations.size();
129   }
130
131   @After
132   public void tearDown() throws Exception
133   {
134   }
135
136   @Test
137   public void test()
138   {
139     String jsonOuput = jsonFile.print();
140     // System.out.println(">>>>>>>>>>>>>> " + jsonOuput);
141     JSONFile output = new JSONFile().parse(jsonOuput);
142
143     int matchedCounter = 0;
144     for (SequenceI in : jsonFile.getSeqs())
145     {
146       for (SequenceI out : output.getSeqs())
147       {
148         if (in.getName().equals(out.getName())
149                 && in.getSequenceAsString().equals(
150                         out.getSequenceAsString())
151                 && in.getStart() == out.getStart()
152                 && in.getEnd() == out.getEnd() && featuresMatched(in, out))
153         {
154           // System.out.println(">>>> Seq Match Detected");
155           ++matchedCounter;
156         }
157       }
158     }
159     Assert.assertTrue(matchedCounter == TEST_SEQ_HEIGHT);
160
161     matchedCounter = 0;
162     for (SequenceGroup in : jsonFile.getSeqGroups())
163     {
164       for (SequenceGroup out : output.getSeqGroups())
165       {
166         if (in.getName().equals(out.getName())
167                 && in.getColourText() == out.getColourText()
168                 && in.getDisplayBoxes() == out.getDisplayBoxes()
169                 && in.getIgnoreGapsConsensus() == out
170                         .getIgnoreGapsConsensus() && in.cs.equals(out.cs)
171                 && in.getSequences().size() == out.getSequences().size())
172         {
173           // System.out.println(">>>> Grp Match Detected");
174           ++matchedCounter;
175         }
176       }
177     }
178     Assert.assertTrue(matchedCounter == TEST_GRP_HEIGHT);
179
180     matchedCounter = 0;
181     for (AlignmentAnnotation in : jsonFile.annotations)
182     {
183       for (AlignmentAnnotation out : output.annotations)
184       {
185         try
186         {
187           // System.out.println("label >>>>> " + in.label + " | " + out.label);
188           // System.out.println("label >>>>> " + in.description + " | "
189           // + out.description);
190           // System.out.println("label >>>>> " + in.annotations.length + " | "
191           // + out.annotations.length);
192           if (in.label.equals(out.label)
193                   && in.description.equals(out.description)
194                 && in.annotations.length == out.annotations.length)
195         {
196           ++matchedCounter;
197         }
198         } catch (Exception e)
199         {
200           e.printStackTrace();
201         }
202       }
203     }
204     // System.out.println("matched >>>>> " + matchedCounter + " | "
205     // + TEST_ANOT_HEIGHT);
206     Assert.assertTrue(matchedCounter == TEST_ANOT_HEIGHT);
207
208   }
209
210   private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
211   {
212     boolean matched = false;
213     try
214     {
215       if (seq1 == null && seq2 == null)
216       {
217         return true;
218       }
219
220       SequenceFeature[] inFeature = seq1.getSequenceFeatures();
221       SequenceFeature[] outFeature = seq2.getSequenceFeatures();
222
223       if (inFeature == null && outFeature == null)
224       {
225         return true;
226       }
227       else if ((inFeature == null && outFeature != null)
228               || (inFeature != null && outFeature == null))
229       {
230         return false;
231       }
232
233       int testSize = inFeature.length;
234       int matchedCount = 0;
235       // System.out.println(">>>>>>>>>>>>> 1");
236       for (SequenceFeature in : inFeature)
237       {
238         for (SequenceFeature out : inFeature)
239         {
240           if (inFeature.length == outFeature.length
241                   && in.getBegin() == out.getBegin()
242                   && in.getEnd() == out.getEnd()
243                   && in.getScore() == out.getScore()
244                   && in.getFeatureGroup().equals(out.getFeatureGroup()))
245           {
246
247             ++matchedCount;
248           }
249         }
250       }
251       if (testSize == matchedCount)
252       {
253         matched = true;
254       }
255     } catch (Exception e)
256     {
257       e.printStackTrace();
258     }
259     // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);
260     return matched;
261   }
262 }