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;
12 import java.util.ArrayList;
14 import org.junit.After;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
19 public class JSONFileTest
21 private JSONFile jsonFile;
23 private int TEST_SEQ_HEIGHT = 0;
25 private int TEST_GRP_HEIGHT = 0;
27 private int TEST_ANOT_HEIGHT = 0;
30 public void setUp() throws Exception
32 jsonFile = new JSONFile();
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);
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);
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);
64 for (Sequence seq : seqs)
66 seq.setDatasetSequence(seq);
67 jsonFile.seqs.add(seq);
70 // create and add sequence groups
71 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
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);
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);
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);
121 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
122 "Secondary Structure", "New description", annot);
123 jsonFile.annotations.add(alignAnnot);
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();
132 public void tearDown() throws Exception
139 String jsonOuput = jsonFile.print();
140 // System.out.println(">>>>>>>>>>>>>> " + jsonOuput);
141 JSONFile output = new JSONFile().parse(jsonOuput);
143 int matchedCounter = 0;
144 for (SequenceI in : jsonFile.getSeqs())
146 for (SequenceI out : output.getSeqs())
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))
154 // System.out.println(">>>> Seq Match Detected");
159 Assert.assertTrue(matchedCounter == TEST_SEQ_HEIGHT);
162 for (SequenceGroup in : jsonFile.getSeqGroups())
164 for (SequenceGroup out : output.getSeqGroups())
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())
173 // System.out.println(">>>> Grp Match Detected");
178 Assert.assertTrue(matchedCounter == TEST_GRP_HEIGHT);
181 for (AlignmentAnnotation in : jsonFile.annotations)
183 for (AlignmentAnnotation out : output.annotations)
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)
198 } catch (Exception e)
204 // System.out.println("matched >>>>> " + matchedCounter + " | "
205 // + TEST_ANOT_HEIGHT);
206 Assert.assertTrue(matchedCounter == TEST_ANOT_HEIGHT);
210 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
212 boolean matched = false;
215 if (seq1 == null && seq2 == null)
220 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
221 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
223 if (inFeature == null && outFeature == null)
227 else if ((inFeature == null && outFeature != null)
228 || (inFeature != null && outFeature == null))
233 int testSize = inFeature.length;
234 int matchedCount = 0;
235 // System.out.println(">>>>>>>>>>>>> 1");
236 for (SequenceFeature in : inFeature)
238 for (SequenceFeature out : inFeature)
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()))
251 if (testSize == matchedCount)
255 } catch (Exception e)
259 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);