File: fai_index_example.cpp

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (48 lines) | stat: -rw-r--r-- 1,217 bytes parent folder | download | duplicates (2)
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
#include <seqan/basic.h>
#include <seqan/seq_io.h>
#include <seqan/sequence.h>

using namespace seqan2;

int main()
{
    CharString path = getAbsolutePath("demos/dox/seq_io/example.fa");

    FaiIndex faiIndex;

    // Try to read the FAI index.
    if (!open(faiIndex, toCString(path)))
    {
        std::cerr << "Could not read the FAI index.  Not fatal, we can just build it.\n";
        return 1;
    }

    // Try to build the FAI index (in memory) if reading was unsuccessful.  If
    // building into memory succeeded, we try to write it out.
    if (!build(faiIndex, toCString(path)))
    {
        std::cerr << "FATAL: Could not build FAI index.\n";
        return 1;
    }

    if (!save(faiIndex))
    {
        std::cerr << "FATAL: Could not write out FAI index after building.\n";
        return 1;
    }

    // Now, read the first 1000 characters of chr1.
    unsigned idx = 0;
    if (!getIdByName(idx, faiIndex, "chr"))
    {
        std::cerr << "FATAL: chr1 not found in FAI index.\n";
        return 1;
    }
    CharString seq;
    readRegion(seq, faiIndex, idx, 0, 100);

    // Now print the first 100 characters we just read.
    std::cout << "chr:1-100 = " << seq << "\n";

    return 0;
}