use of htsjdk.samtools.util.Interval in project gatk by broadinstitute.
the class IntervalListTools method doWork.
@Override
protected Object doWork() {
// Check inputs
for (final File f : INPUT) IOUtil.assertFileIsReadable(f);
for (final File f : SECOND_INPUT) IOUtil.assertFileIsReadable(f);
if (OUTPUT != null) {
if (SCATTER_COUNT == 1) {
IOUtil.assertFileIsWritable(OUTPUT);
} else {
IOUtil.assertDirectoryIsWritable(OUTPUT);
}
}
// Read in the interval lists and apply any padding
final List<IntervalList> lists = openIntervalLists(INPUT);
// same for the second list
final List<IntervalList> secondLists = openIntervalLists(SECOND_INPUT);
if (UNIQUE && !SORT) {
LOG.warn("UNIQUE=true requires sorting but SORT=false was specified. Results will be sorted!");
}
final IntervalList result = ACTION.act(lists, secondLists);
if (SCATTER_COUNT > 1) {
// Scattering requires a uniqued, sorted interval list. We want to do this up front (before BREAKING AT BANDS)
SORT = true;
UNIQUE = true;
}
if (INVERT) {
// no need to sort, since return will be sorted by definition.
SORT = false;
UNIQUE = true;
}
final IntervalList possiblySortedResult = SORT ? result.sorted() : result;
final IntervalList possiblyInvertedResult = INVERT ? IntervalList.invert(possiblySortedResult) : possiblySortedResult;
//only get unique if this has been asked unless inverting (since the invert will return a unique list)
List<Interval> finalIntervals = UNIQUE ? possiblyInvertedResult.uniqued().getIntervals() : possiblyInvertedResult.getIntervals();
if (BREAK_BANDS_AT_MULTIPLES_OF > 0) {
finalIntervals = IntervalList.breakIntervalsAtBandMultiples(finalIntervals, BREAK_BANDS_AT_MULTIPLES_OF);
}
// Decide on a PG ID and make a program group
final SAMFileHeader header = result.getHeader();
final Set<String> pgs = new HashSet<>();
for (final SAMProgramRecord pg : header.getProgramRecords()) pgs.add(pg.getId());
for (int i = 1; i < Integer.MAX_VALUE; ++i) {
if (!pgs.contains(String.valueOf(i))) {
final SAMProgramRecord pg = new SAMProgramRecord(String.valueOf(i));
pg.setCommandLine(getCommandLine());
pg.setProgramName(getClass().getSimpleName());
header.addProgramRecord(pg);
break;
}
}
// Add any comments
if (COMMENT != null) {
for (final String comment : COMMENT) {
header.addComment(comment);
}
}
final IntervalList output = new IntervalList(header);
for (final Interval i : finalIntervals) {
output.add(i);
}
final List<IntervalList> resultIntervals;
if (OUTPUT != null) {
if (SCATTER_COUNT == 1) {
output.write(OUTPUT);
resultIntervals = Arrays.asList(output);
} else {
final List<IntervalList> scattered = writeScatterIntervals(output);
LOG.info(String.format("Wrote %s scatter subdirectories to %s.", scattered.size(), OUTPUT));
if (scattered.size() != SCATTER_COUNT) {
LOG.warn(String.format("Requested scatter width of %s, but only emitted %s. (This may be an expected consequence of running in %s mode.)", SCATTER_COUNT, scattered.size(), IntervalListScatterer.Mode.BALANCING_WITHOUT_INTERVAL_SUBDIVISION));
}
resultIntervals = scattered;
}
} else {
resultIntervals = Arrays.asList(output);
}
long totalUniqueBaseCount = 0;
long intervalCount = 0;
for (final IntervalList finalInterval : resultIntervals) {
totalUniqueBaseCount = finalInterval.getUniqueBaseCount();
intervalCount += finalInterval.size();
}
LOG.info("Produced " + intervalCount + " intervals totalling " + totalUniqueBaseCount + " unique bases.");
return null;
}
use of htsjdk.samtools.util.Interval in project gatk by broadinstitute.
the class IntervalListTools method openIntervalLists.
private List<IntervalList> openIntervalLists(final List<File> files) {
final List<IntervalList> lists = new ArrayList<>();
for (final File f : files) {
final IntervalList list = TYPE.getIntervalList(f, INCLUDE_FILTERED);
if (PADDING != 0) {
final IntervalList out = new IntervalList(list.getHeader());
for (final Interval i : list) {
final int start = i.getStart() - PADDING;
final int end = i.getEnd() + PADDING;
if (start <= end) {
final Interval i2 = new Interval(i.getContig(), start, end, i.isNegativeStrand(), i.getName());
out.add(i2);
}
}
lists.add(out);
} else {
lists.add(list);
}
}
return lists;
}
use of htsjdk.samtools.util.Interval in project gatk by broadinstitute.
the class LiftOverIntervalList method doWork.
/**
* Do the work after command line has been parsed. RuntimeException may be
* thrown by this method, and are reported appropriately.
*/
@Override
protected Object doWork() {
assertFileIsReadable(INPUT);
assertFileIsReadable(SEQUENCE_DICTIONARY);
assertFileIsReadable(CHAIN);
assertFileIsWritable(OUTPUT);
final LiftOver liftOver = new LiftOver(CHAIN);
liftOver.setLiftOverMinMatch(MIN_LIFTOVER_PCT);
final IntervalList fromIntervals = fromFile(INPUT);
final SAMFileHeader toHeader = makeDefault().getFileHeader(SEQUENCE_DICTIONARY);
liftOver.validateToSequences(toHeader.getSequenceDictionary());
final IntervalList toIntervals = new IntervalList(toHeader);
boolean anyFailed = false;
for (final Interval fromInterval : fromIntervals) {
final Interval toInterval = liftOver.liftOver(fromInterval);
if (toInterval != null) {
toIntervals.add(toInterval);
} else {
anyFailed = true;
logger.warn("Liftover failed for ", fromInterval, "(len ", fromInterval.length(), ")");
final List<LiftOver.PartialLiftover> partials = liftOver.diagnosticLiftover(fromInterval);
for (final LiftOver.PartialLiftover partial : partials) {
logger.info(partial);
}
}
}
toIntervals.sorted();
toIntervals.write(OUTPUT);
return anyFailed ? 1 : 0;
}
use of htsjdk.samtools.util.Interval in project gatk-protected by broadinstitute.
the class Pulldown method getIntervals.
/** Returns a new instance of an IntervalList, constructed from the intervals of the internally held
* AllelicCounts. This IntervalList is modifiable and does not change with the state of the Pulldown. */
public IntervalList getIntervals() {
final IntervalList intervals = new IntervalList(header);
intervals.addall(getCounts().stream().map(AllelicCount::getInterval).map(si -> new Interval(si.getContig(), si.getStart(), si.getEnd())).collect(Collectors.toList()));
return intervals;
}
use of htsjdk.samtools.util.Interval in project gatk by broadinstitute.
the class ByIntervalListVariantContextIterator method advance.
/** If the current iterator is null or exhausted, move to the next interval. */
private void advance() {
while ((currentIterator == null || !currentIterator.hasNext()) && this.intervals.hasNext()) {
if (currentIterator != null)
currentIterator.close();
final Interval i = this.intervals.next();
this.currentIterator = this.reader.query(i.getContig(), i.getStart(), i.getEnd());
}
}
Aggregations