bpp-seq-omics  2.2.0
EntropyFilterMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: EntropyFilterMafIterator.cpp
3 // Authors: Julien Dutheil
4 // Created: Tue Sep 07 2010
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (2010)
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 
41 
42 using namespace bpp;
43 
44 //From the STL:
45 #include <string>
46 #include <numeric>
47 
48 using namespace std;
49 
51 {
52  if (blockBuffer_.size() == 0) {
53  //Else there is no more block in the buffer, we need to parse more:
54  do {
55  MafBlock* block = iterator_->nextBlock();
56  if (!block) return 0; //No more block.
57 
58  //Parse block.
59  int gap = AlphabetTools::DNA_ALPHABET.getGapCharacterCode();
60  //unused for now
61  //int unk = AlphabetTools::DNA_ALPHABET.getUnknownCharacterCode();
62  size_t nr;
63  size_t nc = static_cast<size_t>(block->getNumberOfSites());
64 
65  vector< vector<int> > aln;
66  if (missingAsGap_ && !ignoreGaps_) {
67  nr = species_.size();
68  aln.resize(nr);
69  for (size_t i = 0; i < nr; ++i) {
70  if (block->hasSequenceForSpecies(species_[i]))
71  aln[i] = block->getSequenceForSpecies(species_[i]).getContent();
72  else {
73  aln[i].resize(nc);
74  fill(aln[i].begin(), aln[i].end(), gap);
75  }
76  }
77  } else {
78  vector<string> speciesSet = VectorTools::vectorIntersection(species_, block->getSpeciesList());
79  nr = speciesSet.size();
80  aln.resize(nr);
81  for (size_t i = 0; i < nr; ++i) {
82  aln[i] = block->getSequenceForSpecies(species_[i]).getContent();
83  }
84  }
85  //First we create a mask:
86  vector<size_t> pos;
87  //Reset window:
88  window_.clear();
89  //Init window:
90  size_t i;
91  for (i = 0; i < windowSize_; ++i) {
92  vector<int> col;
93  for (size_t j = 0; j < nr; ++j) {
94  int x = aln[j][i];
95  if (x != gap || !ignoreGaps_)
96  col.push_back(x);
97  }
98  double entropy = VectorTools::shannonDiscrete<int, double>(col) / log(5.);
99  window_.push_back(entropy > maxEnt_ ? 1 : 0);
100  }
101  //Slide window:
102  if (verbose_) {
103  ApplicationTools::message->endLine();
104  ApplicationTools::displayTask("Sliding window for entropy filter", true);
105  }
106  while (i + step_ < nc) {
107  if (verbose_)
108  ApplicationTools::displayGauge(i - windowSize_, nc - windowSize_ - 1, '>');
109  //Evaluate current window:
110  unsigned int count = std::accumulate(window_.begin(), window_.end(), 0u);
111  if (count > maxPos_) {
112  if (pos.size() == 0) {
113  pos.push_back(i - windowSize_);
114  pos.push_back(i);
115  } else {
116  if (i - windowSize_ < pos[pos.size() - 1]) {
117  pos[pos.size() - 1] = i; //Windows are overlapping and we extend previous region
118  } else { //This is a new region
119  pos.push_back(i - windowSize_);
120  pos.push_back(i);
121  }
122  }
123  }
124 
125  //Move forward:
126  for (size_t k = 0; k < step_; ++k) {
127  vector<int> col;
128  for (size_t j = 0; j < nr; ++j) {
129  int x = aln[j][i];
130  if (x != gap || !ignoreGaps_)
131  col.push_back(x);
132  }
133  double entropy = VectorTools::shannonDiscrete<int, double>(col) / log(5.);
134  window_.push_back(entropy > maxEnt_ ? 1 : 0);
135  window_.pop_front();
136  ++i;
137  }
138  }
139 
140  //Evaluate last window:
141  unsigned int count = std::accumulate(window_.begin(), window_.end(), 0u);
142  if (count > maxPos_) {
143  if (pos.size() == 0) {
144  pos.push_back(i - windowSize_);
145  pos.push_back(i);
146  } else {
147  if (i - windowSize_ <= pos[pos.size() - 1]) {
148  pos[pos.size() - 1] = i; //Windows are overlapping and we extend previous region
149  } else { //This is a new region
150  pos.push_back(i - windowSize_);
151  pos.push_back(i);
152  }
153  }
154  }
155  if (verbose_)
156  ApplicationTools::displayTaskDone();
157 
158  //Now we remove regions with two many gaps, using a sliding window:
159  if (pos.size() == 0) {
160  blockBuffer_.push_back(block);
161  if (logstream_) {
162  (*logstream_ << "ENTROPY CLEANER: block " << block->getDescription() << " is clean and kept as is.").endLine();
163  }
164  } else if (pos.size() == 2 && pos.front() == 0 && pos.back() == block->getNumberOfSites()) {
165  //Everything is removed:
166  if (logstream_) {
167  (*logstream_ << "ENTROPY CLEANER: block " << block->getDescription() << " was entirely removed. Tried to get the next one.").endLine();
168  }
169  } else {
170  if (logstream_) {
171  (*logstream_ << "ALN CLEANER: block " << block->getDescription() << " with size "<< block->getNumberOfSites() << " will be split into " << (pos.size() / 2 + 1) << " blocks.").endLine();
172  }
173  if (verbose_) {
174  ApplicationTools::message->endLine();
175  ApplicationTools::displayTask("Spliting block", true);
176  }
177  for (i = 0; i < pos.size(); i+=2) {
178  if (verbose_)
179  ApplicationTools::displayGauge(i, pos.size() - 2, '=');
180  if (logstream_) {
181  (*logstream_ << "ENTROPY CLEANER: removing region (" << pos[i] << ", " << pos[i+1] << ") from block " << block->getDescription() << ".").endLine();
182  }
183  if (pos[i] > 0) {
184  MafBlock* newBlock = new MafBlock();
185  newBlock->setScore(block->getScore());
186  newBlock->setPass(block->getPass());
187  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
188  MafSequence* subseq;
189  if (i == 0) {
190  subseq = block->getSequence(j).subSequence(0, pos[i]);
191  } else {
192  subseq = block->getSequence(j).subSequence(pos[i - 1], pos[i] - pos[i - 1]);
193  }
194  newBlock->addSequence(*subseq);
195  delete subseq;
196  }
197  blockBuffer_.push_back(newBlock);
198  }
199 
200  if (keepTrashedBlocks_) {
201  MafBlock* outBlock = new MafBlock();
202  outBlock->setScore(block->getScore());
203  outBlock->setPass(block->getPass());
204  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
205  MafSequence* outseq = block->getSequence(j).subSequence(pos[i], pos[i + 1] - pos[i]);
206  outBlock->addSequence(*outseq);
207  delete outseq;
208  }
209  trashBuffer_.push_back(outBlock);
210  }
211  }
212  //Add last block:
213  if (pos[pos.size() - 1] < block->getNumberOfSites()) {
214  MafBlock* newBlock = new MafBlock();
215  newBlock->setScore(block->getScore());
216  newBlock->setPass(block->getPass());
217  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
218  MafSequence* subseq;
219  subseq = block->getSequence(j).subSequence(pos[pos.size() - 1], block->getNumberOfSites() - pos[pos.size() - 1]);
220  newBlock->addSequence(*subseq);
221  delete subseq;
222  }
223  blockBuffer_.push_back(newBlock);
224  }
225  if (verbose_)
226  ApplicationTools::displayTaskDone();
227 
228  delete block;
229  }
230  } while (blockBuffer_.size() == 0);
231  }
232 
233  MafBlock* block = blockBuffer_.front();
234  blockBuffer_.pop_front();
235  return block;
236 }
237 
const MafSequence & getSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:139
void setScore(double score)
Definition: MafBlock.h:102
unsigned int getPass() const
Definition: MafBlock.h:106
STL namespace.
size_t getNumberOfSites() const
Definition: MafBlock.h:113
MafSequence * subSequence(size_t startAt, size_t length) const
Extract a sub-sequence.
Definition: MafSequence.cpp:48
bool hasSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:129
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:55
void addSequence(const MafSequence &sequence)
Definition: MafBlock.h:115
std::vector< std::string > getSpeciesList() const
Definition: MafBlock.h:162
double getScore() const
Definition: MafBlock.h:105
void setPass(unsigned int pass)
Definition: MafBlock.h:103
size_t getNumberOfSequences() const
Definition: MafBlock.h:111
std::string getDescription() const
Definition: MafBlock.h:177
const MafSequence & getSequence(const std::string &name) const
Definition: MafBlock.h:121
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:62