Skip to content

Commit 63e3ede

Browse files
fixing conflict with HEAD
2 parents 1a23bbd + 228273f commit 63e3ede

File tree

4 files changed

+72
-22
lines changed

4 files changed

+72
-22
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ lib_LIBRARIES = libsmithlab_cpp.a
2525
libsmithlab_cpp_a_SOURCES = GenomicRegion.cpp MappedRead.cpp \
2626
OptionParser.cpp QualityScore.cpp bisulfite_utils.cpp \
2727
chromosome_utils.cpp sim_utils.cpp smithlab_os.cpp smithlab_utils.cpp \
28-
zlib_wrapper.cpp dna_four_bit.cpp cigar_utils.cpp
28+
zlib_wrapper.cpp dna_four_bit.cpp cigar_utils.cpp sam_record.cpp
2929

3030
if ENABLE_HTS
3131
libsmithlab_cpp_a_SOURCES += htslib_wrapper_deprecated.cpp \

bisulfite_utils.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,29 @@ namespace bsflags {
2929
// ADS: this is our addition to the SAM flags, using the "free" bits
3030
/* 4096 0x1000 read is A-rich
3131
*/
32-
static const uint16_t read_is_t_rich = 0x1000;
32+
static const uint16_t read_is_a_rich = 0x1000;
3333
}
3434

3535
inline bool
3636
is_t_rich(const sam_rec &sr) {
37-
return samflags::check(sr.flags, bsflags::read_is_t_rich);
37+
return !samflags::check(sr.flags, bsflags::read_is_a_rich);
3838
}
3939

4040
inline bool
4141
is_a_rich(const sam_rec &sr) {
42-
return !samflags::check(sr.flags, bsflags::read_is_t_rich);
42+
return samflags::check(sr.flags, bsflags::read_is_a_rich);
4343
}
4444

4545
inline void
4646
set_t_rich(sam_rec &sr) {
47-
samflags::set(sr.flags, bsflags::read_is_t_rich);
47+
samflags::unset(sr.flags, bsflags::read_is_a_rich);
4848
}
4949

5050
inline void
5151
set_a_rich(sam_rec &sr) {
52-
samflags::unset(sr.flags, bsflags::read_is_t_rich);
52+
samflags::set(sr.flags, bsflags::read_is_a_rich);
5353
}
5454

55-
5655
void
5756
bisulfite_treatment(std::mt19937 &generator, std::string &seq,
5857
double bs_rate = 1.0, double meth_rate = 0.0);

sam_record.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ using std::istream;
3333
using std::ostream;
3434
using std::begin;
3535
using std::end;
36+
using std::ostringstream;
37+
using std::to_string;
3638

3739
// ADS: this is for debugging purposes
3840
string
3941
format_sam_flags(const uint16_t the_flags) {
40-
std::ostringstream oss;
42+
ostringstream oss;
4143
using samflags::check;
4244
oss << "read_paired: " << check(the_flags, samflags::read_paired) << '\n'
4345
<< "read_pair_mapped: "
@@ -72,23 +74,36 @@ format_sam_flags(const uint16_t the_flags) {
7274
// return regex_match(qual, regex("[!-~]+"));
7375
// }
7476

77+
size_t
78+
sam_rec::estimate_line_size() const {
79+
static const size_t all_field_estimates = 100;
80+
return qname.size() + rname.size() + qual.size() + all_field_estimates;
81+
}
82+
83+
string
84+
sam_rec::tostring() const {
85+
string out;
86+
out.reserve(estimate_line_size());
87+
out.append(qname + "\t" +
88+
to_string(flags) + "\t" +
89+
rname + "\t" +
90+
to_string(pos) + "\t" +
91+
to_string(static_cast<unsigned>(mapq)) + "\t" +
92+
cigar + "\t" +
93+
rnext + "\t" +
94+
to_string(pnext) + "\t" +
95+
to_string(tlen) + "\t" +
96+
seq + "\t" +
97+
qual);
98+
for (auto it(begin(tags)); it != end(tags); ++it)
99+
out.append("\t" + *it);
100+
101+
return out;
102+
}
75103

76104
ostream &
77105
operator<<(std::ostream &the_stream, const sam_rec &r) {
78-
the_stream << r.qname << '\t'
79-
<< r.flags << '\t'
80-
<< r.rname << '\t'
81-
<< r.pos << '\t'
82-
<< static_cast<unsigned>(r.mapq) << '\t'
83-
<< r.cigar << '\t'
84-
<< r.rnext << '\t'
85-
<< r.pnext << '\t'
86-
<< r.tlen << '\t'
87-
<< r.seq << '\t'
88-
<< r.qual;
89-
90-
for (auto it(begin(r.tags)); it != end(r.tags); ++it)
91-
the_stream << '\t' << *it;
106+
the_stream << r.tostring();
92107
return the_stream;
93108
}
94109

sam_record.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <string>
2323
#include <vector>
2424
#include <iostream>
25+
#include <sstream>
26+
#include <iterator>
2527

2628
// from 30 April 2020 SAM documentation
2729
// 1 0x1 template having multiple segments in sequencing
@@ -117,6 +119,8 @@ class sam_rec {
117119
seq(_seq),
118120
qual(_qual) {}
119121
void add_tag(const std::string &the_tag) {tags.push_back(the_tag);}
122+
size_t estimate_line_size() const;
123+
std::string tostring() const;
120124
};
121125

122126
inline bool
@@ -144,4 +148,36 @@ void
144148
inflate_with_cigar(const sam_rec &sr, std::string &to_inflate,
145149
const char inflation_symbol = 'N');
146150

151+
template<typename T>
152+
static void
153+
write_sam_header(const std::vector<std::string> &chrom_names,
154+
const std::vector<T> &chrom_starts,
155+
const std::string program_name,
156+
const std::string program_version,
157+
const int argc, const char **argv,
158+
std::ostream &out) {
159+
static const std::string SAM_VERSION = "1.0";
160+
161+
// sam version
162+
out <<"@HD" << '\t' << "VN:" << SAM_VERSION << '\n'; // sam version
163+
164+
// chromosome sizes
165+
const size_t n_chroms = chrom_names.size() - 1;
166+
for (size_t i = 1; i < n_chroms; ++i) {
167+
out << "@SQ" << '\t'
168+
<< "SN:" << chrom_names[i] << '\t'
169+
<< "LN:" << chrom_starts[i+1] - chrom_starts[i] << '\n';
170+
}
171+
172+
// program details
173+
out << "@PG" << '\t'
174+
<< "ID:" << program_name << '\t'
175+
<< "VN:" << program_version << '\t';
176+
177+
// how the program was run
178+
std::ostringstream the_command;
179+
copy(argv, argv + argc, std::ostream_iterator<const char*>(the_command, " "));
180+
out << "CL:\"" << the_command.str() << "\"" << std::endl;
181+
}
182+
147183
#endif

0 commit comments

Comments
 (0)