From 82d0da843f8697597679b852f3ec47f4fd21a94b Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 8 Nov 2016 14:54:27 +0000 Subject: [PATCH] JAL-2295 method added to return a compact mapped range in the structure --- src/jalview/structure/StructureMapping.java | 78 ++++++++++++++++++++++ test/jalview/structure/StructureMappingTest.java | 46 +++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 test/jalview/structure/StructureMappingTest.java diff --git a/src/jalview/structure/StructureMapping.java b/src/jalview/structure/StructureMapping.java index 78634e0..40789ed 100644 --- a/src/jalview/structure/StructureMapping.java +++ b/src/jalview/structure/StructureMapping.java @@ -23,7 +23,9 @@ package jalview.structure; import jalview.datamodel.AlignmentAnnotation; import jalview.datamodel.SequenceI; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; public class StructureMapping { @@ -47,6 +49,18 @@ public class StructureMapping // and atomNo HashMap mapping; + /** + * Constructor + * + * @param seq + * @param pdbfile + * @param pdbid + * @param chain + * @param mapping + * a map from sequence to two values, { resNo, atomNo } in the + * structure + * @param mappingDetails + */ public StructureMapping(SequenceI seq, String pdbfile, String pdbid, String chain, HashMap mapping, String mappingDetails) @@ -111,6 +125,70 @@ public class StructureMapping } /** + * Returns a (possibly empty) list of [start, end] residue positions in the + * mapped structure, corresponding to the given range of sequence positions + * + * @param fromSeqPos + * @param toSeqPos + * @return + */ + public List getPDBResNumRanges(int fromSeqPos, int toSeqPos) + { + List result = new ArrayList(); + int startRes = -1; + int endRes = -1; + + for (int i = fromSeqPos; i <= toSeqPos; i++) + { + int resNo = getPDBResNum(i); + if (resNo == UNASSIGNED_VALUE) + { + continue; // no mapping from this sequence position + } + if (startRes == -1) + { + startRes = resNo; + endRes = resNo; + } + if (resNo >= startRes && resNo <= endRes) + { + // within the current range - no change + continue; + } + if (resNo == startRes - 1) + { + // extend beginning of current range + startRes--; + continue; + } + if (resNo == endRes + 1) + { + // extend end of current range + endRes++; + continue; + } + + /* + * resNo is not within or contiguous with last range, + * so write out the last range + */ + result.add(new int[] { startRes, endRes }); + startRes = resNo; + endRes = resNo; + } + + /* + * and add the last range + */ + if (startRes != -1) + { + result.add(new int[] { startRes, endRes }); + } + + return result; + } + + /** * * @param pdbResNum * @return -1 or the corresponding sequence position for a pdb residue number diff --git a/test/jalview/structure/StructureMappingTest.java b/test/jalview/structure/StructureMappingTest.java new file mode 100644 index 0000000..f26c5f1 --- /dev/null +++ b/test/jalview/structure/StructureMappingTest.java @@ -0,0 +1,46 @@ +package jalview.structure; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +import java.util.HashMap; +import java.util.List; + +import org.testng.annotations.Test; + +public class StructureMappingTest +{ + @Test(groups = "Functional") + public void testgetPDBResNumRanges() + { + HashMap map = new HashMap(); + + StructureMapping mapping = new StructureMapping(null, null, null, null, + map, null); + + List ranges = mapping.getPDBResNumRanges(1, 2); + assertTrue(ranges.isEmpty()); + + map.put(1, new int[] { 12, 20 }); // 1 maps to 12 + ranges = mapping.getPDBResNumRanges(2, 3); + assertTrue(ranges.isEmpty()); + ranges = mapping.getPDBResNumRanges(1, 2); + assertEquals(ranges.size(), 1); + assertEquals(ranges.get(0)[0], 12); + assertEquals(ranges.get(0)[1], 12); + + map.put(2, new int[] { 13, 20 }); // 2 maps to 13 + ranges = mapping.getPDBResNumRanges(1, 2); + assertEquals(ranges.size(), 1); + assertEquals(ranges.get(0)[0], 12); + assertEquals(ranges.get(0)[1], 13); + + map.put(3, new int[] { 15, 20 }); // 3 maps to 15 - break + ranges = mapping.getPDBResNumRanges(1, 5); + assertEquals(ranges.size(), 2); + assertEquals(ranges.get(0)[0], 12); + assertEquals(ranges.get(0)[1], 13); + assertEquals(ranges.get(1)[0], 15); + assertEquals(ranges.get(1)[1], 15); + } +} -- 1.7.10.2