bpp-seq-omics  2.2.0
FeatureExtractorMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: FeatureExtractorMafIterator.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:
43 #include <Bpp/Seq/SequenceWalker.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  while (blockBuffer_.size() == 0) {
56  //Unless there is no more block in the buffer, we need to parse more:
57  auto_ptr<MafBlock> block;
58  START:
59  block.reset(iterator_->nextBlock());
60  if (!block.get()) return 0; //No more block.
61 
62  //Check if the block contains the reference species:
63  if (!block->hasSequenceForSpecies(refSpecies_))
64  goto START;
65 
66  //Get the feature ranges for this block:
67  const MafSequence& refSeq = block->getSequenceForSpecies(refSpecies_);
68  //first check if there is one (for now we assume that features refer to the chromosome or contig name, with implicit species):
69  std::map<std::string, RangeSet<size_t> >::iterator mr = ranges_.find(refSeq.getChromosome());
70  if (mr == ranges_.end())
71  goto START;
72 
73  RangeSet<size_t> ranges = mr->second;
74  if (completeOnly_)
75  ranges.filterWithin(refSeq.getRange(true));
76  else
77  ranges.restrictTo(refSeq.getRange(true));
78  if (ranges.isEmpty())
79  goto START;
80 
81  //If the reference sequence is on the negative strand, then we have to correct the coordinates:
82  if (refSeq.getStrand() == '-') {
83  RangeSet<size_t> cRanges;
84  for (set<Range<size_t>*>::iterator it = ranges.getSet().begin();
85  it != ranges.getSet().end();
86  ++it)
87  {
88  cRanges.addRange(Range<size_t>(refSeq.getSrcSize() - (**it).end(), refSeq.getSrcSize() - (**it).begin()));
89  }
90  ranges = cRanges;
91  }
92 
93  //We will need to convert to alignment positions, using a sequence walker:
94  SequenceWalker walker(refSeq);
95 
96  //Now creates all blocks for all ranges:
97  if (verbose_) {
98  ApplicationTools::message->endLine();
99  ApplicationTools::displayTask("Extracting annotations", true);
100  }
101  if (logstream_) {
102  (*logstream_ << "FEATURE EXTRACTOR: extracting " << ranges.getSet().size() << " features from block " << block->getDescription() << ".").endLine();
103  }
104 
105  size_t i = 0;
106  for (set<Range<size_t>*>::iterator it = ranges.getSet().begin();
107  it != ranges.getSet().end();
108  ++it)
109  {
110  if (verbose_) {
111  ApplicationTools::displayGauge(i++, ranges.getSet().size() - 1, '=');
112  }
113  MafBlock* newBlock = new MafBlock();
114  newBlock->setScore(block->getScore());
115  newBlock->setPass(block->getPass());
116  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
117  auto_ptr<MafSequence> subseq;
118  size_t a = walker.getAlignmentPosition((**it).begin() - refSeq.start());
119  size_t b = walker.getAlignmentPosition((**it).end() - refSeq.start() - 1);
120  subseq.reset(block->getSequence(j).subSequence(a, b - a + 1));
121  if (!ignoreStrand_) {
122  if (dynamic_cast<SeqRange*>(*it)->isNegativeStrand()) {
123  SequenceTools::invertComplement(*subseq);
124  }
125  }
126  newBlock->addSequence(*subseq);
127  }
128  blockBuffer_.push_back(newBlock);
129  }
130 
131  if (verbose_)
132  ApplicationTools::displayTaskDone();
133 
134  }
135 
136  MafBlock* nxtBlock = blockBuffer_.front();
137  blockBuffer_.pop_front();
138  return nxtBlock;
139 }
140 
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.
char getStrand() const
Definition: MafSequence.h:156
MafSequence * subSequence(size_t startAt, size_t length) const
Extract a sub-sequence.
Definition: MafSequence.cpp:48
bool hasSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:129
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
Range< size_t > getRange(bool origin=true) const
Definition: MafSequence.h:121
double getScore() const
Definition: MafBlock.h:105
void setPass(unsigned int pass)
Definition: MafBlock.h:103
size_t getNumberOfSequences() const
Definition: MafBlock.h:111
std::string getDescription() const
Definition: MafBlock.h:177
const MafSequence & getSequence(const std::string &name) const
Definition: MafBlock.h:121
size_t start() const
Definition: MafSequence.h:106
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:62