synchronized private static IndexedFastaSequenceFile fetchFasta(String file) { return threadLocalFastaFiles.get().computeIfAbsent( file, (f) -> { try { return new IndexedFastaSequenceFile(new File((f))); } catch (FileNotFoundException e) { throw new IllegalArgumentException("Couldn't open reference file: " + f, e); } } ); }
public byte[] getSequenceFromFasta() throws IOException{ IndexedFastaSequenceFile faSeqFile = null; try { faSeqFile = new IndexedFastaSequenceFile(new File(this.fastaFile)); try{ byte[] seq= faSeqFile.getSubsequenceAt(this.chrom, this.from, this.to).getBases(); faSeqFile.close(); return seq; } catch (NullPointerException e){ System.err.println("Cannot fetch sequence " + this.chrom + ":" + this.from + "-" + this.to + " for fasta file " + this.fastaFile); e.printStackTrace(); } faSeqFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }
public static IndexedFastaSequenceFile createIndexedFastaSequenceFile(File file) throws RuntimeException, FileNotFoundException { if (IndexedFastaSequenceFile.canCreateIndexedFastaReader(file)) { IndexedFastaSequenceFile ifsFile = new IndexedFastaSequenceFile(file); return ifsFile; } else throw new RuntimeException( "Reference fasta file is not indexed or index file not found. Try executing 'samtools faidx " + file.getAbsolutePath() + "'"); }
private static File findRequiredFastaIndexFile(File fastaFile) throws FileNotFoundException { File ret = findFastaIndex(fastaFile); if (ret == null) throw new FileNotFoundException(getFastaIndexFileName(fastaFile) + " not found."); return ret; }
private void loadFASTAIndex() { try { this.fasta = new IndexedFastaSequenceFile(new File(options.getPathReferenceFASTA())); } catch (FileNotFoundException e) { throw new UncheckedJannovarException("Could not load FASTA index", e); } if (this.fasta.getSequenceDictionary() == null) { throw new UncheckedJannovarException( "FASTA sequence dictionary empty, you have a REFERENCE.dict file (create with Picard " + "or samtools dict, version >=1.3)"); } this.translator = new NucleotideChangeToGenomeVariantTranslator(jannovarData, fasta); }
faSeqFile= new IndexedFastaSequenceFile(fafile); faSeqFile.close(); } catch (FileNotFoundException e) { System.err.println("\nIndexing '" + fasta + "'.");
/** * Method convert fasta data to array contains header and nucleotide bases. * @param fasta path to fasta file * @param chr chromosome name of region * @param start start position of region * @param end end position of region * @return array of nucleotide bases in the region of fasta */ public String[] retrieveSubSeq(String fasta, String chr, int start, int end) { IndexedFastaSequenceFile idx = fetchFasta(fasta); ReferenceSequence seq = idx.getSubsequenceAt(chr, start, end); byte[] bases = seq.getBases(); return new String[] { ">" + chr + ":" + start + "-" + end, bases != null ? new String(bases) : "" }; }
IndexedFastaSequenceFile faSeqFile = new IndexedFastaSequenceFile(new File(fasta)); String region= faSeqFile.nextSequence().getName(); faSeqFile.close(); return region;
result = super.getSubsequenceAt(contig, start, stop); if ( ! preserveCase ) StringUtil.toUpperCase(result.getBases()); if ( ! preserveIUPAC ) BaseUtils.convertIUPACtoN(result.getBases(), true, start < 1); } else { SAMSequenceRecord contigInfo = super.getSequenceDictionary().getSequence(contig); myCache.start = Math.max(start - cacheMissBackup, 0); myCache.stop = Math.min(start + cacheSize + cacheMissBackup, contigInfo.getSequenceLength()); myCache.seq = super.getSubsequenceAt(contig, myCache.start, myCache.stop);
public GenomeRegionSequenceExtractor(JannovarData jannovarData, IndexedFastaSequenceFile indexedFile) { super(); this.jannovarData = jannovarData; this.indexedFile = indexedFile; if (this.indexedFile.getSequenceDictionary() == null) { throw new UncheckedJannovarException( "FASTA file has no sequence dictionary. Are you missing the REFERENCE.dict file? " + "Hint: create with samtools dict (version >=1.2) or Picard."); } }
/** * Open the given indexed fasta sequence file. Throw an exception if the file cannot be opened. * @param file The file to open. * @param index Pre-built FastaSequenceIndex, for the case in which one does not exist on disk. * @throws FileNotFoundException If the fasta or any of its supporting files cannot be found. */ public IndexedFastaSequenceFile(final File file, final FastaSequenceIndex index) { super(file); if (index == null) throw new IllegalArgumentException("Null index for fasta " + file); this.index = index; IOUtil.assertFileIsReadable(file); final FileInputStream in; try { in = new FileInputStream(file); } catch (FileNotFoundException e) { throw new SAMException("Fasta file should be readable but is not: " + file, e); } channel = in.getChannel(); reset(); if(getSequenceDictionary() != null) sanityCheckDictionaryAgainstIndex(file.getAbsolutePath(),sequenceDictionary,index); }
/** * Open the given indexed fasta sequence file. Throw an exception if the file cannot be opened. * @param path The file to open. * @throws FileNotFoundException If the fasta or any of its supporting files cannot be found. */ public IndexedFastaSequenceFile(final Path path) throws FileNotFoundException { this(path, new FastaSequenceIndex((findRequiredFastaIndexFile(path)))); }
/** * @deprecated use {@link ReferenceSequenceFileFactory#canCreateIndexedFastaReader(Path)} instead. */ @Deprecated public static boolean canCreateIndexedFastaReader(final File fastaFile) { return canCreateIndexedFastaReader(fastaFile.toPath()); }
public static boolean canCreateIndexedFastaReader(final File fastaFile) { return (fastaFile.exists() && findFastaIndex(fastaFile) != null); }
private Variant getVariantFromDeletion(String chromosome, IndexedFastaSequenceFile genomeSequenceFastaFile) throws ParseException { String deletedNucleotides; String[] fields = change.split("(del)"); if (fields.length == 2) { deletedNucleotides = fields[1]; } else if (fields.length > 2) { throw new ParseException("Hgvs deletion malformed: " + change, change.lastIndexOf("del")); } else { if (genomeSequenceFastaFile != null) { deletedNucleotides = new String(genomeSequenceFastaFile.getSubsequenceAt(chromosome, start, stop).getBases()); } else { // in this case, without genome sequence fasta file, deleted nucleotides cannot be obtained deletedNucleotides = null; } } Variant variant = null; if (deletedNucleotides != null) { String alternateString = "-"; variant = new Variant(chromosome, start, stop, deletedNucleotides, alternateString); } return variant; }
/** Map contig name (from genome variant) to contig name in FASTA */ private String mapContigToFasta(String contigName) { // Map genome variant's contig to unique ID Integer contigID = jannovarData.getRefDict().getContigNameToID().get(contigName); if (contigID == null) throw new UncheckedJannovarException("Unknown contig name " + contigName); // Try to find matching contig in fasta String nameInFasta = null; for (SAMSequenceRecord record : fasta.getSequenceDictionary().getSequences()) { if (jannovarData.getRefDict().getContigNameToID().containsKey(record.getSequenceName())) { nameInFasta = record.getSequenceName(); break; } } if (nameInFasta == null) throw new UncheckedJannovarException("Could not find corresponding contig in FASTA for " + contigName); return nameInFasta; }
/** * Open the given indexed fasta sequence file. Throw an exception if the file cannot be opened. * @param path The file to open. * @throws FileNotFoundException If the fasta or any of its supporting files cannot be found. */ public IndexedFastaSequenceFile(final Path path) throws FileNotFoundException { this(path, new FastaSequenceIndex((findRequiredFastaIndexFile(path)))); }
/** * @deprecated use {@link ReferenceSequenceFileFactory#canCreateIndexedFastaReader(Path)} instead. */ @Deprecated public static boolean canCreateIndexedFastaReader(final File fastaFile) { return canCreateIndexedFastaReader(fastaFile.toPath()); }
/** * @deprecated use {@link ReferenceSequenceFileFactory#canCreateIndexedFastaReader(Path)} instead. */ @Deprecated public static boolean canCreateIndexedFastaReader(final Path fastaFile) { try { if (IOUtil.isBlockCompressed(fastaFile, true)) { return false; } return (Files.exists(fastaFile) && findFastaIndex(fastaFile) != null); } catch (IOException e) { return false; } }
/** * Construct new variant normalizer object * * @param fastaPath * Path to indexed FASTA file * @throws JannovarVarDBException * On problems with opening the FASTA/FAI file */ public VariantNormalizer(String fastaPath) throws JannovarVarDBException { this.fastaPath = fastaPath; try { this.fai = new IndexedFastaSequenceFile(new File(fastaPath)); } catch (FileNotFoundException e) { throw new JannovarVarDBException("Could not find FASTA/FAI file", e); } }