bpp-seq-omics  2.4.1
BlockMergerMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: BlockMergerMafIterator.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 "BlockMergerMafIterator.h"
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 (!incomingBlock_) return 0;
53  currentBlock_ = incomingBlock_;
54  incomingBlock_ = iterator_->nextBlock();
55  while (incomingBlock_) {
56  size_t globalSpace = 0;
57  for (size_t i = 0; i < species_.size(); ++i) {
58  try {
59  const MafSequence* seq1 = &currentBlock_->getSequenceForSpecies(species_[i]);
60  const MafSequence* seq2 = &incomingBlock_->getSequenceForSpecies(species_[i]);
61  if (!seq1->hasCoordinates() || !seq2->hasCoordinates())
62  throw Exception("BlockMergerMafIterator::nextBlock. Species '" + species_[i] + "' is missing coordinates in at least one block.");
63 
64  if (seq1->stop() > seq2->start())
65  return currentBlock_;
66  size_t space = seq2->start() - seq1->stop();
67  if (space > maxDist_)
68  return currentBlock_;
69  if (i == 0)
70  globalSpace = space;
71  else {
72  if (space != globalSpace)
73  return currentBlock_;
74  }
75  if (seq1->getChromosome() != seq2->getChromosome()
76  || VectorTools::contains(ignoreChrs_, seq1->getChromosome())
77  || VectorTools::contains(ignoreChrs_, seq2->getChromosome())
78  || seq1->getStrand() != seq2->getStrand()
79  || seq1->getSrcSize() != seq2->getSrcSize())
80  {
81  //There is a syntheny break in this sequence, so we do not merge the blocks.
82  return currentBlock_;
83  }
84  } catch (SequenceNotFoundException& snfe) {
85  //At least one block does not contain the sequence.
86  //We don't merge the blocks:
87  return currentBlock_;
88  }
89  }
90  //We merge the two blocks:
91  if (logstream_) {
92  (*logstream_ << "BLOCK MERGER: merging two consecutive blocks.").endLine();
93  }
94  vector<string> sp1 = currentBlock_->getSpeciesList();
95  vector<string> sp2 = incomingBlock_->getSpeciesList();
96  vector<string> allSp = VectorTools::unique(VectorTools::vectorUnion(sp1, sp2));
97  //We need to create a new MafBlock:
98  MafBlock* mergedBlock = new MafBlock();
99  //We average the score and pass values:
100  unsigned int p1 = currentBlock_->getPass();
101  unsigned int p2 = incomingBlock_->getPass();
102  if (p1 == p2) mergedBlock->setPass(p1);
103  double s1 = currentBlock_->getScore();
104  double n1 = static_cast<double>(currentBlock_->getNumberOfSites());
105  double s2 = incomingBlock_->getScore();
106  double n2 = static_cast<double>(incomingBlock_->getNumberOfSites());
107  mergedBlock->setScore((s1 * n1 + s2 * n2) / (n1 + n2));
108 
109  //Now fill the new block:
110  for (size_t i = 0; i < allSp.size(); ++i) {
111  unique_ptr<MafSequence> seq;
112  try {
113  seq.reset(new MafSequence(currentBlock_->getSequenceForSpecies(allSp[i])));
114 
115  //Check is there is a second sequence:
116  try {
117  unique_ptr<MafSequence> tmp(new MafSequence(incomingBlock_->getSequenceForSpecies(allSp[i])));
118  string ref1 = seq->getDescription(), ref2 = tmp->getDescription();
119  //Add spacer if needed:
120  if (globalSpace > 0) {
121  if (logstream_) {
122  (*logstream_ << "BLOCK MERGER: a spacer of size " << globalSpace <<" is inserted in sequence for species " << allSp[i] << ".").endLine();
123  }
124  seq->append(vector<int>(globalSpace, AlphabetTools::DNA_ALPHABET.getUnknownCharacterCode()));
125  }
126  if (seq->getChromosome() != tmp->getChromosome()) {
127  if (renameChimericChromosomes_) {
128  if (seq->getChromosome().substr(0, 7) != "chimtig") {
129  //Creates a new chimeric chromosome for this species:
130  chimericChromosomeCounts_[seq->getSpecies()]++;
131  seq->setChromosome("chimtig" + TextTools::toString(chimericChromosomeCounts_[seq->getSpecies()]));
132  }
133  } else {
134  seq->setChromosome(seq->getChromosome() + "-" + tmp->getChromosome());
135  }
136  seq->removeCoordinates();
137  }
138  if (seq->getStrand() != tmp->getStrand()) {
139  seq->setStrand('?');
140  seq->removeCoordinates();
141  }
142  if (seq->getName() != tmp->getName())
143  tmp->setName(seq->getName()); //force name conversion to prevent exception in 'merge'.
144  seq->merge(*tmp);
145  if (logstream_) {
146  (*logstream_ << "BLOCK MERGER: merging " << ref1 << " with " << ref2 << " into " << seq->getDescription()).endLine();
147  }
148  } catch (SequenceNotFoundException& snfe2) {
149  //There was a first sequence, we just extend it:
150  string ref1 = seq->getDescription();
151  seq->setToSizeR(seq->size() + incomingBlock_->getNumberOfSites() + globalSpace);
152  if (logstream_) {
153  (*logstream_ << "BLOCK MERGER: extending " << ref1 << " with " << incomingBlock_->getNumberOfSites() << " gaps on the right.").endLine();
154  }
155  }
156  } catch (SequenceNotFoundException& snfe1) {
157  //There must be a second sequence then:
158  seq.reset(new MafSequence(incomingBlock_->getSequenceForSpecies(allSp[i])));
159  string ref2 = seq->getDescription();
160  seq->setToSizeL(seq->size() + currentBlock_->getNumberOfSites() + globalSpace);
161  if (logstream_) {
162  (*logstream_ << "BLOCK MERGER: adding " << ref2 << " and extend it with " << currentBlock_->getNumberOfSites() << " gaps on the left.").endLine();
163  }
164  }
165  mergedBlock->addSequence(*seq);
166  }
167  //Cleaning stuff:
168  delete currentBlock_;
169  delete incomingBlock_;
170  currentBlock_ = mergedBlock;
171  //We check if we can also merge the next block:
172  incomingBlock_ = iterator_->nextBlock();
173  }
174  return currentBlock_;
175 }
176 
static const DNA DNA_ALPHABET
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:57
void setScore(double score)
Definition: MafBlock.h:102
void setPass(unsigned int pass)
Definition: MafBlock.h:103
void addSequence(const MafSequence &sequence)
Definition: MafBlock.h:115
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:64
const std::string & getChromosome() const
Definition: MafSequence.h:164
size_t start() const
Definition: MafSequence.h:116
size_t stop() const
Definition: MafSequence.h:121
bool hasCoordinates() const
Definition: MafSequence.h:112
char getStrand() const
Definition: MafSequence.h:166
size_t getSrcSize() const
Definition: MafSequence.h:170
static std::vector< T > vectorUnion(const std::vector< T > &vec1, const std::vector< T > &vec2)
static bool contains(const std::vector< T > &vec, T el)
static std::vector< T > unique(const std::vector< T > &v)
std::string toString(T t)