#! /bin/bash

# author: X.Gan

function print_mapper_init {
    if [ "$#" -lt 1 ]; then
	exit
	echo "print_mapper_initial mappername" >2
    fi
    mappername=$1

    if [ "$mappername" == "smalt" ]; then
	print_smalt_init
	return
    fi
    if [ "$mappername" == "stampy" ]; then
	print_stampy_init
	return
    fi
    if [ "$mappername" == "bwa" ]; then
	print_bwa_init
	return
    fi
    if [ "$mappername" == "maq" ]; then
	print_maq_init
	return
    fi
}          

function print_mapper_run {
    if [ "$#" -lt 1 ]; then
	echo "print_mapper_run mappername" >2
	exit
    fi
    mappername=$1
    
    tmps=${bampart_file%.bam}
    sampart_file=$tmps.sam
    mappart_file=$tmps"_map.out"
    summary_file=$tmps"_summary.out"
    unmap_file=$tmps"_unmap.out"

    if [ "$mappername" == "smalt" ]; then
	print_smalt_run
	return
    fi
    if [ "$mappername" == "stampy" ]; then
	print_stampy_run
	return
    fi
    if [ "$mappername" == "bwa" ]; then
	print_bwa_run
	return
    fi
    if [ "$mappername" == "maq" ]; then
	print_maq_run
	return
    fi
}          


##################################################

function print_smalt_init {
    if [ "$ref_file.sma" -ot "$ref_fas" ]; then
	echo "$mapper_addr index -k 13 -s 6 $ref_file $ref_fas"
    fi
}
function print_stampy_init {
    if [ "$ref_file.sthash" -ot "$ref_fas" ]; then
        echo "$mapper_addr --species=$(basename $ref_file) --assembly=$(basename $ref_file) -G $ref_file $ref_fas" 
	echo "$mapper_addr -g $ref_file -H $ref_file"
    fi

    if [ "$ref_fas.bwt" -ot "$ref_fas" ]; then 
        echo "$bwa_addr index -a bwtsw $ref_fas" 
	echo "if [ \"$ref_fas.bwt\" -ot \"$ref_fas\" ]; then"
	echo "            $bwa_addr index -a is $ref_fas "
	echo "fi"
    fi    
    
    if [ "$ref_fas.fai" -ot "$ref_fas" ]; then
    	echo "$samtools_addr faidx $ref_fas"
    fi
}


function print_maq_init {
    if [ "$ref_file" -ot "$ref_fas" ]; then
        echo  "$mapper_addr fasta2bfa $ref_fas $ref_file"
    fi     

    if [ "$ref_fas.fai" -ot "$ref_fas" ]; then
    	echo "$samtools_addr faidx $ref_fas"
    fi
}

function print_bwa_init {
    if [ "$ref_fas.bwt" -ot "$ref_fas" ]; then 
        echo "$mapper_addr index -a bwtsw $ref_fas" 
	echo "if [ \"$ref_fas.bwt\" -ot \"$ref_fas\" ]; then"
	echo "            $mapper_addr index -a is $ref_fas "
	echo "fi"
    fi    
    
    if [ "$ref_fas.fai" -ot "$ref_fas" ]; then
    	echo "$samtools_addr faidx $ref_fas"
    fi
}

function sam2sortedbam_print {
    if [ "$#" -lt 2 ]; then
	echo "echo something wrong happen" 
    fi

    sampart_file=$1
    bampart_file=$2	

    tmps=${bampart_file%.bam}
    TempLanebam=$tmps"_x1tmp".bam
    Tem2prefix=$tmps"_x2tmp"
    Tem2Lanebam=$Tem2prefix.bam
    Tem3Lanebam=$tmps"_x3tmp".bam

    TOBam="$samtools_addr import"
    echo "$TOBam $ref_fas.fai $sampart_file $TempLanebam"
    echo "rm $sampart_file"
    echo "$samtools_addr sort -n $TempLanebam $Tem2prefix"
    echo "$samtools_addr fixmate $Tem2Lanebam $Tem3Lanebam" 
    echo "$samtools_addr sort $Tem3Lanebam $tmps"
    echo "rm $TempLanebam  $Tem2Lanebam $Tem3Lanebam"
}


function print_smalt_run {
    echo "echo $scriptname"
    echo "echo \"$mapper_addr map $this_map_para -f samsoft -o $mappart_file $ref_file $read_file 2>$summary_file\""
    echo "$mapper_addr map $this_map_para -f samsoft -o $mappart_file $ref_file $read_file 2>$summary_file"

    if [ -n "$RGname" ]; then 
	echo "echo -e \"$groupseq\" >$sampart_file"
	echo "echo -e \"@PG\tID:IMR\tVN:$IMRVERSION\" >>$sampart_file"
	echo -e "awk '/^@/ {print \$0} /^[^@]/ {print \$0\"\\\tRG:Z:$RGname\"}'  $mappart_file >> $sampart_file"
    else
	echo "echo -e \"@PG\tID:IMR\tVN:$IMRVERSION\" >$sampart_file"
	echo "cat $mappart_file >> $sampart_file"
    fi
    
    sam2sortedbam_print $sampart_file $bampart_file
}


function print_stampy_run {
    if [ "$PARALLELCMD" == 1 -o "$PARALLELCMD" == 3 ]; then
	mkdir -p $tdir/tmp
	zz=`mktemp -p $tdir/tmp/`
        echo "cp $ref_file.sthash $zz.sthash"
	echo "cp $ref_file.stidx $zz.stidx"
	ref_file=$zz
    fi

    echo "echo $scriptname"

    if [ -n "$RGname" ]; then 
	rgkeywd=`echo -e "$groupseq" |sed "s/^@RG[[:space:]]//g" | sed "s/[[:space:]]/,/g" `
	echo -e "$mapper_addr --keepreforder -g $ref_file -h $ref_file $this_map_para --readgroup=$rgkeywd --bwaoptions=\"-q10 $ref_fas\" -o $sampart_file -M $read_file  >$summary_file 2>&1"
	echo -e "echo -e \"$mapper_addr --keepreforder --bwa=$bwa_addr -g $ref_file -h $ref_file $this_map_para --readgroup=$rgkeywd --bwaoptions=\"-q10 $ref_fas\" -o $sampart_file -M $read_file  >$summary_file 2>&1\""
    else
	echo -e "$mapper_addr --keepreforder -g $ref_file -h $ref_file $this_map_para --bwaoptions=\"-q10 $ref_fas\" -o $sampart_file -M $read_file  >$summary_file 2>&1"
	echo -e "echo -e \"$mapper_addr --keepreforder --bwa=$bwa_addr -g $ref_file -h $ref_file $this_map_para --bwaoptions=\"-q10 $ref_fas\" -o $sampart_file -M $read_file  >$summary_file 2>&1\""
    fi
    
    sam2sortedbam_print $sampart_file $bampart_file
}

function print_maq_run {
    echo "echo $scriptname"
    echo "echo \"$mapper_addr map $this_map_para -u $unmap_file $mappart_file $ref_file $read_file 2>$summary_file\""
    echo "$mapper_addr map $this_map_para -u $unmap_file $mappart_file $ref_file $read_file 2>$summary_file"

    if [ "$groupseq" ]; then
	echo "echo -e \"$groupseq\" >$sampart_file"
	echo "echo -e \"@PG\tID:IMR\tVN:$IMRVERSION\" >>$sampart_file"
    else
	echo "echo -e \"@PG\tID:IMR\tVN:$IMRVERSION\" >$sampart_file"
    fi

    TOSam=maq2sam-long	
    echo "$TOSam $mappart_file $RGname >> $sampart_file"

    if [ "$numReads" -eq 2 ]; then
	echo -e "if [ -s $unmap_file ]; then 
	unmap2sam.pl $unmap_file $RGname >>$sampart_file
	fi"
    else
	echo -e  "if [ -s "$unmap_file" ]; then 
	unmap2sam.pl -s $unmap_file $RGname >>$sampart_file
	fi"
    fi
    
    sam2sortedbam_print $sampart_file $bampart_file
}



function print_bwa_run {
    echo "echo $scriptname"
  #  alntemp_filf=$tdir$g"/mt1_"$g$i$vrname".sai"
  #  alntemp_filb=$tdir$g"/mt2_"$g$i$vrname".sai"
    
    tmps=${bampart_file%.bam}
    alntemp_filf=$tmps"_left.sai"	
    alntemp_filb=$tmps"_right.sai"	

    echo "echo bwa align"
    if [ "$numReads" -eq 2 ]; then
	zz=0
	for ff in $read_file 
	do
	    if [ $zz -eq 1 ]; then
		read_file_b=$ff
	    else	
		read_file_f=$ff
	    fi
	    let zz++
	done
	echo "$mapper_addr aln $ref_fas $read_file_f > $alntemp_filf" 
	echo "$mapper_addr aln $ref_fas $read_file_b > $alntemp_filb" 
	echo "$mapper_addr sampe $this_map_para $ref_fas $alntemp_filf $alntemp_filb $read_file >$mappart_file 2>$summary_file"
    else
	echo "$mapper_addr aln $ref_fas $read_file > $alntemp_filf" 
	echo "$mapper_addr samse $this_map_para $ref_fas $alntemp_filf $read_file >$mappart_file 2>$summary_file"
    fi
    
    if [ -n "$RGname" ]; then 
	echo "echo -e \"$groupseq\" >$sampart_file"
	echo "echo -e \"@PG\tID:IMR\tVN:$IMRVERSION\" >>$sampart_file"
	echo -e "awk '/^@/ {print \$0} /^[^@]/ {print \$0\"\\\tRG:Z:$RGname\"}'  $mappart_file >> $sampart_file"
    else
	echo "echo -e \"@PG\tID:IMR\tVN:$IMRVERSION\" >$sampart_file"
	echo "cat $mappart_file >> $sampart_file"
    fi
    echo "rm $mappart_file"
    
    sam2sortedbam_print $sampart_file $bampart_file
}



if [ "$#" -gt 0 ]; then
   eval "$1 $2 $3 $4 $5 $6 $7 $8 $9"
fi
