#!/usr/bin/perl -w
use strict;
use warnings;

my %coordinates           = (); # if coordinates and color are the same ... then avoid the dot (but change the transparency to stronger ?)
my $file = $ARGV[0] or die "Need to get a CSV file AND a SIZE on the command line\n";
my $l    = $ARGV[1] or die "Need to get a CSV file AND a SIZE on the command line\n";
open(my $data, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$data>) {
  chomp $line;
  # downsample each coordinate for printing
  my @linefields = split "\t" , $line;
  for my $i (0 .. $#linefields){
    $linefields[$i] =~ s/(0\.[0-9][0-9][0-9])[0-1][0-9]*/${1}0/;
    $linefields[$i] =~ s/(0\.[0-9][0-9][0-9])[2-3][0-9]*/${1}2/;
    $linefields[$i] =~ s/(0\.[0-9][0-9][0-9])[4-5][0-9]*/${1}4/;
    $linefields[$i] =~ s/(0\.[0-9][0-9][0-9])[6-7][0-9]*/${1}6/;
    $linefields[$i] =~ s/(0\.[0-9][0-9][0-9])[8-9][0-9]*/${1}8/;
  }
  # check if double dot there
  my $ignore = 0;
  for my $i (1 .. $l-1) {
      my $j = $i + $l;
      my $k = $i % ($l/8);
      my $key = "".${k}.":".($linefields[$i]).",".($linefields[$j]);
      if (defined $coordinates{$key}) {
          $linefields[$i] = $linefields[$j] = "\'\'";
          $ignore = $ignore+1;
	  $coordinates{$key} = $coordinates{$key} + 1;
      } else {
          $coordinates{$key} = 1;
      }
  }
  # reprint again if at least one coordinate non empty
  if ($ignore < $l-1) {
    for my $i (0 .. $#linefields){
      print "".($linefields[$i])."\t";
    }
    print "\n";
  }
}
