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.

Saturday, January 30, 2016

c++ preprocessor

c preprocessor

When you work on c++ programming, we always start with #include<.....>

Like following

#include <iostream>
#include <string>

int main()
{
}

or if you look at someone else's coding, you have seen the following..

#define PI 3.14

#ifndef OBJECT_H_
     #define OBJECT_H_
#endif

These lines starting with # are preprocessor command.

Before I go further about these commands, let's talk about compiler little bit.

A compiler is not smart and does not understand C++ coding. C++ coding has to be translated into machine language(which is combination of 1 and 0). Compiler does the translation.

Before a compiler translates your c++ code, your code has be processed by a c preprocessor.
You can think the c preprocessor as a text replacer.


Let's look at the following c++ line.

       #include <iostream>
       #define PI 3.14

       using namespace std;

       int main()
      {
               cout << "hello world" ;
               cout << PI ;
      }

The above is very a simple piece of code.
It will print "hello world" and 3.14 in the monitor.
"cout" and "<<" are used to print "hello world"and 3.14.

#include <iostream> is a preprocessor command that add c++ standard library in you code

When a compiler to compile your c++ code, it does not compile the code right away.

Firstly, c++ preprocessor changes your code.

     #include <iostream>
     #define PI 3.14

     using namespace std;
 
     int main()
     {
         { system.out ~~~~~~~}   (operator~~~~~~~`) " hello world" ;
         { system.out ~~~~~~~}   (operator~~~~~~~`) 3.14
     }

strings "cout" and "<<" does not do anything. preprocessor reads these pieces and convert them to meaningful instances.
Whenever, preprocessor find PI, it will change PI to 3.14

Once preprocessor finish changing all instances, compiler compiles your code.


Think a c preprocessor as a tool that helps a compiler to compile your code.

The following is the actual compiling process.

c++ code ----> processed with  c++preprocessor  ----> compiled with compiler -------> executable.


If you think I misunderstand the concept, please feel free to talk to me or leave a comment.