libpappsomspp
Library for mass spectrometry
pappso::FastaFileIndexer Class Reference

#include <fastafileindexer.h>

Inheritance diagram for pappso::FastaFileIndexer:
pappso::FastaFileIndexerInterface

Public Member Functions

 FastaFileIndexer (const QFileInfo &fastaFile)
 
 FastaFileIndexer (const FastaFileIndexer &other)
 
virtual ~FastaFileIndexer ()
 
void getSequenceByIndex (FastaHandlerInterface &fasta_handler, std::size_t index) override
 
void open () override
 
void close () override
 
FastaFileIndexerSPtr makeFastaFileIndexerSPtr () const
 

Private Member Functions

void parseFastaFile ()
 

Private Attributes

QFile m_fasta_file
 
std::vector< qint64 > m_indexArray
 
QTextStream * mpa_sequenceTxtIn = nullptr
 

Detailed Description

Definition at line 51 of file fastafileindexer.h.

Constructor & Destructor Documentation

◆ FastaFileIndexer() [1/2]

pappso::FastaFileIndexer::FastaFileIndexer ( const QFileInfo &  fastaFile)

Definition at line 39 of file fastafileindexer.cpp.

40  : m_fasta_file(fastaFile.absoluteFilePath())
41 {
42 
43  if(m_fasta_file.fileName().isEmpty())
44  {
45  throw PappsoException(QObject::tr("No FASTA file name specified"));
46  }
47  if(m_fasta_file.open(QIODevice::ReadOnly))
48  {
50  m_fasta_file.close();
51  }
52  else
53  {
54  throw PappsoException(QObject::tr("ERROR opening FASTA file %1 for read")
55  .arg(fastaFile.fileName()));
56  }
57 }

References m_fasta_file, and parseFastaFile().

◆ FastaFileIndexer() [2/2]

pappso::FastaFileIndexer::FastaFileIndexer ( const FastaFileIndexer other)

Definition at line 59 of file fastafileindexer.cpp.

60  : m_fasta_file(other.m_fasta_file.fileName())
61 {
62 
63  m_indexArray = other.m_indexArray;
64  mpa_sequenceTxtIn = nullptr;
65 }
QTextStream * mpa_sequenceTxtIn
std::vector< qint64 > m_indexArray

References m_indexArray, and mpa_sequenceTxtIn.

◆ ~FastaFileIndexer()

pappso::FastaFileIndexer::~FastaFileIndexer ( )
virtual

Definition at line 66 of file fastafileindexer.cpp.

67 {
68  close();
69 }

References close().

Member Function Documentation

◆ close()

void pappso::FastaFileIndexer::close ( )
overridevirtual

Implements pappso::FastaFileIndexerInterface.

Definition at line 138 of file fastafileindexer.cpp.

139 {
140  if(mpa_sequenceTxtIn != nullptr)
141  {
142  delete mpa_sequenceTxtIn;
143  mpa_sequenceTxtIn = nullptr;
144  m_fasta_file.close();
145  }
146 }

References m_fasta_file, and mpa_sequenceTxtIn.

Referenced by ~FastaFileIndexer().

◆ getSequenceByIndex()

void pappso::FastaFileIndexer::getSequenceByIndex ( FastaHandlerInterface fasta_handler,
std::size_t  index 
)
overridevirtual

Implements pappso::FastaFileIndexerInterface.

Definition at line 149 of file fastafileindexer.cpp.

151 {
152  open();
153 
154  qDebug() << " goto=" << index << " pos=" << m_indexArray[index];
155  bool seek_ok;
156  if((index < m_indexArray.size()) &&
157  (seek_ok = mpa_sequenceTxtIn->seek(m_indexArray[index])))
158  {
159 
160  qDebug() << " realpos=" << mpa_sequenceTxtIn->pos();
161  ;
162  if(!seek_ok)
163  {
164 
165  throw PappsoException(QObject::tr("ERROR FastaFileIndexer : seek to "
166  "sequence %1, position %2 failed")
167  .arg(index)
168  .arg(m_indexArray[index]));
169  }
170  FastaReader reader(fasta_handler);
171  reader.parseOnlyOne(*mpa_sequenceTxtIn);
172  }
173  else
174  {
175  throw ExceptionOutOfRange(
176  QObject::tr("ERROR reading FASTA file %1 : sequence index %2 "
177  "unreachable, array size=%3")
178  .arg(m_fasta_file.fileName())
179  .arg(index)
180  .arg(m_indexArray.size()));
181  }
182 }

References m_fasta_file, m_indexArray, mpa_sequenceTxtIn, open(), and pappso::FastaReader::parseOnlyOne().

◆ makeFastaFileIndexerSPtr()

FastaFileIndexerSPtr pappso::FastaFileIndexer::makeFastaFileIndexerSPtr ( ) const

Definition at line 186 of file fastafileindexer.cpp.

187 {
188 
189  return std::make_shared<FastaFileIndexer>(*this);
190 }

◆ open()

void pappso::FastaFileIndexer::open ( )
overridevirtual

Implements pappso::FastaFileIndexerInterface.

Definition at line 122 of file fastafileindexer.cpp.

123 {
124  if(mpa_sequenceTxtIn != nullptr)
125  return;
126  if(m_fasta_file.open(QIODevice::ReadOnly))
127  {
128  mpa_sequenceTxtIn = new QTextStream(&m_fasta_file);
129  }
130  else
131  {
132  throw PappsoException(QObject::tr("ERROR opening FASTA file %1 for read")
133  .arg(m_fasta_file.fileName()));
134  }
135 }

References m_fasta_file, and mpa_sequenceTxtIn.

Referenced by getSequenceByIndex().

◆ parseFastaFile()

void pappso::FastaFileIndexer::parseFastaFile ( )
private

Definition at line 73 of file fastafileindexer.cpp.

74 {
75 
76  qDebug();
77  QDataStream bin_in(&m_fasta_file);
78  qint64 position = 0;
79 
80  // QChar first_char;
81  // txt_in >> first_char;
82  qint8 char_in;
83  bin_in >> char_in;
84  while(!bin_in.atEnd() && (char_in < (qint8)21))
85  { // eat Windows \r\n
86  position++;
87  bin_in >> char_in;
88  }
89  while(!bin_in.atEnd())
90  {
91  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
92  // << " first_char=" << first_char;
93  if(char_in == (qint8)'>')
94  {
95 
96  // qDebug() << __FILE__ << " " << __FUNCTION__ << " " << __LINE__
97  // << " index=" << m_indexArray.size()
98  // << " position=" << position;
99  m_indexArray.push_back(position);
100  }
101  // eat line
102  position++;
103  bin_in >> char_in;
104  while(!bin_in.atEnd() && (char_in > (qint8)20))
105  {
106  position++;
107  bin_in >> char_in;
108  }
109  position++;
110  bin_in >> char_in;
111 
112  if(!bin_in.atEnd() && (char_in < (qint8)21))
113  { // eat Windows \r\n
114  position++;
115  bin_in >> char_in;
116  }
117  }
118  qDebug();
119 }

References m_fasta_file, and m_indexArray.

Referenced by FastaFileIndexer().

Member Data Documentation

◆ m_fasta_file

QFile pappso::FastaFileIndexer::m_fasta_file
private

Definition at line 71 of file fastafileindexer.h.

Referenced by FastaFileIndexer(), close(), getSequenceByIndex(), open(), and parseFastaFile().

◆ m_indexArray

std::vector<qint64> pappso::FastaFileIndexer::m_indexArray
private

Definition at line 72 of file fastafileindexer.h.

Referenced by FastaFileIndexer(), getSequenceByIndex(), and parseFastaFile().

◆ mpa_sequenceTxtIn

QTextStream* pappso::FastaFileIndexer::mpa_sequenceTxtIn = nullptr
private

Definition at line 73 of file fastafileindexer.h.

Referenced by FastaFileIndexer(), close(), getSequenceByIndex(), and open().


The documentation for this class was generated from the following files: