#! /usr/bin/perl 
  
# Copyright 2009 Matteo Cesaroni, Lucilla Luzi
#
# This program is free software; ; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 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.


$fname=$ARGV[0];  #legge il nome del file che gli passi dopo il comando
$col3=$ARGV[1];
 
qx {sort -k 1,1 -k 4,4n $fname >$fname.sortato};   
 
open(FILE, "< $fname.sortato") or die "Cannot find file $fname\n";

@array=<FILE>; 

print "track type=wiggle_0 name=\"$col3\" description=\"raw_data ratio\" visibility=full autoscale=off maxHeightPixels=100:50:20 color=200,100,0 altColor=0,100,200 \n";

for ($i=0;$i<$#array;$i++){
	@array_new= split("\t",$array[$i]);
	@array_new2=split("\t",$array[$i+1]);
	$dist=$array_new[4]-$array_new2[3];
	if (($array_new[4]>=$array_new2[3])&&("$array_new[0]"eq"$array_new2[0]")){
		$fine=$array_new2[3]-1;
	}	  
	else {
		$fine=$array_new[4];
		
		}
	print "$array_new[0]\t$array_new[3]\t$fine\t$array_new[5]\n";
	
	}
	@array_fine=split("\t",	$array[$#array]);
	print "$array_fine[0]\t$array_fine[3]\t$array_fine[4]\t$array_fine[5]\n";

 close FILE;
