+ 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));
+
+ // add with a fake pdbid
+ // (models don't have an embedded ID)
+ String realId = "RealIDQ";
+ PDBEntry pdbe6 = new PDBEntry(realId, null, Type.PDB, "real/localpath");
+ PDBEntry pdbe7 = new PDBEntry("RealID/real/localpath", "C", Type.MMCIF,
+ "real/localpath");
+ pdbe7.setFakedPDBId(true);
+ seq.addPDBId(pdbe6);
+ assertEquals(5, seq.getAllPDBEntries().size());
+ seq.addPDBId(pdbe7);
+ assertEquals(5, seq.getAllPDBEntries().size());
+ assertFalse(pdbe6.fakedPDBId());
+ assertSame(pdbe6, seq.getAllPDBEntries().get(4));
+ assertEquals("C", pdbe6.getChainCode());
+ assertEquals(realId, pdbe6.getId());
+ }
+
+ @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--");
+ final int tok = (int) PA.getValue(sq, "changeCount");
+ assertEquals(1, tok);
+
+ // find F given A, check cursor is now at the found position
+ assertEquals(10, sq.findIndex(13, new SequenceCursor(sq, 8, 2, tok)));
+ SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(13, cursor.residuePosition);
+ assertEquals(10, cursor.columnPosition);
+
+ // find A given F
+ assertEquals(2, sq.findIndex(8, new SequenceCursor(sq, 13, 10, tok)));
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(8, cursor.residuePosition);
+ assertEquals(2, cursor.columnPosition);
+
+ // find C given C (no cursor update is done for this case)
+ assertEquals(6, sq.findIndex(10, new SequenceCursor(sq, 10, 6, tok)));
+ SequenceCursor cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertSame(cursor2, cursor);
+
+ /*
+ * sequence 'end' beyond end of sequence returns length of sequence
+ * (for compatibility with pre-cursor code)
+ * - also verify the cursor is left in a valid state
+ */
+ sq = new Sequence("test/8-99", "-A--B-C-D-E-F--"); // trailing gap case
+ assertEquals(7, sq.findIndex(10)); // establishes a cursor
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(10, cursor.residuePosition);
+ assertEquals(7, cursor.columnPosition);
+ assertEquals(sq.getLength(), sq.findIndex(65));
+ cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertSame(cursor, cursor2); // not updated for this case!
+
+ sq = new Sequence("test/8-99", "-A--B-C-D-E-F"); // trailing residue case
+ sq.findIndex(10); // establishes a cursor
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(sq.getLength(), sq.findIndex(65));
+ cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertSame(cursor, cursor2); // not updated for this case!
+
+ /*
+ * residue after sequence 'start' but before first residue should return
+ * zero (for compatibility with pre-cursor code)
+ */
+ sq = new Sequence("test/8-15", "-A-B-C-"); // leading gap case
+ sq.findIndex(10); // establishes a cursor
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(0, sq.findIndex(3));
+ cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertSame(cursor, cursor2); // not updated for this case!
+
+ sq = new Sequence("test/8-15", "A-B-C-"); // leading residue case
+ sq.findIndex(10); // establishes a cursor
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(0, sq.findIndex(2));
+ cursor2 = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertSame(cursor, cursor2); // not updated for this case!
+ }
+
+ @Test(groups = { "Functional" })
+ public void testFindPosition_withCursor()
+ {
+ Sequence sq = new Sequence("test/8-13", "-A--BCD-EF--");
+ final int tok = (int) PA.getValue(sq, "changeCount");
+ assertEquals(1, tok);
+
+ // find F pos given A - lastCol gets set in cursor
+ assertEquals(13,
+ sq.findPosition(10, new SequenceCursor(sq, 8, 2, tok)));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
+ 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, tok)));
+ assertEquals("test:Pos8:Col2:startCol2:endCol10:tok1",
+ 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, tok)));
+ assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
+ 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, tok)));
+ assertEquals("test:Pos9:Col5:startCol0:endCol0:tok1",
+ 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, tok)));
+ assertEquals("test:Pos11:Col7:startCol0:endCol0:tok1",
+ 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, tok)));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
+ 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, tok)));
+ assertEquals("test:Pos13:Col10:startCol0:endCol10:tok1",
+ 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, tok)));
+ SequenceCursor cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals("test:Pos10:Col6:startCol0:endCol0:tok1",
+ 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:tok1",
+ 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);
+ assertEquals(0, cursor.lastColumnPosition); // not yet found
+ assertEquals(13, sq.findPosition(8)); // E13
+ cursor = (SequenceCursor) PA.getValue(sq, "cursor");
+ assertEquals(9, cursor.lastColumnPosition); // found
+
+ /*
+ * 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);
+
+ /*
+ * changing sequence start should invalidate cursor
+ */
+ sq = new Sequence("test/8-13", "-A--BCD-EF--");
+ assertEquals(8, sq.getStart());
+ assertEquals(9, sq.findPosition(4)); // B(9)
+ sq.setStart(7);
+ assertEquals(8, sq.findPosition(4)); // is now B(8)
+ sq.setStart(10);
+ assertEquals(11, sq.findPosition(4)); // is now B(11)
+ }
+
+ @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);
+ // changeCount is incremented for setStart
+ assertEquals(1, PA.getValue(sq, "changeCount"));
+
+ assertEquals(0, sq.replace('A', 'A')); // same char
+ assertEquals(seqstring, sq.getSequenceAsString());
+ assertEquals(1, PA.getValue(sq, "changeCount"));
+
+ assertEquals(0, sq.replace('X', 'Y')); // not there
+ assertEquals(seqstring, sq.getSequenceAsString());
+ assertEquals(1, PA.getValue(sq, "changeCount"));
+
+ assertEquals(1, sq.replace('A', 'K'));
+ assertEquals("-K--BCD-EF--", sq.getSequenceAsString());
+ assertEquals(2, PA.getValue(sq, "changeCount"));
+
+ assertEquals(6, sq.replace('-', '.'));
+ assertEquals(".K..BCD.EF..", sq.getSequenceAsString());
+ assertEquals(3, PA.getValue(sq, "changeCount"));
+ }
+
+ @Test(groups = { "Functional" })
+ public void testGapBitset()
+ {
+ SequenceI sq = new Sequence("test/8-13", "-ABC---DE-F--");
+ BitSet bs = sq.gapBitset();
+ BitSet expected = new BitSet();
+ expected.set(0);
+ expected.set(4, 7);
+ expected.set(9);
+ expected.set(11, 13);
+
+ assertTrue(bs.equals(expected));
+
+ }
+
+ public void testFindFeatures_largeEndPos()
+ {
+ /*
+ * imitate a PDB sequence where end is larger than end position
+ */
+ SequenceI sq = new Sequence("test", "-ABC--DEF--", 1, 20);
+ sq.createDatasetSequence();
+
+ assertTrue(sq.findFeatures(1, 9).isEmpty());
+ // should be no array bounds exception - JAL-2772
+ assertTrue(sq.findFeatures(1, 15).isEmpty());
+
+ // add feature on BCD
+ SequenceFeature sfBCD = new SequenceFeature("Cath", "desc", 2, 4, 2f,
+ null);
+ sq.addSequenceFeature(sfBCD);
+
+ // no features in columns 1-2 (-A)
+ List<SequenceFeature> found = sq.findFeatures(1, 2);
+ assertTrue(found.isEmpty());
+
+ // columns 1-6 (-ABC--) includes BCD
+ found = sq.findFeatures(1, 6);
+ assertEquals(1, found.size());
+ assertTrue(found.contains(sfBCD));
+
+ // columns 10-11 (--) should find nothing
+ found = sq.findFeatures(10, 11);
+ assertEquals(0, found.size());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testSetName()
+ {
+ SequenceI sq = new Sequence("test", "-ABC---DE-F--");
+ assertEquals("test", sq.getName());
+ assertEquals(1, sq.getStart());
+ assertEquals(6, sq.getEnd());
+
+ sq.setName("testing");
+ assertEquals("testing", sq.getName());
+
+ sq.setName("test/8-10");
+ assertEquals("test", sq.getName());
+ assertEquals(8, sq.getStart());
+ assertEquals(13, sq.getEnd()); // note end is recomputed
+
+ sq.setName("testing/7-99");
+ assertEquals("testing", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd()); // end may be beyond physical end
+
+ sq.setName("/2-3");
+ assertEquals("", sq.getName());
+ assertEquals(2, sq.getStart());
+ assertEquals(7, sq.getEnd());
+
+ sq.setName("test/"); // invalid
+ assertEquals("test/", sq.getName());
+ assertEquals(2, sq.getStart());
+ assertEquals(7, sq.getEnd());
+
+ sq.setName("test/6-13/7-99");
+ assertEquals("test/6-13", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/0-5"); // 0 is invalid - ignored
+ assertEquals("test/0-5", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/a-5"); // a is invalid - ignored
+ assertEquals("test/a-5", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/6-5"); // start > end is invalid - ignored
+ assertEquals("test/6-5", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/5"); // invalid - ignored
+ assertEquals("test/5", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/-5"); // invalid - ignored
+ assertEquals("test/-5", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/5-"); // invalid - ignored
+ assertEquals("test/5-", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName("test/5-6-7"); // invalid - ignored
+ assertEquals("test/5-6-7", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+
+ sq.setName(null); // invalid, gets converted to space
+ assertEquals("", sq.getName());
+ assertEquals(7, sq.getStart());
+ assertEquals(99, sq.getEnd());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testCheckValidRange()
+ {
+ Sequence sq = new Sequence("test/7-12", "-ABC---DE-F--");
+ assertEquals(7, sq.getStart());
+ assertEquals(12, sq.getEnd());
+
+ /*
+ * checkValidRange ensures end is at least the last residue position
+ */
+ PA.setValue(sq, "end", 2);
+ sq.checkValidRange();
+ assertEquals(12, sq.getEnd());
+
+ /*
+ * end may be beyond the last residue position
+ */
+ PA.setValue(sq, "end", 22);
+ sq.checkValidRange();
+ assertEquals(22, sq.getEnd());
+ }
+
+ @Test(groups = { "Functional" })
+ public void testDeleteChars_withGaps()
+ {
+ /*
+ * delete gaps only
+ */
+ SequenceI sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
+ sq.deleteChars(1, 2); // delete first gap
+ assertEquals("AB-C", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(10, sq.getEnd());
+ assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
+
+ /*
+ * delete gaps and residues at start (no new dataset sequence)
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(0, 3); // delete A-B
+ assertEquals("-C", sq.getSequenceAsString());
+ assertEquals(10, sq.getStart());
+ assertEquals(10, sq.getEnd());
+ assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
+
+ /*
+ * delete gaps and residues at end (no new dataset sequence)
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(2, 5); // delete B-C
+ assertEquals("A-", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(8, sq.getEnd());
+ assertEquals("ABC", sq.getDatasetSequence().getSequenceAsString());
+
+ /*
+ * delete gaps and residues internally (new dataset sequence)
+ * first delete from gap to residue
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(1, 3); // delete -B
+ assertEquals("A-C", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(9, sq.getEnd());
+ assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
+ assertEquals(8, sq.getDatasetSequence().getStart());
+ assertEquals(9, sq.getDatasetSequence().getEnd());
+
+ /*
+ * internal delete from gap to gap
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(1, 4); // delete -B-
+ assertEquals("AC", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(9, sq.getEnd());
+ assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
+ assertEquals(8, sq.getDatasetSequence().getStart());
+ assertEquals(9, sq.getDatasetSequence().getEnd());
+
+ /*
+ * internal delete from residue to residue
+ */
+ sq = new Sequence("test/8-10", "A-B-C");
+ sq.createDatasetSequence();
+ sq.deleteChars(2, 3); // delete B
+ assertEquals("A--C", sq.getSequenceAsString());
+ assertEquals(8, sq.getStart());
+ assertEquals(9, sq.getEnd());
+ assertEquals("AC", sq.getDatasetSequence().getSequenceAsString());
+ assertEquals(8, sq.getDatasetSequence().getStart());
+ assertEquals(9, sq.getDatasetSequence().getEnd());
+ }
+
+ /**
+ * Test the code used to locate the reference sequence ruler origin
+ */
+ @Test(groups = { "Functional" })
+ public void testLocateVisibleStartofSequence()
+ {
+ // create random alignment
+ AlignmentGenerator gen = new AlignmentGenerator(false);
+ AlignmentI al = gen.generate(50, 20, 123, 5, 5);
+
+ HiddenColumns cs = al.getHiddenColumns();
+ ColumnSelection colsel = new ColumnSelection();
+
+ SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
+ assertEquals(2, seq.findIndex(seq.getStart()));
+
+ // no hidden columns
+ assertEquals(seq.findIndex(seq.getStart()) - 1,
+ seq.firstResidueOutsideIterator(cs.iterator()));
+
+ // hidden column on gap after end of sequence - should not affect bounds
+ colsel.hideSelectedColumns(13, al.getHiddenColumns());
+ assertEquals(seq.findIndex(seq.getStart()) - 1,
+ seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ // hidden column on gap before beginning of sequence - should vis bounds by
+ // one
+ colsel.hideSelectedColumns(0, al.getHiddenColumns());
+ assertEquals(seq.findIndex(seq.getStart()) - 2,
+ cs.absoluteToVisibleColumn(
+ seq.firstResidueOutsideIterator(cs.iterator())));
+
+ cs.revealAllHiddenColumns(colsel);
+ // hide columns around most of sequence - leave one residue remaining
+ cs.hideColumns(1, 3);
+ cs.hideColumns(6, 11);
+
+ Iterator<int[]> it = cs.getVisContigsIterator(0, 6, false);
+
+ assertEquals("-D", seq.getSequenceStringFromIterator(it));
+ // cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
+ // { seq })[0]);
+
+ assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
+ cs.revealAllHiddenColumns(colsel);
+
+ // hide whole sequence - should just get location of hidden region
+ // containing sequence
+ cs.hideColumns(1, 11);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 15);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(7, 17);
+ assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(3, 17);
+ assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(3, 19);
+ assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 0);
+ assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 1);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 2);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(1, 1);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(1, 2);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(1, 3);
+ assertEquals(4, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 2);
+ cs.hideColumns(5, 6);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 2);
+ cs.hideColumns(5, 6);
+ cs.hideColumns(9, 10);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 2);
+ cs.hideColumns(7, 11);
+ assertEquals(3, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(2, 4);
+ cs.hideColumns(7, 11);
+ assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(2, 4);
+ cs.hideColumns(7, 12);
+ assertEquals(1, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(1, 11);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 12);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 4);
+ cs.hideColumns(6, 12);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 1);
+ cs.hideColumns(3, 12);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(3, 14);
+ cs.hideColumns(17, 19);
+ assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(3, 7);
+ cs.hideColumns(9, 14);
+ cs.hideColumns(17, 19);
+ assertEquals(0, seq2.firstResidueOutsideIterator(cs.iterator()));
+
+ cs.revealAllHiddenColumns(colsel);
+ cs.hideColumns(0, 1);
+ cs.hideColumns(3, 4);
+ cs.hideColumns(6, 8);
+ cs.hideColumns(10, 12);
+ assertEquals(0, seq.firstResidueOutsideIterator(cs.iterator()));
+
+ }
+
+ @Test(groups = { "Functional" })
+ public void testTransferAnnotation()
+ {
+ Sequence origSeq = new Sequence("MYSEQ", "THISISASEQ");
+ Sequence toSeq = new Sequence("MYSEQ", "THISISASEQ");
+ origSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, true));
+ toSeq.transferAnnotation(origSeq, null);
+ assertTrue(toSeq.getDBRefs().size() == 1);
+
+ assertTrue(toSeq.getDBRefs().get(0).isCanonical());
+
+ // check for promotion of non-canonical
+ // to canonical (e.g. fetch-db-refs on a jalview project pre 2.11.2)
+ toSeq.setDBRefs(null);
+ toSeq.addDBRef(new DBRefEntry("UNIPROT", "0", "Q12345", null, false));
+ toSeq.transferAnnotation(origSeq, null);
+ assertTrue(toSeq.getDBRefs().size() == 1);
+
+ assertTrue("Promotion of non-canonical DBRefEntry failed",
+ toSeq.getDBRefs().get(0).isCanonical());
+