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