Skip to content

Commit

Permalink
[FIX] Remove whitespace from FASTA id (#100)
Browse files Browse the repository at this point in the history
* [FIX] Remove flanking whitespace from fasta id
  • Loading branch information
eaasna authored Oct 10, 2023
1 parent ccb9d8c commit e4a7007
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
20 changes: 19 additions & 1 deletion include/valik/split/metadata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
namespace valik
{

/**
* @brief Function that removes leading and trailing whitespace.
*/
template <typename id_t>
void trim_fasta_id(id_t & id)
{
std::string whitespace = " \t\n\r\v";
auto first_valid = id.find_first_not_of(whitespace);
if (first_valid == std::string::npos)
throw std::runtime_error{"Sequence name can not be empty."};
id.erase(0, first_valid);

auto last_valid = id.find_last_not_of(whitespace);
if (last_valid == std::string::npos)
throw std::runtime_error{"Sequence name can not be empty."};
id.erase(last_valid + 1);
}

/**
* @brief Struct that stores the metadata for a split database.
* \param total_len Total database length.
Expand Down Expand Up @@ -105,7 +123,6 @@ struct metadata
size_t seg_count;

private:

std::vector<sequence_stats> sequences;
size_t default_seg_len;
std::vector<segment_stats> segments;
Expand All @@ -123,6 +140,7 @@ struct metadata
size_t fasta_ind = 0;
for (auto & record : fin)
{
trim_fasta_id(record.id());
sequence_stats seq(record.id(), fasta_ind, record.sequence().size());
total_len += seq.len;
sequences.push_back(seq);
Expand Down
2 changes: 2 additions & 0 deletions test/api/valik/split/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ target_use_datasources (write_seg_sequences_test FILES write_out_0_16_reference_
target_use_datasources (write_seg_sequences_test FILES write_out_0_4_reference_metadata.txt)
target_use_datasources (write_seg_sequences_test FILES write_out_20_16_reference_metadata.txt)
target_use_datasources (write_seg_sequences_test FILES write_out_20_4_reference_metadata.txt)

add_api_test (trim_fasta_id_test.cpp)
38 changes: 38 additions & 0 deletions test/api/valik/split/trim_fasta_id_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <gtest/gtest.h>

#include <valik/split/metadata.hpp>

TEST(trim_fasta_id, no_whitespace)
{
std::string id = "xy1";
std::string expected = id;

valik::trim_fasta_id(id);
EXPECT_EQ(id, expected);
}

TEST(trim_fasta_id, only_whitespace)
{
EXPECT_THROW({
try
{
std::string id = "\n \t";
valik::trim_fasta_id(id);
}
catch( const std::runtime_error& e )
{
// and this tests that it has the correct message
EXPECT_STREQ( "Sequence name can not be empty.", e.what() );
throw;
}
}, std::runtime_error );
}

TEST(trim_fasta_id, remove_whitespace)
{
std::string id = " \txy1 \n";
std::string expected = "xy1";

valik::trim_fasta_id(id);
EXPECT_EQ(id, expected);
}

0 comments on commit e4a7007

Please sign in to comment.