~ubuntu-branches/ubuntu/wily/openms/wily

« back to all changes in this revision

Viewing changes to source/FORMAT/FastaIterator.C

  • Committer: Package Import Robot
  • Author(s): Filippo Rusconi
  • Date: 2012-11-12 15:58:12 UTC
  • Revision ID: package-import@ubuntu.com-20121112155812-vr15wtg9b50cuesg
Tags: upstream-1.9.0
ImportĀ upstreamĀ versionĀ 1.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: C++; tab-width: 2; -*-
 
2
// vi: set ts=2:
 
3
//
 
4
// --------------------------------------------------------------------------
 
5
//                   OpenMS Mass Spectrometry Framework
 
6
// --------------------------------------------------------------------------
 
7
//  Copyright (C) 2003-2011 -- Oliver Kohlbacher, Knut Reinert
 
8
//
 
9
//  This library is free software; you can redistribute it and/or
 
10
//  modify it under the terms of the GNU Lesser General Public
 
11
//  License as published by the Free Software Foundation; either
 
12
//  version 2.1 of the License, or (at your option) any later version.
 
13
//
 
14
//  This library is distributed in the hope that it will be useful,
 
15
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
//  Lesser General Public License for more details.
 
18
//
 
19
//  You should have received a copy of the GNU Lesser General Public
 
20
//  License along with this library; if not, write to the Free Software
 
21
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
22
//
 
23
// --------------------------------------------------------------------------
 
24
// $Maintainer: Clemens Groepl, Andreas Bertsch $
 
25
// $Authors: Chris Bauer $
 
26
// --------------------------------------------------------------------------
 
27
 
 
28
 
 
29
#include <OpenMS/FORMAT/FastaIterator.h>
 
30
#include <iostream>
 
31
 
 
32
 
 
33
namespace OpenMS
 
34
{
 
35
 
 
36
typedef std::pair <String, String> FASTAEntry;
 
37
 
 
38
FastaIterator::FastaIterator() :  PepIterator()
 
39
{
 
40
        actual_seq_ = "";
 
41
        is_at_end_ = false;
 
42
        const String ff = "";
 
43
        fasta_file_= ff;
 
44
        input_file_ = new std::ifstream();
 
45
}
 
46
 
 
47
FastaIterator::~FastaIterator()
 
48
{
 
49
        
 
50
}
 
51
 
 
52
FastaIterator::FastaIterator(const FastaIterator & source) : PepIterator(source)
 
53
{
 
54
        is_at_end_ = (source.is_at_end_);
 
55
        input_file_ = (source.input_file_);
 
56
        fasta_file_ = (source.fasta_file_);
 
57
        actual_seq_ =(source.actual_seq_);
 
58
        header_ = (source.header_);
 
59
        last_header_ = (source.last_header_);
 
60
}
 
61
 
 
62
PepIterator * FastaIterator::operator++(int)
 
63
{
 
64
        if (last_header_=="")
 
65
        {
 
66
                throw Exception::InvalidIterator(__FILE__, __LINE__, __PRETTY_FUNCTION__);
 
67
        }
 
68
        PepIterator * old = new FastaIterator (*this);
 
69
        actual_seq_ = next_();
 
70
        return old;
 
71
}
 
72
 
 
73
FASTAEntry FastaIterator::operator*() 
 
74
{
 
75
        if (last_header_=="")
 
76
        {
 
77
                throw Exception::InvalidIterator(__FILE__, __LINE__, __PRETTY_FUNCTION__);
 
78
        }
 
79
        return FASTAEntry (last_header_,actual_seq_);
 
80
}
 
81
 
 
82
PepIterator & FastaIterator::operator++()
 
83
{
 
84
        if (last_header_=="")
 
85
        {
 
86
                throw Exception::InvalidIterator(__FILE__, __LINE__, __PRETTY_FUNCTION__);
 
87
        }
 
88
        actual_seq_ = next_();
 
89
        return *this;   
 
90
}
 
91
 
 
92
void FastaIterator::setFastaFile (const String & f)
 
93
{
 
94
        std::fstream fs;
 
95
        fs.open(f.c_str());
 
96
        if (!fs.is_open())
 
97
        {
 
98
                throw Exception::FileNotFound(__FILE__, __LINE__, __PRETTY_FUNCTION__, f);
 
99
        }
 
100
        fasta_file_ = f;
 
101
}
 
102
 
 
103
String FastaIterator::getFastaFile()
 
104
{
 
105
        return (fasta_file_);
 
106
}
 
107
 
 
108
std::string FastaIterator::next_()
 
109
{
 
110
        if (input_file_->eof())
 
111
        {
 
112
                is_at_end_ = true;
 
113
                return ("");
 
114
        }
 
115
        std::string line;
 
116
        std::getline(*input_file_, line);
 
117
        if (line[0] == '>' || input_file_->eof())
 
118
        {
 
119
                last_header_ = header_;
 
120
                header_ = line;
 
121
                return ("");
 
122
        }
 
123
        return (std::string(line)+next_());
 
124
}
 
125
        
 
126
bool FastaIterator::begin()
 
127
{
 
128
        if (fasta_file_=="")
 
129
        {
 
130
                throw Exception::InvalidIterator(__FILE__, __LINE__, __PRETTY_FUNCTION__);
 
131
        }
 
132
        input_file_->open(fasta_file_.c_str());
 
133
        
 
134
        if (*input_file_)
 
135
        {
 
136
                std::string line;
 
137
                std::getline(*input_file_,line);
 
138
                header_ = line;
 
139
                last_header_ = line;
 
140
                actual_seq_ = next_();
 
141
                return (true);
 
142
        }
 
143
        
 
144
        return (false);
 
145
        
 
146
}
 
147
        
 
148
bool FastaIterator::isAtEnd ()
 
149
{
 
150
        return (is_at_end_);    
 
151
}
 
152
 
 
153
} //namespace OpenMS