include/gn/gnMultiSpec.h

Go to the documentation of this file.
00001 00002 // File: gnMultiSpec.h 00003 // Purpose: multiple spec template class 00004 // Description: Template for specs which can contain multiple data sources 00005 // Changes: 00006 // Version: libGenome 0.5.1 00007 // Author: Aaron Darling 00008 // Modified by: 00009 // Copyright: (c) Aaron Darling 00010 // Licenses: See COPYING file for details 00012 #ifndef _gnMultiSpec_h_ 00013 #define _gnMultiSpec_h_ 00014 00015 #include "gn/gnDefs.h" 00016 #include "gn/gnException.h" 00017 00018 #include <vector> 00019 #include <string> 00020 00021 #include "gn/gnClone.h" 00022 #include "gn/gnBaseFeature.h" 00023 #include "gn/gnBaseHeader.h" 00024 #include "gn/gnBaseSpec.h" 00025 00030 template< class SubSpec > 00031 class GNDLLEXPORT gnMultiSpec : public gnBaseSpec 00032 { 00033 public: 00034 gnMultiSpec(){} 00038 virtual ~gnMultiSpec(){} 00039 virtual gnMultiSpec* Clone() const = 0; 00040 00041 // Base Spec stuff 00042 virtual gnSeqI GetLength() const; 00043 virtual void CropStart( gnSeqI cropLen ); 00044 virtual void CropEnd( gnSeqI cropLen ); 00045 00046 virtual string GetSourceName() const; 00047 virtual void SetSourceName(const string& sourceName); 00048 virtual void Clear(); 00049 00050 virtual boolean SeqRead(const gnSeqI start, gnSeqC* buf, gnSeqI& bufLen, const uint32 contigI ) const; 00051 00052 //Multispec stuff 00057 virtual uint32 GetSpecListLength() const; 00063 virtual SubSpec* GetSpec( const uint32 i ) const; 00069 virtual SubSpec* GetSpecByBase( const gnSeqI baseI ) const; 00075 virtual uint32 GetSpecIndexByBase( const gnSeqI baseI ) const; 00081 virtual uint32 GetSpecIndexByName( const string& name ) const; 00087 virtual gnSeqI GetSpecStartBase( const uint32 specI ) const; 00093 virtual gnSeqI GetSpecEndBase( const uint32 specI ) const; 00101 virtual void AddSpec( SubSpec* spec, const uint32 i = UINT32_MAX ); 00106 virtual void RemoveSpec( uint32 i ); 00107 00112 virtual uint32 GetHeaderListLength() const; 00119 virtual void AddHeader( gnBaseHeader *head, const uint32 i = UINT32_MAX); 00126 virtual gnBaseHeader* GetHeader( const uint32 i ) const; 00133 virtual gnBaseHeader* GetHeader( const string& name, uint32& i) const; 00139 virtual void RemoveHeader( uint32 i ); 00140 00146 uint32 AddFeature( gnBaseFeature* feat ); 00151 virtual uint32 GetFeatureListLength() const = 0; 00157 virtual gnBaseFeature* GetFeature( const uint32 i ) const = 0; 00164 virtual void GetContainedFeatures(const gnLocation& lt, vector<gnBaseFeature*>& feature_vector, vector<uint32>& index_vector) const = 0; 00171 virtual void GetIntersectingFeatures(const gnLocation& lt, vector<gnBaseFeature*>& feature_vector, vector<uint32>& index_vector) const = 0; 00177 virtual void GetBrokenFeatures(const gnLocation& lt, vector<gnBaseFeature*>& feature_vector) const = 0; 00183 virtual void RemoveFeature( const uint32 i ) = 0; 00184 00185 protected: 00186 string m_sourceName; 00187 00188 vector < SubSpec* > m_SpecList; 00189 vector < gnBaseHeader* > m_headerList; 00190 private: 00191 00192 }; // class gnMultiSpec 00193 00194 template< class SubSpec > 00195 inline 00196 uint32 gnMultiSpec< SubSpec >::GetHeaderListLength() const 00197 { 00198 return m_headerList.size(); 00199 } 00200 00201 template< class SubSpec > 00202 inline 00203 gnBaseHeader* gnMultiSpec< SubSpec >::GetHeader(const uint32 i) const 00204 { 00205 if(i < m_headerList.size()){ 00206 return m_headerList[i]; 00207 } 00208 return 0; 00209 } 00210 00211 template< class SubSpec > 00212 inline 00213 string gnMultiSpec< SubSpec >::GetSourceName() const{ 00214 return m_sourceName; 00215 } 00216 00217 template< class SubSpec > 00218 inline 00219 void gnMultiSpec< SubSpec >::SetSourceName(const string& sourceName){ 00220 m_sourceName = sourceName; 00221 } 00222 00223 template< class SubSpec > 00224 uint32 gnMultiSpec< SubSpec >::GetSpecListLength() const{ 00225 return m_SpecList.size(); 00226 } 00227 00228 template< class SubSpec > 00229 SubSpec* gnMultiSpec< SubSpec >::GetSpec( const uint32 i ) const{ 00230 if(i < m_SpecList.size()) 00231 return m_SpecList[i]; 00232 Throw_gnEx(FragmentIndexOutOfBounds()); 00233 } 00234 00235 template< class SubSpec > 00236 SubSpec* gnMultiSpec< SubSpec >::GetSpecByBase( const gnSeqI baseI ) const{ 00237 return m_SpecList[GetSpecIndexByBase(baseI)]; 00238 } 00239 00240 template< class SubSpec > 00241 void gnMultiSpec< SubSpec >::AddSpec( SubSpec* spec, const uint32 i ){ 00242 uint32 index = i == UINT32_MAX ? m_SpecList.size() : i; 00243 if(index <= m_SpecList.size()){ 00244 m_SpecList.insert(m_SpecList.begin() + index, spec); 00245 } 00246 } 00247 00248 template< class SubSpec > 00249 void gnMultiSpec< SubSpec >::RemoveSpec( uint32 i ){ 00250 if(i < GetSpecListLength()){ 00251 m_SpecList.erase(m_SpecList.begin() + i); 00252 } 00253 } 00254 00255 template< class SubSpec > 00256 void gnMultiSpec< SubSpec >::Clear() 00257 { 00258 gnBaseSpec::Clear(); 00259 uint32 list_size = m_headerList.size(); 00260 for(uint32 i=0; i < list_size; i++) 00261 delete m_headerList[i]; 00262 m_headerList.clear(); 00263 } 00264 00265 template< class SubSpec > 00266 gnSeqI gnMultiSpec< SubSpec >::GetLength() const 00267 { 00268 gnSeqI subLen = 0; //aggregate the lower levels. 00269 for(uint32 i=0; i < GetSpecListLength(); i++) 00270 subLen += GetSpec(i)->GetLength(); 00271 return subLen; 00272 } 00273 00274 //Return the index of the subspec which contains the base in question 00275 template< class SubSpec > 00276 uint32 gnMultiSpec< SubSpec >::GetSpecIndexByBase( const gnSeqI baseI ) const{ 00277 gnSeqI cur_length = 0; 00278 for(uint32 i=0; i < GetSpecListLength(); i++){ 00279 cur_length += GetSpec(i)->GetLength(); 00280 if(baseI < cur_length) 00281 return i; 00282 } 00283 //if we made it here then the base was out of bounds 00284 Throw_gnEx(SeqIndexOutOfBounds()); 00285 } 00286 00287 template< class SubSpec > 00288 uint32 gnMultiSpec< SubSpec >::GetSpecIndexByName( const string& name ) const{ 00289 for(uint32 i=0; i < GetSpecListLength(); i++){ 00290 if(name == GetSpec(i)->GetName()) 00291 return i; 00292 } 00293 Throw_gnEx(SpecIndexOutOfBounds()); 00294 } 00295 00296 //returns the starting base pair index of the given subspec. 00297 template< class SubSpec > 00298 gnSeqI gnMultiSpec< SubSpec >::GetSpecStartBase( const uint32 specI ) const{ 00299 uint32 i; 00300 if(specI >= GetSpecListLength()) 00301 Throw_gnEx(SpecIndexOutOfBounds()); 00302 00303 gnSeqI start_base = 0; 00304 for(i=0; i < specI; i++){ 00305 start_base += GetSpec(i)->GetLength(); 00306 } 00307 return start_base; 00308 } 00309 00310 template< class SubSpec > 00311 gnSeqI gnMultiSpec< SubSpec >::GetSpecEndBase( const uint32 specI ) const{ 00312 uint32 i; 00313 if(specI >= GetSpecListLength()) 00314 Throw_gnEx(SpecIndexOutOfBounds()); 00315 00316 gnSeqI end_base = 0; 00317 for(i=0; i <= specI; i++){ 00318 end_base += GetSpec(i)->GetLength(); 00319 } 00320 return end_base; 00321 } 00322 00323 template< class SubSpec > 00324 void gnMultiSpec< SubSpec >::CropStart( gnSeqI cropLen ){ 00325 gnSeqI curbase = 0; 00326 for(uint32 specI = 0; specI < GetSpecListLength(); specI++){ 00327 curbase += GetSpec(specI)->GetLength(); 00328 if(curbase <= cropLen){ 00329 //delete the spec completely 00330 gnBaseSpec *tmp_spec = GetSpec(specI); 00331 RemoveSpec(specI); 00332 delete tmp_spec; 00333 specI--; 00334 }else{ 00335 //recurse and break 00336 gnSeqI sub_len = cropLen - (curbase - GetSpec(specI)->GetLength()); 00337 GetSpec(specI)->CropStart(sub_len); 00338 break; 00339 } 00340 } 00341 } 00342 00343 template< class SubSpec > 00344 void gnMultiSpec< SubSpec >::CropEnd( gnSeqI cropLen ){ 00345 gnSeqI curbase = 0; 00346 gnSeqI cropbase = GetLength() - cropLen; //last base to keep 00347 boolean trash_the_rest = false; 00348 for(uint32 specI = 0; specI < GetSpecListLength(); specI++){ 00349 curbase += GetSpec(specI)->GetLength(); 00350 if(trash_the_rest){ 00351 //delete the spec entirely 00352 gnBaseSpec *tmp_spec = GetSpec(specI); 00353 RemoveSpec(specI); 00354 delete tmp_spec; 00355 specI--; 00356 continue; 00357 }else if(curbase > cropbase){ 00358 GetSpec(specI)->CropEnd(curbase - cropbase); 00359 trash_the_rest = true; 00360 }else if(curbase == cropbase) 00361 trash_the_rest = true; 00362 } 00363 } 00364 00365 template< class SubSpec > 00366 void gnMultiSpec< SubSpec >::AddHeader( gnBaseHeader* head, const uint32 i) 00367 { 00368 uint32 index = i == UINT32_MAX ? m_headerList.size() : i; 00369 m_headerList.insert(m_headerList.begin() + index, head); 00370 } 00371 00372 template< class SubSpec > 00373 gnBaseHeader* gnMultiSpec< SubSpec >::GetHeader( const string& name, uint32& i) const{ 00374 for(; i < m_headerList.size(); i++){ 00375 if( m_headerList[i]->GetHeaderName() == name) 00376 return m_headerList[i]; 00377 } 00378 Throw_gnEx(HeaderIndexOutOfBounds()); 00379 } 00380 00381 template< class SubSpec > 00382 void gnMultiSpec< SubSpec >::RemoveHeader( uint32 i) 00383 { 00384 if(i <= m_headerList.size()){ 00385 m_headerList.erase(m_headerList.begin() + i); 00386 } 00387 Throw_gnEx(HeaderIndexOutOfBounds()); 00388 } 00389 00390 template< class SubSpec > 00391 boolean gnMultiSpec< SubSpec >::SeqRead(const gnSeqI start, gnSeqC* buf, gnSeqI& bufLen, const uint32 contigI ) const{ 00392 if( bufLen == 0 ) 00393 return true; 00394 if(contigI == ALL_CONTIGS){ 00395 gnSeqI curpos = 0; 00396 gnSeqI readBytes = 0; 00397 gnSeqI remainingBytes = bufLen; 00398 uint32 curSpecI = 0; 00399 //seek to start spec. 00400 for(curSpecI=0; curSpecI < GetSpecListLength(); curSpecI++){ 00401 curpos += GetSpec(curSpecI)->GetLength(); 00402 if(curpos > start) 00403 break; 00404 } 00405 if(curpos <= start) 00406 Throw_gnEx(SeqIndexOutOfBounds()); 00407 //read until we're done; 00408 while((remainingBytes > 0) && (curSpecI < GetSpecListLength())) { 00409 gnSeqI readable = GetSpec(curSpecI)->GetLength(); 00410 //check for circular 00411 gnSeqI start_pos = readBytes == 0 ? start - (curpos - readable) : 0; 00412 gnSeqI to_read = readable - start_pos >= remainingBytes ? remainingBytes : readable - start_pos; 00413 boolean success = GetSpec(curSpecI)->SeqRead(start_pos, buf+readBytes, to_read, contigI); 00414 00415 readBytes += to_read; 00416 remainingBytes -= to_read; 00417 if(!success) 00418 break; 00419 curSpecI++; 00420 } 00421 bufLen = readBytes; 00422 return true; 00423 }else{ //read from the specified contig. 00424 if(contigI < GetSpecListLength()) 00425 return GetSpec(contigI)->SeqRead(start, buf, bufLen, ALL_CONTIGS); 00426 else 00427 Throw_gnEx(SpecIndexOutOfBounds()); 00428 } 00429 return false; 00430 } 00431 00432 00433 00434 #endif 00435 // _gnMultiSpec_h_

Generated on Mon Feb 14 19:28:18 2005 for libGenome by doxygen 1.3.8