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

Thursday, February 4, 2016

C++ assert

C++ assert

Assert is a great tool for debugging your c++ code.

Let's have look the following codes.

    #include <iostream>
    #include <functional>
    #include <assert.h>

    using namespace std;

        int main()
        {
            int a = 10;
            assert(a >10);

            char b ='a';
            cout << b << endl
        }

         If I compile and run it, I get the following message

         Assertion failed: (a >10), function main, file main.cpp, line 10.
         Abort trap: 6
   
     int main()
    {
          int a = 10;
          assert(a ==10);
          char b ='a';
          cout << b << endl
     }

     If I compile and run it, it will display a in the monitor

As you have guessed, assert helps you test variable.
You need to put a condion for assert.
If the condition is true, nothing is going to happen.
If it is false, assert terminates program, and tell us what the problem is.


If you  create couple hundreds lines of code, I am sure that you would like to test some critical parts of your work. Why don't you use assert?

In my opinion, assert is a good tool for c++ development.


If you disagree or have better tool, please let me know.