bpp-seq-omics  2.4.1
OutputAlignmentMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: OutputAlignmentMafIterator.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:
44 
45 using namespace bpp;
46 
47 //From the STL:
48 #include <string>
49 #include <numeric>
50 
51 using namespace std;
52 
54 {
55  MafBlock* block = iterator_->nextBlock();
56  if (block) {
57  if (output_) {
58  writeBlock(*output_, *block);
59  } else {
60  string chr = "ChrNA";
61  string start = "StartNA";
62  string stop = "StopNA";
63  if (block->hasSequenceForSpecies(refSpecies_)) {
64  const MafSequence& refseq = block->getSequenceForSpecies(refSpecies_);
65  chr = refseq.getChromosome();
66  start = TextTools::toString(refseq.start());
67  stop = TextTools::toString(refseq.stop());
68  }
69  string file = file_;
70  TextTools::replaceAll(file, "%i", TextTools::toString(++currentBlockIndex_));
71  TextTools::replaceAll(file, "%c", chr);
72  TextTools::replaceAll(file, "%b", start);
73  TextTools::replaceAll(file, "%e", stop);
74  std::ofstream output(file.c_str(), ios::out);
75 
76  writeBlock(output, *block);
77  }
78  }
79  return block;
80 }
81 
82 void OutputAlignmentMafIterator::writeBlock(std::ostream& out, const MafBlock& block) const {
83  //First get alignment:
85  //We cannot copy directly the container because we want to convert from MafSequence to BasicSequence (needed for renaming):
86  SequenceContainerTools::convertContainer<AlignedSequenceContainer, AlignedSequenceContainer, BasicSequence>(block.getAlignment(), aln);
87  //Format sequence names:
88  vector<string> names(aln.getNumberOfSequences());
89  for (size_t i = 0; i < aln.getNumberOfSequences(); ++i) {
90  const MafSequence& mafseq = block.getSequence(i);
91  if (mafseq.hasCoordinates() && outputCoordinates_)
92  names[i] = mafseq.getSpecies() + "-" + mafseq.getChromosome() + "(" + mafseq.getStrand() + ")/" + TextTools::toString(mafseq.start() + 1) + "-" + TextTools::toString(mafseq.stop() + 1);
93  else
94  names[i] = mafseq.getSpecies();
95  }
96  aln.setSequencesNames(names);
97  if (addLDHatHeader_)
98  out << aln.getNumberOfSequences() << " " << aln.getNumberOfSites() << " 1" << endl; //We here assume sequences are haploid.
99  writer_->writeAlignment(out, aln);
100 }
101 
static const DNA DNA_ALPHABET
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:57
const MafSequence & getSequence(const std::string &name) const
Definition: MafBlock.h:121
AlignedSequenceContainer & getAlignment()
Definition: MafBlock.h:108
bool hasSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:129
const MafSequence & getSequenceForSpecies(const std::string &species) const
Definition: MafBlock.h:139
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
const std::string & getSpecies() const
Definition: MafSequence.h:162
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
void writeBlock(std::ostream &out, const MafBlock &block) const
size_t getNumberOfSequences() const
void setSequencesNames(const std::vector< std::string > &names, bool checkNames=true)
void replaceAll(std::string &target, const std::string &query, const std::string &replacement)
std::string toString(T t)