bpp-seq-omics  2.4.1
OutputMafIterator.cpp
Go to the documentation of this file.
1 //
2 // File: OutputMafIterator.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 "OutputMafIterator.h"
41 
42 //From bpp-seq:
45 
46 using namespace bpp;
47 
48 //From the STL:
49 #include <string>
50 #include <numeric>
51 
52 using namespace std;
53 
54 void OutputMafIterator::writeHeader(std::ostream& out) const
55 {
56  out << "##maf version=1 program=Bio++" << endl << "#" << endl;
57  //There are more options in the header that we may want to support...
58 }
59 
60 void OutputMafIterator::writeBlock(std::ostream& out, const MafBlock& block) const
61 {
62  out << "a";
63  if (! std::isinf(block.getScore()))
64  out << " score=" << block.getScore();
65  if (block.getPass() > 0)
66  out << " pass=" << block.getPass();
67  out << endl;
68 
69  //Now we write sequences. First need to count characters for aligning blocks:
70  size_t mxcSrc = 0, mxcStart = 0, mxcSize = 0, mxcSrcSize = 0;
71  for (size_t i = 0; i < block.getNumberOfSequences(); i++) {
72  const MafSequence* seq = &block.getSequence(i);
73  size_t start = 0; //Maybe we should output sthg else here?
74  if (seq->hasCoordinates())
75  start = seq->start();
76  mxcSrc = max(mxcSrc , seq->getName().size());
77  mxcStart = max(mxcStart , TextTools::toString(start).size());
78  mxcSize = max(mxcSize , TextTools::toString(seq->getGenomicSize()).size());
79  mxcSrcSize = max(mxcSrcSize, TextTools::toString(seq->getSrcSize()).size());
80  }
81  //Now print each sequence:
82  for (size_t i = 0; i < block.getNumberOfSequences(); i++) {
83  const MafSequence* seq = &block.getSequence(i);
84  out << "s ";
85  out << TextTools::resizeRight(seq->getName(), mxcSrc, ' ') << " ";
86  size_t start = 0; //Maybe we should output sthg else here?
87  if (seq->hasCoordinates())
88  start = seq->start();
89  out << TextTools::resizeLeft(TextTools::toString(start), mxcStart, ' ') << " ";
90  out << TextTools::resizeLeft(TextTools::toString(seq->getGenomicSize()), mxcSize, ' ') << " ";
91  out << seq->getStrand() << " ";
92  out << TextTools::resizeLeft(TextTools::toString(seq->getSrcSize()), mxcSrcSize, ' ') << " ";
93  //Shall we write the sequence as masked?
94  string seqstr = seq->toString();
95  if (mask_ && seq->hasAnnotation(SequenceMask::MASK)) {
96  const SequenceMask* mask = &dynamic_cast<const SequenceMask&>(seq->getAnnotation(SequenceMask::MASK));
97  for (size_t j = 0; j < seqstr.size(); ++j) {
98  char c = ((*mask)[j] ? static_cast<char>(tolower(static_cast<int>(seqstr[j]))) : seqstr[j]);
99  out << c;
100  }
101  } else {
102  out << seqstr;
103  }
104  out << endl;
105  //Write quality scores if any:
106  if (mask_ && seq->hasAnnotation(SequenceQuality::QUALITY_SCORE)) {
107  const SequenceQuality* qual = &dynamic_cast<const SequenceQuality&>(seq->getAnnotation(SequenceQuality::QUALITY_SCORE));
108  out << "q ";
109  out << TextTools::resizeRight(seq->getName(), mxcSrc + mxcStart + mxcSize + mxcSrcSize + 5, ' ') << " ";
110  string qualStr;
111  for (size_t j = 0; j < seq->size(); ++j) {
112  int s = (*qual)[j];
113  if (s == -1) {
114  qualStr += "-";
115  } else if (s == -2) {
116  qualStr += "?";
117  } else if (s >=0 && s < 10) {
118  qualStr += TextTools::toString(s);
119  } else if (s == 10) {
120  qualStr += "F";
121  } else {
122  throw Exception("MafAlignmentParser::writeBlock. Unsuported score value: " + TextTools::toString(s));
123  }
124  }
125  out << qualStr << endl;
126  }
127  }
128  out << endl;
129 }
130 
virtual std::string toString() const=0
virtual size_t size() const=0
A synteny block data structure, the basic unit of a MAF alignement file.
Definition: MafBlock.h:57
unsigned int getPass() const
Definition: MafBlock.h:106
size_t getNumberOfSequences() const
Definition: MafBlock.h:111
double getScore() const
Definition: MafBlock.h:105
const MafSequence & getSequence(const std::string &name) const
Definition: MafBlock.h:121
A sequence class which is used to store data from MAF files.
Definition: MafSequence.h:64
size_t start() const
Definition: MafSequence.h:116
bool hasCoordinates() const
Definition: MafSequence.h:112
char getStrand() const
Definition: MafSequence.h:166
size_t getSrcSize() const
Definition: MafSequence.h:170
size_t getGenomicSize() const
Definition: MafSequence.h:168
void writeHeader(std::ostream &out) const
void writeBlock(std::ostream &out, const MafBlock &block) const
static const std::string MASK
static const std::string QUALITY_SCORE
virtual bool hasAnnotation(const std::string &type) const
virtual const SequenceAnnotation & getAnnotation(const std::string &type) const
const std::string & getName() const
std::string resizeLeft(const std::string &s, std::size_t newSize, char fill)
std::string toString(T t)
std::string resizeRight(const std::string &s, std::size_t newSize, char fill)