Skip to content

Commit

Permalink
[MISC] clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
seqan-actions committed Nov 29, 2022
1 parent 31b7f53 commit a814aad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
38 changes: 22 additions & 16 deletions include/seqan3/io/sam_file/format_bam.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ format_bam::read_alignment_record(stream_type & stream,
if constexpr (!detail::decays_to_ignore_v<cigar_type>)
{
int32_t seq_length{};
std::tie(cigar_vector, ref_length, seq_length) = parse_binary_cigar(record_str.substr(considered_bytes, core.n_cigar_op * 4), core.n_cigar_op);
std::tie(cigar_vector, ref_length, seq_length) =
parse_binary_cigar(record_str.substr(considered_bytes, core.n_cigar_op * 4), core.n_cigar_op);
}

considered_bytes += core.n_cigar_op * 4;
Expand All @@ -454,15 +455,18 @@ format_bam::read_alignment_record(stream_type & stream,
size_t const number_of_bytes = (core.l_seq + 1) / 2;
std::string_view const seq_str = record_str.substr(considered_bytes, number_of_bytes);

seq.resize(core.l_seq + 1/* reserve one more in case size is uneven. will be corrected */); // TODO: .resize() is not generic
seq.resize(
core.l_seq
+ 1 /* reserve one more in case size is uneven. will be corrected */); // TODO: .resize() is not generic

using alph_t = std::ranges::range_value_t<decltype(seq)>;
constexpr auto from_dna16 = detail::convert_through_char_representation<dna16sam, alph_t>;

for (size_t i = 0,j = 0; i < number_of_bytes; ++i, j+=2)
for (size_t i = 0, j = 0; i < number_of_bytes; ++i, j += 2)
{
seq[j] = from_dna16[to_rank(dna16sam{}.assign_rank(std::min(15, static_cast<uint8_t>(seq_str[i]) >> 4)))];
seq[j + 1] = from_dna16[to_rank(dna16sam{}.assign_rank(std::min(15, static_cast<uint8_t>(seq_str[i]) & 0x0f)))];
seq[j + 1] =
from_dna16[to_rank(dna16sam{}.assign_rank(std::min(15, static_cast<uint8_t>(seq_str[i]) & 0x0f)))];
}

seq.resize(core.l_seq); // remove extra letter
Expand All @@ -488,7 +492,8 @@ format_bam::read_alignment_record(stream_type & stream,
int32_t remaining_bytes = core.block_size - (sizeof(alignment_record_core) - 4 /*block_size excluded*/)
- core.l_read_name - core.n_cigar_op * 4 - (core.l_seq + 1) / 2 - core.l_seq;
assert(remaining_bytes >= 0);
assert(static_cast<size_t>(remaining_bytes) == core.block_size - (sizeof(alignment_record_core) - 4) - considered_bytes);
assert(static_cast<size_t>(remaining_bytes)
== core.block_size - (sizeof(alignment_record_core) - 4) - considered_bytes);

if constexpr (!detail::decays_to_ignore_v<tag_dict_type>)
read_sam_dict(record_str.substr(considered_bytes), tag_dict);
Expand Down Expand Up @@ -1203,14 +1208,14 @@ inline void format_bam::read_sam_dict(std::string_view const tag_str, sam_tag_di
}
case 'Z': // string
{
std::string_view v = std::string_view{static_cast<const char * >(it)}; // parses until '\0'
std::string_view v = std::string_view{static_cast<char const *>(it)}; // parses until '\0'
target[tag] = std::string{v};
it += v.size() + 1;
break;
}
case 'H': // byte array, represented as null-terminated string; specification requires even number of bytes
{
std::string_view str = std::string_view{static_cast<const char * >(it)}; // parses until '\0'
std::string_view str = std::string_view{static_cast<char const *>(it)}; // parses until '\0'

std::vector<std::byte> tmp_vector;
// std::from_chars cannot directly parse into a std::byte
Expand All @@ -1219,17 +1224,18 @@ inline void format_bam::read_sam_dict(std::string_view const tag_str, sam_tag_di
if (str.size() % 2 != 0)
throw format_error{"[CORRUPTED BAM FILE] Hexadecimal tag must have even number of digits."};

for (size_t i = 0; i < str.size(); i+=2)
for (size_t i = 0; i < str.size(); i += 2)
{
auto res = std::from_chars(str.begin() + i, str.begin() + i + 2, dummy_byte, 16);

if (res.ec == std::errc::invalid_argument)
throw format_error{std::string("[CORRUPTED BAM FILE] The string '")
+ std::string(str.begin() + i, str.begin() + i + 2) + "' could not be cast into type uint8_t."};
+ std::string(str.begin() + i, str.begin() + i + 2)
+ "' could not be cast into type uint8_t."};

if (res.ec == std::errc::result_out_of_range)
throw format_error{std::string("[CORRUPTED BAM FILE] Casting '") + std::string(str)
+ "' into type uint8_t would cause an overflow."};
+ "' into type uint8_t would cause an overflow."};

tmp_vector.push_back(std::byte{dummy_byte});
}
Expand Down Expand Up @@ -1278,17 +1284,17 @@ inline void format_bam::read_sam_dict(std::string_view const tag_str, sam_tag_di
break;
default:
throw format_error{detail::to_string("The first character in the numerical id of a SAM tag ",
"must be one of [cCsSiIf] but '",
array_value_type_id,
"' was given.")};
"must be one of [cCsSiIf] but '",
array_value_type_id,
"' was given.")};
}
break;
}
default:
throw format_error{detail::to_string("The second character in the numerical id of a "
"SAM tag must be one of [A,i,Z,H,B,f] but '",
type_id,
"' was given.")};
"SAM tag must be one of [A,i,Z,H,B,f] but '",
type_id,
"' was given.")};
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions include/seqan3/io/stream/detail/fast_istreambuf_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class fast_istreambuf_iterator
raw_record[i] = std::string_view{data_begin + field_positions[i - 1] + 1, data_begin + field_positions[i]};
}


std::string_view cache_bytes(int32_t const size)
{
std::string_view result;
Expand All @@ -186,14 +185,18 @@ class fast_istreambuf_iterator
{
if (stream_buf->egptr() - stream_buf->gptr() < remaining_bytes) // still not fully in buffer
{
std::ranges::copy(stream_buf->gptr(), stream_buf->egptr(), overflow_buffer.data() + size - remaining_bytes);
std::ranges::copy(stream_buf->gptr(),
stream_buf->egptr(),
overflow_buffer.data() + size - remaining_bytes);
size_t const number_of_copied_bytes = stream_buf->egptr() - stream_buf->gptr();
remaining_bytes -= number_of_copied_bytes;
stream_buf->gbump(number_of_copied_bytes);
stream_buf->underflow();
}
}
std::ranges::copy(stream_buf->gptr(), stream_buf->gptr() + remaining_bytes, overflow_buffer.data() + size - remaining_bytes);
std::ranges::copy(stream_buf->gptr(),
stream_buf->gptr() + remaining_bytes,
overflow_buffer.data() + size - remaining_bytes);

result = {overflow_buffer.begin(), overflow_buffer.end()};
}
Expand Down

0 comments on commit a814aad

Please sign in to comment.