bpp-seq-omics  2.4.1
MaskFilterMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: MaskFilterMafIterator.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 
40 #include "MaskFilterMafIterator.h"
41 
42 //Fomr 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<bool> > aln;
63  for (size_t i = 0; i < species_.size(); ++i) {
64  if (block->hasSequenceForSpecies(species_[i])) {
65  const MafSequence* seq = &block->getSequenceForSpecies(species_[i]);
67  aln.push_back(dynamic_cast<const SequenceMask&>(seq->getAnnotation(SequenceMask::MASK)).getMask());
68  }
69  }
70  }
71  size_t nr = aln.size();
72  size_t nc = block->getNumberOfSites();
73  //First we create a mask:
74  vector<size_t> pos;
75  vector<bool> col(nr);
76  //Reset window:
77  window_.clear();
78  //Init window:
79  size_t i;
80  for (i = 0; i < windowSize_; ++i) {
81  for (size_t j = 0; j < nr; ++j) {
82  col[j] = aln[j][i];
83  }
84  window_.push_back(col);
85  }
86  //Slide window:
87  if (verbose_) {
88  ApplicationTools::message->endLine();
89  ApplicationTools::displayTask("Sliding window for mask filter", true);
90  }
91  while (i + step_ < nc) {
92  if (verbose_)
93  ApplicationTools::displayGauge(i - windowSize_, nc - windowSize_ - 1, '>');
94  //Evaluate current window:
95  unsigned int sum = 0;
96  for (size_t u = 0; u < window_.size(); ++u)
97  for (size_t v = 0; v < window_[u].size(); ++v)
98  if (window_[u][v]) sum++;
99  if (sum > maxMasked_) {
100  if (pos.size() == 0) {
101  pos.push_back(i - windowSize_);
102  pos.push_back(i);
103  } else {
104  if (i - windowSize_ <= pos[pos.size() - 1]) {
105  pos[pos.size() - 1] = i; //Windows are overlapping and we extend previous region
106  } else { //This is a new region
107  pos.push_back(i - windowSize_);
108  pos.push_back(i);
109  }
110  }
111  }
112 
113  //Move forward:
114  for (size_t k = 0; k < step_; ++k) {
115  for (size_t j = 0; j < nr; ++j) {
116  col[j] = aln[j][i];
117  }
118  window_.push_back(col);
119  window_.pop_front();
120  ++i;
121  }
122  }
123 
124  //Evaluate last window:
125  unsigned int sum = 0;
126  for (size_t u = 0; u < window_.size(); ++u)
127  for (size_t v = 0; v < window_[u].size(); ++v)
128  if (window_[u][v]) sum++;
129  if (sum > maxMasked_) {
130  if (pos.size() == 0) {
131  pos.push_back(i - windowSize_);
132  pos.push_back(i);
133  } else {
134  if (i - windowSize_ < pos[pos.size() - 1]) {
135  pos[pos.size() - 1] = i; //Windows are overlapping and we extend previous region
136  } else { //This is a new region
137  pos.push_back(i - windowSize_);
138  pos.push_back(i);
139  }
140  }
141  }
142  if (verbose_)
144 
145  //Now we remove regions with two many gaps, using a sliding window:
146  if (pos.size() == 0) {
147  blockBuffer_.push_back(block);
148  if (logstream_) {
149  (*logstream_ << "MASK CLEANER: block is clean and kept as is.").endLine();
150  }
151  } else if (pos.size() == 2 && pos.front() == 0 && pos.back() == block->getNumberOfSites()) {
152  //Everything is removed:
153  if (logstream_) {
154  (*logstream_ << "MASK CLEANER: block was entirely removed. Tried to get the next one.").endLine();
155  }
156  } else {
157  if (logstream_) {
158  (*logstream_ << "MASK CLEANER: block with size "<< block->getNumberOfSites() << " will be split into " << (pos.size() / 2 + 1) << " blocks.").endLine();
159  }
160  if (verbose_) {
161  ApplicationTools::message->endLine();
162  ApplicationTools::displayTask("Spliting block", true);
163  }
164  for (i = 0; i < pos.size(); i+=2) {
165  if (verbose_)
166  ApplicationTools::displayGauge(i, pos.size() - 2, '=');
167  if (logstream_) {
168  (*logstream_ << "MASK CLEANER: removing region (" << pos[i] << ", " << pos[i+1] << ") from block.").endLine();
169  }
170  if (pos[i] > 0) {
171  MafBlock* newBlock = new MafBlock();
172  newBlock->setScore(block->getScore());
173  newBlock->setPass(block->getPass());
174  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
175  MafSequence* subseq;
176  if (i == 0) {
177  subseq = block->getSequence(j).subSequence(0, pos[i]);
178  } else {
179  subseq = block->getSequence(j).subSequence(pos[i - 1], pos[i] - pos[i - 1]);
180  }
181  newBlock->addSequence(*subseq);
182  delete subseq;
183  }
184  blockBuffer_.push_back(newBlock);
185  }
186 
187  if (keepTrashedBlocks_) {
188  MafBlock* outBlock = new MafBlock();
189  outBlock->setScore(block->getScore());
190  outBlock->setPass(block->getPass());
191  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
192  MafSequence* outseq = block->getSequence(j).subSequence(pos[i], pos[i + 1] - pos[i]);
193  outBlock->addSequence(*outseq);
194  delete outseq;
195  }
196  trashBuffer_.push_back(outBlock);
197  }
198  }
199  //Add last block:
200  if (pos[pos.size() - 1] < block->getNumberOfSites()) {
201  MafBlock* newBlock = new MafBlock();
202  newBlock->setScore(block->getScore());
203  newBlock->setPass(block->getPass());
204  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
205  MafSequence* subseq;
206  subseq = block->getSequence(j).subSequence(pos[pos.size() - 1], block->getNumberOfSites() - pos[pos.size() - 1]);
207  newBlock->addSequence(*subseq);
208  delete subseq;
209  }
210  blockBuffer_.push_back(newBlock);
211  }
212  if (verbose_)
214 
215  delete block;
216  }
217  } while (blockBuffer_.size() == 0);
218  }
219 
220  MafBlock* block = blockBuffer_.front();
221  blockBuffer_.pop_front();
222  return block;
223 }
224 
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
bool hasSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:129
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
const std::vector< bool > & getMask() const
static const std::string MASK
virtual bool hasAnnotation(const std::string &type) const
virtual const SequenceAnnotation & getAnnotation(const std::string &type) const