bpp-seq  2.2.0
Clustal.cpp
Go to the documentation of this file.
1 //
2 // File: Clustal.cpp
3 // Created by: Julien Dutheil
4 // Created on: ?
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
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 "Clustal.h"
41 #include <Bpp/Text/TextTools.h>
42 #include <Bpp/Text/StringTokenizer.h>
43 #include <Bpp/Io/FileTools.h>
44 
45 using namespace bpp;
46 
47 // From the STL:
48 #include <iostream>
49 #include <iomanip>
50 using namespace std;
51 
52 void Clustal::appendAlignmentFromStream(std::istream& input, SiteContainer & sc) const throw (Exception)
53 {
54  // Checking the existence of specified file
55  if (!input) { throw IOException ("Clustal::read : fail to open file"); }
56 
57  const Alphabet * alpha = sc.getAlphabet();
58  vector<BasicSequence> sequences;
59 
60  string lineRead("");
61 
62  Comments comments(1);
63  comments[0] = FileTools::getNextLine(input); // First line gives file generator.
64 
65  lineRead = FileTools::getNextLine(input); // This is the first sequence of the first block.
66 
67  string::size_type beginSeq = 0;
68  unsigned int count = 0;
69  for (size_t i = lineRead.size(); i > 0; i--) {
70  char c = lineRead[i-1];
71  if (c == ' ') {
72  count++;
73  if (count == nbSpacesBeforeSeq_) {
74  beginSeq = i - 1 + nbSpacesBeforeSeq_;
75  break;
76  }
77  }
78  else count = 0;
79  }
80  if (beginSeq == 0) throw IOException("Clustal::read. Bad intput file.");
81 
82  unsigned int countSequences = 0;
83 
84  //Read first sequences block:
85  bool test = true;
86  do {
87  sequences.push_back(BasicSequence(TextTools::removeSurroundingWhiteSpaces(lineRead.substr(0, beginSeq - nbSpacesBeforeSeq_)), lineRead.substr(beginSeq), alpha));
88  getline(input, lineRead, '\n');
89  countSequences++;
90  test = !TextTools::isEmpty(lineRead) && !TextTools::isEmpty(lineRead.substr(0, beginSeq - nbSpacesBeforeSeq_));
91  }
92  while (input && test);
93 
94  // Read other blocks
95  lineRead = FileTools::getNextLine(input); // Read first sequence of next block.
96  while (!TextTools::isEmpty(lineRead)) {
97  // Read next block:
98  for (unsigned int i = 0; i < countSequences; ++i) {
99  // Complete sequences
100  if (TextTools::isEmpty(lineRead))
101  throw IOException("Clustal::read. Bad intput file.");
102  sequences[i].append(lineRead.substr(beginSeq));
103  getline(input, lineRead, '\n');
104  }
105  //At this point, lineRead is the first line after the current block.
106  lineRead = FileTools::getNextLine(input);
107  }
108 
109  for (unsigned int i = 0; i < countSequences; ++i)
110  sc.addSequence(sequences[i], checkNames_);
111  sc.setGeneralComments(comments);
112 }
113 
114 void Clustal::writeAlignment(std::ostream& output, const SiteContainer& sc) const throw (Exception)
115 {
116  output << "CLUSTAL W (1.81) multiple sequence alignment" << endl;
117  output << endl;
118  if (sc.getNumberOfSequences() == 0)
119  return;
120 
121  vector<string> text;
122  size_t length = 0;
123  for (size_t i = 0; i < sc.getNumberOfSequences(); ++i ) {
124  const Sequence& seq = sc.getSequence(i);
125  if (seq.getName().size() > length)
126  length = seq.getName().size();
127  text.push_back(sc.getSequence(i).toString());
128  }
129  length += nbSpacesBeforeSeq_;
130  for (unsigned int j = 0; j < text[0].size(); j += charsByLine_) {
131  for (unsigned int i = 0; i < sc.getNumberOfSequences(); ++i ) {
132  output << TextTools::resizeRight(sc.getSequence(i).getName(), length);
133  output << text[i].substr(j, charsByLine_) << endl;
134  }
135  output << endl;
136  }
137 }
138 
std::vector< std::string > Comments
Declaration of Comments type.
Definition: Sequence.h:60
The SiteContainer interface.
Definition: SiteContainer.h:63
This alphabet is used to deal NumericAlphabet.
The Alphabet interface.
Definition: Alphabet.h:130
STL namespace.
virtual const std::string & getName() const =0
Get the name of this sequence.
void appendAlignmentFromStream(std::istream &input, SiteContainer &sc) const
Append sequences to a container from a stream.
Definition: Clustal.cpp:52
A basic implementation of the Sequence interface.
Definition: Sequence.h:207
The sequence interface.
Definition: Sequence.h:74
void writeAlignment(std::ostream &output, const SiteContainer &sc) const
Write a container to a stream.
Definition: Clustal.cpp:114