4 import static org.testng.AssertJUnit.assertNotNull;
5 import org.testng.annotations.AfterMethod;
6 import org.testng.annotations.Test;
7 import org.testng.annotations.BeforeMethod;
8 import org.testng.AssertJUnit;
9 import jalview.datamodel.Alignment;
10 import jalview.datamodel.AlignmentAnnotation;
11 import jalview.datamodel.AlignmentI;
12 import jalview.datamodel.Annotation;
13 import jalview.datamodel.Sequence;
14 import jalview.datamodel.SequenceFeature;
15 import jalview.datamodel.SequenceGroup;
16 import jalview.datamodel.SequenceI;
17 import jalview.gui.AlignFrame;
18 import jalview.gui.AlignmentPanel;
19 import jalview.schemes.ColourSchemeI;
20 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.HashMap;
26 public class JSONFileTest
29 private int TEST_SEQ_HEIGHT = 0;
31 private int TEST_GRP_HEIGHT = 0;
33 private int TEST_ANOT_HEIGHT = 0;
35 private AlignFrame af;
39 AlignmentPanel alignPanel;
41 HashMap<String, SequenceI> testSeqs = new HashMap<String, SequenceI>();
42 HashMap<String, AlignmentAnnotation> testAnnots = new HashMap<String, AlignmentAnnotation>();
43 HashMap<String, SequenceGroup> testGrps = new HashMap<String, SequenceGroup>();
46 public void setup() throws Exception
48 // create and add sequences
49 Sequence[] seqs = new Sequence[5];
50 seqs[0] = new Sequence("FER_CAPAN",
51 "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34);
52 seqs[1] = new Sequence("FER1_SOLLC",
53 "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34);
54 seqs[2] = new Sequence("Q93XJ9_SOLTU",
55 "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34);
56 seqs[3] = new Sequence("FER1_PEA",
57 "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37);
58 seqs[4] = new Sequence("Q7XA98_TRIPR",
59 "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
61 // create and add sequence features
62 SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
63 "desciption", "status", 6, 15, "Jalview");
64 SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
65 "desciption", "status", 9, 18, "Jalview");
66 SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
67 "desciption", "status", 9, 18, "Jalview");
68 seqs[2].addSequenceFeature(seqFeature2);
69 seqs[3].addSequenceFeature(seqFeature3);
70 seqs[4].addSequenceFeature(seqFeature4);
72 // add created features to features displayed
73 FeaturesDisplayed fDis = new FeaturesDisplayed();
74 fDis.setVisible("feature_x");
75 // jsonFile.setDisplayedFeatures(fDis);
76 // jsonFile.setShowSeqFeatures(true);
78 for (Sequence seq : seqs)
80 seq.setDatasetSequence(seq);
81 testSeqs.put(seq.getName(), seq);
82 // jsonFile.seqs.add(seq);
85 // create and add sequence groups
86 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
91 ColourSchemeI scheme = JSONFile.getJalviewColorScheme("zappo");
92 SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585",
93 scheme, true, true, false, 21, 29);
94 seqGrp.setShowNonconserved(false);
95 seqGrp.setDescription(null);
96 // jsonFile.seqGroups.add(seqGrp);
97 testGrps.put(seqGrp.getName(), seqGrp);
99 // create and add annotation
100 Annotation[] annot = new Annotation[35];
101 annot[0] = new Annotation("", "", '\u0000', 0);
102 annot[1] = new Annotation("", "", '\u0000', 0);
103 annot[2] = new Annotation("α", "", 'H', 0);
104 annot[3] = new Annotation("α", "", 'H', 0);
105 annot[4] = new Annotation("α", "", 'H', 0);
106 annot[5] = new Annotation("", "", '\u0000', 0);
107 annot[6] = new Annotation("", "", '\u0000', 0);
108 annot[7] = new Annotation("", "", '\u0000', 0);
109 annot[8] = new Annotation("β", "", 'E', 0);
110 annot[9] = new Annotation("β", "", 'E', 0);
111 annot[10] = new Annotation("β", "", 'E', 0);
112 annot[11] = new Annotation("β", "", 'E', 0);
113 annot[12] = new Annotation("β", "", 'E', 0);
114 annot[13] = new Annotation("β", "", 'E', 0);
115 annot[14] = new Annotation("β", "", 'E', 0);
116 annot[15] = new Annotation("β", "", 'E', 0);
117 annot[16] = new Annotation("", "", '\u0000', 0);
118 annot[17] = new Annotation("", "", '\u0000', 0);
119 annot[18] = new Annotation("", "", '\u0000', 0);
120 annot[19] = new Annotation("", "", '\u0000', 0);
121 annot[20] = new Annotation("", "", '\u0000', 0);
122 annot[21] = new Annotation("", "", '\u0000', 0);
123 annot[22] = new Annotation("", "", '\u0000', 0);
124 annot[23] = new Annotation("", "", '\u0000', 0);
125 annot[24] = new Annotation("", "", '\u0000', 0);
126 annot[25] = new Annotation("", "", '\u0000', 0);
127 annot[26] = new Annotation("α", "", 'H', 0);
128 annot[27] = new Annotation("α", "", 'H', 0);
129 annot[28] = new Annotation("α", "", 'H', 0);
130 annot[29] = new Annotation("α", "", 'H', 0);
131 annot[30] = new Annotation("α", "", 'H', 0);
132 annot[31] = new Annotation("", "", '\u0000', 0);
133 annot[32] = new Annotation("", "", '\u0000', 0);
134 annot[33] = new Annotation("", "", '\u0000', 0);
135 annot[34] = new Annotation("", "", '\u0000', 0);
137 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
138 "Secondary Structure", "New description", annot);
139 // jsonFile.annotations.add(alignAnnot);
140 testAnnots.put(alignAnnot.label, alignAnnot);
142 // Alignment al = new Alignment(seqs);
143 TEST_SEQ_HEIGHT = testSeqs.size();
144 TEST_GRP_HEIGHT = testGrps.size();
145 TEST_ANOT_HEIGHT = testAnnots.size();
149 public void tearDown() throws Exception
154 public void testParse()
156 String jsonFile = "examples/example.json";
157 AppletFormatAdapter rf = new AppletFormatAdapter();
161 al = rf.readFile(jsonFile, AppletFormatAdapter.FILE,
163 } catch (IOException e)
167 assertNotNull("Couldn't read supplied alignment data.", al);
170 for (SequenceI seq : al.getSequences())
172 SequenceI expectedSeq = testSeqs.get(seq.getName());
173 AssertJUnit.assertTrue("Failed Sequence Test for >>> " + seq.getName(),
174 isSeqMatched(expectedSeq, seq));
177 AssertJUnit.assertEquals("Some Sequences did not pass the test",
178 TEST_SEQ_HEIGHT, passedCount);
181 for (SequenceGroup seqGrp : al.getGroups())
183 SequenceGroup expectedGrp = testGrps.get(seqGrp.getName());
184 AssertJUnit.assertTrue(
185 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
186 isGroupMatched(expectedGrp, seqGrp));
189 AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
190 TEST_GRP_HEIGHT, passedCount);
193 for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
195 AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label);
196 AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
197 + annot.label, isAnnotationMatched(expectedAnnot, annot));
200 AssertJUnit.assertEquals("Some Sequences did not pass the test",
201 TEST_ANOT_HEIGHT, passedCount);
203 // af = new AlignFrame(al, 700, 500);
204 // AlignViewport viewport = af.getViewport();
205 // alignPanel = new AlignmentPanel(af, viewport);
208 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
209 AlignmentAnnotation annot)
211 if (!eAnnot.label.equals(annot.label)
212 || !eAnnot.description.equals(annot.description)
213 || eAnnot.annotations.length != annot.annotations.length)
218 for (int x = 0; x < annot.annotations.length; x++)
220 Annotation y = annot.annotations[x];
221 Annotation z = annot.annotations[x];
223 if (!y.displayCharacter.equals(z.displayCharacter)
224 || y.value != z.value
225 || y.secondaryStructure != z.secondaryStructure)
233 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
235 System.out.println("Testing >>> " + actualSeq.getName());
237 if (expectedSeq.getName().equals(actualSeq.getName())
238 && expectedSeq.getSequenceAsString().equals(
239 actualSeq.getSequenceAsString())
240 && expectedSeq.getStart() == actualSeq.getStart()
241 && expectedSeq.getEnd() == actualSeq.getEnd()
242 && featuresMatched(expectedSeq, actualSeq))
249 public boolean isGroupMatched(SequenceGroup expectedGrp,
250 SequenceGroup actualGrp)
253 System.out.println("Testing >>> " + actualGrp.getName());
254 System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
255 System.out.println(expectedGrp.getColourText() + " | "
256 + actualGrp.getColourText());
257 System.out.println(expectedGrp.getDisplayBoxes() + " | "
258 + actualGrp.getDisplayBoxes());
259 System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
260 + actualGrp.getIgnoreGapsConsensus());
261 System.out.println(expectedGrp.getSequences().size() + " | "
262 + actualGrp.getSequences().size());
263 System.out.println(expectedGrp.getStartRes() + " | "
264 + actualGrp.getStartRes());
265 System.out.println(expectedGrp.getEndRes() + " | "
266 + actualGrp.getEndRes());
268 if (expectedGrp.getName().equals(actualGrp.getName())
269 && expectedGrp.getColourText() == actualGrp.getColourText()
270 && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
271 && expectedGrp.getIgnoreGapsConsensus() == actualGrp
272 .getIgnoreGapsConsensus()
273 && expectedGrp.cs.equals(actualGrp.cs)
274 && expectedGrp.getSequences().size() == actualGrp
275 .getSequences().size()
276 && expectedGrp.getStartRes() == actualGrp.getStartRes()
277 && expectedGrp.getEndRes() == actualGrp.getEndRes())
284 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
286 boolean matched = false;
289 if (seq1 == null && seq2 == null)
294 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
295 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
297 if (inFeature == null && outFeature == null)
301 else if ((inFeature == null && outFeature != null)
302 || (inFeature != null && outFeature == null))
307 int testSize = inFeature.length;
308 int matchedCount = 0;
309 for (SequenceFeature in : inFeature)
311 for (SequenceFeature out : outFeature)
313 System.out.println(out.getType() + " | " + in.getType());
314 System.out.println(out.getBegin() + " | " + in.getBegin());
315 System.out.println(out.getEnd() + " | " + in.getEnd());
317 if (inFeature.length == outFeature.length
318 && in.getBegin() == out.getBegin()
319 && in.getEnd() == out.getEnd()
320 && in.getScore() == out.getScore()
321 && in.getFeatureGroup().equals(out.getFeatureGroup())
322 && in.getType().equals(out.getType()))
329 System.out.println("matched count >>>>>> " + matchedCount);
330 if (testSize == matchedCount)
334 } catch (Exception e)
338 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);