bpp-seq3  3.0.0
GenBank.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
6 #include <Bpp/Text/TextTools.h>
7 
8 #include "GenBank.h"
9 
10 using namespace bpp;
11 using namespace std;
12 
13 /****************************************************************************************/
14 
16 {
17  if (!input)
18  {
19  throw IOException ("GenBank::read: fail to open file");
20  }
21 
22  string temp, name, sequence = ""; // Initialization
23  auto alphaPtr = vsc.getAlphabet();
24 
25  // Main loop : for all file lines
26  while (!input.eof())
27  {
28  getline(input, temp, '\n'); // Copy current line in temporary string
29 
30  if (temp.size() >= 9 && temp.substr(0, 9) == "ACCESSION")
31  {
32  name = TextTools::removeSurroundingWhiteSpaces(temp.substr(10));
33  StringTokenizer st(name, " ");
34  name = st.nextToken();
35  // cout << name << endl;
36  }
37  if (temp.size() >= 6 && temp.substr(0, 6) == "ORIGIN")
38  {
39  sequence = "";
40  getline(input, temp, '\n'); // Copy current line in temporary string
41  while (!input.eof() && temp.size() > 2 && temp.substr(0, 2) != "//")
42  {
43  sequence += TextTools::removeWhiteSpaces(temp.substr(10));
44  getline(input, temp, '\n'); // Copy current line in temporary string
45  }
46  if (name == "")
47  throw Exception("GenBank::read(). Sequence with no ACCESSION number!");
48  auto seq = make_unique<Sequence>(name, sequence, alphaPtr);
49  vsc.addSequence(seq->getName(), seq);
50  name = "";
51  }
52  }
53 }
54 
55 /****************************************************************************************/
void appendSequencesFromStream(std::istream &input, SequenceContainerInterface &sc) const
Append sequences to a container from a stream.
Definition: GenBank.cpp:15
const std::string & nextToken()
The SequenceContainer interface.
virtual void addSequence(const HashType &sequenceKey, std::unique_ptr< SequenceType > &sequencePtr)=0
Add a sequence to the container.
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get a pointer toward the container's alphabet.
std::string removeWhiteSpaces(const std::string &s)
std::string removeSurroundingWhiteSpaces(const std::string &s)
This alphabet is used to deal NumericAlphabet.