diff --git a/java/src/org/broadinstitute/sting/playground/gatk/walkers/FastaReferenceWalker.java b/java/src/org/broadinstitute/sting/playground/gatk/walkers/FastaReferenceWalker.java new file mode 100755 index 000000000..957cf05d0 --- /dev/null +++ b/java/src/org/broadinstitute/sting/playground/gatk/walkers/FastaReferenceWalker.java @@ -0,0 +1,58 @@ +package org.broadinstitute.sting.playground.gatk.walkers; + +import org.broadinstitute.sting.gatk.LocusContext; +import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; +import org.broadinstitute.sting.gatk.walkers.RefWalker; +import org.broadinstitute.sting.gatk.walkers.WalkerName; +import org.broadinstitute.sting.utils.GenomeLoc; +import org.broadinstitute.sting.utils.Pair; + +// create a fasta sequence file from a reference and intervals + +@WalkerName("FastaReferenceMaker") +public class FastaReferenceWalker extends RefWalker, Pair> { + + public Pair map(RefMetaDataTracker rodData, char ref, LocusContext context) { + return new Pair(context.getLocation(), ref); + } + + public Pair reduceInit() { + return new Pair(null, ""); + } + + public Pair reduce(Pair value, Pair sum) { + // if there is no interval to the left, then this is the first one + if ( sum.first == null ) { + sum.first = value.first; + sum.second = value.second.toString(); + } + // if the intervals don't overlap, print out the leftmost one and start a new one + else if ( value.first.getStart() != sum.first.getStop() + 1 ) { + printFasta(sum.first, sum.second); + sum.first = value.first; + sum.second = value.second.toString(); + } + // otherwise, merge them + else { + sum.first.setStop(value.first.getStop()); + sum.second = new String(sum.second + value.second); + } + return sum; + } + + public void onTraversalDone(Pair sum) { + if (sum.second != null) + printFasta(sum.first, sum.second); + } + + private void printFasta(GenomeLoc loc, String s) { + out.println(">" + loc); + int lines = s.length() / 60; + int currentStart = 0; + for (int i=0; i < lines; i++) { + out.println(s.substring(currentStart, currentStart+60)); + currentStart += 60; + } + out.println(s.substring(currentStart)); + } +} \ No newline at end of file