Google caliper example execution script

-- FragmentPileup with final performance testing
This commit is contained in:
Mark DePristo 2011-10-24 14:04:53 -04:00
parent 42bf9adede
commit 166174a551
2 changed files with 105 additions and 22 deletions

View File

@ -30,6 +30,13 @@ public class FragmentPileup {
Collection<PileupElement> oneReadPile = null; Collection<PileupElement> oneReadPile = null;
Collection<TwoReadPileupElement> twoReadPile = null; Collection<TwoReadPileupElement> twoReadPile = null;
public enum FragmentMatchingAlgorithm {
ORIGINAL,
FAST_V1,
skipNonOverlapping,
skipNonOverlappingNotLazy
}
/** /**
* Create a new Fragment-based pileup from the standard read-based pileup * Create a new Fragment-based pileup from the standard read-based pileup
* @param pileup * @param pileup
@ -39,15 +46,17 @@ public class FragmentPileup {
fastNewCalculation(pileup); fastNewCalculation(pileup);
} }
protected FragmentPileup(ReadBackedPileup pileup, boolean useOldAlgorithm) { protected FragmentPileup(ReadBackedPileup pileup, FragmentMatchingAlgorithm algorithm) {
if ( useOldAlgorithm ) switch ( algorithm ) {
oldSlowCalculation(pileup); case ORIGINAL: oldSlowCalculation(pileup); break;
else case FAST_V1: fastNewCalculation(pileup); break;
fastNewCalculation(pileup); case skipNonOverlapping: skipNonOverlapping(pileup); break;
case skipNonOverlappingNotLazy: skipNonOverlappingNotLazy(pileup); break;
}
} }
private final void oldSlowCalculation(final ReadBackedPileup pileup) { private final void oldSlowCalculation(final ReadBackedPileup pileup) {
final Map<String, PileupElement> nameMap = new HashMap<String, PileupElement>(); final Map<String, PileupElement> nameMap = new HashMap<String, PileupElement>(pileup.size());
// build an initial map, grabbing all of the multi-read fragments // build an initial map, grabbing all of the multi-read fragments
for ( final PileupElement p : pileup ) { for ( final PileupElement p : pileup ) {
@ -104,7 +113,6 @@ public class FragmentPileup {
break; break;
} }
// in this case we need to see if our mate is already present, and if so // in this case we need to see if our mate is already present, and if so
// grab the read from the list // grab the read from the list
case RIGHT_MAYBE: { case RIGHT_MAYBE: {
@ -120,9 +128,74 @@ public class FragmentPileup {
} }
} }
if ( nameMap != null && ! nameMap.isEmpty() ) // could be slightly more optimally if ( nameMap != null && ! nameMap.isEmpty() ) {
for ( final PileupElement p : nameMap.values() ) if ( oneReadPile == null )
addToOnePile(p); oneReadPile = nameMap.values();
else
oneReadPile.addAll(nameMap.values());
}
}
/**
* @param pileup
*/
private final void skipNonOverlappingNotLazy(final ReadBackedPileup pileup) {
oneReadPile = new ArrayList<PileupElement>(pileup.size());
twoReadPile = new ArrayList<TwoReadPileupElement>();
final Map<String, PileupElement> nameMap = new HashMap<String, PileupElement>(pileup.size());
// build an initial map, grabbing all of the multi-read fragments
for ( final PileupElement p : pileup ) {
// if we know that this read won't overlap its mate, or doesn't have one, jump out early
final SAMRecord read = p.getRead();
final int mateStart = read.getMateAlignmentStart();
if ( mateStart == 0 || mateStart > read.getAlignmentEnd() ) {
oneReadPile.add(p);
} else {
final String readName = p.getRead().getReadName();
final PileupElement pe1 = nameMap.get(readName);
if ( pe1 != null ) {
// assumes we have at most 2 reads per fragment
twoReadPile.add(new TwoReadPileupElement(pe1, p));
nameMap.remove(readName);
} else {
nameMap.put(readName, p);
}
}
}
oneReadPile.addAll(nameMap.values());
}
private final void skipNonOverlapping(final ReadBackedPileup pileup) {
Map<String, PileupElement> nameMap = null;
// build an initial map, grabbing all of the multi-read fragments
for ( final PileupElement p : pileup ) {
// if we know that this read won't overlap its mate, or doesn't have one, jump out early
final SAMRecord read = p.getRead();
final int mateStart = read.getMateAlignmentStart();
if ( mateStart == 0 || mateStart > read.getAlignmentEnd() ) {
if ( oneReadPile == null ) oneReadPile = new ArrayList<PileupElement>(pileup.size());
oneReadPile.add(p);
} else {
final String readName = p.getRead().getReadName();
final PileupElement pe1 = nameMap == null ? null : nameMap.get(readName);
if ( pe1 != null ) {
// assumes we have at most 2 reads per fragment
if ( twoReadPile == null ) twoReadPile = new ArrayList<TwoReadPileupElement>();
twoReadPile.add(new TwoReadPileupElement(pe1, p));
nameMap.remove(readName);
} else {
nameMap = addToNameMap(nameMap, p);
}
}
}
if ( oneReadPile == null )
oneReadPile = nameMap == null ? Collections.<PileupElement>emptyList() : nameMap.values();
else if ( nameMap != null )
oneReadPile.addAll(nameMap.values());
} }
private final Map<String, PileupElement> addToNameMap(Map<String, PileupElement> map, final PileupElement p) { private final Map<String, PileupElement> addToNameMap(Map<String, PileupElement> map, final PileupElement p) {

View File

@ -39,16 +39,17 @@ import java.util.*;
* Caliper microbenchmark of fragment pileup * Caliper microbenchmark of fragment pileup
*/ */
public class FragmentPileupBenchmark extends SimpleBenchmark { public class FragmentPileupBenchmark extends SimpleBenchmark {
final int N_PILEUPS_TO_GENERATE = 100; List<ReadBackedPileup> pileups;
List<ReadBackedPileup> pileups = new ArrayList<ReadBackedPileup>(N_PILEUPS_TO_GENERATE);
@Param({"10", "100", "1000"}) // , "10000"}) @Param({"0", "4", "30", "150", "1000"})
int pileupSize; // set automatically by framework int pileupSize; // set automatically by framework
@Param({"150", "400"}) @Param({"200", "400"})
int insertSize; // set automatically by framework int insertSize; // set automatically by framework
@Override protected void setUp() { @Override protected void setUp() {
final int nPileupsToGenerate = 100;
pileups = new ArrayList<ReadBackedPileup>(nPileupsToGenerate);
SAMFileHeader header = ArtificialSAMUtils.createArtificialSamHeader(1, 1, 1000); SAMFileHeader header = ArtificialSAMUtils.createArtificialSamHeader(1, 1, 1000);
GenomeLocParser genomeLocParser; GenomeLocParser genomeLocParser;
genomeLocParser = new GenomeLocParser(header.getSequenceDictionary()); genomeLocParser = new GenomeLocParser(header.getSequenceDictionary());
@ -61,7 +62,7 @@ public class FragmentPileupBenchmark extends SimpleBenchmark {
final boolean leftIsNegative = false; final boolean leftIsNegative = false;
final int insertSizeVariation = insertSize / 10; final int insertSizeVariation = insertSize / 10;
for ( int pileupN = 0; pileupN < N_PILEUPS_TO_GENERATE; pileupN++ ) { for ( int pileupN = 0; pileupN < nPileupsToGenerate; pileupN++ ) {
List<PileupElement> pileupElements = new ArrayList<PileupElement>(); List<PileupElement> pileupElements = new ArrayList<PileupElement>();
for ( int i = 0; i < pileupSize / 2; i++ ) { for ( int i = 0; i < pileupSize / 2; i++ ) {
final String readName = "read" + i; final String readName = "read" + i;
@ -87,19 +88,28 @@ public class FragmentPileupBenchmark extends SimpleBenchmark {
} }
} }
public void timeNaiveNameMatch(int rep) { private void run(int rep, FragmentPileup.FragmentMatchingAlgorithm algorithm) {
int nFrags = 0; int nFrags = 0;
for ( int i = 0; i < rep; i++ ) { for ( int i = 0; i < rep; i++ ) {
for ( ReadBackedPileup rbp : pileups ) for ( ReadBackedPileup rbp : pileups )
nFrags += new FragmentPileup(rbp, true).getTwoReadPileup().size(); nFrags += new FragmentPileup(rbp, algorithm).getTwoReadPileup().size();
} }
} }
public void timeFastNameMatch(int rep) { public void timeOriginal(int rep) {
int nFrags = 0; run(rep, FragmentPileup.FragmentMatchingAlgorithm.ORIGINAL);
for ( int i = 0; i < rep; i++ ) }
for ( ReadBackedPileup rbp : pileups )
nFrags += new FragmentPileup(rbp, false).getTwoReadPileup().size(); public void timeFullOverlapPotential(int rep) {
run(rep, FragmentPileup.FragmentMatchingAlgorithm.FAST_V1);
}
public void timeSkipNonOverlapping(int rep) {
run(rep, FragmentPileup.FragmentMatchingAlgorithm.skipNonOverlapping);
}
public void timeSkipNonOverlappingNotLazy(int rep) {
run(rep, FragmentPileup.FragmentMatchingAlgorithm.skipNonOverlappingNotLazy);
} }
public static void main(String[] args) { public static void main(String[] args) {