~ubuntu-branches/ubuntu/oneiric/libbpp-seq/oneiric

« back to all changes in this revision

Viewing changes to src/Bpp/Seq/Io/Phylip.h

  • Committer: Bazaar Package Importer
  • Author(s): Julien Dutheil
  • Date: 2011-06-09 11:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110609110000-hnfrd9it0np58l54
Tags: upstream-2.0.2
Import upstream version 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// File: Phylip.h
 
3
// Created by: Julien Dutheil
 
4
// Created on: Mon Oct 27 12:22:56 2003
 
5
//
 
6
 
 
7
/*
 
8
Copyright or © or Copr. CNRS, (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
#ifndef _PHYLIP_H_
 
41
#define _PHYLIP_H_
 
42
 
 
43
#include "AbstractISequence2.h"
 
44
#include "AbstractOSequence.h"
 
45
#include "../Sequence.h"
 
46
#include "../Container/SequenceContainer.h"
 
47
#include "../Container/VectorSequenceContainer.h"
 
48
#include "../Container/AlignedSequenceContainer.h"
 
49
 
 
50
// From the STL:
 
51
#include <iostream>
 
52
 
 
53
namespace bpp
 
54
{
 
55
 
 
56
/**
 
57
 * @brief The Phylip & co format.
 
58
 *
 
59
 * An AlignedSequenceContainer is used instead of a VectorSequenceContainer.
 
60
 *
 
61
 * This format is described on the Phylip package documentation website:
 
62
 * http://evolution.genetics.washington.edu/phylip/doc/sequence.html
 
63
 */
 
64
class Phylip :
 
65
  public AbstractIAlignment,
 
66
  public AbstractOAlignment
 
67
{
 
68
  private:
 
69
 
 
70
    /* this class allows two kind of Phylip format:
 
71
     * traditional, with names limited to 10 chars,
 
72
     * and 'extended', defined by PAML, with names separated from sequences by at least 6 white spaces.
 
73
     */
 
74
    bool extended_;
 
75
    /* tells if sequences are in the seuqential or the interleave format/
 
76
     */
 
77
    bool sequential_;
 
78
 
 
79
    /**
 
80
     * @brief The maximum number of chars to be written on a line.
 
81
     */
 
82
    unsigned int charsByLine_;
 
83
 
 
84
    bool checkNames_;
 
85
 
 
86
    std::string namesSplit_;
 
87
  
 
88
  public:
 
89
    /**
 
90
     * @brief Build a new Phylip file reader.
 
91
     *
 
92
     * @param extended If true, sequences with names longer than 10 characters are allowed.
 
93
     * @param sequential If false, sequences are supposed to be interlaved.
 
94
     * @param charsByLine The number of base to display in a row.
 
95
     * @param checkSequenceNames Tell if the names in the file should be checked for unicity (slower, in o(n*n) where n is the number of sequences).
 
96
     * @param split The string to use to split sequence name from content (only for 'extended' format). This will typically be "  " (two spaces) or "\t" (a tabulation).
 
97
     */
 
98
    Phylip(bool extended = true, bool sequential = true, unsigned int charsByLine = 100, bool checkSequenceNames = true, const std::string& split = "  "):
 
99
      extended_(extended), sequential_(sequential), charsByLine_(charsByLine), checkNames_(checkSequenceNames), namesSplit_(split) {}
 
100
 
 
101
    virtual ~Phylip() {}
 
102
 
 
103
  public:
 
104
 
 
105
    /**
 
106
     * @name The AbstractISequence2 interface.
 
107
     *
 
108
     * @{
 
109
     */
 
110
    void appendFromStream(std::istream& input, SiteContainer& sc) const throw (Exception);
 
111
    /** @} */
 
112
 
 
113
    /**
 
114
     * @return The number of sequences contained in the specified file.
 
115
     *
 
116
     * This methods parses the firt line of the phylip file.
 
117
     * @param path The path of the file to parse.
 
118
     */
 
119
    unsigned int getNumberOfSequences(const std::string& path) const throw (IOException);
 
120
 
 
121
    /**
 
122
     * @name The OSequence interface.
 
123
     *
 
124
     * @{
 
125
     */
 
126
    void write(std::ostream& output, const SiteContainer& sc) const throw (Exception);
 
127
    void write(const std::string& path, const SiteContainer& sc, bool overwrite) const throw (Exception)
 
128
    {
 
129
      AbstractOAlignment::write(path, sc, overwrite);
 
130
    }
 
131
    /** @} */
 
132
 
 
133
 
 
134
    /**
 
135
     * @name The IOSequence interface.
 
136
     *
 
137
     * @{
 
138
     */
 
139
    const std::string getFormatName() const;
 
140
    const std::string getFormatDescription() const;
 
141
    /** @} */
 
142
 
 
143
    /**
 
144
     * @return true if the names are to be checked when reading sequences from files.
 
145
     */
 
146
    bool checkNames() const { return checkNames_; }
 
147
 
 
148
    /**
 
149
     * @brief Tell whether the sequence names should be checked when reading from files.
 
150
     *
 
151
     * @param yn whether the sequence names should be checked when reading from files.
 
152
     */
 
153
    void checkNames(bool yn) { checkNames_ = yn; }
 
154
 
 
155
    /**
 
156
     * @return The string used to split sequence name from content.
 
157
     */
 
158
    const std::string& getSplit() const { return namesSplit_; }
 
159
 
 
160
    /**
 
161
     * @param split The string to be used to split sequence name from content.
 
162
     */
 
163
    void setSplit(const std::string& split) { namesSplit_ = split; }
 
164
     
 
165
  protected:
 
166
    //Reading tools:
 
167
    const std::vector<std::string> splitNameAndSequence(const std::string& s) const throw (Exception); 
 
168
    void readSequential (std::istream& in, SiteContainer& asc) const throw (Exception);
 
169
    void readInterleaved(std::istream& in, SiteContainer& asc) const throw (Exception);
 
170
    //Writing tools:
 
171
    std::vector<std::string> getSizedNames(const std::vector<std::string>& names) const;
 
172
    void writeSequential (std::ostream& out, const SequenceContainer& sc, int charsByLine) const;
 
173
    void writeInterleaved(std::ostream& out, const SequenceContainer& sc, int charsByLine) const;
 
174
};
 
175
 
 
176
} //end of namespace bpp.
 
177
 
 
178
#endif  //_PHYLIP_H_
 
179