Skip to content

Commit

Permalink
VCFFormatSNPDataSink: update to handle 'completely missing' samples
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 1f5956eb6041d47b6a2fc8931d04ae458e9db06fc3ba2b9ad1e538ee93d13641
  • Loading branch information
[email protected] committed Dec 19, 2017
1 parent 75b8ccc commit b670c5d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions genfile/src/VCFFormatSNPDataSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ namespace genfile {

namespace {
struct DataWriter: public VariantDataReader::PerSampleSetter {
enum State { eUninitialised = 0, eInitialised = 1, eSampleSet = 2, eNumberOfEntriesSet = 3, eValueSet = 4, eFinalised = 5 } ;

DataWriter( boost::ptr_vector< std::ostringstream >& streams, bool field_is_genotype ):
m_streams( streams ),
m_field_is_genotype( field_is_genotype ),
m_sample_i( 0 ),
m_sep( m_field_is_genotype ? '/' : ',' )
m_sep( m_field_is_genotype ? '/' : ',' ),
m_state( eUninitialised )
{
}

Expand All @@ -66,10 +69,20 @@ namespace genfile {
void initialise( std::size_t nSamples, std::size_t nAlleles ) {
assert( m_streams.size() == nSamples ) ;
m_sample_i = 0 ;
m_state = eInitialised ;
}

bool set_sample( std::size_t i ) {
// Ensure we are in a correct state
assert( m_state == eInitialised || m_state == eValueSet || m_state == eSampleSet ) ;
// Ensure samples are delivered contiguously
assert( (m_sample_i == 0 && i == 0) || ( i == m_sample_i+1 )) ;
if( m_state == eSampleSet ) {
// last sample was completely missing, write a .
m_streams[ m_sample_i ] << "." ;
}
m_sample_i = i ;
m_state = eSampleSet ;
return true ;
}

Expand All @@ -82,37 +95,44 @@ namespace genfile {
} else {
m_sep = ',' ;
}
m_state = eNumberOfEntriesSet ;
}

void set_value( std::size_t, MissingValue const value ) {
if( m_entry_i++ > 0 ) {
m_streams[ m_sample_i ] << m_sep ;
}
m_streams[ m_sample_i ] << "." ;
m_state = eValueSet ;
}

void set_value( std::size_t, std::string& value ) {
if( m_entry_i++ > 0 ) {
m_streams[ m_sample_i ] << m_sep ;
}
m_streams[ m_sample_i ] << value ;
m_state = eValueSet ;
}

void set_value( std::size_t, Integer const value ) {
if( m_entry_i++ > 0 ) {
m_streams[ m_sample_i ] << m_sep ;
}
m_streams[ m_sample_i ] << value ;
m_state = eValueSet ;
}

void set_value( std::size_t, double const value ) {
if( m_entry_i++ > 0 ) {
m_streams[ m_sample_i ] << m_sep ;
}
m_streams[ m_sample_i ] << value ;
m_state = eValueSet ;
}

void finalise() {}
void finalise() {
m_state = eFinalised ;
}

private:
boost::ptr_vector< std::ostringstream >& m_streams ;
Expand All @@ -122,6 +142,7 @@ namespace genfile {
std::size_t m_entry_i ;
OrderType m_order_type ;
char m_sep ;
State m_state ;
} ;
}

Expand Down

0 comments on commit b670c5d

Please sign in to comment.