File: FastqReader.cpp

package info (click to toggle)
libseqlib 1.1.2+dfsg-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 1,460 kB
  • sloc: cpp: 7,176; sh: 805; makefile: 60
file content (59 lines) | stat: -rw-r--r-- 1,200 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "SeqLib/FastqReader.h"

#include <cctype>
#include <algorithm>

namespace SeqLib {

  bool FastqReader::Open(const std::string& f) {
    
    m_file = f;

    // check if file exists
    struct stat buffer;   
    if (stat(m_file.c_str(), &buffer) != 0) {
      std::cerr << "FastqReader: Failed to read non-existant file " << m_file << std::endl;
      return false;
    }

    fp = NULL;
    fp = (m_file != "-") ? gzopen(m_file.c_str(), "r") : gzdopen(fileno(stdin), "r");

    if (!fp) {
      std::cerr << "FastqReader: Failed to read " << m_file << std::endl;
      return false;
    }

    seq = kseq_init(fp); // set to first seq

    return true;
    
  }

  FastqReader::FastqReader(const std::string& file) : m_file(file) {
    Open(m_file);
  }

bool FastqReader::GetNextSequence(UnalignedSequence& s) {

  // kseq_read parses fastq and fasta

  if (!fp || !seq)
    return false;

  // no more reads
  if (kseq_read(seq) < 0)
    return false;

  if (seq->name.s)
    s.Name = std::string(seq->name.s, seq->name.l);
  if (seq->seq.s)
    s.Seq = std::string(seq->seq.s, seq->seq.l);
  if (seq->qual.s)
    s.Qual = std::string(seq->qual.s, seq->qual.l);
  
  return true;

}

}