#!/bin/bash

cat $1 | {
	#read ignore
	#read from
	read ignore

	read chr0_from
	read chr1_from
	read chr2_from
	read chr3_from
	read chr4_from
	read chr5_from
	read chr6_from
	read chr7_from
	read chr8_from
	read chr9_from
	read chr10_from
	read chr11_from
	read chr12_from
	read chr13_from
	read chr14_from
	read chr15_from
	read chr16_from
	read chr17_from
	read chr18_from
	read chr19_from
	read chr20_from
	read chr21_from
	read chr22_from
	read chr23_from
	read chr24_from
	read chr25_from
	read chr26_from
	read chr27_from
	read chr28_from
	read chr29_from
	read chr30_from
	read chr31_from
	read chr32_from
	read chr33_from
	read chr34_from
	read chr35_from
	read chr36_from
	read chr37_from
	read chr38_from
	read chr39_from
	read chr40_from
	read chr41_from
	read chr42_from
	read chr43_from
	read chr44_from
	read chr45_from

	read ignore #empty dividing line

	#read ignore
	#read to
	read ignore

	read chr0_to
	read chr1_to
	read chr2_to
	read chr3_to
	read chr4_to
	read chr5_to
	read chr6_to
	read chr7_to
	read chr8_to
	read chr9_to
	read chr10_to
	read chr11_to
	read chr12_to
	read chr13_to
	read chr14_to
	read chr15_to
	read chr16_to
	read chr17_to
	read chr18_to
	read chr19_to
	read chr20_to
	read chr21_to
	read chr22_to
	read chr23_to
	read chr24_to
	read chr25_to
	read chr26_to
	read chr27_to
	read chr28_to
	read chr29_to
	read chr30_to
	read chr31_to
	read chr32_to
	read chr33_to
	read chr34_to
	read chr35_to
	read chr36_to
	read chr37_to
	read chr38_to
	read chr39_to
	read chr40_to
	read chr41_to
	read chr42_to
	read chr43_to
	read chr44_to
	read chr45_to

	# now that the input is parsed...
	# let us process it first (because of the bc tool)
	# and then print the metadata

	# let us create 46 lines of this format
	# initial position	density 	new position	density	chr number
	# x y z					d			x y z				d			color
	echo -e "$chr0_from $chr0_to 0"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr1_from $chr1_to 1"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr2_from $chr2_to 2"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr3_from $chr3_to 3"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr4_from $chr4_to 4"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr5_from $chr5_to 5"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr6_from $chr6_to 6"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr7_from $chr7_to 7"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr8_from $chr8_to 8"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr9_from $chr9_to 9"    | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr10_from $chr10_to 10" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr11_from $chr11_to 11" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr12_from $chr12_to 12" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr13_from $chr13_to 13" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr14_from $chr14_to 14" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr15_from $chr15_to 15" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr16_from $chr16_to 16" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr17_from $chr17_to 17" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr18_from $chr18_to 18" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr19_from $chr19_to 19" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr20_from $chr20_to 20" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr21_from $chr21_to 21" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr22_from $chr22_to 22" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr23_from $chr23_to 23" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr24_from $chr24_to 24" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr25_from $chr25_to 25" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr26_from $chr26_to 26" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr27_from $chr27_to 27" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr28_from $chr28_to 28" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr29_from $chr29_to 29" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr30_from $chr30_to 30" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr31_from $chr31_to 31" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr32_from $chr32_to 32" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr33_from $chr33_to 33" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr34_from $chr34_to 34" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr35_from $chr35_to 35" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr36_from $chr36_to 36" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr37_from $chr37_to 37" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr38_from $chr38_to 38" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr39_from $chr39_to 39" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr40_from $chr40_to 40" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr41_from $chr41_to 41" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr42_from $chr42_to 42" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr43_from $chr43_to 43" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr44_from $chr44_to 44" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'
	echo -e "$chr45_from $chr45_to 45" | cut -d' ' -f 7,15,22,30,31 | tr -d '()' | tr ',' '\t'

	#echo "$from $to" | cut -d ' ' -f 4,6,20 | { read a b c; echo "# chromosome ID $a from time $b to time $c"; }
	#echo "#"
	#echo "#tripplet of inital coordinate"
	#echo "#			tripplet of new coordinate"
	#echo "#						chromosome number"
}


# jak to zobrazovat pomoci gnuplotu:
# "data" je ulozeny vystup z tohoto skriptu
#
# otoci a pojmenuje osy:
# set yrange [] reverse
# set zrange [] reverse
# set xlabel "x"
# set ylabel "y"
# set zlabel "z"
#
# vymalovani chromocenter na aktualnich souradnicich (v mikrometrech):
# splot "data" u 1:2:3 pt 6 ps 1;
#
# vymalovani chromocenter pred a po do jednoho obrazku (ze zelenych do cervenych):
# splot "data" u 1:2:3 pt 6 ps 1 lc 2 t "start position", "data" u 4:5:6 pt 6 ps 1 lc 1 t "end position"
#
# vymalovani sipek s relativnimi posuny od nuly (v mikrometrech):
# prepocitani souradnic : cat data | bc -q ruzice_fromZero.bc > data_rel
# gnuplot prikaz        : splot "data_rel" w l
#
# vymalovani sipek s relativnimi posuny umistenymi v prostoru (v mikrometrech):
# prepocitani souradnic : cat data | bc -q ruzice_fromInitial.bc > data_rel2
# gnuplot prikaz        : splot "data_rel2" w l
#
# maluje x,y,densita, pred (zelena), po (cervena) a modre spojnice
# splot [][][] "dataB" u 1:2:4 pt 6 ps 1 lc 2 t "Brown: start xy+density", "dataB" u 5:6:8 pt 6 ps 1 lc 1 t "Brown: end xy+density", "dataB_rel2" u 1:2:4 w l, 10 lc 0
#
# maluje 3D pozice, pred (zelena), po (cervena) a modre spojnice
# splot [][][] "dataB" u 1:2:3 pt 6 ps 1 lc 2 t "Brown: start xy+z", "dataB" u 5:6:7 pt 6 ps 1 lc 1 t "Brown: end xy+z", "dataB_rel2" u 1:2:3 w l, 10 lc 0
