# Run bowtie2 of the negative cleaned samples against the contig reference database
# First build bowtie reference of the contigs
mkdir ./uniprot_contig_virome_trembl_rel_abund
bowtie2-build -f ./ray_contigs_from_total_cat_pairs/Contigs_no_block_with_names.fasta ./uniprot_contig_virome_trembl_rel_abund/bowtie2_contig_build
# Map the sequences to the contigs
mkdir ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits
run.bowtie2.against.contigs () {
    bowtie2 -x ./uniprot_contig_virome_trembl_rel_abund/bowtie2_contig_build -f ./negative_clean_seqs/$1 -S ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits/$1 -L 25 -N 1
}
export -f run.bowtie2.against.contigs
# Run as a subroutine because it can be run much quicker with multiple procs at a time.
ls ./negative_clean_seqs/ | xargs -I {} --max-procs=128 bash run.bowtie2.against.contigs {}

# Rename the files to be .sam files
for file in $(ls ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits); do
    mv ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits/"${file}" ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits/"${file/%.fa/.sam}"
done

# Calculate the hit abundances from bowtie2 using bowtie2 estimation perl script
mkdir ./uniprot_contig_virome_trembl_rel_abund/abundance_from_sam
for file in $(ls ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits); do
    perl calculate_abundance_from_sam.pl ./uniprot_contig_virome_trembl_rel_abund/bowtie2_neg_cleaned_hits/${file} ./uniprot_contig_virome_trembl_rel_abund/abundance_from_sam/${file}
done