bpp-seq3  3.0.0
SequenceWithQualityTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 
7 using namespace bpp;
8 using namespace std;
9 
13 
14 /******************************************************************************/
15 
16 unique_ptr<SequenceWithQuality> SequenceWithQualityTools::concatenate(
17  const SequenceWithQuality& seqwq1,
18  const SequenceWithQuality& seqwq2)
19 {
20  // Sequence's alphabets matching verification
21  if ((seqwq1.getAlphabet()->getAlphabetType()) != (seqwq2.getAlphabet()->getAlphabetType()))
22  throw AlphabetMismatchException("SequenceTools::concatenate : Sequence's alphabets don't match ", seqwq1.getAlphabet(), seqwq2.getAlphabet());
23 
24  // Sequence's names matching verification
25  if (seqwq1.getName() != seqwq2.getName())
26  throw Exception ("SequenceTools::concatenate : Sequence's names don't match");
27 
28  // Concatenate sequences and send result
29  vector<int> sequence = seqwq1.getContent();
30  vector<int> sequence2 = seqwq2.getContent();
31  vector<int> qualities = seqwq1.getQualities();
32  vector<int> qualities2 = seqwq2.getQualities();
33 
34  sequence.insert(sequence.end(), sequence2.begin(), sequence2.end());
35  qualities.insert(qualities.end(), qualities2.begin(), qualities2.end());
36  auto alphaPtr = seqwq1.getAlphabet();
37  return make_unique<SequenceWithQuality>(seqwq1.getName(), sequence, qualities, seqwq1.getComments(), alphaPtr);
38 }
39 
40 /******************************************************************************/
41 
42 unique_ptr<SequenceWithQuality> SequenceWithQualityTools::complement(
43  const SequenceWithQuality& sequence)
44 {
45  // Alphabet type checking
47  if (sequence.getAlphabet()->getAlphabetType() == "DNA alphabet")
48  {
49  NAR = &DNARep_;
50  }
51  else if (sequence.getAlphabet()->getAlphabetType() == "RNA alphabet")
52  {
53  NAR = &RNARep_;
54  }
55  else
56  {
57  throw AlphabetException ("SequenceTools::complement : Sequence must be nucleic.", sequence.getAlphabet());
58  }
59  auto seq = NAR->translate(sequence);
60  auto seqwq = make_unique<SequenceWithQuality>(*seq, sequence.getQualities());
61  return seqwq;
62 }
63 
64 /******************************************************************************/
65 
66 unique_ptr<SequenceWithQuality> SequenceWithQualityTools::transcript(
67  const SequenceWithQuality& sequence)
68 {
69  // Alphabet type checking
70  if (sequence.getAlphabet()->getAlphabetType() != "DNA alphabet")
71  {
72  throw AlphabetException ("SequenceTools::transcript : Sequence must be DNA", sequence.getAlphabet());
73  }
74  auto seq = transc_.translate(sequence);
75  auto seqwq = make_unique<SequenceWithQuality>(*seq, sequence.getQualities());
76  return seqwq;
77 }
78 
79 /******************************************************************************/
80 
81 unique_ptr<SequenceWithQuality> SequenceWithQualityTools::reverseTranscript(
82  const SequenceWithQuality& sequence)
83 {
84  // Alphabet type checking
85  if (sequence.getAlphabet()->getAlphabetType() != "RNA alphabet")
86  {
87  throw AlphabetException ("SequenceTools::reverseTranscript : Sequence must be RNA", sequence.getAlphabet());
88  }
89 
90  auto seq = transc_.reverse(sequence);
91  // Here we must also reverse the scores:
92  vector<int> scores(sequence.getQualities().rbegin(), sequence.getQualities().rend());
93  auto seqwq = make_unique<SequenceWithQuality>(*seq, scores);
94  return seqwq;
95 }
96 
97 /******************************************************************************/
98 
99 unique_ptr<SequenceWithQuality> SequenceWithQualityTools::invert(
100  const SequenceWithQuality& sequence)
101 {
102  vector<int> iContent(sequence.getContent().rbegin(), sequence.getContent().rend());
103  vector<int> iQualities(sequence.getQualities().rbegin(), sequence.getQualities().rend());
104  auto iSeq = unique_ptr<SequenceWithQuality>(sequence.clone());
105  iSeq->setContent(iContent);
106  iSeq->setQualities(iQualities);
107 
108  return iSeq;
109 }
110 
111 /******************************************************************************/
112 
113 unique_ptr<SequenceWithQuality> SequenceWithQualityTools::removeGaps(const SequenceWithQuality& seq)
114 {
115  vector<int> content;
116  vector<int> qualities;
117  auto alpha = seq.getAlphabet();
118  for (size_t i = 0; i < seq.size(); ++i)
119  {
120  if (!alpha->isGap(seq[i]))
121  {
122  content.push_back(seq[i]);
123  qualities.push_back(seq.getQualities()[i]);
124  }
125  }
126  auto newSeq = unique_ptr<SequenceWithQuality>(seq.clone());
127  newSeq->setContent(content);
128  newSeq->setQualities(qualities);
129  return newSeq;
130 }
131 
132 /******************************************************************************/
const std::string & getName() const override
Get the name of this sequence.
Definition: CoreSequence.h:170
The alphabet exception base class.
Exception thrown when two alphabets do not match.
static std::shared_ptr< const DNA > DNA_ALPHABET
Definition: AlphabetTools.h:34
static std::shared_ptr< const RNA > RNA_ALPHABET
Definition: AlphabetTools.h:35
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
virtual size_t size() const =0
Get the number of elements in the list.
Replication between to nucleic acids.
int translate(int state) const override
Translate a given state coded as a int from source alphabet to target alphabet.
static std::unique_ptr< SequenceWithQuality > transcript(const SequenceWithQuality &sequence)
Get the transcription sequence of a DNA sequence.
static std::unique_ptr< SequenceWithQuality > invert(const SequenceWithQuality &sequence)
Inverse a sequence from 5'->3' to 3'->5' and vice-versa.
static NucleicAcidsReplication RNARep_
static NucleicAcidsReplication transc_
static NucleicAcidsReplication DNARep_
static std::unique_ptr< SequenceWithQuality > removeGaps(const SequenceWithQuality &seq)
Remove gaps from a SequenceWithQuality.
static std::unique_ptr< SequenceWithQuality > complement(const SequenceWithQuality &sequence)
Get the complementary sequence of a nucleotide sequence.
static std::unique_ptr< SequenceWithQuality > reverseTranscript(const SequenceWithQuality &sequence)
Get the reverse-transcription sequence of a RNA sequence.
static std::unique_ptr< SequenceWithQuality > concatenate(const SequenceWithQuality &seqwq1, const SequenceWithQuality &seqwq2)
Concatenate two sequences.
A SequenceWithAnnotation class with quality scores attached.
SequenceWithQuality * clone() const override
const std::vector< int > & getQualities() const
Get the whole quality scores.
const Comments & getComments() const override
Get the comments.
Definition: Commentable.h:79
virtual const std::vector< T > & getContent() const =0
This alphabet is used to deal NumericAlphabet.