Search in sources :

Example 1 with RandomAccessFastaIndex

use of com.milaboratory.core.io.sequence.fasta.RandomAccessFastaIndex in project repseqio by repseqio.

the class AbstractRAFastaResolver method resolveReader.

public synchronized RandomAccessFastaReader<NucleotideSequence> resolveReader(SequenceAddress address) {
    for (int retry = 0; retry < 2; ++retry) {
        // Getting reader key
        String readerKey = resolveReaderId(address);
        // Checking if reader already opened
        RandomAccessFastaReader<NucleotideSequence> reader = readers.get(readerKey);
        Path file = null;
        try {
            // Getting fasta file path
            // Download occur here
            file = getFASTAFile(address);
            // Creating or loading index
            RandomAccessFastaIndex index = RandomAccessFastaIndex.index(file, true, getReporter());
            // Caching reader
            readers.put(readerKey, reader = new RandomAccessFastaReader<>(file, index, NucleotideSequence.ALPHABET));
            return reader;
        } catch (Exception e) {
            // Something went wrong with file, removing for re-download.
            log.warn("Error opening {}." + (deleteOnError ? " Removing." : ""), file, e);
            // removing source file and index if exists
            if (deleteOnError && file != null)
                try {
                    Files.delete(file);
                    Path indexFile = file.resolveSibling(file.getFileName() + RandomAccessFastaIndex.INDEX_SUFFIX);
                    if (Files.exists(indexFile))
                        Files.delete(indexFile);
                } catch (IOException e1) {
                    throw new RuntimeException(e1);
                }
        // Retry
        }
    }
    throw new RuntimeException();
}
Also used : Path(java.nio.file.Path) RandomAccessFastaIndex(com.milaboratory.core.io.sequence.fasta.RandomAccessFastaIndex) RandomAccessFastaReader(com.milaboratory.core.io.sequence.fasta.RandomAccessFastaReader) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

RandomAccessFastaIndex (com.milaboratory.core.io.sequence.fasta.RandomAccessFastaIndex)1 RandomAccessFastaReader (com.milaboratory.core.io.sequence.fasta.RandomAccessFastaReader)1 NucleotideSequence (com.milaboratory.core.sequence.NucleotideSequence)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1