4 import static org.testng.AssertJUnit.assertNotNull;
6 import jalview.datamodel.Alignment;
7 import jalview.datamodel.AlignmentAnnotation;
8 import jalview.datamodel.AlignmentI;
9 import jalview.datamodel.Annotation;
10 import jalview.datamodel.Sequence;
11 import jalview.datamodel.SequenceFeature;
12 import jalview.datamodel.SequenceGroup;
13 import jalview.datamodel.SequenceI;
14 import jalview.gui.AlignFrame;
15 import jalview.gui.AlignmentPanel;
16 import jalview.schemes.ColourSchemeI;
17 import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
23 import org.testng.AssertJUnit;
24 import org.testng.annotations.AfterMethod;
25 import org.testng.annotations.BeforeMethod;
26 import org.testng.annotations.Test;
28 public class JSONFileTest
31 private int TEST_SEQ_HEIGHT = 0;
33 private int TEST_GRP_HEIGHT = 0;
35 private int TEST_ANOT_HEIGHT = 0;
37 private AlignFrame af;
41 AlignmentPanel alignPanel;
43 HashMap<String, SequenceI> testSeqs = new HashMap<String, SequenceI>();
44 HashMap<String, AlignmentAnnotation> testAnnots = new HashMap<String, AlignmentAnnotation>();
45 HashMap<String, SequenceGroup> testGrps = new HashMap<String, SequenceGroup>();
48 public void setup() throws Exception
50 // create and add sequences
51 Sequence[] seqs = new Sequence[5];
52 seqs[0] = new Sequence("FER_CAPAN",
53 "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34);
54 seqs[1] = new Sequence("FER1_SOLLC",
55 "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34);
56 seqs[2] = new Sequence("Q93XJ9_SOLTU",
57 "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34);
58 seqs[3] = new Sequence("FER1_PEA",
59 "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37);
60 seqs[4] = new Sequence("Q7XA98_TRIPR",
61 "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
63 // create and add sequence features
64 SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
65 "desciption", "status", 6, 15, "Jalview");
66 SequenceFeature seqFeature3 = new SequenceFeature("feature_x",
67 "desciption", "status", 9, 18, "Jalview");
68 SequenceFeature seqFeature4 = new SequenceFeature("feature_x",
69 "desciption", "status", 9, 18, "Jalview");
70 seqs[2].addSequenceFeature(seqFeature2);
71 seqs[3].addSequenceFeature(seqFeature3);
72 seqs[4].addSequenceFeature(seqFeature4);
74 // add created features to features displayed
75 FeaturesDisplayed fDis = new FeaturesDisplayed();
76 fDis.setVisible("feature_x");
77 // jsonFile.setDisplayedFeatures(fDis);
78 // jsonFile.setShowSeqFeatures(true);
80 for (Sequence seq : seqs)
82 seq.setDatasetSequence(seq);
83 testSeqs.put(seq.getName(), seq);
84 // jsonFile.seqs.add(seq);
87 // create and add sequence groups
88 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
93 ColourSchemeI scheme = JSONFile.getJalviewColorScheme("zappo");
94 SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1883305585",
95 scheme, true, true, false, 21, 29);
96 seqGrp.setShowNonconserved(false);
97 seqGrp.setDescription(null);
98 // jsonFile.seqGroups.add(seqGrp);
99 testGrps.put(seqGrp.getName(), seqGrp);
101 // create and add annotation
102 Annotation[] annot = new Annotation[35];
103 annot[0] = new Annotation("", "", '\u0000', 0);
104 annot[1] = new Annotation("", "", '\u0000', 0);
105 annot[2] = new Annotation("α", "", 'H', 0);
106 annot[3] = new Annotation("α", "", 'H', 0);
107 annot[4] = new Annotation("α", "", 'H', 0);
108 annot[5] = new Annotation("", "", '\u0000', 0);
109 annot[6] = new Annotation("", "", '\u0000', 0);
110 annot[7] = new Annotation("", "", '\u0000', 0);
111 annot[8] = new Annotation("β", "", 'E', 0);
112 annot[9] = new Annotation("β", "", 'E', 0);
113 annot[10] = new Annotation("β", "", 'E', 0);
114 annot[11] = new Annotation("β", "", 'E', 0);
115 annot[12] = new Annotation("β", "", 'E', 0);
116 annot[13] = new Annotation("β", "", 'E', 0);
117 annot[14] = new Annotation("β", "", 'E', 0);
118 annot[15] = new Annotation("β", "", 'E', 0);
119 annot[16] = new Annotation("", "", '\u0000', 0);
120 annot[17] = new Annotation("", "", '\u0000', 0);
121 annot[18] = new Annotation("", "", '\u0000', 0);
122 annot[19] = new Annotation("", "", '\u0000', 0);
123 annot[20] = new Annotation("", "", '\u0000', 0);
124 annot[21] = new Annotation("", "", '\u0000', 0);
125 annot[22] = new Annotation("", "", '\u0000', 0);
126 annot[23] = new Annotation("", "", '\u0000', 0);
127 annot[24] = new Annotation("", "", '\u0000', 0);
128 annot[25] = new Annotation("", "", '\u0000', 0);
129 annot[26] = new Annotation("α", "", 'H', 0);
130 annot[27] = new Annotation("α", "", 'H', 0);
131 annot[28] = new Annotation("α", "", 'H', 0);
132 annot[29] = new Annotation("α", "", 'H', 0);
133 annot[30] = new Annotation("α", "", 'H', 0);
134 annot[31] = new Annotation("", "", '\u0000', 0);
135 annot[32] = new Annotation("", "", '\u0000', 0);
136 annot[33] = new Annotation("", "", '\u0000', 0);
137 annot[34] = new Annotation("", "", '\u0000', 0);
139 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
140 "Secondary Structure", "New description", annot);
141 // jsonFile.annotations.add(alignAnnot);
142 testAnnots.put(alignAnnot.label, alignAnnot);
144 // Alignment al = new Alignment(seqs);
145 TEST_SEQ_HEIGHT = testSeqs.size();
146 TEST_GRP_HEIGHT = testGrps.size();
147 TEST_ANOT_HEIGHT = testAnnots.size();
151 public void tearDown() throws Exception
156 public void testParse()
158 String jsonFile = "examples/example.json";
159 AppletFormatAdapter rf = new AppletFormatAdapter();
163 al = rf.readFile(jsonFile, AppletFormatAdapter.FILE,
165 } catch (IOException e)
169 assertNotNull("Couldn't read supplied alignment data.", al);
172 for (SequenceI seq : al.getSequences())
174 SequenceI expectedSeq = testSeqs.get(seq.getName());
175 AssertJUnit.assertTrue("Failed Sequence Test for >>> " + seq.getName(),
176 isSeqMatched(expectedSeq, seq));
179 AssertJUnit.assertEquals("Some Sequences did not pass the test",
180 TEST_SEQ_HEIGHT, passedCount);
183 for (SequenceGroup seqGrp : al.getGroups())
185 SequenceGroup expectedGrp = testGrps.get(seqGrp.getName());
186 AssertJUnit.assertTrue(
187 "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
188 isGroupMatched(expectedGrp, seqGrp));
191 AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
192 TEST_GRP_HEIGHT, passedCount);
195 for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
197 AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label);
198 AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
199 + annot.label, isAnnotationMatched(expectedAnnot, annot));
202 AssertJUnit.assertEquals("Some Sequences did not pass the test",
203 TEST_ANOT_HEIGHT, passedCount);
205 // af = new AlignFrame(al, 700, 500);
206 // AlignViewport viewport = af.getViewport();
207 // alignPanel = new AlignmentPanel(af, viewport);
210 public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,
211 AlignmentAnnotation annot)
213 if (!eAnnot.label.equals(annot.label)
214 || !eAnnot.description.equals(annot.description)
215 || eAnnot.annotations.length != annot.annotations.length)
220 for (int x = 0; x < annot.annotations.length; x++)
222 Annotation y = annot.annotations[x];
223 Annotation z = annot.annotations[x];
225 if (!y.displayCharacter.equals(z.displayCharacter)
226 || y.value != z.value
227 || y.secondaryStructure != z.secondaryStructure)
235 public boolean isSeqMatched(SequenceI expectedSeq, SequenceI actualSeq)
237 System.out.println("Testing >>> " + actualSeq.getName());
239 if (expectedSeq.getName().equals(actualSeq.getName())
240 && expectedSeq.getSequenceAsString().equals(
241 actualSeq.getSequenceAsString())
242 && expectedSeq.getStart() == actualSeq.getStart()
243 && expectedSeq.getEnd() == actualSeq.getEnd()
244 && featuresMatched(expectedSeq, actualSeq))
251 public boolean isGroupMatched(SequenceGroup expectedGrp,
252 SequenceGroup actualGrp)
255 System.out.println("Testing >>> " + actualGrp.getName());
256 System.out.println(expectedGrp.getName() + " | " + actualGrp.getName());
257 System.out.println(expectedGrp.getColourText() + " | "
258 + actualGrp.getColourText());
259 System.out.println(expectedGrp.getDisplayBoxes() + " | "
260 + actualGrp.getDisplayBoxes());
261 System.out.println(expectedGrp.getIgnoreGapsConsensus() + " | "
262 + actualGrp.getIgnoreGapsConsensus());
263 System.out.println(expectedGrp.getSequences().size() + " | "
264 + actualGrp.getSequences().size());
265 System.out.println(expectedGrp.getStartRes() + " | "
266 + actualGrp.getStartRes());
267 System.out.println(expectedGrp.getEndRes() + " | "
268 + actualGrp.getEndRes());
270 if (expectedGrp.getName().equals(actualGrp.getName())
271 && expectedGrp.getColourText() == actualGrp.getColourText()
272 && expectedGrp.getDisplayBoxes() == actualGrp.getDisplayBoxes()
273 && expectedGrp.getIgnoreGapsConsensus() == actualGrp
274 .getIgnoreGapsConsensus()
275 && expectedGrp.cs.equals(actualGrp.cs)
276 && expectedGrp.getSequences().size() == actualGrp
277 .getSequences().size()
278 && expectedGrp.getStartRes() == actualGrp.getStartRes()
279 && expectedGrp.getEndRes() == actualGrp.getEndRes())
286 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
288 boolean matched = false;
291 if (seq1 == null && seq2 == null)
296 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
297 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
299 if (inFeature == null && outFeature == null)
303 else if ((inFeature == null && outFeature != null)
304 || (inFeature != null && outFeature == null))
309 int testSize = inFeature.length;
310 int matchedCount = 0;
311 for (SequenceFeature in : inFeature)
313 for (SequenceFeature out : outFeature)
315 System.out.println(out.getType() + " | " + in.getType());
316 System.out.println(out.getBegin() + " | " + in.getBegin());
317 System.out.println(out.getEnd() + " | " + in.getEnd());
319 if (inFeature.length == outFeature.length
320 && in.getBegin() == out.getBegin()
321 && in.getEnd() == out.getEnd()
322 && in.getScore() == out.getScore()
323 && in.getFeatureGroup().equals(out.getFeatureGroup())
324 && in.getType().equals(out.getType()))
331 System.out.println("matched count >>>>>> " + matchedCount);
332 if (testSize == matchedCount)
336 } catch (Exception e)
340 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);