#!/bin/bash

path=$HOME/research/chromonomer_paper/ssc

#
# Create BWA databases of the two genome assemblies
#
bwa index -p $path/genome/bwa_db/ssc $path/genome/cms10.final.assembly.fasta.gz

#
# Align markers to each reference
#
bwa mem $path/genome/bwa_db/ssc $path/markers/all_markers.fa.gz | samtools view -b > $path/aligned/ssc_markers_vs_ssc.bam

#
# Run chromonomer to integrate the pipefish genetic map and genome assembly.
# Include S.acus synteny.
#
outpath=20190917_ssc

chromonomer -p $path/genetic_map/ssc_unprocessed_consensus_genetic_map.tsv \
            -s $path/aligned/ssc_markers_vs_ssc.bam \
            -a $path/genome/cms10.final.assembly.agp.gz \
            -o $path/chrr/${outpath}/ \
            --gff       $path/synteny/ssc_2016_12_20_scafflevel.gff.gz \
            --orth_gtf  $path/synteny/sac.gtf.gz \
            --orthologs $path/synteny/ssc-sac_orthologs.tsv \
            --fasta $path/genome/cms10.final.assembly.fasta.gz \
            --scaffold_prefix SSC --data_version ${outpath} --allpaths_agp \
            --description "Chromonomer 1.10. Including synteny data from S.acus to order scaffolds." \
            2> $path/chrr/${outpath}/chromonomer.log

#
# Generate translated annotation.
#
translate_gtf.py \
    --agp $path/chrr/${outpath}/SSC_genome.agp \
    --scaffolds $path/chrr/${outpath}/SSC_unplaced_scaffolds.agp \
    --gff $path/genome/ssc_2016_12_20_scafflevel.gff.gz \
    --out $path/chrr/${outpath}
