Skip to content

Commit

Permalink
Merge pull request #594 from samtools/nh_liftover_helper
Browse files Browse the repository at this point in the history
Provide a method that for each source contig gives the set of destination contigs used in liftover.
  • Loading branch information
nh13 authored Jun 21, 2016
2 parents bcc3f4a + 3629291 commit dbee9ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/htsjdk/samtools/liftover/LiftOver.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Java port of UCSC liftOver. Only the most basic liftOver functionality is implemented.
Expand All @@ -47,13 +52,28 @@ public class LiftOver {

private double liftOverMinMatch = DEFAULT_LIFTOVER_MINMATCH;
private final OverlapDetector<Chain> chains;
private final Map<String, Set<String>> contigMap = new HashMap<>();

/**
* Load UCSC chain file in order to lift over Intervals.
*/
public LiftOver(File chainFile) {
IOUtil.assertFileIsReadable(chainFile);
chains = Chain.loadChains(chainFile);

for (final Chain chain : this.chains.getAll()) {
final String from = chain.fromSequenceName;
final String to = chain.toSequenceName;
final Set<String> names;
if (contigMap.containsKey(from)) {
names = contigMap.get(from);
}
else {
names = new HashSet<>();
contigMap.put(from, names);
}
names.add(to);
}
}

/**
Expand Down Expand Up @@ -139,6 +159,13 @@ public List<PartialLiftover> diagnosticLiftover(final Interval interval) {
return ret;
}

/**
* @return the set of destination contigs for each source contig in the chains file.
*/
public Map<String, Set<String>> getContigMap() {
return Collections.unmodifiableMap(contigMap);
}

private static Interval createToInterval(final String intervalName, final boolean sourceNegativeStrand, final TargetIntersection targetIntersection) {
// Compute the query interval given the offsets of the target interval start and end into the first and
// last ContinuousBlocks.
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/htsjdk/samtools/liftover/LiftOverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
Expand All @@ -44,10 +45,12 @@ public class LiftOverTest {
private static final File CHAIN_FILE = new File(TEST_DATA_DIR, "hg18ToHg19.over.chain");

private LiftOver liftOver;
Map<String, Set<String>> contigMap;

@BeforeClass
public void initLiftOver() {
liftOver = new LiftOver(CHAIN_FILE);
contigMap = liftOver.getContigMap();
}

@Test(dataProvider = "testIntervals")
Expand Down Expand Up @@ -455,4 +458,11 @@ public void testWriteChain() throws Exception {
}
Assert.assertEquals(newChainMap, originalChainMap);
}

@Test(dataProvider = "testIntervals")
public void testGetContigMap(final Interval in, final Interval expected) {
if (expected != null) {
Assert.assertTrue(contigMap.get(in.getContig()).contains(expected.getContig()));
}
}
}

0 comments on commit dbee9ac

Please sign in to comment.