00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef __AbstractMatch_h__
00010 #define __AbstractMatch_h__
00011
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #endif
00015
00016 #include "libGenome/gnClone.h"
00017 #include <vector>
00018 #include <algorithm>
00019 #include <boost/type_traits/remove_pointer.hpp>
00020 #include <boost/type_traits/add_pointer.hpp>
00021 #include <boost/dynamic_bitset.hpp>
00022 #include <libMems/SlotAllocator.h>
00023 #include <libMems/configuration.h>
00024
00025 namespace mems {
00026
00027 static const gnSeqI NO_MATCH = 0;
00028
00029
00030 #ifdef WIN32
00031
00032
00033
00034
00035
00036 #define _USE_SLOT_ALLOCATOR
00037 #else
00038 #define _USE_SLOT_ALLOCATOR
00039 #endif
00040 typedef boost::dynamic_bitset<> bitset_t;
00041
00042 #ifdef _USE_SLOT_ALLOCATOR
00043 #include "libMems/SlotAllocator.h"
00044 #elif defined(_USE_BOOST_MATCH_ALLOCATOR)
00045 #include <boost/pool/pool_alloc.hpp>
00046 #endif
00047
00048 template< typename T >
00049 T* m_allocateAndCopy( const T& t )
00050 {
00051 #ifdef _USE_SLOT_ALLOCATOR
00052 SlotAllocator<T>& sat = SlotAllocator<T>::GetSlotAllocator();
00053 T* newt = sat.Allocate();
00054 newt = new(newt) T(t);
00055
00056 return newt;
00057 #elif defined(_USE_BOOST_MATCH_ALLOCATOR)
00058 boost::fast_pool_allocator< T > fpa;
00059 T* newt = boost::fast_pool_allocator< T >::allocate();
00060 fpa.construct(newt, t);
00061 return newt;
00062 #else
00063 return new T(t);
00064 #endif
00065 }
00066
00067 template< typename T >
00068 void m_free( T* t )
00069 {
00070 #ifdef _USE_SLOT_ALLOCATOR
00071 SlotAllocator<T>& sat = SlotAllocator<T>::GetSlotAllocator();
00072 sat.Free(t);
00073 #elif defined(_USE_BOOST_MATCH_ALLOCATOR)
00074 boost::fast_pool_allocator< T > fpa;
00075 fpa.destroy(t);
00076 boost::fast_pool_allocator< T >::deallocate(t);
00077 #else
00078 delete t;
00079 #endif
00080 }
00081
00087 class AbstractMatch : public genome::gnClone {
00088 public:
00089
00090 enum orientation {
00091 forward,
00092 reverse,
00093 undefined
00094 };
00095
00097 virtual AbstractMatch* Copy() const = 0;
00098
00100 virtual void Free() = 0;
00101
00103 virtual gnSeqI Length( uint seqI ) const = 0;
00104
00106 virtual void SetLength( gnSeqI len, uint seqI ) = 0;
00107
00110 virtual int64 Start(uint startI) const = 0;
00111
00114 virtual void SetStart(uint seqI, int64 start) = 0;
00115
00118 int64 operator[](uint seqI) const{return Start(seqI);}
00119
00122 virtual int64 End(uint seqI) const;
00123
00125 virtual gnSeqI LeftEnd(uint seqI) const = 0;
00126
00129 virtual gnSeqI RightEnd(uint seqI) const{ return LeftEnd(seqI) + Length( seqI ) - 1; };
00130
00134 virtual orientation Orientation(uint seqI) const = 0;
00135
00137 virtual void SetLeftEnd(uint seqI, gnSeqI start) = 0;
00138
00140 virtual void SetOrientation(uint seqI, orientation o) = 0;
00141
00143 virtual void MoveStart(int64 move_amount) = 0;
00145 virtual void MoveEnd(int64 move_amount) = 0;
00146
00148 virtual uint Multiplicity() const = 0;
00149
00151 virtual uint SeqCount() const = 0;
00152
00154 virtual uint FirstStart() const = 0;
00155
00157 virtual gnSeqI AlignmentLength() const = 0;
00158
00160 virtual void Invert() = 0;
00161
00162
00167 virtual void CropStart(gnSeqI crop_amount) = 0;
00172 virtual void CropEnd(gnSeqI crop_amount) = 0;
00173
00178 virtual void CropLeft(gnSeqI crop_amount, uint seqI) = 0;
00183 virtual void CropRight(gnSeqI crop_amount, uint seqI) = 0;
00184
00185
00186
00190 virtual void GetAlignment( std::vector< bitset_t >& align_matrix ) const = 0;
00191
00199 virtual void GetColumn( gnSeqI col, std::vector<gnSeqI>& pos, std::vector<bool>& column ) const = 0;
00200
00201
00203 virtual bool IsGap( uint seq, gnSeqI col ) const = 0;
00205 virtual uint UsedSeq( uint seqI ) const = 0;
00206 };
00207
00208 inline
00209 int64 AbstractMatch::End(uint endI) const
00210 {
00211 if( Start(endI) > 0 )
00212 return Start(endI) + Length(endI) - 1;
00213 return Start(endI);
00214 }
00215
00216
00217 template< typename MatchType >
00218 class AbstractMatchStartComparator {
00219 public:
00220 AbstractMatchStartComparator( unsigned seq = 0 ){
00221 m_seq = seq;
00222 }
00223 AbstractMatchStartComparator( const AbstractMatchStartComparator& msc ){
00224 m_seq = msc.m_seq;
00225 }
00226 AbstractMatchStartComparator<MatchType>& operator=( const AbstractMatchStartComparator<MatchType>& msc )
00227 {
00228 m_seq = msc.m_seq;
00229 }
00230
00231 boolean operator()(const MatchType& a, const MatchType& b) const{
00232 int start_diff = std::max( a.FirstStart(), m_seq ) - std::max( a.FirstStart(), m_seq );
00233 if(start_diff == 0){
00234 uint m_count = a.SeqCount();
00235 m_count = m_count <= b.SeqCount() ? m_count : b.SeqCount();
00236 for(uint seqI = m_seq; seqI < m_count; seqI++){
00237 gnSeqI a_start = a.Orientation(seqI) == AbstractMatch::forward ? a.LeftEnd( seqI ) : a.RightEnd( seqI );
00238 gnSeqI b_start = b.Orientation(seqI) == AbstractMatch::forward ? b.LeftEnd( seqI ) : b.RightEnd( seqI );
00239 int64 diff = a_start - b_start;
00240 if(a_start == NO_MATCH || b_start == NO_MATCH)
00241 continue;
00242 else if(a_start == b_start)
00243 continue;
00244 else
00245 return a_start < b_start;
00246 }
00247 }
00248 return start_diff < 0;
00249 }
00250 private:
00251 unsigned m_seq;
00252 };
00253
00254 template< typename MatchType >
00255 class AbstractMatchSingleStartComparator {
00256 public:
00257 AbstractMatchSingleStartComparator( unsigned seq = 0 ){
00258 m_seq = seq;
00259 }
00260 AbstractMatchSingleStartComparator( const AbstractMatchSingleStartComparator& msc ){
00261 m_seq = msc.m_seq;
00262 }
00263 AbstractMatchSingleStartComparator<MatchType>& operator=( const AbstractMatchSingleStartComparator<MatchType>& msc )
00264 {
00265 m_seq = msc.m_seq;
00266 }
00270 boolean operator()(const MatchType& a, const MatchType& b) const{
00271 int64 a_start = a.LeftEnd( m_seq ), b_start = b.LeftEnd( m_seq );
00272 if( a_start == NO_MATCH || b_start == NO_MATCH ){
00273 if( b_start != NO_MATCH )
00274 return true;
00275 return false;
00276 }
00277
00278 return a_start < b_start;
00279 }
00280 private:
00281 unsigned m_seq;
00282 };
00283
00284
00285
00286 template< typename MatchType >
00287 class MatchStartComparator {
00288 public:
00289 MatchStartComparator( unsigned seq = 0 ){
00290 m_seq = seq;
00291 }
00292 MatchStartComparator( const MatchStartComparator& msc ){
00293 m_seq = msc.m_seq;
00294 }
00295 MatchStartComparator<MatchType>& operator=( const MatchStartComparator<MatchType>& msc )
00296 {
00297 m_seq = msc.m_seq;
00298 }
00299
00300 boolean operator()(const MatchType* a, const MatchType* b) const{
00301 int start_diff = std::max( a->FirstStart(), m_seq ) - std::max( a->FirstStart(), m_seq );
00302 if(start_diff == 0){
00303 uint m_count = a->SeqCount();
00304 m_count = m_count <= b->SeqCount() ? m_count : b->SeqCount();
00305 for(uint seqI = m_seq; seqI < m_count; seqI++){
00306 gnSeqI a_start = a->Orientation(seqI) == AbstractMatch::forward ? a->LeftEnd( seqI ) : a->RightEnd( seqI );
00307 gnSeqI b_start = b->Orientation(seqI) == AbstractMatch::forward ? b->LeftEnd( seqI ) : b->RightEnd( seqI );
00308 int64 diff = a_start - b_start;
00309 if(a_start == NO_MATCH || b_start == NO_MATCH)
00310 continue;
00311 else if(a_start == b_start)
00312 continue;
00313 else
00314 return a_start < b_start;
00315 }
00316 }
00317 return start_diff < 0;
00318 }
00319 private:
00320 unsigned m_seq;
00321 };
00322
00323 template< typename MatchType >
00324 class SingleStartComparator {
00325 public:
00326 SingleStartComparator( unsigned seq = 0 ){
00327 m_seq = seq;
00328 }
00329 SingleStartComparator( const SingleStartComparator& msc ){
00330 m_seq = msc.m_seq;
00331 }
00332 SingleStartComparator<MatchType>& operator=( const SingleStartComparator<MatchType>& msc )
00333 {
00334 m_seq = msc.m_seq;
00335 }
00339 boolean operator()(const MatchType* a, const MatchType* b) const{
00340 int64 a_start = a->LeftEnd( m_seq ), b_start = b->LeftEnd( m_seq );
00341 if( a_start == NO_MATCH || b_start == NO_MATCH ){
00342 if( b_start != NO_MATCH )
00343 return true;
00344 return false;
00345 }
00346
00347 return a_start < b_start;
00348 }
00349 private:
00350 unsigned m_seq;
00351 };
00352
00353
00354 template< typename MatchType >
00355 class SSC {
00356 public:
00357 SSC( unsigned seq = 0 ){
00358 m_seq = seq;
00359 }
00360 SSC( const SSC<MatchType>& msc ){
00361 m_seq = msc.m_seq;
00362 }
00363 SSC<MatchType>& operator=( const SSC<MatchType>& msc )
00364 {
00365 m_seq = msc.m_seq;
00366 }
00367 boolean operator()( const typename boost::add_pointer<MatchType>::type& a,
00368 const typename boost::add_pointer<MatchType>::type& b) const
00369 {
00370 return operator()(*a,*b);
00371 }
00375 boolean operator()(const typename boost::remove_pointer<MatchType>::type& a,
00376 const typename boost::remove_pointer<MatchType>::type& b) const{
00377 int64 a_start = a.LeftEnd( m_seq ), b_start = b.LeftEnd( m_seq );
00378 if( a_start == NO_MATCH || b_start == NO_MATCH ){
00379 if( b_start != NO_MATCH )
00380 return true;
00381 return false;
00382 }
00383
00384 return a_start < b_start;
00385 }
00386 private:
00387 unsigned m_seq;
00388 };
00389
00390 }
00391
00392 #endif // __AbstractMatch_h__