fasta.hpp 2.72 KB
Newer Older
Armin Sobhani's avatar
Armin Sobhani committed
1
2
3
4
5
6
7
8
9
10
11
12
//----------------------------------------------------------------------------//
//  Copyright (c) 2018 Armin Sobhani <arminms@gmail.com>
//
//  Distributed under the Boost Software License Version 1.0.
//  (See accompanying ${file} LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
//----------------------------------------------------------------------------//

#ifndef BOOST_BIOSEQ_IO_FORMATS_FASTA_HPP
#define BOOST_BIOSEQ_IO_FORMATS_FASTA_HPP

13
// #include <iostream>
14
#include <boost/algorithm/string.hpp>
15

Armin Sobhani's avatar
Armin Sobhani committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
namespace boost {
namespace bioseq {

//----------------------------------------------------------------------------//

template
<
    class CharT
,   class Traits
,   class SeqT
>
class fasta_format
:   public io_format_base<CharT,Traits,SeqT>
{
public:
    fasta_format()
    :   io_format_base<CharT,Traits,SeqT>()
    {}

private:
    virtual void do_print(std::basic_ostream<CharT,Traits>& out, const SeqT& s)
37
38
39
40
41
42
43
44
45
46
47
48
    {
        out << '>' << s.long_name();

        for (size_t i = 0; i < s.size(); ++i)
        {
            if (0 == i % 70)
                out << std::endl;
            out << s[i];
        }

        out << std::endl;
    }
49

Armin Sobhani's avatar
Armin Sobhani committed
50
    virtual void do_scan(std::basic_istream<CharT,Traits>& in, SeqT& s)
51
    {
52
        if (in.peek() != 0x3E)  // 0x3E == '>'
53
54
55
56
57
        {
            in.setstate(std::ios::failbit);
            return;
        }

58
59
        s.clear();

60
        std::basic_string<CharT,Traits> line;
61
        line.reserve(200);
62
        std::getline(in, line);
63

64
        scan_name(line, s, has_name_component<SeqT>());
65
        scan_description(line, s, has_description_component<SeqT>());
66

67
        while (in.peek() != 0x3E && std::getline(in, line)) // 0x3E == '>'
68
        {
69
            boost::trim_right(line);
70
            s.insert(s.end(), line.begin(), line.end());
71
        }
72
73

        in.clear();
74
    }
75

76
77
78
79
80
private:
    void scan_name(
        std::basic_string<CharT,Traits>& line
    ,   SeqT& s
    ,   std::true_type) const
81
82
    {   // 0x20 == ' '
        s.name(boost::trim_right_copy(line.substr(1, line.find(0x20))));
83
    }
84
85
86
87
88
    void scan_name(
        std::basic_string<CharT,Traits>& line
    ,   SeqT& s
    ,   std::false_type) const
    {}
89
90
91
92
93
94

    void scan_description(
        std::basic_string<CharT,Traits>& line
    ,   SeqT& s
    ,   std::true_type) const
    {
95
        auto pos = line.find(0x20); // 0x20 == ' '
96
97
98
99
100
101
102
103
        if (pos != line.npos)
            s.description(boost::trim_copy(line.substr(pos)));
    }
    void scan_description(
        std::basic_string<CharT,Traits>& line
    ,   SeqT& s
    ,   std::false_type) const
    {}
Armin Sobhani's avatar
Armin Sobhani committed
104
105
106
107
108
109
110
111
};

//----------------------------------------------------------------------------//

} // end bioseq namespace
} // end boost namespace

#endif  // BOOST_BIOSEQ_IO_FORMATS_FASTA_HPP