Skip to content
Snippets Groups Projects
gmapRescue.sh 4.23 KiB
Newer Older
#!/bin/bash
##SBATCH --array=0-7
##SBATCH -n1
##SBATCH --mail-user=helene.rimbert@inrae.fr
##SBATCH --mail-type=ALL

# for use with slurm
#chroms=($(echo TraesCS{1..7} TraesCSU))
#chrom=${chroms[$SLURM_ARRAY_TASK_ID]}

# get the list of query and target file for gmap

inputDir=$1
blastdb=$2
Helene Rimbert's avatar
Helene Rimbert committed
gmapdb=$3
gmapdbdir=$4
outputRescueGff=$5
outputRescueWholeGenomeGff=$6

querylist=($(find  $inputDir  -type f -name 'query.fasta'))
targetlist=($(find $inputDir -type f -name 'target.fasta'))

numquery=${#querylist[@]}
numtarget=${#targetlist[@]}
#blastdb='/banks/blast/IWGSCv1.1_repr_mrna'

echo "STARTING AT `date` FOR DATASET $inputDir"
echo "found $numquery query fasta file"
echo "found $numtarget target fasta file"

echo "Check for genes with invilid isbp pairs"
fgrep -h 'Could not identify a pair of marker to use to anchor' *.out |cut -d ' ' -f 15|cut -d '.' -f 1|sort > 3_mapping_wrongISBP_pair.txt

for i in "${querylist[@]}";
do
	rootdir=`dirname $i`
	# extract the name of the gene
Helene Rimbert's avatar
Helene Rimbert committed
	geneid=$(cat $rootdir'/query.fasta'|fgrep '>'|perl -ne 'print $1 if /^\>(TraesCS.+)\ coords/')
	echo "================================================"
	echo " gene is $geneid"
	echo " datadir is $rootdir"

	echo "1. creating gmap db for dataset $rootdir"
Helene Rimbert's avatar
Helene Rimbert committed
	echo "================================================"
	gmap_build -D $rootdir -d query $rootdir'/query.fasta' &
	pid=$!
Helene Rimbert's avatar
Helene Rimbert committed
	if [ -f "$rootdir/GMAPRESCUE" ]
	then
		gmap_build -D $rootdir -d target $rootdir'/target.fasta' &
		pid2=$!
	fi
Helene Rimbert's avatar
Helene Rimbert committed
	wait $pid $pid2
Helene Rimbert's avatar
Helene Rimbert committed
	echo "================================================"
	# on dump d'abord tous les IDs de la banque blast ('-outfmt %i)'),
	# puis on ne garde que les mrna du gène en cours de traitement,
	# et on refait un appel sur la banque blast pour extraire toutes les sequences mrna
	blastdbcmd -db $blastdb -entry all -outfmt %i |fgrep -w $geneid |blastdbcmd -db $blastdb -entry_batch - > $rootdir'/query.mrna.fasta'
		

	echo "3. run gmap on target and query"
Helene Rimbert's avatar
Helene Rimbert committed
	echo "================================================"
	targetindex='target'
	targetindexdir=$rootdir
	targetoutputGff=$rootdir'/gmap.target.gff3'
	gmapexe='gmap'

	# check if we use as target the sub sequence or the entire genome
	if [ -f "$rootdir/UNMAPPED" ]
	then
		echo " Gene $geneid has status UNMAPPED: mapping on the whole genome target"
		targetindex=$gmapdb
		targetindexdir=$gmapdbdir
		targetoutputGff=$rootdir'/gmap.wholeGenome.gff3'
		gmapexe='gmapl'
Helene Rimbert's avatar
Helene Rimbert committed
	eval $gmapexe -n 1 -d $targetindex -D $targetindexdir -f 2 $rootdir'/query.mrna.fasta' > $targetoutputGff &
	pid=$!
	eval gmap -n 1 -d query -D $rootdir -f 2 $rootdir'/query.mrna.fasta' > $rootdir'/gmap.query.gff3' &
	pid2=$!
	wait $pid $pid2

	rm -rf $rootdir'/query' $rootdir'/target'

	echo "4. indexing the query and target genomic"
Helene Rimbert's avatar
Helene Rimbert committed
	echo "================================================"
	samtools faidx $rootdir'/query.fasta'
	samtools faidx $rootdir'/target.fasta'
Helene Rimbert's avatar
Helene Rimbert committed
	if [ -f "$rootdir/GMAPRESCUE" ]
	then
Helene Rimbert's avatar
Helene Rimbert committed
		echo "5. running gffread to extract the pep sequence"
		echo "================================================"
		gffread -x $rootdir'/target.cds.fasta' -y $rootdir'/target.pep.faa' -g $rootdir'/target.fasta' $rootdir'/gmap.target.gff3'
		gffread -x $rootdir'/query.cds.fasta' -y $rootdir'/query.pep.faa' -g $rootdir'/query.fasta' $rootdir'/gmap.query.gff3'
Helene Rimbert's avatar
Helene Rimbert committed
		
		echo "6. running diff to check if the pep sequences are different or not"
		diffresult=$(diff $rootdir'/target.pep.faa' $rootdir'/query.pep.faa')
		if [ "$diffresult" != "" ]
		then
			echo "RESULTS pep of gene $geneid has changed"
			echo $diffresults > $rootdir/CDS_CHANGED
		else
			echo "RESULTS pep of gene $geneid stays the same"
			touch $rootdir/CDS_OK
		fi
Helene Rimbert's avatar
Helene Rimbert committed

	else
		echo " rescue done on the whole genome: we do not check for pep sequences"
		touch $rootdir/CDS_UNKNOWN
Helene Rimbert's avatar
Helene Rimbert committed
### concat all the gff files created when mapping done on whole genome
# for genes mapped on subsequence
gffList=($(find $inputDir -type f -name "gmap.target.gff3" ))
gt gff3 -sort -tidy -retainids "${gffList[@]}" 1> $outputRescueGff 2> $outputRescueGff.log
Helene Rimbert's avatar
Helene Rimbert committed
# for genes mapped on whole genome
gffList=($(find $inputDir -type f -name "gmap.wholeGenome.gff3" ))
gt gff3 -sort -tidy -retainids "${gffList[@]}" 1> $outputRescueWholeGenomeGff 2> $outputRescueWholeGenomeGff.log

echo "ENDED AT `date`"