use of htsjdk.samtools.reference.IndexedFastaSequenceFile in project jvarkit by lindenb.
the class Bam2Raster method doWork.
@Override
public int doWork(final List<String> args) {
if (this.regionStr == null) {
LOG.error("Region was not defined.");
return -1;
}
if (this.WIDTH < 100) {
LOG.info("adjusting WIDTH to 100");
this.WIDTH = 100;
}
SamReader samFileReader = null;
try {
final SamReaderFactory srf = super.createSamReaderFactory();
if (this.referenceFile != null) {
LOG.info("loading reference");
this.indexedFastaSequenceFile = new IndexedFastaSequenceFile(this.referenceFile);
srf.referenceSequence(this.referenceFile);
}
final IntervalParser intervalParser = new IntervalParser(this.indexedFastaSequenceFile == null ? null : this.indexedFastaSequenceFile.getSequenceDictionary()).setFixContigName(true);
this.interval = intervalParser.parse(this.regionStr);
if (this.interval == null) {
LOG.error("Cannot parse interval " + regionStr + " or chrom doesn't exists in sam dictionary.");
return -1;
}
LOG.info("Interval is " + this.interval);
loadVCFs();
for (final String bamFile : IOUtils.unrollFiles(args)) {
samFileReader = srf.open(SamInputResource.of(bamFile));
final SAMFileHeader header = samFileReader.getFileHeader();
final SAMSequenceDictionary dict = header.getSequenceDictionary();
if (dict == null) {
LOG.error("no dict in " + bamFile);
return -1;
}
if (dict.getSequence(this.interval.getContig()) == null) {
LOG.error("no such chromosome in " + bamFile + " " + this.interval);
return -1;
}
scan(samFileReader);
samFileReader.close();
samFileReader = null;
}
if (this.key2partition.isEmpty()) {
LOG.error("No data was found.(not Read-Group specified ?");
return -1;
}
this.key2partition.values().stream().forEach(P -> P.build());
saveImages(this.key2partition.values().stream().map(P -> P.image).collect(Collectors.toList()));
return RETURN_OK;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(indexedFastaSequenceFile);
CloserUtil.close(samFileReader);
indexedFastaSequenceFile = null;
}
}
use of htsjdk.samtools.reference.IndexedFastaSequenceFile in project jvarkit by lindenb.
the class LowResBam2Raster method doWork.
@Override
public int doWork(final List<String> args) {
if (this.regionStr == null) {
LOG.error("Region was not defined.");
return -1;
}
if (this.WIDTH < 100) {
LOG.info("adjusting WIDTH to 100");
this.WIDTH = 100;
}
if (this.gcWinSize <= 0) {
LOG.info("adjusting GC win size to 5");
this.gcWinSize = 5;
}
SamReader samFileReader = null;
try {
if (this.referenceFile != null) {
LOG.info("loading reference");
this.indexedFastaSequenceFile = new IndexedFastaSequenceFile(this.referenceFile);
}
final SamReaderFactory srf = super.createSamReaderFactory();
this.interval = new IntervalParser(this.indexedFastaSequenceFile == null ? null : this.indexedFastaSequenceFile.getSequenceDictionary()).parse(this.regionStr);
if (this.interval == null) {
LOG.error("Cannot parse interval " + regionStr + " or chrom doesn't exists in sam dictionary.");
return -1;
}
LOG.info("Interval is " + this.interval);
loadVCFs();
if (this.knownGeneUrl != null) {
IntervalTreeMap<List<KnownGene>> map = KnownGene.loadUriAsIntervalTreeMap(this.knownGeneUrl, (KG) -> (KG.getContig().equals(this.interval.getContig()) && !(KG.getEnd() < this.interval.getStart() || KG.getStart() + 1 > this.interval.getEnd())));
this.knownGenes.addAll(map.values().stream().flatMap(L -> L.stream()).collect(Collectors.toList()));
}
for (final String bamFile : IOUtils.unrollFiles(args)) {
samFileReader = srf.open(SamInputResource.of(bamFile));
final SAMFileHeader header = samFileReader.getFileHeader();
final SAMSequenceDictionary dict = header.getSequenceDictionary();
if (dict == null) {
LOG.error("no dict in " + bamFile);
return -1;
}
if (dict.getSequence(this.interval.getContig()) == null) {
LOG.error("no such chromosome in " + bamFile + " " + this.interval);
return -1;
}
scan(samFileReader);
samFileReader.close();
samFileReader = null;
}
if (this.key2partition.isEmpty()) {
LOG.error("No data was found. no Read-Group specified ? no data in that region ?");
return -1;
}
this.key2partition.values().stream().forEach(P -> P.make());
saveImages(this.key2partition.values().stream().map(P -> P.image).collect(Collectors.toList()));
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(samFileReader);
}
}
Aggregations