diff --git a/COPYING b/COPYING
index 54c4590d27e25f6788125288cc57af17d2c9c1d1..c0fae431a6a476ad683bf9f26c93a59e97671911 100644
--- a/COPYING
+++ b/COPYING
@@ -1 +1,340 @@
-<Place your desired license here.>
\ No newline at end of file
+      GNU GENERAL PUBLIC LICENSE
+         Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+       Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+      GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+       NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+       END OF TERMS AND CONDITIONS
+
+     How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year  name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/README b/README
index 3c010f078155648e9c96753a0ef210edd84aee9d..91d183ba58584d5755d57109842f658642591ae5 100644
--- a/README
+++ b/README
@@ -1 +1,27 @@
-Sample readme file for rock project.
+ROCK
+
+ROCK stands for "Reducing Over Covering Kmers".
+It is designed to take as input several fastq files (PE as well as single) and process them efficiently (memory usage and execution time).
+
+Inside ROCK, reads are filtered by a count min sketch.
+
+What makes ROCK different from other read filtering tools is that reads are processed by decreasing order of quality scores. Ie reads with the highest quality score are sure to be selected by the filter and those with low quality score have more chances to be discarded.
+
+ROCK provides different options for parameterizing the count min sketch. They are detailed in the man documentation.
+
+ROCK compiles with gcc 4.4.7 and clang 503.0.40. 
+It has been tested under OS X 10.9 and linux CentOS 6.7 and should run on other systems of the unix family.
+
+For distribuability reasons, choice has been made to develop in C++ 98 and make ROCK compatible with C++ 11. 
+
+You can compile/install ROCK by rectly running configure :
+
+./configure
+make check
+make
+make install
+
+Any remark/suggestion/problem should be reported to <vlegrand@pasteur.fr>.
+
+
+
diff --git a/src/Makefile.am b/src/Makefile.am
index d1ebc4d41887a87eb6d569d8a9f243fba6b83705..8176ea400dbac74fb73c19791aef892be93c1516 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,7 +3,7 @@ LINTDEFS = $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS)
 AM_CPPFLAGS = -DDATADIR=\"$(pkgdatadir)\"
 
 bin_PROGRAMS=rock
-noinst_PROGRAMS = unit_test_fqreader perf_test_fqreader unit_test_read_utils
+noinst_PROGRAMS = unit_test_fqreader unit_test_read_utils unit_test_cms unit_test_fqwriter unit_test_main_utils
 ## noinst_PROGRAMS = 
 
 noinst_LIBRARIES = librock.a
@@ -15,13 +15,19 @@ rock_LDADD = librock.a
 unit_test_fqreader_SOURCES=unit_test_fqreader.cpp
 unit_test_fqreader_LDADD=librock.a
 
-perf_test_fqreader_SOURCES=perf_test_fqreader.cpp
-perf_test_fqreader_LDADD=librock.a
-
 unit_test_read_utils_SOURCES=unit_test_read_utils.cpp
 unit_test_read_utils_LDADD=librock.a
 
+unit_test_cms_SOURCES=unit_test_cms.cpp
+unit_test_cms_LDADD=librock.a
+
+unit_test_fqwriter_SOURCES=unit_test_fqwriter.cpp
+unit_test_fqwriter_LDADD=librock.a
+
+unit_test_main_utils_SOURCES=unit_test_main_utils.cpp
+unit_test_main_utils_LDADD=librock.a
+
 librock_a_SOURCES = $(SRC)
 
-SRC = fqreader.cpp FqBaseBackend.cpp FqAuxBackend.cpp FqMainBackend.cpp read_utils.cpp unit_test_read_utils.cpp
-HDR = srp.h fqreader.h FqConstants.h FqBaseBackend.h FqAuxBackend.h FqMainBackend.h rock_types.h read_utils.h
\ No newline at end of file
+SRC = fqreader.cpp FqBaseBackend.cpp FqAuxBackend.cpp FqMainBackend.cpp read_utils.cpp ReadProcessor.cpp fqwriter.cpp main_utils.cpp ROCKparams.cpp
+HDR = srp.h fqreader.h FqConstants.h FqBaseBackend.h FqAuxBackend.h FqMainBackend.h ReadProcessor.h rock_commons.h read_utils.h CountMinSketch.hpp Filter.hpp fqwriter.h main_utils.h ROCKparams.h
diff --git a/src/ReadProcessor.cpp b/src/ReadProcessor.cpp
index 37297b6aa287fe96dd96e121edf2a8bfcfe18426..85f761b2f95da1767d917f9465e3cf26470b2094 100644
--- a/src/ReadProcessor.cpp
+++ b/src/ReadProcessor.cpp
@@ -25,7 +25,7 @@ inline void ReadProcessor::init_mask(int k) {
     mask_kMer=mask_kMer-1;
 }*/
 
-inline unsigned long ReadProcessor::nucleoToNumberWrap(char s) {
+unsigned long ReadProcessor::nucleoToNumberWrap(char s) {
     if (s=='N') n=k;
     n--;
     return nucleoToNumber(s);
@@ -59,17 +59,20 @@ inline unsigned long ReadProcessor::nucleoToNumber(char s) {
     // n--;
     return nbr;
 }*/
+#include <iostream>
 
-inline unsigned long ReadProcessor::kMerToNumber(char * k_m,unsigned long * p_prev) {
+unsigned long ReadProcessor::kMerToNumber(char * k_m,unsigned long * p_prev) {
     unsigned long nbr=0;
     unsigned long c;
     if (p_prev==NULL) { // first k_mer conversion
         int i;
         for (i=0;i<k;i++) {
+            //std::cout<<k_m[i];
             c=nucleoToNumberWrap(k_m[i]); // do not catch exception for the moment (not until I have checked it doesn't slow down execution). If nucleoToNumber returns -1, program will simply crash
             nbr=nbr<<2;
             nbr=nbr|c;
         }
+        //std::cout<<std::endl;
     } else {
         nbr=*p_prev;
         c=nucleoToNumberWrap(k_m[k-1]);
@@ -89,7 +92,7 @@ inline unsigned long ReadProcessor::nucleoToNumberReverse(char s,int i) {
     return nbr;
 }*/
 
-inline unsigned long ReadProcessor::kMerToNumberReverse(char * k_m,unsigned long * p_prev) {
+unsigned long ReadProcessor::kMerToNumberReverse(char * k_m,unsigned long * p_prev) {
     unsigned long nbr=0;
     unsigned long c;
     if (p_prev==NULL) { // first k_mer conversion
@@ -109,8 +112,6 @@ inline unsigned long ReadProcessor::kMerToNumberReverse(char * k_m,unsigned long
 }
 
 void ReadProcessor::getKMerNumbers(char * dnaStr,int l,std::vector<unsigned long>& my_vect) { // See simple_test.cpp and results. benchmark showed that std::vector is very slightly faster than C array and doesn't require more memory in our case. So, I am using it since it makes code simpler.
-    /*std::vector<unsigned long> my_vect;
-    return my_vect;*/
     int i;
     unsigned long num;
     unsigned long num_rev;
@@ -122,8 +123,7 @@ void ReadProcessor::getKMerNumbers(char * dnaStr,int l,std::vector<unsigned long
         num=kMerToNumber(p_char,p_prev);
         num_rev=kMerToNumberReverse(p_char,p_prev_rev);
         if (n<0) {
-            my_vect.push_back(num);
-            my_vect.push_back(num_rev);
+            (num<num_rev)?my_vect.push_back(num_rev):my_vect.push_back(num); // finding a k-mer is equivalent to finding its reverse complement; as numeric values for k-mers are unique, just take into account the biggest of the 2.
         }
         p_char++;
         p_prev=&num;
diff --git a/src/fqreader.cpp b/src/fqreader.cpp
index 3615555a2a9f07eb6af60e45f9d7f9a107564758..0cd758e782b6b1615244bf39b85d11ef238957ab 100644
--- a/src/fqreader.cpp
+++ b/src/fqreader.cpp
@@ -5,28 +5,34 @@
  *      Author: vlegrand
  */
 #include <stdio.h>
-//#include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
 #include <err.h>
 #include <limits.h>
+#include <vector>
+#include <string.h>
+#include <cstdlib>
 
-//#include "srp.h"
+#include "rock_commons.h"
 #include "fqreader.h"
 #include "FqMainBackend.h"
 
 
 
+
+
 /*
- * Processes 1 file containing single reads
+ * Processes 1 file containing single reads.
+ * Used for unit testing only.
  */
 void processSingleFile(char * fq_s,unsigned char f_id, srp* io_sr) {
     FqMainBackend be_fq=FqMainBackend(io_sr);
     be_fq.processFile(fq_s,f_id);
 }
 
-/* Processes 1 pair of files containing PE reads.*/
+/* Processes 1 pair of files containing PE reads.
+ * Used for unit testing only.*/
 void processPEFiles(char * fq_1, unsigned char f_id1,char * fq_2, unsigned char f_id2,srp * io_sr ) {
     FqAuxBackend be_fq2;
     FqMainBackend be_fq1(io_sr);
@@ -38,34 +44,37 @@ void processPEFiles(char * fq_1, unsigned char f_id1,char * fq_2, unsigned char
 }
 
 /*
- * This function processes the content of an rpos structure and returns the character string "ATCG..." corresponding to the read record.
- * PE reads are concatenated.
+ * Processes a list of single files and a list of PE files and returns an plain old C array containing
+ * pointers to Fqbackend objects.
+ * This function ALLOCATES MEMORY with new.
  */
-/*void getRead(fq_file_map& fic_map,const rpos& sr, unsigned int j, char * dna_read){
-    unsigned char f_id1;
-    unsigned char f_id2=2;
-    int is_PE=0;
-    f_id1=sr.fileid >>4;
-    f_id2=sr.fileid &mask;
-
-    if (f_id2!=0) is_PE=1;
-
-    fq_file struc_f1=fic_map[f_id1];
-    if (is_PE) fq_file struc_f2=fic_map[f_id2];
-
-    if (struc_f1.fic_desc==0) { // file is not yet open
-        struc_f1.fic_desc=open(filename,O_RDONLY,mode);
-                if (f_pe2==-1) {
-                    err(errno,"cannot open file: %s.",filename);
-                }
-
-                fp2=fdopen(f_pe2,"r");
-                if (fp2==NULL) {
-                    err(errno,"cannot open file: %s.",filename);
-                }
-
+void processInputFiles(const std::vector<IO_fq_files>& single_files,const vector<PE_files>& v_PE_files,FqBaseBackend * array_be[],srp* io_sr) {
+    unsigned char f_id=1;
+    std::vector<IO_fq_files>::const_iterator it_single;
+    for (it_single=single_files.begin();it_single!=single_files.end();it_single++) {
+        FqMainBackend * be_fq=new FqMainBackend(io_sr);
+        be_fq->processFile((char *) it_single->in_fq_file.c_str(),f_id);
+        be_fq->setOutputFile((char *) it_single->out_fq_file.c_str());
+        array_be[f_id-1]=be_fq;
+        f_id+=1;
     }
+    vector<PE_files>::const_iterator it_pe;
+    for (it_pe=v_PE_files.begin();it_pe!=v_PE_files.end();it_pe++) {
+        FqMainBackend * be_fq1=new FqMainBackend(io_sr);
+        unsigned char f_id1=f_id;
+        f_id++;
+        FqAuxBackend * be_fq2=new FqAuxBackend();
+        be_fq2->openFile((char *) (it_pe->PE2.in_fq_file).c_str(),(unsigned char) f_id);
+        be_fq2->setOutputFile((char *) (it_pe->PE2.out_fq_file).c_str());
+        be_fq1->setAuxProcessor(be_fq2);
+        be_fq1->processFile((char *) (it_pe->PE1.in_fq_file).c_str(),f_id-1);
+        be_fq1->setOutputFile((char *) (it_pe->PE1.out_fq_file).c_str());
+        be_fq2->closeFile();
+        array_be[f_id1-1]=be_fq1;
+        array_be[f_id-1]=be_fq2;
+        f_id++;
+    }
+}
 
 
-}*/
 
diff --git a/src/fqreader.h b/src/fqreader.h
index f5f0eb1266785671541e1445b1cd4216f441b487..1247137b3cce71996213fd197422da7d0b08f900 100644
--- a/src/fqreader.h
+++ b/src/fqreader.h
@@ -6,11 +6,18 @@
  */
 #ifndef FQREADER_H
 #define FQREADER_H
+#include <string>
+#include "rock_commons.h"
 #include "srp.h"
+#include "FqBaseBackend.h"
+
+using namespace std;
+
+
+
 
 
-/*void processBuf(char * buf,int nread,int cur_offset);*/
 void processSingleFile(char *, unsigned char, srp*);
 void processPEFiles(char * fq_1, unsigned char f_id1,char * gq_2, unsigned char f_id2,srp *io_sr );
-// void getRead(fq_file_map&, const rpos& sr, const unsigned int& j, char * read);
+void processInputFiles(const std::vector<IO_fq_files>& ,const vector<PE_files>& ,FqBaseBackend * [], srp* );
 #endif
diff --git a/src/read_utils.cpp b/src/read_utils.cpp
index 26442b3025cdfe3476667419db951f8817f29224..ee39cf0442316d43cc1375fd2403709101e96aaf 100644
--- a/src/read_utils.cpp
+++ b/src/read_utils.cpp
@@ -13,18 +13,36 @@
  */
 
 #include "FqConstants.h"
-#include "rock_types.h"
 #include "srp.h"
 #include "read_utils.h"
+#include "rock_commons.h"
+//#define DEBUG
 
+#ifdef DEBUG
+#include <cassert>
+#endif
 
+void UpdateReadForScoreThreshold(DnaSeqStr * dna_seq,const int & nucl_qual_score_thres,const int& idx_start_qual_score) {
+    char * read_data_start=dna_seq->fq_record_buf+dna_seq->start_idx;
+    char * qual_score_start=dna_seq->fq_record_buf+idx_start_qual_score;
+    int idx=0;
+    while (idx<dna_seq->length) {
+        if (*(qual_score_start+idx)-k_phred_32<=nucl_qual_score_thres) *(read_data_start+idx)=k_nucl_in_error;
+        idx++;
+    }
+}
 
-
-
-void getFqRecord(FqBaseBackend* fq_files_be[],const unsigned char& f_id,const long& offset,
+void getFqRecord(FqBaseBackend* fq_files_be[],const unsigned char& f_id,const unsigned long& offset,
                  char * fq_record) {
-    FqBaseBackend * be=fq_files_be[f_id];
-    be->getRead(offset,fq_record);
+    FqBaseBackend * be=fq_files_be[f_id-1];
+    try {
+        int fq_rec_size=be->getRead(offset,fq_record);
+    } catch (int e) {
+#ifdef DEBUG
+        cout<<"couldn't retrieve read at offset: "<<offset<<endl;
+#endif
+        throw e;
+    }
 }
 
 
@@ -34,12 +52,14 @@ void init_DnaSeqStr(DnaSeqStr * dna_seq) {
     dna_seq->start_idx=0;
 }
 
-void processFqRecord(DnaSeqStr * dna_seq) {
+void processFqRecord(DnaSeqStr * dna_seq,const int & nucl_qual_score_thres) {
     char * pchar=dna_seq->fq_record_buf;
     int cnt=0;
     int nb_l=0;
+    int idx_start_qual_score;
 #ifdef DEBUG
-    assert(*pchar==k_read_id_start);
+    //assert(*pchar==k_read_id_start);
+    if (*pchar!=k_read_id_start) throw -1;
 #endif
     while (cnt<=MAX_FQ_RECORD_LENGTH-1) {
         if (*pchar=='\n') {
@@ -47,22 +67,29 @@ void processFqRecord(DnaSeqStr * dna_seq) {
                 if (nb_l==1) dna_seq->start_idx=cnt+1;
                 if (nb_l==2) {
                     dna_seq->length=cnt-dna_seq->start_idx;
+                    // break;
+                }
+                if (nb_l==3) {
+                    idx_start_qual_score=cnt+1;
                     break;
                 }
         }
         pchar++;
         cnt++;
     }
+    // here, change read if there is a threshold for nucleotide quality score.
+    if (nucl_qual_score_thres) UpdateReadForScoreThreshold(dna_seq,nucl_qual_score_thres,idx_start_qual_score);
+
 #ifdef DEBUG
     assert(nb_l>=2);
 #endif
 }
 
 
-void getDNASeqstr(FqBaseBackend* fq_files_be[],
+void getDNASeqstr(FqBaseBackend* fq_files_be [],
                 const rpos& sr,
-                unsigned int j,
-                DnaSeqStr * dna_seqs)
+                unsigned long j,
+                DnaSeqStr * dna_seqs,const int& nucl_qual_score_thres)
 {
     unsigned char f_id1;
     unsigned char f_id2;
@@ -73,19 +100,54 @@ void getDNASeqstr(FqBaseBackend* fq_files_be[],
     f_id1=fid_stored >>4;
     f_id2=fid_stored &mask_fid;
 
-    long offset1=j*INT_MAX+sr.read_a1;
-    getFqRecord(fq_files_be,f_id1,offset1,dna_seqs[0].fq_record_buf);
-    processFqRecord(p_dna_seqs);
-
+    unsigned long offset1=j*UINT_MAX+sr.read_a1;
+#ifdef DEBUG
+    cout<<"processing record at j="<<j<<"and read_a1="<<sr.read_a1<<endl;
+    cout<<"getting record at offset: "<<offset1<<endl;
+#endif
+    try {
+        getFqRecord(fq_files_be,f_id1,offset1,dna_seqs[0].fq_record_buf);
+        processFqRecord(p_dna_seqs,nucl_qual_score_thres);
+    } catch (int e) {
+#ifdef DEBUG
+        cout<<"j="<<j<<" sr content: a1="<<sr.read_a1<<" a2= "<<sr.read_a2<<endl;
+        cout<<"pb reading/seeking at offset1="<<offset1<<endl;
+        cout<<"got: "<<endl;
+        cout<<dna_seqs[0].fq_record_buf<<endl;
+#endif
+        exit(e); 
+    }
     if (f_id2!=0) { // case of PE reads.
         p_dna_seqs+=1;
-        long offset2=sr.read_a2+j*INT_MAX;
-        getFqRecord(fq_files_be,f_id2,offset2,dna_seqs[1].fq_record_buf);
-        processFqRecord(p_dna_seqs);
+        unsigned long offset2=sr.read_a2+offset1;
+        try {
+            getFqRecord(fq_files_be,f_id2,offset2,dna_seqs[1].fq_record_buf);
+        } catch (int e) {
+#ifdef DEBUG
+            cout<<"j="<<j<<" sr content: a1="<<sr.read_a1<<" a2= "<<sr.read_a2<<endl;
+            cout<<"pb reading/seeking at offset2="<<offset2<<endl;
+            cout<<"got: "<<endl;
+            cout<<dna_seqs[1].fq_record_buf<<endl;
+#endif
+            exit(e);
+        }
+        processFqRecord(p_dna_seqs,nucl_qual_score_thres);
     }
 
 }
 
+void decomposeReadInKMerNums(ReadProcessor& read_p, readNumericValues& nbrKmerDecompo,int k,DnaSeqStr a_seqs[2]) {
+    int nb_expected_k_mers=a_seqs[0].length+a_seqs[1].length+1;
+    nb_expected_k_mers-=k;
+    nbrKmerDecompo.reserve(nb_expected_k_mers);
+    char * start_dna_str=a_seqs[0].fq_record_buf+a_seqs[0].start_idx;
+    read_p.getKMerNumbers(start_dna_str,a_seqs[0].length,nbrKmerDecompo);
+    if (a_seqs[1].length) { // case of PE reads
+        start_dna_str=a_seqs[1].fq_record_buf+a_seqs[1].start_idx;
+        read_p.getKMerNumbers(start_dna_str,a_seqs[1].length,nbrKmerDecompo);
+    }
+}
+
 
 
 
diff --git a/src/read_utils.h b/src/read_utils.h
index 5076898771eb7092963b248d3cca37d28cd855d8..55119f16ecf85b729eec5359a8000fdefec6260c 100644
--- a/src/read_utils.h
+++ b/src/read_utils.h
@@ -9,26 +9,33 @@
 #define READ_UTILS_H_
 
 #include "FqConstants.h"
-#include "rock_types.h"
+#include "rock_commons.h"
+#include "ReadProcessor.h"
+
+#define k_nucl_in_error 'N'
 
 /*
  * Here, for performance matters, do not want to have to do a memmove nor strcpy or strcat to extract only DNA sequence for all
  * the fastq records that we'll have to process (potentially millions). So, use a structure to store start position of DNA sequence in buffer and its length.
  * We will not process more than 2 fastq records at a time (case of he PE reads) so it will not take too much space in memory.
  */
-
 typedef struct {
     char fq_record_buf[MAX_FQ_RECORD_LENGTH];
     int start_idx;
     int length;
 }DnaSeqStr;
 
+
+
 void getDNASeqstr(FqBaseBackend* [],
                 const rpos&,
-                unsigned int,
-                DnaSeqStr *);
+                unsigned long,
+                DnaSeqStr *,const int& nucl_qual_score_thres=0);
+
 
 void init_DnaSeqStr(DnaSeqStr * dna_seq);
 
+void decomposeReadInKMerNums(ReadProcessor& read_p, readNumericValues& nbrKmerDecompo,int k,DnaSeqStr a_seqs[2]);
+
 
 #endif /* READ_UTILS_H_ */
diff --git a/src/rock.cpp b/src/rock.cpp
index 5cfb275d6c491835ecf093ffcb819ad550017058..7bd1657d0eb7149d4117244f83a169cb865334b1 100644
--- a/src/rock.cpp
+++ b/src/rock.cpp
@@ -7,86 +7,130 @@
  Description : see : https://projets.pasteur.fr/projects/homogeneisation-couverture-pre-assemblage/wiki
  ============================================================================
  */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <err.h>
+#include <sys/sysctl.h>
+#include <cstdlib>
+#include "err.h"
 #include <string.h>
 
-// #include "srp.h"
-#include "fqreader.h"
-
-#define k_max_input_files 15
-
-
-
-
-
-int main(void) {
-	/* 1rst step : read a fastq and compute quality score for each record (PE or single) */
-    char * fq_pe1="../data/LM201200065_S106_R1.TM.fq";
-    char * fq_pe2="../data/LM201200065_S106_R2.TM.fq";
-    char * fq_s="../data/LM201200065_S106_RS.TM.fq";
-    char* fq_s_test="data/test_single.fq";
+#include <iostream>
+#include <map>
+#include <vector>
+#include <unistd.h>
 
-    /* All these data will be given from input. */
-    char ** list_single=(char *[]){"../data/LM201200065_S106_RS.TM.fq"};
-    int nb_single=1;
-    char ** list_pe=(char *[]) {"../data/LM201200065_S106_R1.TM.fq",
-                     "../data/LM201200065_S106_R2.TM.fq"};
-    int nb_pairs=1;
 
+#include "CountMinSketch.hpp"
+#include "rock_commons.h"
+#include "srp.h"
 
-    char ** l_files; /* array of filenames; indexed on fileid */
-    int maxlen=0;
-    int len;
+#include "FqMainBackend.h"
+#include "fqreader.h"
+#include "fqwriter.h"
+#include "Filter.hpp"
+#include "main_utils.h"
+#include "ROCKparams.h"
 
-    int cnt;
-    int nb_all=nb_pairs*2+nb_single; /* TODO : check that nb_all doesn't exceed 15. We only have 4 bits to store fileid in memory! */
 
+using namespace std;
 
-    for (cnt=0;cnt<nb_pairs;cnt++) {
-        char * pe1=list_pe[0];
-        len=strlen(pe1);
-        if (len>maxlen) maxlen=len;
-        char * pe2=list_pe[1];
-        len=strlen(pe2);
-        if (len>maxlen) maxlen=len;
-        list_pe+=2;
-    }
 
-    for (cnt=0;cnt<nb_single;cnt++) {
-        char * single=list_single[0];
-        len=strlen(single);
-        if (len>maxlen) maxlen=len;
-        list_single++;
+//#define BENCHMARK
+#ifdef BENCHMARK
+#include <sys/time.h>
+#include <sys/resource.h>
+void printRUsage() {
+    struct rusage usage;
+    int res=getrusage(RUSAGE_SELF,&usage);
+    std::cout<<"memory info: maxrss="<<usage.ru_maxrss<<" ixrss="<<usage.ru_ixrss<<" idrss="<<usage.ru_idrss<<" isrss="<<usage.ru_isrss<<" minflt="<<usage.ru_minflt<<" majflt="<<usage.ru_majflt<<endl;
+    std::cout<<"time info: user time="<<usage.ru_utime.tv_sec<<" system time="<<usage.ru_stime.tv_sec<<endl;
+}
+#endif
+
+
+
+
+
+int main(int argc,char * argv[]) {
+#ifdef BENCHMARK
+    cout<<"program startup"<<endl;
+    printRUsage();
+#endif
+    srp sr;
+    ROCKparams main_parms;
+    main_parms.initFromMainOptsArgs(argc,argv);
+    int f_id=main_parms.get_f_id();
+    CMSparams parms=main_parms.getCMSparams();
+    std::vector<IO_fq_files> single_files=main_parms.get_single_files();
+    vector<PE_files> v_PE_files=main_parms.get_PE_files();
+
+    FqBaseBackend * map_id_backend[k_max_input_files];
+    Filter the_filter(parms);
+
+#ifdef BENCHMARK
+    cout<<"processed input args; going to start reading fastq files"<<endl;
+    printRUsage();
+#endif
+    processInputFiles(single_files,v_PE_files,map_id_backend,&sr);
+#ifdef BENCHMARK
+    cout<<"finished loading fastq file into sr structure"<<endl;
+    cout<<"size of srp structure="<<sizeof(sr)<<endl;
+    printRUsage();
+    cout<<"Now going to fill CMS"<<endl;
+#endif
+
+    int k=main_parms.get_k();
+
+    the_filter.fillCMS(map_id_backend,f_id,k, &sr);
+
+#ifdef BENCHMARK
+    cout<<"finished filling CCountMinSketch"<<endl;
+    // cout<<"size of CountMinSketch object="<<sizeof(*pcms)<<endl;
+    printRUsage();
+    cout<<"going to get approximated number of distinct k-mers in the CMS."<<endl;
+#endif
+
+    unsigned long approx_nb_k_mers=the_filter.getApproxNbDistinctKMers();
+#ifdef BENCHMARK
+    printRUsage();
+    cout<<"found "<<approx_nb_k_mers<<" distinct k-mers"<<endl;
+    cout<<"Now going to remove read with cov<kappa_prime"<<endl;
+#endif
+
+    main_parms.setFilterSize(the_filter.getSize());
+    // Now, remove reads that are beneath kappa_prime
+    if (parms.kappa_prime) the_filter.lowFilterCMS(map_id_backend,f_id,k,&sr);
+
+#ifdef BENCHMARK
+    cout<<"finished remove read with cov<kappa_prime"<<endl;
+    // cout<<"size of CountMinSketch object="<<sizeof(*pcms)<<endl;
+    printRUsage();
+    cout<<"Should now write output files"<<endl;
+#endif
+    writeFilteredFastq(map_id_backend,f_id,sr);
+#ifdef BENCHMARK
+    cout<<"finished writing filtered reads"<<endl;
+    printRUsage();
+    cout<<"Should now free memory"<<endl;
+#endif
+    int i;
+    for (i=1;i<=f_id;i++) {
+       // cout<<"deleting backend: "<<i-1<<endl;
+        delete map_id_backend[i-1];
     }
 
-    l_files=(char **) malloc((nb_all)*(maxlen+1));
+#ifdef BENCHMARK
+    cout<<"going to compute collision probability"<<endl;
+    printRUsage();
+#endif
 
-    for (cnt=0;cnt<nb_pairs*2;cnt++) {
-        if (list_pe!=NULL) strcpy(l_files[cnt],list_pe[cnt]);
-    }
-    for (cnt=nb_pairs*2;cnt<nb_all;cnt++) {
-        if (list_single!=NULL) strcpy(l_files[cnt],list_single[cnt-nb_pairs*2]);
-    }
+    float p =getCollisionProba(approx_nb_k_mers,parms.lambda);
+    cout<<"estimated probability of collision:"<<p<<endl;
+    cout<<"estimated number of distinct k-mers:"<<approx_nb_k_mers<<endl;
 
-    /* process files. Sequential processing for the moment.
-     * fileid is file position inside l_files array starting at 1.
-     */
-    srp all_sr;
-    cnt=0;
-    while (cnt<2*nb_pairs) {
-        processPEFiles(l_files[cnt],cnt+1,l_files[cnt+1],cnt+2,&all_sr);
-        cnt+=2;
-    }
-    while (cnt<nb_all) {
-        processSingleFile(l_files[cnt],cnt+1,&all_sr);
-        cnt+=1;
-    }
+    if (main_parms.is_verbose()) main_parms.printVerboseInfo();
 
+#ifdef BENCHMARK
+    cout<<"finished,going to exit "<<endl;
+    printRUsage();
+#endif
 	return EXIT_SUCCESS;
 }
diff --git a/src/srp.h b/src/srp.h
index b4b2b22d2743ad1a52305fe64d7f73af0a141990..f77ff1e7a54b70e24abb00c2f9999369c2cc44d4 100644
--- a/src/srp.h
+++ b/src/srp.h
@@ -8,15 +8,19 @@
 #ifndef SRP_H_
 #define SRP_H_
 
+#include <climits>
+
 #include <vector>
 #include <map>
 
 #define K_SCORE_NORM_FACTOR 1000
 
+#define debug
+
 typedef struct { /* Here store read offset in files whose ids are stored in the fileid field .*/
     unsigned char fileid;
-    unsigned long read_a1;
-    unsigned long read_a2;
+    unsigned int read_a1;
+    long read_a2;
 }rpos;
 
 
@@ -28,13 +32,16 @@ typedef struct {
 
 typedef std::vector<rpos> k_dim;
 typedef std::map<unsigned long,k_dim> i_dim;
-typedef std::map<unsigned long,i_dim> srp;
+typedef std::map<int,i_dim> srp;
 
 
 inline rpos init_rpos(unsigned char f_id, unsigned long rstart_offset) {
     rpos rp;
     rp.fileid=f_id <<4;
-    rp.read_a1=rstart_offset%INT_MAX;
+    rp.read_a1=rstart_offset%UINT_MAX; // That's Ok since this remainder cannot be bigger than UINT_MAX.
+#ifdef DEBUG
+    cout<<"rp.read_a1="<<rp.read_a1<<endl;
+#endif
     return rp;
 }
 
@@ -43,7 +50,7 @@ inline rpos init_rpos(unsigned char f_id, unsigned long rstart_offset) {
  */
 inline void update_rpos(unsigned char f_id,unsigned long rstart_offset,unsigned long j, rpos * rp) {
     rp->fileid=rp->fileid|f_id;
-    rp->read_a2=rstart_offset-INT_MAX*j;
+    rp->read_a2=rstart_offset-UINT_MAX*j-rp->read_a1;
 }
 
 
diff --git a/src/unit_test_read_utils.cpp b/src/unit_test_read_utils.cpp
index 33dc150a60eb9304e450a92e2bd4e5f2c49deed3..fb795e74e483052f3309869976d1c3f5807169d8 100644
--- a/src/unit_test_read_utils.cpp
+++ b/src/unit_test_read_utils.cpp
@@ -7,11 +7,13 @@
  *      Keep using assert for the moment, don't want to add a dependency on boost (or any other test framework) just for the tests.
  */
 #include <iostream>
-#include <assert.h>
+
+#include <cassert>
+#include <cstring>
 
 #include "read_utils.h"
-#include "rock_types.h"
 #include "FqMainBackend.h"
+#include "rock_commons.h"
 #include "srp.h"
 #include "ReadProcessor.h"
 
@@ -31,13 +33,13 @@ void test_getReadSingle() {
     DnaSeqStr a_seqs;
     char dna_read[MAX_READ_LENGTH];
 
-    char * fq_single2=(char *) "../data/test_single2.fq";
+    char * fq_single2=(char *) "../test/data/unit/test_single2.fq";
     FqMainBackend be_fq=FqMainBackend(&io_sr); // TODO, remove argument from constructor
-    be_fq.openFile(fq_single2, 4);
+    be_fq.openInputFile(fq_single2, 4);
 
 
     FqBaseBackend * fic_map[k_max_input_files];
-    fic_map[4]=&be_fq;
+    fic_map[3]=&be_fq;
 
     init_DnaSeqStr(&a_seqs);
     getDNASeqstr(fic_map, my_struct1, 0, &a_seqs);
@@ -60,11 +62,11 @@ void test_getReadSingle() {
     if (a_seqs.length<MAX_READ_LENGTH) dna_read[a_seqs.length]='\0';
     std::cout<<dna_read<<endl;
     assert(strcmp(dna_read,"ACCCAAACTTGCCAGACTTGTGTAGAACGTCCAATATGTATCGGCATCGCTTCCACATGAATGAATCCTTGTTCCACACTTTTTATATGATTCGCATTAATTTCTTGTCCGAAAATCAACTGATTTTTTGCAACATTTTCTCCCGCTCCAAGACTGGCTGCATGTTCTGCAAGCGCAACAGAAACACCACCATGCAAGTAGCCAAAGGGTTGTTTGACCTGATCTGTTATTTCAAGCGCCAGTTCCACTC")==0);
-    be_fq.closeFile();
+    be_fq.closeInputFile();
 }
 
-void getDnaStr(FqBaseBackend * fic_map[],rpos my_struct,DnaSeqStr* a_seqs,char * dna_read) { // Auxilliary method for test_getReadPE().
-    getDNASeqstr(fic_map, my_struct, 0, a_seqs);
+void getDnaStr(FqBaseBackend * fic_map[],rpos my_struct,DnaSeqStr* a_seqs,char * dna_read, int min_score_qual=0) { // Auxilliary method for test_getReadPE().
+    getDNASeqstr(fic_map, my_struct, 0, a_seqs,min_score_qual);
 
     char * tmp=(char *) a_seqs[0].fq_record_buf;
     tmp+=a_seqs[0].start_idx;
@@ -80,10 +82,11 @@ void getDnaStr(FqBaseBackend * fic_map[],rpos my_struct,DnaSeqStr* a_seqs,char *
     if (a_seqs[0].length+a_seqs[1].length<MAX_READ_LENGTH) dna_read[a_seqs[0].length+a_seqs[1].length]='\0';
 }
 
+
 void test_getReadPE() {
     rpos my_struct1,my_struct2;
-    char * fq_PE1=(char *) "../data/test_PE1_2.fq";
-    char * fq_PE2=(char *) "../data/test_PE2_2.fq";
+    char * fq_PE1=(char *) "../test/data/unit/test_PE1_2.fq";
+    char * fq_PE2=(char *) "../test/data/unit/test_PE2_2.fq";
     srp io_sr;
     unsigned int j=0;
     char dna_read[MAX_READ_LENGTH];
@@ -92,12 +95,12 @@ void test_getReadPE() {
     FqAuxBackend be_fq2;
     be_fq1.setAuxProcessor(&be_fq2);
 
-    be_fq1.openFile(fq_PE1, 4);
-    be_fq2.openFile(fq_PE2, 4);
+    be_fq1.openInputFile(fq_PE1, 4);
+    be_fq2.openFile(fq_PE2, 5);
 
     FqBaseBackend * fic_map[k_max_input_files];
-    fic_map[4]=&be_fq1;
-    fic_map[5]=&be_fq2;
+    fic_map[3]=&be_fq1;
+    fic_map[4]=&be_fq2;
 
     DnaSeqStr a_seqs[2];
 
@@ -107,13 +110,13 @@ void test_getReadPE() {
     my_struct1=init_rpos(4,0);
     update_rpos(5,0,j,&my_struct1);
 
-    my_struct2=init_rpos(4,13648);
-    update_rpos(5,13654,j,&my_struct2);
+    my_struct2=init_rpos(4,13647);
+    update_rpos(5,13653,j,&my_struct2);
 
     assert(my_struct1.read_a1==0);
     assert(my_struct1.read_a2==0);
-    assert(my_struct2.read_a1==13648);
-    assert(my_struct2.read_a2==13654);
+    assert(my_struct2.read_a1==13647);
+    assert(my_struct2.read_a2==6);
 
     getDnaStr(fic_map,my_struct1,a_seqs,dna_read);
     std::cout<<dna_read<<endl;
@@ -123,10 +126,88 @@ void test_getReadPE() {
     getDnaStr(fic_map,my_struct2,a_seqs,dna_read);
     assert(strcmp(dna_read,"ATTGTGGGGTTCCTTTTTGTAGCATTGGAATGGAAATTAAAAATGGGGCTTCAGGATGCCCGCTCCATTATTTAATTCCAGAATGTAACGATGCTGTTTACCGGGGGGACTGGAAAGATGCACTTGAGCTTTTGATTAAAACAAATAACATGCCCAGAACCAATCACTGCAATTTTTTTATCCCACCGCACTATCGGTGGAGTCGGCATGAACCAACCTAAACCAAACCCACGGTCAATAATAGCCCGTTCAATCGAATTAATACCCACAGCAGGATCAGAAATTGCAACCGTACAACTTC")==0);
 
-    be_fq1.closeFile();
+    be_fq1.closeInputFile();
+    be_fq2.closeFile();
+    //cout<<"done"<<endl;
+}
+
+void test_getReadPEWithNQST() {
+    rpos my_struct1,my_struct2;
+    char * fq_PE1=(char *) "../test/data/unit/test_PE1_2.fq";
+    char * fq_PE2=(char *) "../test/data/unit/test_PE2_2.fq";
+    srp io_sr;
+    unsigned int j=0;
+    char dna_read[MAX_READ_LENGTH];
+
+    FqMainBackend be_fq1=FqMainBackend(&io_sr);
+    FqAuxBackend be_fq2;
+    be_fq1.setAuxProcessor(&be_fq2);
+
+    be_fq1.openInputFile(fq_PE1, 4);
+    be_fq2.openFile(fq_PE2, 5);
+
+    FqBaseBackend * fic_map[k_max_input_files];
+    fic_map[3]=&be_fq1;
+    fic_map[4]=&be_fq2;
+
+    DnaSeqStr a_seqs[2];
+
+    init_DnaSeqStr(&a_seqs[0]);
+    init_DnaSeqStr(&a_seqs[1]);
+
+    my_struct1=init_rpos(4,0);
+    update_rpos(5,0,j,&my_struct1);
+
+    my_struct2=init_rpos(4,13647);
+    update_rpos(5,13653,j,&my_struct2);
+
+    assert(my_struct1.read_a1==0);
+    assert(my_struct1.read_a2==0);
+    assert(my_struct2.read_a1==13647);
+    assert(my_struct2.read_a2==6);
+
+    int min_score_qual=14;
+    getDnaStr(fic_map,my_struct1,a_seqs,dna_read,min_score_qual);
+    std::cout<<"PE dna read:"<<endl;
+    std::cout<<dna_read<<endl;
+    assert(strcmp(dna_read,"GGNTCTGTTGGCTCAAACGCCNTCACNTCNTTGNTCAAAAGCTTATAATGCGNGCCAAANTCCGCCATCGNGACNNCTACGCCTTCCCCNGCTNTCCCGNCAAANNCNNGTCTTGCCGGANCTTCNCNNNCTCCNNTCGAAAGCGGCGAAATCTTAGNGGAAGGNGGANATAATGCNNTCNCNTCGNACNNTGAANNTNTANNCGGCANNCNGCAGNNTCNAGGNCTTNCNGNGNAACGNTTAANNGCAGATGGCTACGGNTTTGCNGGGGNANGAGACNGGANANCNNNGNCGNTCGNCCN")==0);
+
+    /*getDnaStr(fic_map,my_struct2,a_seqs,dna_read);
+    assert(strcmp(dna_read,"ATTGTGGGGTTCCTTTTTGTAGCATTGGAATGGAAATTAAAAATGGGGCTTCAGGATGCCCGCTCCATTATTTAATTCCAGAATGTAACGATGCTGTTTACCGGGGGGACTGGAAAGATGCACTTGAGCTTTTGATTAAAACAAATAACATGCCCAGAACCAATCACTGCAATTTTTTTATCCCACCGCACTATCGGTGGAGTCGGCATGAACCAACCTAAACCAAACCCACGGTCAATAATAGCCCGTTCAATCGAATTAATACCCACAGCAGGATCAGAAATTGCAACCGTACAACTTC")==0);
+*/
+    be_fq1.closeInputFile();
     be_fq2.closeFile();
     //cout<<"done"<<endl;
+}
 
+void test_decomposeReadInkMerNums() {
+    int k=5;
+    ReadProcessor read_p(k);
+    readNumericValues nbrKmerDecompo;
+    
+    DnaSeqStr a_seqs[2];
+    init_DnaSeqStr(&a_seqs[0]);
+    init_DnaSeqStr(&a_seqs[1]);
+    DnaSeqStr * seq1=a_seqs;
+    //cout<<strlen("AAAAAAAAAA")<<endl;
+    strcpy(seq1->fq_record_buf,"@fake_stuff\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n+\n....@\n");
+    seq1->start_idx=12;
+    seq1->length=75;
+    decomposeReadInKMerNums(read_p,nbrKmerDecompo,k,a_seqs);
+    assert(nbrKmerDecompo.size()==71);
+    readNumericValues::iterator it;
+    for (it=nbrKmerDecompo.begin();it!=nbrKmerDecompo.end();it++) {
+        // cout<<*it<<endl;
+        assert(*it==1023);
+    }
+    DnaSeqStr * seq2=&a_seqs[1];
+    strcpy(seq2->fq_record_buf,"@another_fake_stuff\nATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCGATCG\n+\n....@\n");
+    seq2->start_idx=20;
+    seq2->length=80;
+    nbrKmerDecompo.clear();
+    decomposeReadInKMerNums(read_p,nbrKmerDecompo,k,a_seqs);
+    //cout<<nbrKmerDecompo.size()<<endl;
+    assert(nbrKmerDecompo.size()==147);
 }
 
 
@@ -201,9 +282,9 @@ void testDNAToNumberMoreComplex() { // TODO in main, check that k<=32.
     char dnaStr[]="TTTTTAGGTGCTACCATAACACCAACTGTTTTCACCATAATTTTAAAATCAAGCATTAGAGACGCTTCTCTAATGTATTGCAAATCTAGTTCTACCATTTGATGAAAATCTAATTCATTTCTTCCACTAACCTGCCATAATCCAGTACAACCTGGTATAACGGTCAAACGCTTTTTATCATAGGAACTGTATTCTCCTACCTCACGTGGCAAAGGAGGTCTTGGACCAACAATTGCCATGTCTCCTTTAACCACATTCCAAAGCTGTGGTA";
     int k=30;
     ReadProcessor read_p(k);
-    std::vector<unsigned long> nbrKmerDecompo;
+    readNumericValues nbrKmerDecompo;
 
-    std::vector<unsigned long>::iterator it;
+    readNumericValues::iterator it;
     int nb_expected_k_mers=strlen(dnaStr)+1;
     nb_expected_k_mers-=k;
     nbrKmerDecompo.reserve(nb_expected_k_mers);
@@ -211,7 +292,7 @@ void testDNAToNumberMoreComplex() { // TODO in main, check that k<=32.
     unsigned long expected_rev=read_p.kMerToNumber((char *) "TATGGTGAAAACAGTTGGTGTTATGGTAGC",NULL);
 
     read_p.getKMerNumbers((char *) dnaStr,strlen(dnaStr),nbrKmerDecompo);
-    assert(nbrKmerDecompo.size()==nb_expected_k_mers*2); // for each k_mer, we also expect to have a number for its reverse complement.
+    assert(nbrKmerDecompo.size()==nb_expected_k_mers); // for each k_mer, we also expect to have a number for its reverse complement.
     int cnt_k_mer=0;
     unsigned long num,num2;
     for (it=nbrKmerDecompo.begin();it!=nbrKmerDecompo.end();it++) {
@@ -222,31 +303,32 @@ void testDNAToNumberMoreComplex() { // TODO in main, check that k<=32.
             assert(*it==1151987406330741231);
             num=*it;
         }
-        if (cnt_k_mer==3) {
+        if (cnt_k_mer==2) {
             // 2nd k-mer is : TTTTAGGTGCTACCATAACACCAACTGTTT
             num2=read_p.kMerToNumber((char *) "TTTTAGGTGCTACCATAACACCAACTGTTT",NULL);
             assert(*it==num2);
             unsigned long num3=read_p.kMerToNumber((char *) "TTTTAGGTGCTACCATAACACCAACTGTTT",&num);
             assert(num3==num2);
         }
-        if (cnt_k_mer==5) {
+        if (cnt_k_mer==3) {
             num=read_p.kMerToNumber((char *) "TTTAGGTGCTACCATAACACCAACTGTTTT",NULL);
-            assert(num==*it);
+            unsigned long num_rev=read_p.kMerToNumber((char *) "AAAACAGTTGGTGTTATGGTAGCACCTAAA",NULL);
+            assert(max(num,num_rev)==*it);
         }
-        if (cnt_k_mer==7) {
+        if (cnt_k_mer==4) {
             num=read_p.kMerToNumber((char *) "TTAGGTGCTACCATAACACCAACTGTTTTC",NULL);
             assert(num==*it);
         }
 
         // k-mer number 10 is GCTACCATAACACCAACTGTTTTCACCATA
         // its reverse complement is : TATGGTGAAAACAGTTGGTGTTATGGTAGC
-        if (cnt_k_mer==19) {
+        if (cnt_k_mer==10) {
             num=read_p.kMerToNumber((char *) "GCTACCATAACACCAACTGTTTTCACCATA",NULL);
-            assert(*it==704021989794238796);
+            assert(*it==max(704021989794238796,930978566989888201));
         }
-        if (cnt_k_mer==20) {
+        /*if (cnt_k_mer==20) {
             assert(*it==930978566989888201);
-        }
+        }*/
     }
 }
 
@@ -269,17 +351,71 @@ void testDNAToNumberWithN() {
 
     nbrKmerDecompo.reserve(nb_expected_k_mers);
     read_p.getKMerNumbers((char *) dnaStr,strlen(dnaStr),nbrKmerDecompo);
-    assert(nbrKmerDecompo.size()==nb_expected_k_mers*2);
+    assert(nbrKmerDecompo.size()==nb_expected_k_mers);
 
     it=nbrKmerDecompo.begin();
-    it+=22;
-    // The 1rst expected k-mer is the one after the 3rd N char (starting at position 47 in dnaStr). ATCAAGCATTAGAGACGCTTCTCTAATGTA
-    assert(*it==234837138459816172);
-    it++;
-    assert(*it==886965076742957027);
+    it+=11;
+    // The 1rst expected k-mer is the one after the 3rd N char (starting at position 47 in dnaStr). ATCAAGCATTAGAGACGCTTCTCTAATGTA or its reverse complement depending
+    assert(*it==max(234837138459816172,886965076742957027));
+
+  
+    char dnaStr2[]="GCCTTTTCTTTTTCCAGGGAAAACCATCCAGGAGGAACTTTATTATGGCGATGTATGAAGTCGGTACCGTCACGGGTGCCGCGTCGCAGGCACGGGTGACAGGTGCGACAACAAAATGGTCACAGGAGGCGCTGNGGATACAGCCCGGGTCGATTCTGGTGGTCTACCGCAGCGGTAGTGCTGACCTGTATGCGATCAAATCCGTGGACAGCGACACGCAACTGACGCTGACCCGGAATATCACCACCGC";
+    nb_expected_k_mers=strlen(dnaStr2)+1;
+    nb_expected_k_mers-=k;
+
+    nbrKmerDecompo.clear();
+    nbrKmerDecompo.reserve(nb_expected_k_mers);
+    read_p.getKMerNumbers((char *) dnaStr2,strlen(dnaStr2),nbrKmerDecompo);
+  assert(nbrKmerDecompo.size()==nb_expected_k_mers-k); // there is only one N in danStr2.
 
+}
+
+
+
+// testing getRead with a quality score threshold for nucleotides.
+void test_getReadWithNQST() {
+    int nucl_qual_score_thres;
+    rpos my_struct1,my_struct2;
+    my_struct1=init_rpos(4,639);
+    my_struct2=init_rpos(4,1228);
 
+    assert(my_struct1.read_a1==639);
+    assert(my_struct2.read_a1==1228);
+    srp io_sr;
+    unsigned int j=0;
+    DnaSeqStr a_seqs;
+    char dna_read[MAX_READ_LENGTH];
+
+    char * fq_single2=(char *) "../test/data/unit/test_single2.fq";
+    FqMainBackend be_fq=FqMainBackend(&io_sr); // TODO, remove argument from constructor
+    be_fq.openInputFile(fq_single2, 4);
+
+
+    FqBaseBackend * fic_map[k_max_input_files];
+    fic_map[3]=&be_fq;
+
+    init_DnaSeqStr(&a_seqs);
+    nucl_qual_score_thres=32;
+    getDNASeqstr(fic_map, my_struct1, 0, &a_seqs,nucl_qual_score_thres);
+    char * tmp=(char *) a_seqs.fq_record_buf;
+    tmp+=a_seqs.start_idx;
+    memcpy(dna_read,tmp,a_seqs.length);
+    dna_read[a_seqs.length]='\0';
+    /*std::cout<<"modified dna read:"<<endl;
+    std::cout<<dna_read<<endl;*/
+    assert(strcmp(dna_read,"NNNNNAGGTGCTACCATAACACCAACTGTTTTCACNATAATTTTAAAATCAAGCATTAGAGACGCNTCTCTAATGTATTGCAAATCTAGTTCTACCATTTGATGAAAATCTAATTNATTTCTTCCACTANCCTGCCATAATCCAGTACAACCTGGTATAACGGNCAANCGCTTTTTATCATAGGANCTGTATTCTCCTACCTCACGTGGCAAAGGAGGNCTTGGACCAACAATTGCCATGTCTCCTTTAACCACATTCCAAAGCTGNNNNN")==0);
+
+    init_DnaSeqStr(&a_seqs);
+    nucl_qual_score_thres=50;
+    getDNASeqstr(fic_map, my_struct1, 0, &a_seqs,nucl_qual_score_thres);
+    tmp=(char *) a_seqs.fq_record_buf;
+    tmp+=a_seqs.start_idx;
+    memcpy(dna_read,tmp,a_seqs.length);
+    // std::cout<<dna_read<<endl;
+    assert(strcmp(dna_read,"NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN")==0);
 
+    // now test the same thing with PE reads
+    test_getReadPEWithNQST();
 }
 
 int main(int argc, char **argv) {
@@ -287,12 +423,16 @@ int main(int argc, char **argv) {
     test_getReadSingle();
     cout<<"test getting PE reads."<<endl;
     test_getReadPE();
+    cout<<"test getting reads with a quality score constraint on nucleotides."<<endl;
+    test_getReadWithNQST();
     cout<<"test converting k_mers of different size into number"<<endl;
     testDNAToNumberSimple();
     cout<<"test converting an entire read to a series of numbers"<<endl;
     testDNAToNumberMoreComplex();
     cout<<"testing the case of N nucleotides"<<endl;
     testDNAToNumberWithN();
+    cout<<"testing higher level function: decomposeReadInKMerNums"<<endl;
+    test_decomposeReadInkMerNums();
     cout<<"done"<<endl;
 }