bpp-seq-omics  2.4.1
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  unique_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  (*logstream_ << "Strand: " << refSeq.getStrand()).endLine();
83  if (refSeq.getStrand() == '-') {
84  RangeSet<size_t> cRanges;
85  for (set<Range<size_t>*>::iterator it = ranges.getSet().begin();
86  it != ranges.getSet().end();
87  ++it)
88  {
89  cRanges.addRange(SeqRange(refSeq.getSrcSize() - (**it).end(), refSeq.getSrcSize() - (**it).begin(), dynamic_cast<SeqRange*>(*it)->getStrand()));
90  }
91  ranges = cRanges;
92  }
93 
94  //We will need to convert to alignment positions, using a sequence walker:
95  SequenceWalker walker(refSeq);
96 
97  //Now creates all blocks for all ranges:
98  if (verbose_) {
99  ApplicationTools::message->endLine();
100  ApplicationTools::displayTask("Extracting annotations", true);
101  }
102  if (logstream_) {
103  (*logstream_ << "FEATURE EXTRACTOR: extracting " << ranges.getSet().size() << " features from block " << block->getDescription() << ".").endLine();
104  }
105 
106  size_t i = 0;
107  for (set<Range<size_t>*>::iterator it = ranges.getSet().begin();
108  it != ranges.getSet().end();
109  ++it)
110  {
111  if (verbose_) {
112  ApplicationTools::displayGauge(i++, ranges.getSet().size() - 1, '=');
113  }
114  //This does not go after i=0, problem with ranges?????
115  MafBlock* newBlock = new MafBlock();
116  newBlock->setScore(block->getScore());
117  newBlock->setPass(block->getPass());
118  size_t a = walker.getAlignmentPosition((**it).begin() - refSeq.start());
119  size_t b = walker.getAlignmentPosition((**it).end() - refSeq.start() - 1);
120  for (size_t j = 0; j < block->getNumberOfSequences(); ++j) {
121  unique_ptr<MafSequence> subseq;
122  subseq.reset(block->getSequence(j).subSequence(a, b - a + 1));
123  if (!ignoreStrand_) {
124  if ((dynamic_cast<SeqRange*>(*it)->isNegativeStrand() && refSeq.getStrand() == '+') ||
125  (!dynamic_cast<SeqRange*>(*it)->isNegativeStrand() && refSeq.getStrand() == '-'))
126  {
128  }
129  }
130  (*logstream_ << subseq->getName()).endLine();
131  newBlock->addSequence(*subseq);
132  }
133  blockBuffer_.push_back(newBlock);
134  }
135 
136  if (verbose_)
138 
139  }
140 
141  MafBlock* nxtBlock = blockBuffer_.front();
142  blockBuffer_.pop_front();
143  return nxtBlock;
144 }
145 
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="")
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
char getStrand() const
Definition: MafSequence.h:166
size_t getSrcSize() const
Definition: MafSequence.h:170
Range< size_t > getRange(bool origin=true) const
Definition: MafSequence.h:131
void addRange(const Range< T > &r)
const std::set< Range< T > *, rangeComp_< T > > & getSet() const
void filterWithin(const Range< T > &r)
bool isEmpty() const
void restrictTo(const Range< T > &r)
a coordinate range on a sequence. Stores coordinates as a Range<size_t> object, but also keep the str...
virtual char getStrand() const
virtual bool isNegativeStrand() const
static Sequence & invertComplement(Sequence &seq)
size_t getAlignmentPosition(size_t seqPos)