001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 * Created on 01-21-2010 021 */ 022package org.biojava.nbio.core.sequence.io; 023 024import org.biojava.nbio.core.exceptions.CompoundNotFoundException; 025import org.biojava.nbio.core.sequence.DNASequence; 026import org.biojava.nbio.core.sequence.compound.NucleotideCompound; 027import org.biojava.nbio.core.sequence.io.template.SequenceCreatorInterface; 028import org.biojava.nbio.core.sequence.io.template.SequenceParserInterface; 029import org.biojava.nbio.core.sequence.loader.SequenceFileProxyLoader; 030import org.biojava.nbio.core.sequence.template.AbstractSequence; 031import org.biojava.nbio.core.sequence.template.CompoundSet; 032import org.biojava.nbio.core.sequence.template.ProxySequenceReader; 033 034import java.io.File; 035import java.io.IOException; 036import java.util.List; 037 038/** 039 * This class is a good example of using the SequenceCreatorInterface where during parsing of the stream 040 * the sequence and the offset index are passed to create a Protein sequence that will be loaded in lazily. 041 * This way you can load very large fasta files and store accession id and delay loading the sequence to save 042 * memory. The index is the file stream offset so when a DNASequence has a call to getSequence() the 043 * SequenceFileProxyLoader will open the file and offset to the index and retrieve the sequence. 044 * 045 * Same approach can be used for genome sequence data stored in a local fasta file, in a database or via http 046 * interface to a remote server 047 * 048 * @author Scooter Willis 049 */ 050public class FileProxyDNASequenceCreator implements 051 SequenceCreatorInterface<NucleotideCompound> { 052 053 CompoundSet<NucleotideCompound> compoundSet = null; 054 File file = null; 055 SequenceParserInterface sequenceParser; 056 057 /** 058 * Need File so that we can store full path name in SequenceFileProxyLoader for Random File access as a quick read 059 * @param file 060 * @param compoundSet 061 * @param sequenceParser 062 */ 063 public FileProxyDNASequenceCreator(File file, 064 CompoundSet<NucleotideCompound> compoundSet, 065 SequenceParserInterface sequenceParser) { 066 this.compoundSet = compoundSet; 067 this.file = file; 068 this.sequenceParser = sequenceParser; 069 } 070 071 /** 072 * Even though we are passing in the sequence we really only care about the length of the sequence and the offset 073 * index in the fasta file. 074 * @param sequence 075 * @param index 076 * @return 077 * @throws CompoundNotFoundException 078 * @throws IOException 079 */ 080 @Override 081 public AbstractSequence<NucleotideCompound> getSequence(String sequence, long index ) throws CompoundNotFoundException, IOException { 082 SequenceFileProxyLoader<NucleotideCompound> sequenceFileProxyLoader = new SequenceFileProxyLoader<>( 083 file, 084 sequenceParser, 085 index, 086 sequence.length(), 087 compoundSet); 088 return new DNASequence(sequenceFileProxyLoader, compoundSet); 089 } 090 091 /** 092 * Should be able to extend the same concept to a remote URL call or database connection. Not supported yet 093 * @param proxyLoader 094 * @param index 095 * @return 096 */ 097 @Override 098 public AbstractSequence<NucleotideCompound> getSequence( 099 ProxySequenceReader<NucleotideCompound> proxyLoader, long index) { 100 throw new UnsupportedOperationException("Not supported yet."); 101 } 102 103 /** 104 * Not sure of use case and currently not supported 105 * @param list 106 * @return 107 */ 108 @Override 109 public AbstractSequence<NucleotideCompound> getSequence( 110 List<NucleotideCompound> list) { 111 throw new UnsupportedOperationException("Not supported yet."); 112 } 113}