#!/usr/bin/perl
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;

# main
main : {
  # options hash table
  my %h_options;

  Getopt::Long::Configure( "no_ignorecase" );
  Getopt::Long::Configure( "bundling" );

  # retrieve all options
  GetOptions (\%h_options,
	      'h|help'
	     ) or  pod2usage( -verbose => 1 , noperldoc => 1) ;
	
  # help
  pod2usage( -verbose => 1 , noperldoc => 1) if (exists $h_options{'h'});
	
  # test ARGV parameters
  pod2usage( -verbose => 2 , noperldoc => 1) if (@ARGV != 1);

  my %h_uniq=();
  open (FQ, $ARGV[0]) or die "Can't open file $ARGV[0] : $!";
  while (my $line=<FQ>) {
    my $ac=$line;
    my $seq=<FQ>;
    chomp $seq;
    my $ac2=<FQ>;
    my $qual=<FQ>;
    if (exists($h_uniq{$seq})) {
      $h_uniq{$seq}++;
      next;
    }
    $h_uniq{$seq}=1;
    #printf "%s%s\n+\n%s",$ac,$seq,$qual;
  }
  foreach my $k (keys(%h_uniq)) {
    print "$k\t$h_uniq{$k}\n";
  }
}

=pod

=head1 NAME

        fastqnr.pl - Count occurence number from a fastq file

=head1 DESCRIPTION

        From fastq file provided build a file like : sequence<tab>number
        sorted by sequence.
        Warning: each sequence of fastq file must be on 4 lines

=head1 SYNOPSIS

        fastqnr.pl FASTQ > FASTQ.nr

=head1 OPTIONS

        -h,--help
	
=cut
