bpp-seq-omics  2.4.1
QualityFilterMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: QualityFilterMafIterator.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 //From bpp-seq:
44 
45 using namespace bpp;
46 
47 //From the STL:
48 #include <string>
49 #include <numeric>
50 
51 using namespace std;
52 
54 {
55  if (blockBuffer_.size() == 0) {
56  do {
57  //Else there is no more block in the buffer, we need parse more:
58  MafBlock* block = iterator_->nextBlock();
59  if (!block) return 0; //No more block.
60 
61  //Parse block.
62  vector< vector<int> > aln;
63  for (size_t i = 0; i < species_.size(); ++i) {
64  const MafSequence* seq = &block->getSequenceForSpecies(species_[i]);
66  aln.push_back(dynamic_cast<const SequenceQuality&>(seq->getAnnotation(SequenceQuality::QUALITY_SCORE)).getScores());
67  }
68  }
69  if (aln.size() != species_.size()) {
70  blockBuffer_.push_back(block);
71  if (logstream_) {
72  (*logstream_ << "QUAL CLEANER: block is missing quality score for at least one species and will therefore not be filtered.").endLine();
73  }
74  //NB here we could decide to discard the block instead!
75  } else {
76  size_t nr = aln.size();
77  size_t nc = block->getNumberOfSites();
78  //First we create a mask:
79  vector<size_t> pos;
80  vector<int> col(nr);
81  //Reset window:
82  window_.clear();
83  //Init window:
84  size_t i;
85  for (i = 0; i < windowSize_; ++i) {
86  for (size_t j = 0; j < nr; ++j) {
87  col[j] = aln[j][i];
88  }
89  window_.push_back(col);
90  }
91  //Slide window:
92  if (verbose_) {
93  ApplicationTools::message->endLine();
94  ApplicationTools::displayTask("Sliding window for quality filter", true);
95  }
96  while (i + step_ < nc) {
97  if (verbose_)
98  ApplicationTools::displayGauge(i - windowSize_, nc - windowSize_ - 1, '>');
99  //Evaluate current window:
100  double mean = 0;
101  double n = static_cast<double>(aln.size() * windowSize_);
102  for (size_t u = 0; u < window_.size(); ++u)
103  for (size_t v = 0; v < window_[u].size(); ++v) {
104  mean += (window_[u][v] > 0 ? static_cast<double>(window_[u][v]) : 0.);
105  if (window_[u][v] == -1) n--;
106  }
107  if (n > 0 && (mean / n) < minQual_) {
108  if (pos.size() == 0) {
109  pos.push_back(i - windowSize_);
110  pos.push_back(i);
111  } else {
112  if (i - windowSize_ <= pos[pos.size() - 1]) {
113  pos[pos.size() - 1] = i; //Windows are overlapping and we extend previous region
114  } else { //This is a new region
115  pos.push_back(i - windowSize_);
116  pos.push_back(i);
117  }
118  }
119  }
120 
121  //Move forward:
122  for (size_t k = 0; k < step_; ++k) {
123  for (size_t j = 0; j < nr; ++j) {
124  col[j] = aln[j][i];
125  }
126  window_.push_back(col);
127  window_.pop_front();
128  ++i;
129  }
130  }
131 
132  //Evaluate last window:
133  double mean = 0;
134  double n = static_cast<double>(aln.size() * windowSize_);
135  for (size_t u = 0; u < window_.size(); ++u)
136  for (size_t v = 0; v < window_[u].size(); ++v) {
137  mean += (window_[u][v] > 0 ? static_cast<double>(window_[u][v]) : 0.);
138  if (window_[u][v] == -1) n--;
139  }
140  if (n > 0 && (mean / n) < minQual_) {
141  if (pos.size() == 0) {
142  pos.push_back(i - windowSize_);
143  pos.push_back(i);
144  } else {
145  if (i - windowSize_ < pos[pos.size() - 1]) {
146  pos[pos.size() - 1] = i; //Windows are overlapping and we extend previous region
147  } else { //This is a new region
148  pos.push_back(i - windowSize_);
149  pos.push_back(i);
150  }
151  }
152  }
153  if (verbose_)
155 
156  //Now we remove regions with two many gaps, using a sliding window:
157  if (pos.size() == 0) {
158  blockBuffer_.push_back(block);
159  if (logstream_) {
160  (*logstream_ << "QUAL CLEANER: block is clean and kept as is.").endLine();
161  }
162  } else if (pos.size() == 2 && pos.front() == 0 && pos.back() == block->getNumberOfSites()) {
163  //Everything is removed:
164  if (logstream_) {
165  (*logstream_ << "QUAL CLEANER: block was entirely removed. Tried to get the next one.").endLine();
166  }
167  } else {
168  if (logstream_) {
169  (*logstream_ << "QUAL CLEANER: block with size "<< block->getNumberOfSites() << " will be split into " << (pos.size() / 2 + 1) << " blocks.").endLine();
170  }
171  if (verbose_) {
172  ApplicationTools::message->endLine();
173  ApplicationTools::displayTask("Spliting block", true);
174  }
175  for (i = 0; i < pos.size(); i+=2) {
176  if (verbose_)
177  ApplicationTools::displayGauge(i, pos.size() - 2, '=');
178  if (logstream_) {
179  (*logstream_ << "QUAL CLEANER: removing region (" << pos[i] << ", " << pos[i+1] << ") from block.").endLine();
180  }
181  if (pos[i] > 0) {
182  MafBlock* newBlock = new MafBlock();
183  newBlock->setScore(block->getScore());
184  newBlock->setPass(block->getPass());
185  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
186  MafSequence* subseq;
187  if (i == 0) {
188  subseq = block->getSequence(j).subSequence(0, pos[i]);
189  } else {
190  subseq = block->getSequence(j).subSequence(pos[i - 1], pos[i] - pos[i - 1]);
191  }
192  newBlock->addSequence(*subseq);
193  delete subseq;
194  }
195  blockBuffer_.push_back(newBlock);
196  }
197 
198  if (keepTrashedBlocks_) {
199  MafBlock* outBlock = new MafBlock();
200  outBlock->setScore(block->getScore());
201  outBlock->setPass(block->getPass());
202  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
203  MafSequence* outseq = block->getSequence(j).subSequence(pos[i], pos[i + 1] - pos[i]);
204  outBlock->addSequence(*outseq);
205  delete outseq;
206  }
207  trashBuffer_.push_back(outBlock);
208  }
209  }
210  //Add last block:
211  if (pos[pos.size() - 1] < block->getNumberOfSites()) {
212  MafBlock* newBlock = new MafBlock();
213  newBlock->setScore(block->getScore());
214  newBlock->setPass(block->getPass());
215  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
216  MafSequence* subseq;
217  subseq = block->getSequence(j).subSequence(pos[pos.size() - 1], block->getNumberOfSites() - pos[pos.size() - 1]);
218  newBlock->addSequence(*subseq);
219  delete subseq;
220  }
221  blockBuffer_.push_back(newBlock);
222  }
223  if (verbose_)
225 
226  delete block;
227  }
228  }
229  } while (blockBuffer_.size() == 0);
230  }
231 
232  MafBlock* block = blockBuffer_.front();
233  blockBuffer_.pop_front();
234  return block;
235 }
236 
static void displayTask(const std::string &text, bool eof=false)
static std::shared_ptr< OutputStream > message
static void displayTaskDone()
static void displayGauge(size_t iter, size_t total, char symbol='>', const std::string &mes="")
virtual size_t size() const=0
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:57
unsigned int getPass() const
Definition: MafBlock.h:106
void setScore(double score)
Definition: MafBlock.h:102
void setPass(unsigned int pass)
Definition: MafBlock.h:103
size_t getNumberOfSequences() const
Definition: MafBlock.h:111
double getScore() const
Definition: MafBlock.h:105
size_t getNumberOfSites() const
Definition: MafBlock.h:113
const MafSequence & getSequence(const std::string &name) const
Definition: MafBlock.h:121
void addSequence(const MafSequence &sequence)
Definition: MafBlock.h:115
const MafSequence & getSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:139
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:64
MafSequence * subSequence(size_t startAt, size_t length) const
Extract a sub-sequence.
Definition: MafSequence.cpp:48
static const std::string QUALITY_SCORE
const std::vector< int > & getScores() const
virtual bool hasAnnotation(const std::string &type) const
virtual const SequenceAnnotation & getAnnotation(const std::string &type) const