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

« back to all changes in this revision

Viewing changes to src/Bpp/Seq/Io/Mase.cpp

  • 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 Mase.cpp
 
3
// Author : Guillaume Deuchst
 
4
//          Julien Dutheil
 
5
// Last modification : Tuesday August 21 2003
 
6
//
 
7
 
 
8
/*
 
9
Copyright or © or Copr. CNRS, (November 17, 2004)
 
10
 
 
11
This software is a computer program whose purpose is to provide classes
 
12
for sequences analysis.
 
13
 
 
14
This software is governed by the CeCILL  license under French law and
 
15
abiding by the rules of distribution of free software.  You can  use, 
 
16
modify and/ or redistribute the software under the terms of the CeCILL
 
17
license as circulated by CEA, CNRS and INRIA at the following URL
 
18
"http://www.cecill.info". 
 
19
 
 
20
As a counterpart to the access to the source code and  rights to copy,
 
21
modify and redistribute granted by the license, users are provided only
 
22
with a limited warranty  and the software's author,  the holder of the
 
23
economic rights,  and the successive licensors  have only  limited
 
24
liability. 
 
25
 
 
26
In this respect, the user's attention is drawn to the risks associated
 
27
with loading,  using,  modifying and/or developing or reproducing the
 
28
software by the user in light of its specific status of free software,
 
29
that may mean  that it is complicated to manipulate,  and  that  also
 
30
therefore means  that it is reserved for developers  and  experienced
 
31
professionals having in-depth computer knowledge. Users are therefore
 
32
encouraged to load and test the software's suitability as regards their
 
33
requirements in conditions enabling the security of their systems and/or 
 
34
data to be ensured and,  more generally, to use and operate it in the 
 
35
same conditions as regards security. 
 
36
 
 
37
The fact that you are presently reading this means that you have had
 
38
knowledge of the CeCILL license and that you accept its terms.
 
39
*/
 
40
 
 
41
#include "Mase.h"
 
42
#include "../StringSequenceTools.h"
 
43
 
 
44
using namespace bpp;
 
45
using namespace std;
 
46
 
 
47
/****************************************************************************************/
 
48
 
 
49
void Mase::appendFromStream(std::istream& input, SequenceContainer& vsc) const throw (Exception)
 
50
{
 
51
        if (!input) { throw IOException ("Mase::read : fail to open file"); }
 
52
        
 
53
        // Initialization
 
54
        Comments seqComments, fileComments;
 
55
        string temp, name, sequence = "";
 
56
        bool comments = false;
 
57
 
 
58
        // Get current general comments is VectorSequenceContainer
 
59
        fileComments = vsc.getGeneralComments();
 
60
 
 
61
        // Main loop : for all file lines
 
62
        while (!input.eof())
 
63
  {
 
64
                getline(input, temp, '\n');  // Copy current line in temporary string
 
65
                
 
66
                // If first character is ;
 
67
                if (temp[0] == ';')
 
68
    {
 
69
                        // If second character is also ;
 
70
                        if (temp[1] == ';')
 
71
      {
 
72
                                // File comments isolation
 
73
                                temp.erase(0,2);  // Characters ;; deletion
 
74
                                if(temp != "") fileComments.push_back(temp);
 
75
                        }
 
76
      else
 
77
      {
 
78
                                // If a name and a sequence were founded
 
79
                                if ((name != "") && (sequence != ""))
 
80
        {
 
81
                                        // New sequence creation, and addition in existing VectorSequenceContainer
 
82
                                        vsc.addSequence(BasicSequence(name, sequence, seqComments, vsc.getAlphabet()), checkNames_);
 
83
                                        name = "";
 
84
                                        sequence = "";
 
85
                                        seqComments.clear();
 
86
                                }
 
87
                                
 
88
                                // Sequence commentaries isolation
 
89
                                temp.erase(temp.begin());  // Character ; deletion
 
90
                                if (temp != "") seqComments.push_back(temp);
 
91
                                comments = true;
 
92
                        }
 
93
                }
 
94
    else
 
95
    {
 
96
                        // If sequence commentaries were just isolated
 
97
                        if (comments)
 
98
      {
 
99
                                // Sequence name isolation
 
100
                                name = temp;
 
101
                                comments = false;
 
102
                        }
 
103
      else sequence += temp;  // Sequence isolation
 
104
                }
 
105
        }
 
106
        
 
107
        // Addition of the last sequence in file
 
108
        if ((name != "") && (sequence != ""))
 
109
  {
 
110
                vsc.addSequence(BasicSequence(name, sequence, seqComments, vsc.getAlphabet()), checkNames_);
 
111
        }
 
112
 
 
113
        // Set new general comments in VectorSequenceContainer (old + new comments)
 
114
        vsc.setGeneralComments(fileComments);
 
115
}
 
116
 
 
117
/****************************************************************************************/
 
118
 
 
119
void Mase::write(ostream& output, const SequenceContainer& sc) const throw (Exception)
 
120
{
 
121
        // Checking the existence of specified file, and possibility to open it in write mode
 
122
        if (!output) { throw IOException ("Mase::write : failed to open file"); }
 
123
 
 
124
        Comments comments = sc.getGeneralComments();
 
125
 
 
126
        // Writing all general commentaries in file
 
127
        for (unsigned int i = 0 ; i < comments.size() ; i++)
 
128
  {
 
129
                output << ";;" << comments[i] << endl;
 
130
        }
 
131
 
 
132
        string seq, temp = "";  // Initialization
 
133
 
 
134
        // Main loop : for all sequences
 
135
        vector<string> names = sc.getSequencesNames();
 
136
        for (unsigned int i = 0 ; i < names.size() ; i ++)
 
137
  {
 
138
                comments = sc.getComments(names[i]);
 
139
 
 
140
                // Writing all sequence comments in file
 
141
                // If no comments are associated with current sequence, an empy commentary line will be writed
 
142
                if (comments.size() == 0)
 
143
    {
 
144
                        output << ";" << endl;
 
145
                }
 
146
    else
 
147
    {
 
148
                        for (unsigned int j = 0 ; j < comments.size() ; j++)
 
149
      {
 
150
                                output << ";" << comments[j] << endl;
 
151
                        }
 
152
                }
 
153
 
 
154
                // Sequence name writing
 
155
                output << names[i] << endl;
 
156
 
 
157
                // Sequence cutting to specified characters number per line
 
158
                seq = sc.toString(names[i]);
 
159
                while (seq != "")
 
160
    {
 
161
                        if (seq.size() > charsByLine_)
 
162
      {
 
163
                                temp = seq;
 
164
                                temp.erase(temp.begin() + charsByLine_ , temp.end());
 
165
                                output << temp  << endl;
 
166
                                seq.erase(seq.begin(), seq.begin() + charsByLine_);
 
167
                        }
 
168
                        else
 
169
      {
 
170
                                output << seq << endl;
 
171
                                seq = "";
 
172
                        }
 
173
                }
 
174
        }
 
175
}
 
176
 
 
177
/****************************************************************************************/
 
178