File: seq_reader_multiline_fasta_module.cpp

package info (click to toggle)
btllib 1.4.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,340 kB
  • sloc: cpp: 61,642; sh: 301; python: 222; makefile: 6
file content (79 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "btllib/seq_reader.hpp"
#include "btllib/util.hpp"
#include "helpers.hpp"

#include <fstream>
#include <iostream>

int
main()
{
  const char* ids[] = { "asdf", "ghjk" };
  const char* seqs[] = { "ACTG", "TGCA" };
  std::string random_filename;

  for (int iteration = 0; iteration < 3; iteration++) {
    std::cerr << "Iteration " << iteration + 1 << std::endl;

    std::cerr << "Test multiline FASTA" << std::endl;
    btllib::SeqReader reader(btllib::get_dirname(__FILE__) +
                               "/input_multiline.fa",
                             btllib::SeqReader::Flag::SHORT_MODE);

    TEST_ASSERT_EQ(reader.get_format(), btllib::SeqReader::Format::FASTA);

    size_t i = 0;
    for (const auto record : reader) {
      TEST_ASSERT_EQ(record.id, ids[i]);
      TEST_ASSERT_EQ(record.seq, seqs[i]);
      i++;
    }
    TEST_ASSERT_EQ(i, 2);
    reader.close();

    std::cerr << "Test random multiline FASTA file" << std::endl;
    std::vector<std::string> generated_ids;
    std::vector<std::string> generated_comments;
    std::vector<std::string> generated_seqs;
    random_filename = get_random_name(64);
    std::ofstream random_seqs_multiline(random_filename);
    for (int s = 0; s < 500; s++) {
      std::string id, comment_spaces, comment, seq, qual;

      id = get_random_name(10);
      comment_spaces = std::string(get_random(1, 10), ' ');
      comment = get_random_name(20);
      size_t seq_size = get_random(100, 2000);
      seq = get_random_seq(seq_size);

      std::string seq_multiline = split_seq_multiline(seq);
      std::string newline = get_random(0, 1) == 1 ? "\r\n" : "\n";

      random_seqs_multiline << '>' << id << ' ' << comment << newline
                            << seq_multiline << newline;

      generated_ids.push_back(id);
      generated_comments.push_back(comment);
      generated_seqs.push_back(seq);
    }
    random_seqs_multiline.close();
    std::cerr << random_filename << std::endl;

    btllib::SeqReader random_reader(random_filename,
                                    btllib::SeqReader::Flag::SHORT_MODE);
    TEST_ASSERT_EQ(random_reader.get_format(),
                   btllib::SeqReader::Format::FASTA);
    btllib::SeqReader::Record record;
    for (i = 0; (record = random_reader.read()); i++) {
      TEST_ASSERT_EQ(record.id, generated_ids[i]);
      TEST_ASSERT_EQ(record.comment, generated_comments[i]);
      TEST_ASSERT_EQ(record.seq, generated_seqs[i]);
    }
    TEST_ASSERT_EQ(i, 500);

    random_reader.close();
    std::remove(random_filename.c_str());
  }

  return 0;
}