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;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.HashMap;
22 import org.junit.After;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
27 public class JSONFileTest
30 private int TEST_SEQ_HEIGHT = 0;
32 private int TEST_GRP_HEIGHT = 0;
34 private int TEST_ANOT_HEIGHT = 0;
36 private AlignFrame af;
40 AlignmentPanel alignPanel;
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>();
47 public void setup() throws Exception
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);
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);
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);
79 for (Sequence seq : seqs)
81 seq.setDatasetSequence(seq);
82 testSeqs.put(seq.getName(), seq);
83 // jsonFile.seqs.add(seq);
86 // create and add sequence groups
87 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
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);
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);
138 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
139 "Secondary Structure", "New description", annot);
140 // jsonFile.annotations.add(alignAnnot);
141 testAnnots.put(alignAnnot.label, alignAnnot);
143 // Alignment al = new Alignment(seqs);
144 TEST_SEQ_HEIGHT = testSeqs.size();
145 TEST_GRP_HEIGHT = testGrps.size();
146 TEST_ANOT_HEIGHT = testAnnots.size();
150 public void tearDown() throws Exception
155 public void testParse()
157 String jsonFile = "examples/example.json";
158 AppletFormatAdapter rf = new AppletFormatAdapter();
162 al = rf.readFile(jsonFile, AppletFormatAdapter.FILE,
164 } catch (IOException e)
168 assertNotNull("Couldn't read supplied alignment data.", al);
171 for (SequenceI seq : al.getSequences())
173 SequenceI expectedSeq = testSeqs.get(seq.getName());
174 Assert.assertTrue("Failed Sequence Test for >>> " + seq.getName(),
175 isSeqMatched(expectedSeq, seq));
178 Assert.assertEquals("Some Sequences did not pass the test",
179 TEST_SEQ_HEIGHT, passedCount);
182 for (SequenceGroup seqGrp : al.getGroups())
184 SequenceGroup expectedGrp = testGrps.get(seqGrp.getName());
186 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
187 isGroupMatched(expectedGrp, seqGrp));
190 Assert.assertEquals("Some SequenceGroups did not pass the test",
191 TEST_GRP_HEIGHT, passedCount);
194 for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
196 AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label);
197 Assert.assertTrue("Failed AlignmentAnnotation Test for >>> "
198 + annot.label, isAnnotationMatched(expectedAnnot, annot));
201 Assert.assertEquals("Some Sequences did not pass the test",
202 TEST_ANOT_HEIGHT, passedCount);
204 // af = new AlignFrame(al, 700, 500);
205 // AlignViewport viewport = af.getViewport();
206 // alignPanel = new AlignmentPanel(af, viewport);
209 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
210 AlignmentAnnotation annot)
212 if (!eAnnot.label.equals(annot.label)
213 || !eAnnot.description.equals(annot.description)
214 || eAnnot.annotations.length != annot.annotations.length)
219 for (int x = 0; x < annot.annotations.length; x++)
221 Annotation y = annot.annotations[x];
222 Annotation z = annot.annotations[x];
224 if (!y.displayCharacter.equals(z.displayCharacter)
225 || y.value != z.value
226 || y.secondaryStructure != z.secondaryStructure)
234 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
236 System.out.println("Testing >>> " + actualSeq.getName());
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))
250 public boolean isGroupMatched(SequenceGroup expectedGrp,
251 SequenceGroup actualGrp)
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());
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())
285 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
287 boolean matched = false;
290 if (seq1 == null && seq2 == null)
295 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
296 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
298 if (inFeature == null && outFeature == null)
302 else if ((inFeature == null && outFeature != null)
303 || (inFeature != null && outFeature == null))
308 int testSize = inFeature.length;
309 int matchedCount = 0;
310 for (SequenceFeature in : inFeature)
312 for (SequenceFeature out : outFeature)
314 System.out.println(out.getType() + " | " + in.getType());
315 System.out.println(out.getBegin() + " | " + in.getBegin());
316 System.out.println(out.getEnd() + " | " + in.getEnd());
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()))
330 System.out.println("matched count >>>>>> " + matchedCount);
331 if (testSize == matchedCount)
335 } catch (Exception e)
339 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);