#!/bin/bash

# This script has no looping ability, so should be run $for FI in `cat files.list`; do run_mia $FI fasta name; done

SAMPLE=$1 # This cannot be from multiple samples! This is list of fastq files, with r1,r2,merged,whatever separated by ';'
echo $SAMPLE
shift
REFERENCE=$1
echo $REFERENCE
shift
REF_NAME=$1
echo $REFERENCE


FILE=${SAMPLE##*/}
BASENAME=${FILE%.fastq.gz}

echo "${BASENAME}.${REF_NAME}"

SAMPLE_NEW=`echo $SAMPLE | tr ";" " "`

cat $SAMPLE_NEW > ${BASENAME}.${REF_NAME}.fastq.gz

FASTQ=${BASENAME}.${REF_NAME}.fastq.gz
echo "$FASTQ"
# run MIA
echo "command line:"
echo mia -r "${REFERENCE}" -f <(zcat "${FASTQ}") -c -C -U -s /h/andrewsharo/Downloads/mapping-iterative-assembler/matrices/ancient.submat.txt -i -F -k 14 -m ${BASENAME}.${REF_NAME}.maln
mia -r "${REFERENCE}" -f <(zcat "${FASTQ}") -c -C -U -s /h/andrewsharo/Downloads/mapping-iterative-assembler/matrices/ancient.submat.txt -i -F -k 14 -m ${BASENAME}.${REF_NAME}.maln

# run MA to get stats/consensus
ma -M ${BASENAME}.${REF_NAME}.maln* -f 3 > ${BASENAME}.${REF_NAME}.maln.mia_stats.txt &

ma -M ${BASENAME}.${REF_NAME}.maln* -f 2 > ${BASENAME}.${REF_NAME}.maln.mia_coverage_per_site.txt &

ma -M ${BASENAME}.${REF_NAME}.maln* -f 5 > ${BASENAME}.${REF_NAME}.maln.mia_consensus.fasta &

# create file needed to generate consensus mitochondria genome
ma -M ${BASENAME}.${REF_NAME}.maln* -f 41 > ${BASENAME}.${REF_NAME}.maln.cov_filter_input.txt

# remove the fastq I created
rm $FASTQ

