*/
package jalview.io;
+import jalview.bin.Cache;
import jalview.datamodel.CigarParser;
import jalview.datamodel.Range;
import jalview.datamodel.Sequence;
import java.util.PrimitiveIterator.OfInt;
import java.util.SortedMap;
+import htsjdk.samtools.SAMFormatException;
import htsjdk.samtools.SAMRecord;
import htsjdk.samtools.SAMRecordIterator;
import htsjdk.samtools.SAMSequenceRecord;
// first position in alignment
private int alignmentStart = -1;
+ private File _bamFile;
+
/**
* Creates a new BamFile object.
*/
throws IOException
{
super(true, inFile, sourceType);
+ _bamFile = new File(inFile);
+ initFileReader();
+ }
+ private void initFileReader() throws IOException
+ {
final SamReaderFactory factory = SamReaderFactory.makeDefault()
.enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,
SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
.validationStringency(ValidationStringency.SILENT);
- fileReader = factory.open(new File(inFile));
+ // File-based bam
+ if (_bamFile!=null)
+ {
+ fileReader = factory.open(_bamFile); // will need to be adapted for JalviewJS/etc
+ }
+ else
+ {
+ // try and locate index ?
+ String index = getDataName() + ".bai";
+ fileReader = factory.open(SamInputResource.of(getDataName())
+ .index(new URL(index)));
+ }
}
-
/**
* Creates a new BamFile object
*
*/
public BamFile(FileParse source) throws IOException
{
- super(true, source);
+ super(false, source);
parseSuffix();
- final SamReaderFactory factory = SamReaderFactory.makeDefault()
- .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,
- SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
- .validationStringency(ValidationStringency.SILENT);
-
- // File-based bam
if (source.getDataSourceType() == DataSourceType.FILE)
- {
- fileReader = factory.open(source.inFile);
- }
- else
- {
- // locate index ?
- String index = source.getDataName() + ".bai";
- fileReader = factory.open(SamInputResource.of(source.getDataName())
- .index(new URL(index)));
- }
+ {
+ _bamFile = source.inFile;
+ } else {
+
+ }
+ initFileReader();
+ doParse();
}
@Override
@Override
public void parse()
{
+ boolean needToReopen=false;
// only actually parse if params are set
if (chromosome != null && chromosome != "")
{
- SAMRecordIterator it = fileReader.query(chromosome, start, end,
+ SAMRecordIterator it;
+ try {
+ it = fileReader.query(chromosome, start, end,
false);
+ } catch (UnsupportedOperationException ex)
+ {
+ needToReopen=true;
+ // could be a sam text file, so we just iterate through without query
+ it = fileReader.iterator();
+ }
CigarParser parser = new CigarParser('-');
Range[] xtent = new Range[] { new Range(start, end) };
SortedMap<Integer, Integer> insertions[] = parser.getInsertions(it,
padRef(refSeq, parser);
padRef(revRefSeq, parser);
- it = fileReader.query(chromosome, start, end, false);
+ if (needToReopen)
+ {
+ try {
+ initFileReader();
+ } catch (IOException x)
+ {
+ Cache.log.warn("Couldn't reopen S/BAM file",x);
+ }
+ }
+ try {
+ it = fileReader.query(chromosome, start, end,
+ false);
+ } catch (UnsupportedOperationException ex)
+ {
+ // could be a sam text file, so we just iterate through without query
+ it = fileReader.iterator();
+ }
ArrayList<SequenceI> fwd = new ArrayList(), rev = new ArrayList();
while (it.hasNext())
{
- SAMRecord rec = it.next();
+ SAMRecord rec=null;
+ try {
+ rec = it.next();
+ } catch (SAMFormatException q)
+ {
+ Cache.log.info("Bailing on bad SAM line again",q);
+ break;
+ }
// set the alignment start to be start of first read (we assume reads
// are sorted)