bpp-seq-omics  2.4.1
SequenceFeatureTools.cpp
Go to the documentation of this file.
1 //
2 // File: SequenceFeatureTools.h
3 // Created by: Julien Dutheil
4 // Created on: Mon Jul 30 2012
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for sequences analysis.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #include "SequenceFeatureTools.h"
41 
42 //From bpp-seq:
43 #include <Bpp/Seq/SequenceTools.h>
47 
48 //From STL
49 #include <vector>
50 
51 using namespace bpp;
52 
53 /******************************************************************************/
54 
56 {
57  if (range.end() > seq.size())
58  throw IndexOutOfBoundsException ("SequenceTools::extract: Invalid upper bound", range.end(), 0, seq.size());
59  Sequence* sout = SequenceTools::subseq(seq, range.begin(), range.end() - 1);
60  if (range.isNegativeStrand()) {
62  }
63  return sout;
64 }
65 
66 /******************************************************************************/
67 
68 void SequenceFeatureTools::extract(const Sequence& seq, const SeqRange& range, Sequence& output) {
69  if (range.end() > seq.size())
70  throw IndexOutOfBoundsException ("SequenceTools::extract: Invalid upper bound", range.end(), 0, seq.size());
71  SequenceTools::subseq(seq, range.begin(), range.end() - 1, output);
72  if (range.isNegativeStrand()) {
74  }
75 }
76 
77 /******************************************************************************/
78 
79 unsigned int SequenceFeatureTools::getOrfs(const Sequence& seq, SequenceFeatureSet& featSet, const GeneticCode& gCode)
80 {
82  throw AlphabetException("SequenceFeatureTools::getOrfs: Sequence alphabet must be nucleic!", seq.getAlphabet());
83  }
84  unsigned int orfCpt = 0;
85  const CodonAlphabet* codonAlpha = gCode.getSourceAlphabet();
86  std::vector< std::vector<size_t> > starts(3), stops(3);
87  size_t phase = 0;
88  for (size_t p = 0 ; p < seq.size() - 2 ; p++) {
89  phase = p % 3;
90  if (gCode.isStart(codonAlpha->getCodon(seq.getValue(p), seq.getValue(p + 1), seq.getValue(p + 2)))) {
91  starts[phase].push_back(p);
92  //std::cerr << "Start: " << p << " (" << phase << ")" << std::endl;
93  } else if (gCode.isStop(codonAlpha->getCodon(seq.getValue(p), seq.getValue(p + 1), seq.getValue(p + 2)))) {
94  stops[phase].push_back(p);
95  //std::cerr << "Stop: " << p << " (" << phase << ")" << std::endl;
96  }
97  }
98  for (size_t i = 0 ; i < 3 ; ++i) {
99  std::vector< size_t >::iterator start(starts[i].begin()), stop(stops[i].begin());
100  while (stop != stops[i].end() && start != starts[i].end()) {
101  if (*stop < *start) {
102  stop++;
103  } else {
104  orfCpt++;
105  //std::cerr << "ORF: " << *start << " - " << *stop + 2 << " (" << i << ")" << std::endl;
106  bpp::BasicSequenceFeature feat("", seq.getName(), "Bio++", "CDS", *start, *stop + 2, '+');
107  featSet.addFeature(feat);
108  start++;
109  }
110  }
111  }
112  return orfCpt;
113 }
114 
115 /******************************************************************************/
static bool isNucleicAlphabet(const Alphabet *alphabet)
A very simple implementation of the SequenceFeature class.
int getCodon(int pos1, int pos2, int pos3) const
virtual const T & getValue(size_t pos) const=0
virtual const Alphabet * getAlphabet() const=0
virtual size_t size() const=0
const CodonAlphabet * getSourceAlphabet() const
virtual bool isStart(int state) const
virtual bool isStop(int state) const=0
T end() const
T begin() const
a coordinate range on a sequence. Stores coordinates as a Range<size_t> object, but also keep the str...
virtual bool isNegativeStrand() const
A simple ensemble of sequence features.
void addFeature(const SequenceFeature &feature)
Add a feature to the container. The feature will be copied and the copy owned by the container.
static Sequence * extract(const Sequence &seq, const SeqRange &range)
Extract a sub-sequence given a SeqRange.
static unsigned int getOrfs(const Sequence &seq, SequenceFeatureSet &featSet, const GeneticCode &gCode)
Get ORF features for a Sequence.
static void subseq(const Sequence &sequence, size_t begin, size_t end, Sequence &output)
static Sequence & invertComplement(Sequence &seq)
virtual const std::string & getName() const=0