package jalview.io; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.Annotation; import jalview.datamodel.Sequence; import jalview.datamodel.SequenceFeature; import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.schemes.ColourSchemeI; import jalview.viewmodel.seqfeatures.FeaturesDisplayed; import java.util.ArrayList; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class JSONFileTest { private JSONFile jsonFile; private int TEST_SEQ_HEIGHT = 0; private int TEST_GRP_HEIGHT = 0; private int TEST_ANOT_HEIGHT = 0; @Before public void setUp() throws Exception { jsonFile = new JSONFile(); // create and add sequences Sequence[] seqs = new Sequence[5]; seqs[0] = new Sequence("FER_CAPAN", "SVSATMISTSFMPRKPAVTSL-KPIPNVGE--ALF", 3, 34); seqs[1] = new Sequence("FER1_SOLLC", "SISGTMISTSFLPRKPAVTSL-KAISNVGE--ALF", 3, 34); seqs[2] = new Sequence("Q93XJ9_SOLTU", "SISGTMISTSFLPRKPVVTSL-KAISNVGE--ALF", 3, 34); seqs[3] = new Sequence("FER1_PEA", "ALYGTAVSTSFLRTQPMPMSV-TTTKAFSN--GFL", 6, 37); seqs[4] = new Sequence("Q7XA98_TRIPR", "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39); // create and add sequence features SequenceFeature seqFeature2 = new SequenceFeature("feature_x", "desciption", "status", 22, 29, "jalview"); SequenceFeature seqFeature3 = new SequenceFeature("feature_x", "desciption", "status", 25, 32, "jalview"); SequenceFeature seqFeature4 = new SequenceFeature("feature_x", "desciption", "status", 25, 32, "jalview"); seqs[2].addSequenceFeature(seqFeature2); seqs[3].addSequenceFeature(seqFeature3); seqs[4].addSequenceFeature(seqFeature4); // add created features to features displayed FeaturesDisplayed fDis = new FeaturesDisplayed(); fDis.setVisible("feature_x"); jsonFile.setDisplayedFeatures(fDis); JSONFile.setSeqFeaturesEnabled(true); for (Sequence seq : seqs) { seq.setDatasetSequence(seq); jsonFile.seqs.add(seq); } // create and add sequence groups ArrayList grpSeqs = new ArrayList(); grpSeqs.add(seqs[0]); grpSeqs.add(seqs[1]); grpSeqs.add(seqs[2]); ColourSchemeI scheme = jsonFile.getJalviewColorScheme("zappo"); SequenceGroup seqGrp = new SequenceGroup(grpSeqs, "JGroup:1114606272", scheme, true, true, false, 2, 9); seqGrp.setShowNonconserved(false); seqGrp.setDescription(null); jsonFile.seqGroups.add(seqGrp); // create and add annotation Annotation[] annot = new Annotation[35]; annot[0] = new Annotation("", "", '\u0000', 0); annot[1] = new Annotation("", "", '\u0000', 0); annot[2] = new Annotation("α", "", 'H', 0); annot[3] = new Annotation("α", "", 'H', 0); annot[4] = new Annotation("α", "", 'H', 0); annot[5] = new Annotation("α", "", 'H', 0); annot[6] = new Annotation("", "", '\u0000', 0); annot[7] = new Annotation("", "", '\u0000', 0); annot[8] = new Annotation("", "", '\u0000', 0); annot[9] = new Annotation("", "", '\u0000', 0); annot[10] = new Annotation("β", "", 'E', 0); annot[11] = new Annotation("β", "", 'E', 0); annot[12] = new Annotation("", "", '\u0000', 0); annot[13] = new Annotation("", "", '\u0000', 0); annot[14] = new Annotation("", "", '\u0000', 0); annot[15] = new Annotation("", "", '\u0000', 0); annot[16] = new Annotation("α", "", 'H', 0); annot[17] = new Annotation("α", "", 'H', 0); annot[18] = new Annotation("α", "", 'H', 0); annot[19] = new Annotation("α", "", 'H', 0); annot[20] = new Annotation("α", "", 'H', 0); annot[21] = new Annotation("", "", '\u0000', 0); annot[22] = new Annotation("", "", '\u0000', 0); annot[23] = new Annotation("", "", '\u0000', 0); annot[24] = new Annotation("", "", '\u0000', 0); annot[25] = new Annotation("", "", '\u0000', 0); annot[26] = new Annotation("", "", '\u0000', 0); annot[27] = new Annotation("", "", '\u0000', 0); annot[28] = new Annotation("", "", '\u0000', 0); annot[29] = new Annotation("", "", '\u0000', 0); annot[30] = new Annotation("", "", '\u0000', 0); annot[31] = new Annotation("", "", '\u0000', 0); annot[32] = new Annotation("β", "", 'E', 0); annot[33] = new Annotation("β", "", 'E', 0); annot[34] = new Annotation("β", "", 'E', 0); AlignmentAnnotation alignAnnot = new AlignmentAnnotation( "Secondary Structure", "New description", annot); jsonFile.annotations.add(alignAnnot); // Alignment al = new Alignment(seqs); TEST_SEQ_HEIGHT = jsonFile.seqs.size(); TEST_GRP_HEIGHT = jsonFile.seqGroups.size(); TEST_ANOT_HEIGHT = jsonFile.annotations.size(); } @After public void tearDown() throws Exception { } @Test public void test() { String jsonOuput = jsonFile.print(); // System.out.println(">>>>>>>>>>>>>> " + jsonOuput); JSONFile output = new JSONFile().parse(jsonOuput); int matchedCounter = 0; for (SequenceI in : jsonFile.getSeqs()) { for (SequenceI out : output.getSeqs()) { if (in.getName().equals(out.getName()) && in.getSequenceAsString().equals( out.getSequenceAsString()) && in.getStart() == out.getStart() && in.getEnd() == out.getEnd() && featuresMatched(in, out)) { // System.out.println(">>>> Seq Match Detected"); ++matchedCounter; } } } Assert.assertTrue(matchedCounter == TEST_SEQ_HEIGHT); matchedCounter = 0; for (SequenceGroup in : jsonFile.getSeqGroups()) { for (SequenceGroup out : output.getSeqGroups()) { if (in.getName().equals(out.getName()) && in.getColourText() == out.getColourText() && in.getDisplayBoxes() == out.getDisplayBoxes() && in.getIgnoreGapsConsensus() == out .getIgnoreGapsConsensus() && in.cs.equals(out.cs) && in.getSequences().size() == out.getSequences().size()) { // System.out.println(">>>> Grp Match Detected"); ++matchedCounter; } } } Assert.assertTrue(matchedCounter == TEST_GRP_HEIGHT); matchedCounter = 0; for (AlignmentAnnotation in : jsonFile.annotations) { for (AlignmentAnnotation out : output.annotations) { try { // System.out.println("label >>>>> " + in.label + " | " + out.label); // System.out.println("label >>>>> " + in.description + " | " // + out.description); // System.out.println("label >>>>> " + in.annotations.length + " | " // + out.annotations.length); if (in.label.equals(out.label) && in.description.equals(out.description) && in.annotations.length == out.annotations.length) { ++matchedCounter; } } catch (Exception e) { e.printStackTrace(); } } } // System.out.println("matched >>>>> " + matchedCounter + " | " // + TEST_ANOT_HEIGHT); Assert.assertTrue(matchedCounter == TEST_ANOT_HEIGHT); } private boolean featuresMatched(SequenceI seq1, SequenceI seq2) { boolean matched = false; try { if (seq1 == null && seq2 == null) { return true; } SequenceFeature[] inFeature = seq1.getSequenceFeatures(); SequenceFeature[] outFeature = seq2.getSequenceFeatures(); if (inFeature == null && outFeature == null) { return true; } else if ((inFeature == null && outFeature != null) || (inFeature != null && outFeature == null)) { return false; } int testSize = inFeature.length; int matchedCount = 0; // System.out.println(">>>>>>>>>>>>> 1"); for (SequenceFeature in : inFeature) { for (SequenceFeature out : inFeature) { if (inFeature.length == outFeature.length && in.getBegin() == out.getBegin() && in.getEnd() == out.getEnd() && in.getScore() == out.getScore() && in.getFeatureGroup().equals(out.getFeatureGroup())) { ++matchedCount; } } } if (testSize == matchedCount) { matched = true; } } catch (Exception e) { e.printStackTrace(); } // System.out.println(">>>>>>>>>>>>>> features matched : " + matched); return matched; } }