- Notifications
You must be signed in to change notification settings - Fork 260
/
Copy pathstep3.cpp
115 lines (98 loc) · 4.08 KB
/
step3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include"mmap.hpp"
#include"tools.hpp"
#include"rr_lookup.hpp"
/* =======================================================================
the tool receives all relevant columns of the ALIGNMENT-table via stdin
GRS[tab]REF_ORIENT
the tool receives these values via commandline :
argv[ 1 ] ... file-name of ref-lookup-file
argv[ 2 ] ... base-output-filename aka BASE
the tool produces these files:
BASE.ref_idx ... uint32_t for each ALIGNMENT the idx of the reference
BASE.ref_ofs ... uint32_t for each ALIGNMENT the offset into the reference
======================================================================= */
classwriter_t {
public:
writer_t( constchar * base )
: f_ref_idx( tools::make_filename( base, "ref_idx" ), std::ios::out | std::ios::binary ),
f_ref_ofs( tools::make_filename( base, "ref_ofs" ), std::ios::out | std::ios::binary ),
f_ref_len( tools::make_filename( base, "ref_len" ), std::ios::out | std::ios::binary ) {}
boolis_valid( void ) {
return f_ref_idx.is_open() && f_ref_ofs.is_open() && f_ref_len.is_open();
}
voidwrite( uint32_t idx, uint32_t ofs, bool orientation, uint32_t ref_len ) {
uint32_t v = idx & 0x7FFFFFFF;
if ( orientation ) v |= 0x80000000;
f_ref_idx.write( (char*) &v, sizeof v );
f_ref_ofs.write( (char*) &ofs, sizeof ofs );
f_ref_len.write( (char*) &ref_len, sizeof ref_len );
}
private:
std::ofstream f_ref_idx;
std::ofstream f_ref_ofs;
std::ofstream f_ref_len;
};
classtab_event : publictools::iter_func {
public :
virtualvoidoperator()( uint64_t id, const std::string& s ) {
switch( id ) {
case0 : grs = tools::str_2_uint64_t( s ); break;
case1 : ro = ( 0 == s.compare( "true" ) ) ; break;
case2 : ref_len = tools::str_2_uint32_t( s ); break;
}
}
uint64_t grs;
bool ro;
uint32_t ref_len;
};
classline_event : publictools::iter_func {
public:
line_event( const RR_Lookup& lookup, writer_t& writer )
: f_lookup( lookup ), f_writer( writer ), f_invalid( 0 ) {}
virtualvoidoperator()( uint64_t id, const std::string& s ) {
tab_event e;
tools::for_each_word( s, '\t', e );
RR_Idx_result idx = f_lookup.find( e.grs + 1 );
if ( idx.is_valid() ) {
RR_Ofs_result ofs = f_lookup.ofs_at_idx( idx.idx(), e.grs + 1 );
if ( ofs.is_valid() ) {
f_writer.write( idx.idx(), ofs.ofs(), e.ro, e.ref_len );
} else {
f_invalid++;
std::cout << "#3" << id << " : " << s << std::endl;
}
} else {
f_invalid++;
std::cout << "#" << id << " : " << s << std::endl;
}
}
uint64_tin_valid( void ) const { return f_invalid; }
private:
const RR_Lookup& f_lookup;
writer_t& f_writer;
uint64_t f_invalid;
};
intmain( int argc, char *argv[] ) {
if ( argc > 1 ) {
std::string ref_lookup_filename = tools::make_filename( argv[ 1 ], "ref_lookup" );
RR_Lookup lookup( ref_lookup_filename.c_str() );
if ( lookup.valid() ) {
writer_twriter( argv[ 1 ] );
if ( writer.is_valid() ) {
line_event e( lookup, writer );
uint64_t lines = tools::for_each_line( std::cin, e );
if ( 0 == e.in_valid() ) {
return0;
} else {
std::cout << e.in_valid() << " invalid values found in " << lines << " lines" << std::endl;
}
} else {
std::cout << "cannot open output-files!" << std::endl;
}
} else {
std::cout << "ref-lookup-file is invalid!" << std::endl;
}
}
return1;
}