1 #include <ncbi_pch.hpp>
2 #include "cbitmaskbuilder.hpp"
3 #include "cprogressindicator.hpp"
4 #include "fourplanes.hpp"
5 
6 USING_OLIGOFAR_SCOPES;
7 
CBitmaskBuilder(int maxamb,int wsize,int wstep)8 CBitmaskBuilder::CBitmaskBuilder( int maxamb, int wsize, int wstep ) :
9     CBitmaskBase( maxamb, wsize, wstep ), m_bitCount( 0 )
10 {
11     x_SetWordSize( wsize, true );
12 }
13 
Write(const string & name) const14 bool CBitmaskBuilder::Write( const string& name ) const
15 {
16     ofstream o( ( name + "~" ).c_str() );
17     if( o.fail() ) THROW( runtime_error, "Failed to open file "+name+"~ for write: "+strerror( errno ) );
18     if( Write( o ) ) {
19         o.flush();
20         o.close();
21         if( o.fail() ) THROW( runtime_error, "Failed to complete writing file "+name+"~: "+strerror( errno ) );
22         if( rename( (name + "~").c_str(), name.c_str() ) )
23             THROW( runtime_error, "Failed to rename " + name + "~ to " + name + ": "+strerror( errno ) );
24     } else {
25         unlink( (name + "~").c_str() );
26         THROW( runtime_error, "Failed to write file "+name+"~: "+strerror( errno ) );
27     }
28     return true;
29 }
30 
CountBits()31 Uint8 CBitmaskBuilder::CountBits()
32 {
33     m_bitCount = 0;
34     for( Uint8 i = 0; i < m_size; ++i ) {
35         m_bitCount += CBitHacks::BitCount4( m_data[i] );
36     }
37     return m_bitCount;
38 }
39 
SequenceBuffer(CSeqBuffer * ncbi8na)40 void CBitmaskBuilder::SequenceBuffer( CSeqBuffer * ncbi8na )
41 {
42     CProgressIndicator progress( "Listing "+NStr::IntToString( m_wSize )+"-mers of length "+NStr::IntToString( m_wLength )+" for "+m_seqId );
43     fourplanes::CHashGenerator hgen( m_wLength );
44     for( const char * seq = ncbi8na->GetBeginPtr(); seq < ncbi8na->GetEndPtr(); ++seq ) {
45         hgen.AddBaseMask( CNcbi8naBase( seq ) );
46         // cerr << CIupacnaBase( CNcbi8naBase( seq ) ) << "\t";
47         if( hgen.GetAmbiguityCount() <= (int)m_maxAmb ) {
48             for( fourplanes::CHashIterator h( hgen ); h; ++h ) {
49                 Uint8 packed = CBitHacks::PackWord( Uint8(*h), m_pattern2na );
50                 // cerr << hex << DISPLAY( Uint8(*h) ) << DISPLAY( m_pattern2na ) << DISPLAY( packed ) << dec << "\n";
51                 SetBit( packed );
52             }
53         } //else cerr << "-\n";
54         progress.Increment();
55     }
56     progress.Summary();
57 }
58 
59