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;
28 public void setUp() throws Exception
30 jsonFile = new JSONFile();
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);
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);
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);
62 for (Sequence seq : seqs)
64 seq.setDatasetSequence(seq);
65 jsonFile.seqs.add(seq);
68 // create and add sequence groups
69 ArrayList<SequenceI> grpSeqs = new ArrayList<SequenceI>();
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);
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);
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);
119 AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
120 "Secondary Structure", "New description", annot);
121 jsonFile.annotations.add(alignAnnot);
123 // Alignment al = new Alignment(seqs);
124 TEST_SEQ_HEIGHT = jsonFile.seqs.size();
125 TEST_GRP_HEIGHT = jsonFile.seqGroups.size();
129 public void tearDown() throws Exception
136 String jsonOuput = jsonFile.print();
137 // System.out.println(">>>>>>>>>>>>>> " + jsonOuput);
138 JSONFile output = new JSONFile().parse(jsonOuput);
140 int matchedCounter = 0;
141 for (SequenceI in : jsonFile.getSeqs())
143 for (SequenceI out : output.getSeqs())
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))
151 // System.out.println(">>>> Seq Match Detected");
156 Assert.assertTrue(matchedCounter == TEST_SEQ_HEIGHT);
159 for (SequenceGroup in : jsonFile.getSeqGroups())
161 for (SequenceGroup out : output.getSeqGroups())
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())
170 // System.out.println(">>>> Grp Match Detected");
174 Assert.assertTrue(matchedCounter == TEST_GRP_HEIGHT);
179 private boolean featuresMatched(SequenceI seq1, SequenceI seq2)
181 boolean matched = false;
184 if (seq1 == null && seq2 == null)
189 SequenceFeature[] inFeature = seq1.getSequenceFeatures();
190 SequenceFeature[] outFeature = seq2.getSequenceFeatures();
192 if (inFeature == null && outFeature == null)
196 else if ((inFeature == null && outFeature != null)
197 || (inFeature != null && outFeature == null))
202 int testSize = inFeature.length;
203 int matchedCount = 0;
204 // System.out.println(">>>>>>>>>>>>> 1");
205 for (SequenceFeature in : inFeature)
207 for (SequenceFeature out : inFeature)
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()))
220 if (testSize == matchedCount)
224 } catch (Exception e)
228 // System.out.println(">>>>>>>>>>>>>> features matched : " + matched);