Thursday, February 11, 2016

C++ kseq

C++ kseq.h

(I assume you are using ubuntu)
kseq.h is a c++ tool that helps you read fastq/fasta file line by line.


If you go to https://github.com/samtools/htslib/tree/develop/htslib , You can download kseq.h

place kseq.h  in same directory as your c++ program.

In order to run kseq, you need to intall zlib for your c++ compiler.
Tpe following command in your terminal

sudo apt-get install zlib1g-dev

 

This command will install zlib for your compiler.

I will give you a sample program that read fastq/a file.
After giving you a sample code, I will tell you how to compile your code.

#include <iostream>

// include kseq.h
#include "kseq.h"
// include zlib.h
#include "/home/tom/software/zlib-1.2.8/zlib.h" // download zlib (http://www.zlib.net/)
                                                                             // extract the downloaded file and find the zlib.h
                                                                              // get full path (the command you did for zlib if for your compiler only, so you have to do this)

//kseq is initialized by zlib
KSEQ_INIT(gzFile, gzread)

using namespace std;

int main()
{
     // your fastq/a file has to be read by zlib
     gzFile fp;
     char * input_fastq = "path to your fastq file";
     fp = gzopen(input_fastq, "r");

     //  initializing fastq/a stream
    kseq_t *seq;
    seq= kseq_init(fp);

    // read fastq/a
    while( kseq_read(seq) >=0){
        //printing id
        cout << seq->name.s << endl;

       //printing seq
       cout << seq->seq.s << endl;

       // if it is fastq
       //printing comment
       if (seq->comment.l) cout<< seq->comment.s;

       // if it is fastq
       // printing quality
       if (seq->qual.l) cout <<seq->qual.s;  

    }

     // clean up
     kseq_destroy(seq);
     gzclose(fp);

}


The below command is compiling your program. Make sure you add "-lz"

g++ your_program.cpp -lz

 

If you have a concern or question, feel free to ask.
Thank you

No comments:

Post a Comment