bpp-seq-omics  2.2.0
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  auto_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  auto_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  seq->setChromosome(seq->getChromosome() + "-" + tmp->getChromosome());
128  seq->removeCoordinates();
129  }
130  if (seq->getStrand() != tmp->getStrand()) {
131  seq->setStrand('?');
132  seq->removeCoordinates();
133  }
134  if (seq->getName() != tmp->getName())
135  tmp->setName(seq->getName()); //force name conversion to prevent exception in 'merge'.
136  seq->merge(*tmp);
137  if (logstream_) {
138  (*logstream_ << "BLOCK MERGER: merging " << ref1 << " with " << ref2 << " into " << seq->getDescription()).endLine();
139  }
140  } catch (SequenceNotFoundException& snfe2) {
141  //There was a first sequence, we just extend it:
142  string ref1 = seq->getDescription();
143  seq->setToSizeR(seq->size() + incomingBlock_->getNumberOfSites() + globalSpace);
144  if (logstream_) {
145  (*logstream_ << "BLOCK MERGER: extending " << ref1 << " with " << incomingBlock_->getNumberOfSites() << " gaps on the right.").endLine();
146  }
147  }
148  } catch (SequenceNotFoundException& snfe1) {
149  //There must be a second sequence then:
150  seq.reset(new MafSequence(incomingBlock_->getSequenceForSpecies(allSp[i])));
151  string ref2 = seq->getDescription();
152  seq->setToSizeL(seq->size() + currentBlock_->getNumberOfSites() + globalSpace);
153  if (logstream_) {
154  (*logstream_ << "BLOCK MERGER: adding " << ref2 << " and extend it with " << currentBlock_->getNumberOfSites() << " gaps on the left.").endLine();
155  }
156  }
157  mergedBlock->addSequence(*seq);
158  }
159  //Cleaning stuff:
160  delete currentBlock_;
161  delete incomingBlock_;
162  currentBlock_ = mergedBlock;
163  //We check if we can also merge the next block:
164  incomingBlock_ = iterator_->nextBlock();
165  }
166  return currentBlock_;
167 }
168 
void setName(const std::string &name)
Definition: MafSequence.h:132
void setScore(double score)
Definition: MafBlock.h:102
void setStrand(char s)
Definition: MafSequence.h:174
size_t stop() const
Definition: MafSequence.h:111
bool hasCoordinates() const
Definition: MafSequence.h:102
STL namespace.
void setChromosome(const std::string &chr)
Definition: MafSequence.h:164
char getStrand() const
Definition: MafSequence.h:156
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:55
size_t getSrcSize() const
Definition: MafSequence.h:160
void addSequence(const MafSequence &sequence)
Definition: MafBlock.h:115
const std::string & getChromosome() const
Definition: MafSequence.h:154
void removeCoordinates()
Definition: MafSequence.h:104
void setPass(unsigned int pass)
Definition: MafBlock.h:103
size_t start() const
Definition: MafSequence.h:106
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:62
std::string getDescription() const
Definition: MafSequence.h:178