00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00013
00014 #include "gn/gnRAWSource.h"
00015 #include "gn/gnGenomeSpec.h"
00016 #include "gn/gnFragmentSpec.h"
00017 #include "gn/gnSourceSpec.h"
00018 #include "gn/gnStringTools.h"
00019 #include "gn/gnDebug.h"
00020
00021 gnRAWSource::gnRAWSource()
00022 {
00023 m_openString = "";
00024 m_contig = NULL;
00025 }
00026
00027 gnRAWSource::gnRAWSource( const gnRAWSource& s ) : gnFileSource(s)
00028 {
00029 if(m_contig != NULL)
00030 m_contig = s.m_contig->Clone();
00031 }
00032
00033 gnRAWSource::~gnRAWSource()
00034 {
00035 m_ifstream.close();
00036 delete m_contig;
00037 }
00038
00039 boolean gnRAWSource::HasContig( const string& name ) const
00040 {
00041 if( name.length() == 0 )
00042 return true;
00043 return false;
00044 }
00045
00046 uint32 gnRAWSource::GetContigID( const string& name ) const
00047 {
00048 return ALL_CONTIGS;
00049 }
00050
00051 string gnRAWSource::GetContigName( const uint32 i ) const
00052 {
00053 return "";
00054 }
00055
00056 gnSeqI gnRAWSource::GetContigSeqLength( const uint32 i ) const
00057 {
00058 if( m_contig && (i == 0 || i == ALL_CONTIGS))
00059 return m_contig->GetSeqLength();
00060 return GNSEQI_ERROR;
00061 }
00062
00063 boolean gnRAWSource::SeqRead( const gnSeqI start, char* buf, uint32& bufLen, const uint32 contigI ){
00064 return Read( start, buf, bufLen );
00065 }
00066
00067 gnGenomeSpec *gnRAWSource::GetSpec() const{
00068 return m_spec->Clone();
00069 }
00070
00071 boolean gnRAWSource::Write(gnSequence& seq, const string& filename){
00072 ofstream m_ofstream(filename.c_str(), ios::out | ios::binary);
00073 if(!m_ofstream.is_open())
00074 return false;
00075
00076 gnSeqC buf[BUFFER_SIZE + 1];
00077 buf[BUFFER_SIZE] = 0;
00078 gnSeqI readOffset = 0;
00079 gnSeqI readLength = seq.length();
00080
00081 while(readLength > 0){
00082 gnSeqI writeLen = readLength < BUFFER_SIZE ? readLength : BUFFER_SIZE;
00083
00084 if(!seq.ToArray(buf, writeLen, readOffset))
00085 return false;
00086 m_ofstream << buf;
00087 readLength -= writeLen;
00088 readOffset += writeLen;
00089 }
00090 return true;
00091 }
00092
00093 gnFileContig* gnRAWSource::GetFileContig( const uint32 contigI ) const{
00094 if(contigI > 0)
00095 return NULL;
00096 return m_contig;
00097 }
00098
00099
00100 boolean gnRAWSource::ParseStream( istream& fin )
00101 {
00102
00103 uint64 streamPos = 0;
00104 uint64 bufReadLen = 0;
00105 char* buf = new char[BUFFER_SIZE];
00106 gnSeqI seqLength = 0;
00107
00108 m_contig->SetRepeatSeqGap(true);
00109 m_contig->SetSectStart(gnContigSequence, 0);
00110
00111 while( !fin.eof() )
00112 {
00113
00114 fin.read( buf , BUFFER_SIZE );
00115 bufReadLen = fin.gcount();
00116
00117 for( uint32 i=0 ; i < bufReadLen ; i++ )
00118 {
00119 if(m_pFilter->IsValid(buf[i]))
00120 seqLength++;
00121 else
00122 m_contig->SetRepeatSeqGap(false);
00123 break;
00124 }
00125 streamPos += bufReadLen;
00126 }
00127 m_contig->SetSectEnd(gnContigSequence, streamPos);
00128 m_contig->SetSeqLength(seqLength);
00129 m_spec = new gnGenomeSpec();
00130 gnFragmentSpec* fragspec = new gnFragmentSpec();
00131 gnSourceSpec* sspec = new gnSourceSpec(this);
00132 sspec->SetSourceName(m_openString);
00133 m_spec->AddSpec(fragspec);
00134 fragspec->AddSpec(sspec);
00135
00136 m_ifstream.clear();
00137 delete[] buf;
00138 return true;
00139 }