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;
}
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;
}
}
}
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();
}
}
Aggregations