libpappsomspp
Library for mass spectrometry
fastareader.cpp
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * Copyright (c) 2015 Olivier Langella <Olivier.Langella@u-psud.fr>.
4  *
5  * This file is part of the PAPPSOms++ library.
6  *
7  * PAPPSOms++ is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * PAPPSOms++ is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with PAPPSOms++. If not, see <http://www.gnu.org/licenses/>.
19  *
20  ******************************************************************************/
21 
22 #include <QDebug>
23 #include <QTextStream>
24 #include "fastareader.h"
25 #include "../pappsoexception.h"
26 
27 namespace pappso
28 {
29 FastaReader::FastaReader(FastaHandlerInterface &handler) : m_handler(handler)
30 {
31 }
32 
34 {
35 }
36 
37 
38 void
39 FastaReader::parse(QFile &fastaFile)
40 {
41  if(fastaFile.fileName().isEmpty())
42  {
43  throw PappsoException(QObject::tr("No FASTA file name specified"));
44  }
45  if(fastaFile.open(QIODevice::ReadOnly))
46  {
47  parse(&fastaFile);
48  fastaFile.close();
49  }
50  else
51  {
52  throw PappsoException(QObject::tr("ERROR opening FASTA file %1 for read")
53  .arg(fastaFile.fileName()));
54  }
55 }
56 
57 void
58 FastaReader::parse(QIODevice *p_inputstream)
59 {
60 
61  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
62  QTextStream p_in(p_inputstream);
63 
64 
65  QString accession = "";
66  QString sequence = "";
67  // Search accession conta
68  // QTextStream in(p_in);
69  QString line = p_in.readLine();
70  while(!p_in.atEnd())
71  {
72  if(line.startsWith(">"))
73  {
74  if(!accession.isEmpty())
75  {
76  m_handler.setSequence(accession, sequence);
77  }
78  sequence = "";
79  accession = line.remove(0, 1);
80  }
81  else
82  {
83  sequence.append(line);
84  // m_handler.setSequence(line);
85  }
86  line = p_in.readLine();
87  }
88  if(!accession.isEmpty())
89  {
90  sequence.append(line);
91  m_handler.setSequence(accession, sequence);
92  }
93  // p_in->close();
94  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
95 }
96 
97 
98 void
99 FastaReader::parseOnlyOne(QTextStream &p_in)
100 {
101 
102 
103  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
104 
105 
106  QString accession = "";
107  QString sequence = "";
108  // Search accession conta
109  // QTextStream in(p_in);
110  QString line = p_in.readLine();
111  while(!p_in.atEnd())
112  {
113  if(line.startsWith(">"))
114  {
115  if(!accession.isEmpty())
116  {
117  m_handler.setSequence(accession, sequence);
118  return;
119  }
120  sequence = "";
121  accession = line.remove(0, 1);
122  }
123  else
124  {
125  sequence.append(line);
126  // m_handler.setSequence(line);
127  }
128  line = p_in.readLine();
129  }
130  if(!accession.isEmpty())
131  {
132  sequence.append(line);
133  m_handler.setSequence(accession, sequence);
134  }
135  qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__;
136 }
137 
138 } // namespace pappso
virtual void setSequence(const QString &description, const QString &sequence)=0
void parse(QFile &fastaFile)
Definition: fastareader.cpp:39
FastaReader(FastaHandlerInterface &handler)
Definition: fastareader.cpp:29
FastaHandlerInterface & m_handler
Definition: fastareader.h:48
void parseOnlyOne(QTextStream &p_in)
Definition: fastareader.cpp:99
tries to keep as much as possible monoisotopes, removing any possible C13 peaks and changes multichar...
Definition: aa.cpp:39