#! /usr/bin/perl -w


# Author : HqD
# Version : 0.0.1

use strict;
#use Switch;
use POSIX qw(ceil floor);
use List::Util qw[min max];

&main();

sub main() {
	my $flag = 0;
	my $file_name = "roudier";
	foreach my $arg (@ARGV){
		if ($arg =~ /^-[f]/) {$flag = 1; next;}
		if ($flag==1) {$file_name = $arg;next;}
	}

	col_scanning2($file_name);
	col_freq_counting($file_name.".cols");
}

sub col_scanning2 {
	my $in_file = $_[0];
	my $coor_file= $in_file.".coor";
	
	print "Reading coordinating file ", $coor_file, " ...\n";
	my @coors = undef; my $N_str = 0;
	open(IN, $coor_file ) or die "$!\n";
	while (<IN>) {
		chomp;
		my @line=split /\s+/,$_;
		$coors[$N_str] = $line[0];
		$N_str++;
	}
	close(IN);

	print $in_file, "\n";
	my @histones = undef; my $N_histone=0; my @epi_str = undef; my $temp_str = "";
	open(IN, $in_file ) or die "$!\n";
	while (<IN>) {
		chomp;
		my @line=split /\s+/,$_;
		if (index($line[0], ">")>=0) {
			if ($N_histone>0) {
				$epi_str[$N_histone-1] = $temp_str;
				print length($epi_str[$N_histone-1]), " loci.\n";
			}
			$histones[$N_histone] = substr($line[0], 1, length($line[0])-1);
			print $histones[$N_histone], "\n";
			$temp_str = "";
			$N_histone++;
		} else {
			$temp_str = $temp_str.$line[0];
		}
	}
	close(IN);
	$epi_str[$N_histone-1] = $temp_str;
	print $N_histone, "marks & ", length($epi_str[0]), " loci.\n";
	$N_str = length($epi_str[0]);

	#my @letters = ("S", "H", "M", "I", "L", "N"); my $N_letter = @letters;
	my @letters = ("H", "M", "L", "N"); my $N_letter = @letters;
	my $out_file = $in_file.".cols";
	print $out_file, "\n";
	open(OUT, ">$out_file") or die "$out_file: $!\n";
	for (my $j=0; $j<$N_str; $j++) {
		my $temp_str = "";
		for (my $i=0; $i<$N_histone; $i++) {
			$temp_str = $temp_str.substr($epi_str[$i], $j, 1);
		}
			
		print OUT $coors[$j], "\t", $j+1, "\t", $temp_str, "\n";

	}
	close(OUT); 
	
}

sub col_freq_counting {
	my $in_file = $_[0];
	open(IN, $in_file ) or die "$!\n";
	my $get = 0;
	my $out_file = $in_file.".freq";
	open(OUT, ">$out_file") or die "$out_file: $!\n";
	while (<IN>) {
		if ($get % 1000==0) {print $get, "...\n";}
		chomp;
		my @line=split /\s+/,$_;
		my @result=`grep $line[2] $in_file -n `;
		#print "@result", "\n"; exit;
		my $count = @result;	
		if ($count>=0) {print OUT $line[2], "\t", $count, "\n";}
		$get++;
	}
	close(IN); close(OUT);
	#system("rm ".$in_file); 
}

sub index_letters {
	my $letters = "SHMIL";
	return index($letters, $_[0]);
}

sub hd {
    return ($_[0] ^ $_[1]) =~ tr/\001-\255//;
}

sub permute {
	my $last = pop @_;
	unless (@_) {
		return map [$_], @$last;
	}
	return map { my $left = $_; map [@$left, $_], @$last } permute(@_);
}

sub convert {
	my $strNumber = $_[0];
	$strNumber = sprintf("%04d", $strNumber);
	my $number = $strNumber + 1 - 1;
	return $number;
}
