#!/bin/csh
#The covariance files of the example proteins are given.
#They are generated using a Gaussian Network Model with a covalent force-constant of 10 kcal/(mol A^2), non-covalent force constant of 5 kcal/(mol A^2) and a cutoff radius of 7 A.
#The covariance matrices of the single dynamic domains are generated by rescaling the splitted covariance matrices instead of recalculating them.
#The *.pqrm files can be loaded in VMD by selecting "Determine file type: PDB".

setenv CoMoDo $HOME/CoMoDoPlus/programs/CoMoDo # must be changed to your path!!!
set programPath = $CoMoDo/src
set examplePath = $CoMoDo/examples/data
set scriptPath = $CoMoDo/examples/scripts

# DomainTester setup
set PosCovSegMinSize = 40  # minimal size of positive-covariance segments

# calculate dynamic domains of all proteins
foreach prot (`cat names.dat`)
	cd $examplePath/$prot
	echo "============================================"
	echo $prot
	echo "============================================"
	rm -r structures_NormCoMoDo
	rm -r Dom1
	rm -r Dom2

	# run DomainTester: check if several dynamic domains exist
	$programPath/DomainTester $prot.cov $PosCovSegMinSize > $prot.posCovSeg
	grep nodes $prot.posCovSeg | awk '{printf("%e %d\n", $6/$2, $11);}' > percentNoSegment.dat 
	
	# protein consists only of one domain if more than half of the nodes don't belong to a positive-covariance segment or if less than two non-overlapping positive-covariance segments were found
	set boolDomain = `cat percentNoSegment.dat | awk '{if($1 >= 0.5 || $2 < 2) i=1; else i=0; printf("%d\n", i)}'`

	# 1-domain protein
	if ( $boolDomain == 1) then
		echo "Cluster consists of only 1 dynamic domain"
		echo "1" > domains.dat
	
	# more than 1 domain
	else
		echo "Cluster consists of several dynamic domains"
		mkdir structures_NormCoMoDo # pqrm files for all domains will be stored here
		rm domains.dat

		# run DomainClusterer and create two clusters
		$programPath/DomainClusterer $prot.cov -d 2 > NormCoMoDo.out
		# $prot.domains contains cluster assignment of all residues
		grep nodes NormCoMoDo.out > $prot.domains

		# create splitted pqrm files and splitted, renormalized covariance matrices
		$programPath/splitPQRM.pl $prot
		$programPath/splitCov.pl $prot
		$programPath/rescaleCov.pl $prot.1
		$programPath/rescaleCov.pl $prot.2

		# create subdirectory for each cluster and copy the CoMoDo script, covariance matrices and pqrm files
		mkdir Dom1
		mkdir Dom2
		cp $scriptPath/NormCoMoDo_sub.csh Dom1
		cp $scriptPath/NormCoMoDo_sub.csh Dom2
		mv $prot.1.scaled.cov Dom1/$prot.cov
		mv $prot.2.scaled.cov Dom2/$prot.cov
		mv $prot.1.pqrm Dom1/$prot.pqrm
		mv $prot.2.pqrm Dom2/$prot.pqrm
	
		# repeat procedure for each cluster using the script NormCoMoDo_sub.csh
		echo " "
		echo "Domain 1:"
		cd Dom1
		csh NormCoMoDo_sub.csh $prot $PosCovSegMinSize 1 # last number keeps track of iterations
		echo " "
		echo "Domain 2:"
		cd ../Dom2
		csh NormCoMoDo_sub.csh $prot $PosCovSegMinSize 2
		cd ..
	endif	
	echo " "
	# determine number of dynamic domains
	wc -l domains.dat | awk '{printf("Dynamic domains: %d\n", $1)}'
	echo " "

end
