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