All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencb.biodata.tools.sequence.SamtoolsFastaIndex Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * 
 *
 */

package org.opencb.biodata.tools.sequence;

import htsjdk.samtools.SAMSequenceDictionary;
import htsjdk.samtools.reference.ReferenceSequence;
import htsjdk.samtools.reference.ReferenceSequenceFile;
import htsjdk.samtools.reference.ReferenceSequenceFileFactory;
import htsjdk.samtools.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.opencb.commons.utils.FileUtils;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Created by imedina on 21/10/16.
 */
@Deprecated
public class SamtoolsFastaIndex implements SequenceAdaptor {

    private ReferenceSequenceFile indexedFastaSequenceFile;
    private String samtoolsBin;

    public SamtoolsFastaIndex() {

    }

    public SamtoolsFastaIndex(String fastaFileName) throws IOException {
        if (!ReferenceSequenceFileFactory.canCreateIndexedFastaReader(Paths.get(fastaFileName))) {
            throw new IOException("Fasta file '" + fastaFileName + "' is not indexed.");
        }
        this.indexedFastaSequenceFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(Paths.get(fastaFileName));
    }

//    public void index(Path fastaFilePath) throws IOException, RocksDBException {
//        index(fastaFilePath, Paths.get(fastaFilePath.toString() + ".fai"));
//    }

    /**
     * Checks if the set FASTA file is indexed
     * @return
     */
    public Boolean hasIndex() {
        Boolean hasIndex = false;
        if (this.indexedFastaSequenceFile != null) {
            hasIndex = this.indexedFastaSequenceFile.isIndexed();
        }
        return hasIndex;
    }

    public void index(Path fastaFilePath) throws IOException {
        FileUtils.checkFile(fastaFilePath);

        String samtoolspath = "samtools";
        if (StringUtils.isNotEmpty(samtoolsBin) ) {
            samtoolspath = samtoolsBin;
        }
        System.out.println(samtoolspath + " faidx " + fastaFilePath.toFile().toString());
        Runtime.getRuntime().exec(samtoolspath + " faidx " + fastaFilePath.toFile().toString());
    }

    public void index(Path fastaFilePath, Path fastaIndexFilePath) throws IOException {
        throw new UnsupportedOperationException();
    }

    public String query(String chromosome, int start, int end) {
        ReferenceSequence subsequenceAt = indexedFastaSequenceFile.getSubsequenceAt(chromosome, start, end);
        return StringUtil.bytesToString(subsequenceAt.getBases());
    }

    public ReferenceSequence queryReferenceSequence(String chromosome, int start, int end) {
        ReferenceSequence referenceSequence = indexedFastaSequenceFile.getSubsequenceAt(chromosome, start, end);
        return referenceSequence;
    }

    public SAMSequenceDictionary getSequenceDictionary() {
        return indexedFastaSequenceFile.getSequenceDictionary();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy