#!/bin/csh -efv

set atlas=/hpc/group/vertgenlab/riley/20220126_HAQER_SCREEN/singleCellAtacAtlas/PeaksFixed
set dDir=/work/cf189/runPairwiseAlignments/deadNode/hg38
set bDir=/work/cf189/runPairwiseAlignments/birthNode
set coverage=$bDir/coverageCalls.hg38
set speciesNodes=/hpc/group/vertgenlab/vertCons/60way/speciesNodeFiles/hg38.speciesNode.list
set out=allDeathStats.updated.tsv
set db="hg38"

foreach s (`cat species.list`)

if ($s != $db) then

cat $dDir/node*.$s.*.bed > $dDir/$s.dead.bed

$GOBIN/bedMerge -keepAllNames $dDir/$s.dead.bed $dDir/$s.dead.merged.bed
endif
end

touch $out
rm $out
touch $out

foreach cellType ($atlas/*.gz)
set name=$cellType:t:r:r

	foreach sN (`cat $speciesNodes`)
	set species=$sN:r:t
	set node=$sN:e:t
	

		if ($species != "petMar3") then
			set sum=0
			foreach i (`seq 17 -1 $node`)
			echo $i
	
			set tmp=`grep $name $coverage/node$i.bed | wc -l` 
			set sum=`expr $sum + $tmp`
			end

		set dead=`grep $name $dDir/$species.dead.bed | wc -l`


		echo "$name	$species	$sum	$dead" >> $out

		endif
	end

end

