+
+ @Test(groups = { "Functional" })
+ public void testCopyConstructor_noDataset()
+ {
+ SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
+ seq1.setDescription("description");
+ seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
+ 1.3d));
+ seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
+ 12.4f, "group"));
+ seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
+ seq1.addDBRef(new DBRefEntry("EMBL", "1.2", "AZ12345"));
+
+ SequenceI copy = new Sequence(seq1);
+
+ assertNull(copy.getDatasetSequence());
+
+ verifyCopiedSequence(seq1, copy);
+
+ // copy has a copy of the DBRefEntry
+ // this is murky - DBrefs are only copied for dataset sequences
+ // where the test for 'dataset sequence' is 'dataset is null'
+ // but that doesn't distinguish it from an aligned sequence
+ // which has not yet generated a dataset sequence
+ // NB getDBRef looks inside dataset sequence if not null
+ DBRefEntry[] dbrefs = copy.getDBRefs();
+ assertEquals(1, dbrefs.length);
+ assertFalse(dbrefs[0] == seq1.getDBRefs()[0]);
+ assertTrue(dbrefs[0].equals(seq1.getDBRefs()[0]));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testCopyConstructor_withDataset()
+ {
+ SequenceI seq1 = new Sequence("Seq1", "AB-C.D EF");
+ seq1.createDatasetSequence();
+ seq1.setDescription("description");
+ seq1.addAlignmentAnnotation(new AlignmentAnnotation("label", "desc",
+ 1.3d));
+ // JAL-2046 - what is the contract for using a derived sequence's
+ // addSequenceFeature ?
+ seq1.addSequenceFeature(new SequenceFeature("type", "desc", 22, 33,
+ 12.4f, "group"));
+ seq1.addPDBId(new PDBEntry("1A70", "B", Type.PDB, "File"));
+ // here we add DBRef to the dataset sequence:
+ seq1.getDatasetSequence().addDBRef(
+ new DBRefEntry("EMBL", "1.2", "AZ12345"));
+
+ SequenceI copy = new Sequence(seq1);
+
+ assertNotNull(copy.getDatasetSequence());
+ assertSame(copy.getDatasetSequence(), seq1.getDatasetSequence());
+
+ verifyCopiedSequence(seq1, copy);
+
+ // getDBRef looks inside dataset sequence and this is shared,
+ // so holds the same dbref objects
+ DBRefEntry[] dbrefs = copy.getDBRefs();
+ assertEquals(1, dbrefs.length);
+ assertSame(dbrefs[0], seq1.getDBRefs()[0]);
+ }
+
+ /**
+ * Helper to make assertions about a copied sequence
+ *
+ * @param seq1
+ * @param copy
+ */
+ protected void verifyCopiedSequence(SequenceI seq1, SequenceI copy)
+ {
+ // verify basic properties:
+ assertEquals(copy.getName(), seq1.getName());
+ assertEquals(copy.getDescription(), seq1.getDescription());
+ assertEquals(copy.getStart(), seq1.getStart());
+ assertEquals(copy.getEnd(), seq1.getEnd());
+ assertEquals(copy.getSequenceAsString(), seq1.getSequenceAsString());
+
+ // copy has a copy of the annotation:
+ AlignmentAnnotation[] anns = copy.getAnnotation();
+ assertEquals(1, anns.length);
+ assertFalse(anns[0] == seq1.getAnnotation()[0]);
+ assertEquals(anns[0].label, seq1.getAnnotation()[0].label);
+ assertEquals(anns[0].description, seq1.getAnnotation()[0].description);
+ assertEquals(anns[0].score, seq1.getAnnotation()[0].score);
+
+ // copy has a copy of the sequence feature:
+ List<SequenceFeature> sfs = copy.getSequenceFeatures();
+ assertEquals(1, sfs.size());
+ if (seq1.getDatasetSequence() != null
+ && copy.getDatasetSequence() == seq1.getDatasetSequence())
+ {
+ assertSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
+ }
+ else
+ {
+ assertNotSame(sfs.get(0), seq1.getSequenceFeatures().get(0));
+ }
+ assertEquals(sfs.get(0), seq1.getSequenceFeatures().get(0));
+
+ // copy has a copy of the PDB entry
+ Vector<PDBEntry> pdbs = copy.getAllPDBEntries();
+ assertEquals(1, pdbs.size());
+ assertFalse(pdbs.get(0) == seq1.getAllPDBEntries().get(0));
+ assertTrue(pdbs.get(0).equals(seq1.getAllPDBEntries().get(0)));
+ }
+
+ @Test(groups = "Functional")
+ public void testGetCharAt()
+ {
+ SequenceI sq = new Sequence("", "abcde");
+ assertEquals('a', sq.getCharAt(0));
+ assertEquals('e', sq.getCharAt(4));
+ assertEquals(' ', sq.getCharAt(5));
+ assertEquals(' ', sq.getCharAt(-1));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testAddSequenceFeatures()
+ {
+ SequenceI sq = new Sequence("", "abcde");
+ // type may not be null
+ assertFalse(sq.addSequenceFeature(new SequenceFeature(null, "desc", 4,
+ 8, 0f, null)));
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
+ 8, 0f, null)));
+ // can't add a duplicate feature
+ assertFalse(sq.addSequenceFeature(new SequenceFeature("Cath", "desc",
+ 4, 8, 0f, null)));
+ // can add a different feature
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Scop", "desc", 4,
+ 8, 0f, null))); // different type
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath",
+ "description", 4, 8, 0f, null)));// different description
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 3,
+ 8, 0f, null))); // different start position
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
+ 9, 0f, null))); // different end position
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
+ 8, 1f, null))); // different score
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
+ 8, Float.NaN, null))); // score NaN
+ assertTrue(sq.addSequenceFeature(new SequenceFeature("Cath", "desc", 4,
+ 8, 0f, "Metal"))); // different group
+ assertEquals(8, sq.getFeatures().getAllFeatures().size());
+ }
+
+ /**
+ * Tests for adding (or updating) dbrefs
+ *
+ * @see DBRefEntry#updateFrom(DBRefEntry)
+ */
+ @Test(groups = { "Functional" })
+ public void testAddDBRef()
+ {
+ SequenceI sq = new Sequence("", "abcde");
+ assertNull(sq.getDBRefs());
+ DBRefEntry dbref = new DBRefEntry("Uniprot", "1", "P00340");
+ sq.addDBRef(dbref);
+ assertEquals(1, sq.getDBRefs().length);
+ assertSame(dbref, sq.getDBRefs()[0]);
+
+ /*
+ * change of version - new entry
+ */
+ DBRefEntry dbref2 = new DBRefEntry("Uniprot", "2", "P00340");
+ sq.addDBRef(dbref2);
+ assertEquals(2, sq.getDBRefs().length);
+ assertSame(dbref, sq.getDBRefs()[0]);
+ assertSame(dbref2, sq.getDBRefs()[1]);
+
+ /*
+ * matches existing entry - not added
+ */
+ sq.addDBRef(new DBRefEntry("UNIPROT", "1", "p00340"));
+ assertEquals(2, sq.getDBRefs().length);
+
+ /*
+ * different source = new entry
+ */
+ DBRefEntry dbref3 = new DBRefEntry("UniRef", "1", "p00340");
+ sq.addDBRef(dbref3);
+ assertEquals(3, sq.getDBRefs().length);
+ assertSame(dbref3, sq.getDBRefs()[2]);
+
+ /*
+ * different ref = new entry
+ */
+ DBRefEntry dbref4 = new DBRefEntry("UniRef", "1", "p00341");
+ sq.addDBRef(dbref4);
+ assertEquals(4, sq.getDBRefs().length);
+ assertSame(dbref4, sq.getDBRefs()[3]);
+
+ /*
+ * matching ref with a mapping - map updated
+ */
+ DBRefEntry dbref5 = new DBRefEntry("UniRef", "1", "p00341");
+ Mapping map = new Mapping(new MapList(new int[] { 1, 3 }, new int[] {
+ 1, 1 }, 3, 1));
+ dbref5.setMap(map);
+ sq.addDBRef(dbref5);
+ assertEquals(4, sq.getDBRefs().length);
+ assertSame(dbref4, sq.getDBRefs()[3]);
+ assertSame(map, dbref4.getMap());
+
+ /*
+ * 'real' version replaces "0" version
+ */
+ dbref2.setVersion("0");
+ DBRefEntry dbref6 = new DBRefEntry(dbref2.getSource(), "3",
+ dbref2.getAccessionId());
+ sq.addDBRef(dbref6);
+ assertEquals(4, sq.getDBRefs().length);
+ assertSame(dbref2, sq.getDBRefs()[1]);
+ assertEquals("3", dbref2.getVersion());
+
+ /*
+ * 'real' version replaces "source:0" version
+ */
+ dbref3.setVersion("Uniprot:0");
+ DBRefEntry dbref7 = new DBRefEntry(dbref3.getSource(), "3",
+ dbref3.getAccessionId());
+ sq.addDBRef(dbref7);
+ assertEquals(4, sq.getDBRefs().length);
+ assertSame(dbref3, sq.getDBRefs()[2]);
+ assertEquals("3", dbref2.getVersion());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testGetPrimaryDBRefs_peptide()
+ {
+ SequenceI sq = new Sequence("aseq", "ASDFKYLMQPRST", 10, 22);
+
+ // no dbrefs
+ List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
+ assertTrue(primaryDBRefs.isEmpty());
+
+ // empty dbrefs
+ sq.setDBRefs(new DBRefEntry[] {});
+ primaryDBRefs = sq.getPrimaryDBRefs();
+ assertTrue(primaryDBRefs.isEmpty());
+
+ // primary - uniprot
+ DBRefEntry upentry1 = new DBRefEntry("UNIPROT", "0", "Q04760");
+ sq.addDBRef(upentry1);
+
+ // primary - uniprot with congruent map
+ DBRefEntry upentry2 = new DBRefEntry("UNIPROT", "0", "Q04762");
+ upentry2.setMap(new Mapping(null, new MapList(new int[] { 10, 22 },
+ new int[] { 10, 22 }, 1, 1)));
+ sq.addDBRef(upentry2);
+
+ // primary - uniprot with map of enclosing sequence
+ DBRefEntry upentry3 = new DBRefEntry("UNIPROT", "0", "Q04763");
+ upentry3.setMap(new Mapping(null, new MapList(new int[] { 8, 24 },
+ new int[] { 8, 24 }, 1, 1)));
+ sq.addDBRef(upentry3);
+
+ // not primary - uniprot with map of sub-sequence (5')
+ DBRefEntry upentry4 = new DBRefEntry("UNIPROT", "0", "Q04764");
+ upentry4.setMap(new Mapping(null, new MapList(new int[] { 10, 18 },
+ new int[] { 10, 18 }, 1, 1)));
+ sq.addDBRef(upentry4);
+
+ // not primary - uniprot with map that overlaps 3'
+ DBRefEntry upentry5 = new DBRefEntry("UNIPROT", "0", "Q04765");
+ upentry5.setMap(new Mapping(null, new MapList(new int[] { 12, 22 },
+ new int[] { 12, 22 }, 1, 1)));
+ sq.addDBRef(upentry5);
+
+ // not primary - uniprot with map to different coordinates frame
+ DBRefEntry upentry6 = new DBRefEntry("UNIPROT", "0", "Q04766");
+ upentry6.setMap(new Mapping(null, new MapList(new int[] { 12, 18 },
+ new int[] { 112, 118 }, 1, 1)));
+ sq.addDBRef(upentry6);
+
+ // not primary - dbref to 'non-core' database
+ DBRefEntry upentry7 = new DBRefEntry("Pfam", "0", "PF00903");
+ sq.addDBRef(upentry7);
+
+ // primary - type is PDB
+ DBRefEntry pdbentry = new DBRefEntry("PDB", "0", "1qip");
+ sq.addDBRef(pdbentry);
+
+ // not primary - PDBEntry has no file
+ sq.addDBRef(new DBRefEntry("PDB", "0", "1AAA"));
+
+ // not primary - no PDBEntry
+ sq.addDBRef(new DBRefEntry("PDB", "0", "1DDD"));
+
+ // add corroborating PDB entry for primary DBref -
+ // needs to have a file as well as matching ID
+ // note PDB ID is not treated as case sensitive
+ sq.addPDBId(new PDBEntry("1QIP", null, Type.PDB, new File("/blah")
+ .toString()));
+
+ // not valid DBRef - no file..
+ sq.addPDBId(new PDBEntry("1AAA", null, null, null));
+
+ primaryDBRefs = sq.getPrimaryDBRefs();
+ assertEquals(4, primaryDBRefs.size());
+ assertTrue("Couldn't find simple primary reference (UNIPROT)",
+ primaryDBRefs.contains(upentry1));
+ assertTrue("Couldn't find mapped primary reference (UNIPROT)",
+ primaryDBRefs.contains(upentry2));
+ assertTrue("Couldn't find mapped context reference (UNIPROT)",
+ primaryDBRefs.contains(upentry3));
+ assertTrue("Couldn't find expected PDB primary reference",
+ primaryDBRefs.contains(pdbentry));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testGetPrimaryDBRefs_nucleotide()
+ {
+ SequenceI sq = new Sequence("aseq", "TGATCACTCGACTAGCATCAGCATA", 10, 34);
+
+ // primary - Ensembl
+ DBRefEntry dbr1 = new DBRefEntry("ENSEMBL", "0", "ENSG1234");
+ sq.addDBRef(dbr1);
+
+ // not primary - Ensembl 'transcript' mapping of sub-sequence
+ DBRefEntry dbr2 = new DBRefEntry("ENSEMBL", "0", "ENST1234");
+ dbr2.setMap(new Mapping(null, new MapList(new int[] { 15, 25 },
+ new int[] { 1, 11 }, 1, 1)));
+ sq.addDBRef(dbr2);
+
+ // primary - EMBL with congruent map
+ DBRefEntry dbr3 = new DBRefEntry("EMBL", "0", "J1234");
+ dbr3.setMap(new Mapping(null, new MapList(new int[] { 10, 34 },
+ new int[] { 10, 34 }, 1, 1)));
+ sq.addDBRef(dbr3);
+
+ // not primary - to non-core database
+ DBRefEntry dbr4 = new DBRefEntry("CCDS", "0", "J1234");
+ sq.addDBRef(dbr4);
+
+ // not primary - to protein
+ DBRefEntry dbr5 = new DBRefEntry("UNIPROT", "0", "Q87654");
+ sq.addDBRef(dbr5);
+
+ List<DBRefEntry> primaryDBRefs = sq.getPrimaryDBRefs();
+ assertEquals(2, primaryDBRefs.size());
+ assertTrue(primaryDBRefs.contains(dbr1));
+ assertTrue(primaryDBRefs.contains(dbr3));
+ }
+
+ /**
+ * Test the method that updates the list of PDBEntry from any new DBRefEntry
+ * for PDB
+ */
+ @Test(groups = { "Functional" })
+ public void testUpdatePDBIds()
+ {
+ PDBEntry pdbe1 = new PDBEntry("3A6S", null, null, null);
+ seq.addPDBId(pdbe1);
+ seq.addDBRef(new DBRefEntry("Ensembl", "8", "ENST1234"));
+ seq.addDBRef(new DBRefEntry("PDB", "0", "1A70"));
+ seq.addDBRef(new DBRefEntry("PDB", "0", "4BQGa"));
+ seq.addDBRef(new DBRefEntry("PDB", "0", "3a6sB"));
+ // 7 is not a valid chain code:
+ seq.addDBRef(new DBRefEntry("PDB", "0", "2GIS7"));
+
+ seq.updatePDBIds();
+ List<PDBEntry> pdbIds = seq.getAllPDBEntries();
+ assertEquals(4, pdbIds.size());
+ assertSame(pdbe1, pdbIds.get(0));
+ // chain code got added to 3A6S:
+ assertEquals("B", pdbe1.getChainCode());
+ assertEquals("1A70", pdbIds.get(1).getId());
+ // 4BQGA is parsed into id + chain
+ assertEquals("4BQG", pdbIds.get(2).getId());
+ assertEquals("a", pdbIds.get(2).getChainCode());
+ assertEquals("2GIS7", pdbIds.get(3).getId());
+ assertNull(pdbIds.get(3).getChainCode());
+ }
+
+ /**
+ * Test the method that either adds a pdbid or updates an existing one
+ */
+ @Test(groups = { "Functional" })
+ public void testAddPDBId()
+ {
+ PDBEntry pdbe = new PDBEntry("3A6S", null, null, null);
+ seq.addPDBId(pdbe);
+ assertEquals(1, seq.getAllPDBEntries().size());
+ assertSame(pdbe, seq.getPDBEntry("3A6S"));
+ assertSame(pdbe, seq.getPDBEntry("3a6s")); // case-insensitive
+
+ // add the same entry
+ seq.addPDBId(pdbe);
+ assertEquals(1, seq.getAllPDBEntries().size());
+ assertSame(pdbe, seq.getPDBEntry("3A6S"));
+
+ // add an identical entry
+ seq.addPDBId(new PDBEntry("3A6S", null, null, null));
+ assertEquals(1, seq.getAllPDBEntries().size());
+ assertSame(pdbe, seq.getPDBEntry("3A6S"));
+
+ // add a different entry
+ PDBEntry pdbe2 = new PDBEntry("1A70", null, null, null);
+ seq.addPDBId(pdbe2);
+ assertEquals(2, seq.getAllPDBEntries().size());
+ assertSame(pdbe, seq.getAllPDBEntries().get(0));
+ assertSame(pdbe2, seq.getAllPDBEntries().get(1));
+
+ // update pdbe with chain code, file, type
+ PDBEntry pdbe3 = new PDBEntry("3a6s", "A", Type.PDB, "filepath");
+ seq.addPDBId(pdbe3);
+ assertEquals(2, seq.getAllPDBEntries().size());
+ assertSame(pdbe, seq.getAllPDBEntries().get(0)); // updated in situ
+ assertEquals("3A6S", pdbe.getId()); // unchanged
+ assertEquals("A", pdbe.getChainCode()); // updated
+ assertEquals(Type.PDB.toString(), pdbe.getType()); // updated
+ assertEquals("filepath", pdbe.getFile()); // updated
+ assertSame(pdbe2, seq.getAllPDBEntries().get(1));
+
+ // add with a different file path
+ PDBEntry pdbe4 = new PDBEntry("3a6s", "A", Type.PDB, "filepath2");
+ seq.addPDBId(pdbe4);
+ assertEquals(3, seq.getAllPDBEntries().size());
+ assertSame(pdbe4, seq.getAllPDBEntries().get(2));
+
+ // add with a different chain code
+ PDBEntry pdbe5 = new PDBEntry("3a6s", "B", Type.PDB, "filepath");
+ seq.addPDBId(pdbe5);
+ assertEquals(4, seq.getAllPDBEntries().size());
+ assertSame(pdbe5, seq.getAllPDBEntries().get(3));
+ }
+
+ @Test(
+ groups = { "Functional" },
+ expectedExceptions = { IllegalArgumentException.class })
+ public void testSetDatasetSequence_toSelf()
+ {
+ seq.setDatasetSequence(seq);
+ }
+
+ @Test(
+ groups = { "Functional" },
+ expectedExceptions = { IllegalArgumentException.class })
+ public void testSetDatasetSequence_cascading()
+ {
+ SequenceI seq2 = new Sequence("Seq2", "xyz");
+ seq2.createDatasetSequence();
+ seq.setDatasetSequence(seq2);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testFindFeatures()
+ {
+ SequenceI sq = new Sequence("test/8-16", "-ABC--DEF--GHI--");
+ sq.createDatasetSequence();
+
+ assertTrue(sq.findFeatures(1, 99).isEmpty());
+
+ // add non-positional feature
+ SequenceFeature sf0 = new SequenceFeature("Cath", "desc", 0, 0, 2f,
+ null);
+ sq.addSequenceFeature(sf0);
+ // add feature on BCD
+ SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 9, 11, 2f,
+ null);
+ sq.addSequenceFeature(sfBCD);
+ // add feature on DE
+ SequenceFeature sfDE = new SequenceFeature("Cath", "desc", 11, 12, 2f,
+ null);
+ sq.addSequenceFeature(sfDE);
+ // add contact feature at [B, H]
+ SequenceFeature sfContactBH = new SequenceFeature("Disulphide bond",
+ "desc", 9, 15, 2f, null);
+ sq.addSequenceFeature(sfContactBH);
+ // add contact feature at [F, G]
+ SequenceFeature sfContactFG = new SequenceFeature("Disulfide Bond",
+ "desc", 13, 14, 2f, null);
+ sq.addSequenceFeature(sfContactFG);
+ // add single position feature at [I]
+ SequenceFeature sfI = new SequenceFeature("Disulfide Bond",
+ "desc", 16, 16, null);
+ sq.addSequenceFeature(sfI);
+
+ // no features in columns 1-2 (-A)
+ List<SequenceFeature> found = sq.findFeatures(1, 2);
+ assertTrue(found.isEmpty());
+
+ // columns 1-6 (-ABC--) includes BCD and B/H feature but not DE
+ found = sq.findFeatures(1, 6);
+ assertEquals(2, found.size());
+ assertTrue(found.contains(sfBCD));
+ assertTrue(found.contains(sfContactBH));
+
+ // columns 5-6 (--) includes (enclosing) BCD but not (contact) B/H feature
+ found = sq.findFeatures(5, 6);
+ assertEquals(1, found.size());
+ assertTrue(found.contains(sfBCD));
+
+ // columns 7-10 (DEF-) includes BCD, DE, F/G but not B/H feature
+ found = sq.findFeatures(7, 10);
+ assertEquals(3, found.size());
+ assertTrue(found.contains(sfBCD));
+ assertTrue(found.contains(sfDE));
+ assertTrue(found.contains(sfContactFG));
+
+ // columns 10-11 (--) should find nothing
+ found = sq.findFeatures(10, 11);
+ assertEquals(0, found.size());
+
+ // columns 14-14 (I) should find variant feature
+ found = sq.findFeatures(14, 14);
+ assertEquals(1, found.size());
+ assertTrue(found.contains(sfI));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testFindIndex_withCursor()
+ {
+ Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
+
+ // find F given A
+ assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, 0)));
+
+ // find A given F
+ assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, 0)));
+
+ // find C given C
+ assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, 0)));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testFindPosition_withCursor()
+ {
+ Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
+
+ // find F pos given A - lastCol gets set in cursor
+ assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ // find A pos given F - first residue column is saved in cursor
+ assertEquals(8, sq.findPosition(2, new SequenceCursor(sq, 13, 10, 0)));
+ assertEquals("test:Pos8:Col2:startCol2:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ // find C pos given C (neither startCol nor endCol is set)
+ assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 10, 6, 0)));
+ assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ // now the grey area - what residue position for a gapped column? JAL-2562
+
+ // find 'residue' for column 3 given cursor for D (so working left)
+ // returns B9; cursor is updated to [B 5]
+ assertEquals(9, sq.findPosition(3, new SequenceCursor(sq, 11, 7, 0)));
+ assertEquals("test:Pos9:Col5:startCol0:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ // find 'residue' for column 8 given cursor for D (so working right)
+ // returns E12; cursor is updated to [D 7]
+ assertEquals(12, sq.findPosition(8, new SequenceCursor(sq, 11, 7, 0)));
+ assertEquals("test:Pos11:Col7:startCol0:endCol0:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ // find 'residue' for column 12 given cursor for B
+ // returns 1 more than last residue position; cursor is updated to [F 10]
+ // lastCol position is saved in cursor
+ assertEquals(14, sq.findPosition(12, new SequenceCursor(sq, 9, 5, 0)));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ /*
+ * findPosition for column beyond length of sequence
+ * returns 1 more than the last residue position
+ * cursor is set to last real residue position [F 10]
+ */
+ assertEquals(14, sq.findPosition(99, new SequenceCursor(sq, 8, 2, 0)));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
+
+ /*
+ * and the case without a trailing gap
+ */
+ sq = new Sequence("test/8-13", "-A--BCD-EF");
+ // first find C from A
+ assertEquals(10, sq.findPosition(6, new SequenceCursor(sq, 8, 2, 0)));
+ SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals("test:Pos10:Col6:startCol0:endCol0:tok0",
+ cursor.toString());
+ // now 'find' 99 from C
+ // cursor is set to [F 10] and saved lastCol
+ assertEquals(14, sq.findPosition(99, cursor));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok0",
+ PA.getValue(sq, "cursor").toString());
+ }
+
+ @Test
+ public void testIsValidCursor()
+ {
+ Sequence sq = new Sequence("Seq", "ABC--DE-F", 8, 13);
+ assertFalse(sq.isValidCursor(null));
+
+ /*
+ * cursor is valid if it has valid sequence ref and changeCount token
+ * and positions within the range of the sequence
+ */
+ int changeCount = (int) PA.getValue(sq, "changeCount");
+ SequenceCursor cursor = new SequenceCursor(sq, 13, 1, changeCount);
+ assertTrue(sq.isValidCursor(cursor));
+
+ /*
+ * column position outside [0 - length] is rejected
+ */
+ cursor = new SequenceCursor(sq, 13, -1, changeCount);
+ assertFalse(sq.isValidCursor(cursor));
+ cursor = new SequenceCursor(sq, 13, 10, changeCount);
+ assertFalse(sq.isValidCursor(cursor));
+ cursor = new SequenceCursor(sq, 7, 8, changeCount);
+ assertFalse(sq.isValidCursor(cursor));
+ cursor = new SequenceCursor(sq, 14, 2, changeCount);
+ assertFalse(sq.isValidCursor(cursor));
+
+ /*
+ * wrong sequence is rejected
+ */
+ cursor = new SequenceCursor(null, 13, 1, changeCount);
+ assertFalse(sq.isValidCursor(cursor));
+ cursor = new SequenceCursor(new Sequence("Seq", "abc"), 13, 1,
+ changeCount);
+ assertFalse(sq.isValidCursor(cursor));
+
+ /*
+ * wrong token value is rejected
+ */
+ cursor = new SequenceCursor(sq, 13, 1, changeCount + 1);
+ assertFalse(sq.isValidCursor(cursor));
+ cursor = new SequenceCursor(sq, 13, 1, changeCount - 1);
+ assertFalse(sq.isValidCursor(cursor));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testFindPosition_withCursorAndEdits()
+ {
+ Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
+
+ // find F pos given A
+ assertEquals(13, sq.findPosition(10, new SequenceCursor(sq, 8, 2, 0)));
+ int token = (int) PA.getValue(sq, "changeCount"); // 0
+ SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(new SequenceCursor(sq, 13, 10, token), cursor);
+
+ /*
+ * setSequence should invalidate the cursor cached by the sequence
+ */
+ sq.setSequence("-A-BCD-EF---"); // one gap removed
+ assertEquals(8, sq.getStart()); // sanity check
+ assertEquals(11, sq.findPosition(5)); // D11
+ // cursor should now be at [D 6]
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(new SequenceCursor(sq, 11, 6, ++token), cursor);
+
+ /*
+ * deleteChars should invalidate the cached cursor
+ */
+ sq.deleteChars(2, 5); // delete -BC
+ assertEquals("-AD-EF---", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart()); // sanity check
+ assertEquals(10, sq.findPosition(4)); // E10
+ // cursor should now be at [E 5]
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(new SequenceCursor(sq, 10, 5, ++token), cursor);
+
+ /*
+ * Edit to insert gaps should invalidate the cached cursor
+ * insert 2 gaps at column[3] to make -AD---EF---
+ */
+ SequenceI[] seqs = new SequenceI[] { sq };
+ AlignmentI al = new Alignment(seqs);
+ new EditCommand().appendEdit(Action.INSERT_GAP, seqs, 3, 2, al, true);
+ assertEquals("-AD---EF---", sq.getSequenceAsString());
+ assertEquals(10, sq.findPosition(4)); // E10
+ // cursor should now be at [D 3]
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(new SequenceCursor(sq, 9, 3, ++token), cursor);
+
+ /*
+ * insertCharAt should invalidate the cached cursor
+ * insert CC at column[4] to make -AD-CC--EF---
+ */
+ sq.insertCharAt(4, 2, 'C');
+ assertEquals("-AD-CC--EF---", sq.getSequenceAsString());
+ assertEquals(13, sq.findPosition(9)); // F13
+ // cursor should now be at [F 10]
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(new SequenceCursor(sq, 13, 10, ++token), cursor);
+ }
+
+ @Test(groups = { "Functional" })
+ public void testGetSequence()
+ {
+ String seqstring = "-A--BCD-EF--";
+ Sequence sq = new Sequence("test/8-13", seqstring);
+ sq.createDatasetSequence();
+ assertTrue(Arrays.equals(sq.getSequence(), seqstring.toCharArray()));
+ assertTrue(Arrays.equals(sq.getDatasetSequence().getSequence(),
+ "ABCDEF".toCharArray()));
+
+ // verify a copy of the sequence array is returned
+ char[] theSeq = (char[]) PA.getValue(sq, "sequence");
+ assertNotSame(theSeq, sq.getSequence());
+ theSeq = (char[]) PA.getValue(sq.getDatasetSequence(), "sequence");
+ assertNotSame(theSeq, sq.getDatasetSequence().getSequence());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testReplace()
+ {
+ String seqstring = "-A--BCD-EF--";
+ SequenceI sq = new Sequence("test/8-13", seqstring);
+ assertEquals(0, PA.getValue(sq, "changeCount"));
+
+ assertEquals(0, sq.replace('A', 'A')); // same char
+ assertEquals(seqstring, sq.getSequenceAsString());
+ assertEquals(0, PA.getValue(sq, "changeCount"));
+
+ assertEquals(0, sq.replace('X', 'Y')); // not there
+ assertEquals(seqstring, sq.getSequenceAsString());
+ assertEquals(0, PA.getValue(sq, "changeCount"));
+
+ assertEquals(1, sq.replace('A', 'K'));
+ assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
+ assertEquals(1, PA.getValue(sq, "changeCount"));
+
+ assertEquals(6, sq.replace('-', '.'));
+ assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
+ assertEquals(2, PA.getValue(sq, "changeCount"));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testFindPositions()
+ {
+ SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
+
+ /*
+ * invalid inputs
+ */
+ assertNull(sq.findPositions(6, 5));
+ assertNull(sq.findPositions(0, 5));
+ assertNull(sq.findPositions(-1, 5));
+
+ /*
+ * all gapped ranges
+ */
+ assertNull(sq.findPositions(1, 1)); // 1-based columns
+ assertNull(sq.findPositions(5, 5));
+ assertNull(sq.findPositions(5, 6));
+ assertNull(sq.findPositions(5, 7));
+
+ /*
+ * all ungapped ranges
+ */
+ assertEquals(new Range(8, 8), sq.findPositions(2, 2)); // A
+ assertEquals(new Range(8, 9), sq.findPositions(2, 3)); // AB
+ assertEquals(new Range(8, 10), sq.findPositions(2, 4)); // ABC
+ assertEquals(new Range(9, 10), sq.findPositions(3, 4)); // BC
+
+ /*
+ * gap to ungapped range
+ */
+ assertEquals(new Range(8, 10), sq.findPositions(1, 4)); // ABC
+ assertEquals(new Range(11, 12), sq.findPositions(6, 9)); // DE
+
+ /*
+ * ungapped to gapped range
+ */
+ assertEquals(new Range(10, 10), sq.findPositions(4, 5)); // C
+ assertEquals(new Range(9, 13), sq.findPositions(3, 11)); // BCDEF
+
+ /*
+ * ungapped to ungapped enclosing gaps
+ */
+ assertEquals(new Range(10, 11), sq.findPositions(4, 8)); // CD
+ assertEquals(new Range(8, 13), sq.findPositions(2, 11)); // ABCDEF
+
+ /*
+ * gapped to gapped enclosing ungapped
+ */
+ assertEquals(new Range(8, 10), sq.findPositions(1, 5)); // ABC
+ assertEquals(new Range(11, 12), sq.findPositions(5, 10)); // DE
+ assertEquals(new Range(8, 13), sq.findPositions(1, 13)); // the lot
+ assertEquals(new Range(8, 13), sq.findPositions(1, 99));
+ }