From 00166d852729b77384104baacd55580095fa3b2f Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Thu, 13 Apr 2006 19:34:08 +0000 Subject: [PATCH] fix ticket:83 throw an error when we don't have a fasta file If the user gives us someting that doesn't look like a fasta file throw an error. --- alg/sequence.cpp | 38 +++++++++++++++++---------------- alg/test/test_sequence.cpp | 10 +++++++++ examples/seq/misformated_seq.fa | 6 ++++++ 3 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 examples/seq/misformated_seq.fa diff --git a/alg/sequence.cpp b/alg/sequence.cpp index 7e7468b..9df5fd5 100644 --- a/alg/sequence.cpp +++ b/alg/sequence.cpp @@ -136,28 +136,30 @@ Sequence::load_fasta(fs::path file_path, int seq_num, header_counter++; } - header = file_data_line.substr(1); + if (header_counter > 0) { + header = file_data_line.substr(1); - sequence_raw = ""; + sequence_raw = ""; - while ( !data_file.eof() && read_seq ) - { - getline(data_file,file_data_line); - if (file_data_line.substr(0,1) == ">") - read_seq = false; - else sequence_raw += file_data_line; - } - - data_file.close(); + while ( !data_file.eof() && read_seq ) { + getline(data_file,file_data_line); + if (file_data_line.substr(0,1) == ">") + read_seq = false; + else sequence_raw += file_data_line; + } - // Lastly, if subselection of the sequence was specified we keep cut out - // and only keep that part - // end_index = 0 means no end was specified, so cut to the end - if (end_index == 0) - end_index = sequence_raw.size(); + // Lastly, if subselection of the sequence was specified we keep cut out + // and only keep that part + // end_index = 0 means no end was specified, so cut to the end + if (end_index == 0) + end_index = sequence_raw.size(); - // sequence filtering for upcasing agctn and convert non AGCTN to N - set_filtered_sequence(sequence_raw, start_index, end_index-start_index); + // sequence filtering for upcasing agctn and convert non AGCTN to N + set_filtered_sequence(sequence_raw, start_index, end_index-start_index); + } else { + throw mussa_load_error("%s did not have a fasta header"); + } + data_file.close(); } } diff --git a/alg/test/test_sequence.cpp b/alg/test/test_sequence.cpp index 1a4e9ac..09c621e 100644 --- a/alg/test/test_sequence.cpp +++ b/alg/test/test_sequence.cpp @@ -63,6 +63,16 @@ BOOST_AUTO_TEST_CASE( sequence_load ) "5' flank"); } +// ticket:83 when you try to load a sequence from a file that doesn't +// have fasta headers it crashes. +BOOST_AUTO_TEST_CASE( sequence_past_end ) +{ + fs::path seq_path(fs::path(EXAMPLE_DIR)/ "seq" ); + seq_path /= "misformated_seq.fa"; + Sequence s; + BOOST_CHECK_THROW( s.load_fasta(seq_path), mussa_load_error ); +} + BOOST_AUTO_TEST_CASE ( sequence_empty ) { Sequence s; diff --git a/examples/seq/misformated_seq.fa b/examples/seq/misformated_seq.fa new file mode 100644 index 0000000..7cddfd3 --- /dev/null +++ b/examples/seq/misformated_seq.fa @@ -0,0 +1,6 @@ +GGATCCTTCCTCCTTGGCCTCCCAAAGTGCTGGGATTACAGGTGTGAGCCACTGCACCTGGCCTATTACC +CTTCTCAGGCTCTGGAGTCCATCCTTCTGCTCTGTCTCCCTCAGTTCAATTGTTTTTTGTTTTTTGTTTT +TTTTTTAGACACAGTCTCGCTCTGTCACCAAGGCTGGAGTGCAGCAGTGCGATCACAGCTCACCGCAGCC +TCACCTCCCAGGCTCAAGTGATCCTCCCATCTCGGCCTCTGAGTAGCTGAGACTATAGGTGTGTCCACAT +GTCCGGCTAATTTTTGTATTTTTAGTAGAGACAGGGTTTCACCGCGTTGGCCAGGGTGGTCTTGAACTCC +TGAGCTCAAGCAATCCTCCTGCCTCAGCCTCCTTGTTTTGATTTTTAGATCCCACAAATAACTTGTGATG -- 2.30.2