Search in sources :

Example 1 with Faidx

use of faidx.Faidx in project ASCIIGenome by dariober.

the class GenomicCoords method setSamSeqDictFromFasta.

private boolean setSamSeqDictFromFasta(String fasta) throws IOException {
    try {
        if (new File(fasta + ".fai").exists() && !new File(fasta + ".fai").isDirectory()) {
        // 
        } else {
            throw new FileNotFoundException();
        }
    // fa= new IndexedFastaSequenceFile(new File(fasta));
    } catch (FileNotFoundException e) {
        try {
            new Faidx(new File(fasta));
            (new File(fasta + ".fai")).deleteOnExit();
        } catch (Exception e1) {
        // 
        }
    }
    BufferedReader br = new BufferedReader(new FileReader(new File(fasta + ".fai")));
    // null;
    SAMSequenceDictionary seqDict = new SAMSequenceDictionary();
    while (true) {
        String line = br.readLine();
        if (line == null) {
            break;
        }
        SAMSequenceRecord ssqRec = new SAMSequenceRecord(line.split("\t")[0], Integer.parseInt(line.split("\t")[1]));
        seqDict.addSequence(ssqRec);
    }
    br.close();
    // fa.close();
    this.setSamSeqDictSource(new File(fasta).getAbsolutePath());
    this.setSamSeqDict(seqDict);
    return true;
// }
// fa.close();
// return false;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) BufferedReader(java.io.BufferedReader) Faidx(faidx.Faidx) FileReader(java.io.FileReader) SAMSequenceRecord(htsjdk.samtools.SAMSequenceRecord) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) SAMSequenceDictionary(htsjdk.samtools.SAMSequenceDictionary) InvalidCommandLineException(exceptions.InvalidCommandLineException) InvalidColourException(exceptions.InvalidColourException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidGenomicCoordsException(exceptions.InvalidGenomicCoordsException)

Example 2 with Faidx

use of faidx.Faidx in project ASCIIGenome by dariober.

the class GenomicCoords method setGenome.

/* Methods */
/**
 * Set genome dictionary and fasta file ref if available. See
 * GenomicCoords.getSamSeqDictFromAnyFile() for available inputs.
 * @param includeGenomeFile: Should the input data be treated as a genome file?
 * Set to true only if the input can be a genome file. Other files (bed, vcf, gff)
 * look like valid genome file and this can result in wring dictionary.
 */
public void setGenome(List<String> input, boolean includeGenomeFile) throws IOException {
    List<String> cleanList = new ArrayList<String>();
    for (String x : input) {
        if (x != null && !x.trim().isEmpty()) {
            cleanList.add(Utils.tildeToHomeDir(x));
        }
    }
    if (cleanList.size() == 0) {
        return;
    }
    // Set Dictionary
    this.setSamSeqDictFromAnySource(cleanList, includeGenomeFile);
    // Try to set fasta sequence
    for (String x : cleanList) {
        boolean done = true;
        try {
            if (new File(x + ".fai").exists()) {
                this.setFastaFile(x);
            } else {
                throw new FileNotFoundException();
            }
        // IndexedFastaSequenceFile fa= new IndexedFastaSequenceFile(new File(x));
        // this.setFastaFile(x);
        // fa.close();
        } catch (FileNotFoundException e) {
            try {
                new Faidx(new File(x));
                (new File(x + ".fai")).deleteOnExit();
                this.setFastaFile(x);
            } catch (Exception e1) {
                done = false;
            }
        }
        if (done) {
            break;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Faidx(faidx.Faidx) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) InvalidCommandLineException(exceptions.InvalidCommandLineException) InvalidColourException(exceptions.InvalidColourException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvalidGenomicCoordsException(exceptions.InvalidGenomicCoordsException)

Example 3 with Faidx

use of faidx.Faidx in project ASCIIGenome by dariober.

the class Utils method checkFasta.

public static void checkFasta(String fasta, int debug) throws IOException, UnindexableFastaFileException {
    if (fasta == null) {
        return;
    }
    File fafile = new File(fasta);
    if (!fafile.isFile()) {
        System.err.println("Fasta file '" + fasta + "' not found.");
        if (debug == 0 || debug == 1) {
            System.exit(1);
        } else if (debug == 2) {
            throw new IOException();
        }
    }
    if (!fafile.canRead()) {
        System.err.println("Fasta file '" + fasta + "' is not readable.");
        if (debug == 0 || debug == 1) {
            System.exit(1);
        } else if (debug == 2) {
            throw new IOException();
        }
    }
    IndexedFastaSequenceFile faSeqFile = null;
    try {
        faSeqFile = new IndexedFastaSequenceFile(fafile);
        faSeqFile.close();
    } catch (FileNotFoundException e) {
        System.err.println("\nIndexing '" + fasta + "'.");
        new Faidx(new File(fasta));
        (new File(fasta + ".fai")).deleteOnExit();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) Faidx(faidx.Faidx) IOException(java.io.IOException) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile)

Aggregations

Faidx (faidx.Faidx)3 IndexedFastaSequenceFile (htsjdk.samtools.reference.IndexedFastaSequenceFile)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 InvalidColourException (exceptions.InvalidColourException)2 InvalidCommandLineException (exceptions.InvalidCommandLineException)2 InvalidGenomicCoordsException (exceptions.InvalidGenomicCoordsException)2 MalformedURLException (java.net.MalformedURLException)2 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)1 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1