libsequence  1.9.5
FastaOperations.cc
1 //\file FastaOperations.cc
2 
3 #include <Sequence/Fasta.hpp>
4 #include <string>
5 #include <iostream>
6 #include <algorithm>
7 #include <numeric>
8 #include <boost/test/unit_test.hpp>
9 
10 struct fasta_operations_fixture
11 {
12  std::string name, seq;
13  fasta_operations_fixture()
14  : name{ "seqname" }, seq{ "AGCGTAGACAGTAGAGTGAT" }
15  {
16  }
17 };
18 
19 BOOST_FIXTURE_TEST_SUITE(FastaOperationsTest, fasta_operations_fixture)
20 
21 //A generic revcom routine written for this test
22 std::string rcom( const std::string & s )
23 {
24  std::string rv(s);
25  std::reverse(rv.begin(),rv.end());
26  std::transform(rv.begin(),rv.end(),
27  rv.begin(),
28  [](const char & ch)
29  {
30  switch(ch)
31  {
32  case 'A':
33  return 'T';
34  break;
35  case 'G':
36  return 'C';
37  break;
38  case 'C':
39  return 'G';
40  break;
41  case 'T':
42  return 'A';
43  break;
44  }
45  return 'N';
46  });
47  return rv;
48 }
49 
50 
51 BOOST_AUTO_TEST_CASE( revcom )
52 {
53  std::string name("seqname"),seq("AGCGTAGACAGTAGAGTGAT");
54  Sequence::Fasta f(name,seq);
55 
56  Sequence::Fasta f2 = f;
57  f2.Revcom();
58 
59  BOOST_REQUIRE( f2.seq == rcom(seq) );
60 }
61 
62 BOOST_AUTO_TEST_CASE( subseq )
63 {
64  std::string name("seqname"),seq("AGCGTAGACAGTAGAGTGAT");
65  Sequence::Fasta f(name,seq);
66 
67  Sequence::Fasta f3(f);
68  f3.Subseq(1,3);
69 
70  BOOST_REQUIRE( f3.seq == "GCG" );
71 
72  f3.Complement();
73 
74  BOOST_REQUIRE( f3.seq == "CGC" );
75 
76  BOOST_REQUIRE( std::string(f3) == "CGC" ); //operator string()
77 
78 }
79 
80 
81 BOOST_AUTO_TEST_CASE( gapped )
82 {
83  Sequence::Fasta f3("seqname","GCG");
84 
85  BOOST_REQUIRE( !f3.IsGapped() );
86 
87  f3.seq += '-';
88 
89  BOOST_REQUIRE( f3.IsGapped() );
90 
91  BOOST_REQUIRE( f3.length() == 4 );
92 
93  BOOST_REQUIRE( f3.UngappedLength() == 3 );
94 
95  //Remove the gap
96  f3.seq.erase( f3.seq.find('-'), 1 );
97 
98  BOOST_REQUIRE( f3.length() == 3 );
99 
100  BOOST_REQUIRE( f3.UngappedLength() == 3 );
101 }
102 
103 BOOST_AUTO_TEST_CASE( cpp11access_1 )
104 {
105  Sequence::Fasta f3("seqname","GCG");
106  for( auto & d : f3 )
107  {
108  d = 'A';
109  }
110  BOOST_REQUIRE_EQUAL(f3.seq,"AAA");
111 }
112 
113 BOOST_AUTO_TEST_SUITE_END()
114 //EOF
FASTA sequence stream.
Definition: Fasta.hpp:49
void Revcom(void)
Definition: Seq.cc:175
Declaration of Sequence::Fasta streams.