###Make sure this script is in the folder with the .fastq files and the .bt2 bowtie2 indexes you want to map to.

###Make bowtie2 index separately from this script
###bowtie2-build hg38_cyto_mt_tRNAs_collapsed_nameFix.fa hg38_tRNA

###Create file of tRNA names and sizes and remove first 6 lines for future R analysis. ###Also creates FASTA for viewing bam files in IGV.

REF=/Volumes/LaCie/Pinkard_et_al_2019/References/hg38_tRNA/hg38_tRNA

bowtie2-inspect -s $REF > tRNA_names_sizes.txt
ex -sc '1d6|x' tRNA_names_sizes.txt
bowtie2-inspect $REF > hg38_tRNA.fa

###Remove 5’ adapter, then 3’ adapter. Remove 5’ trimmed file to save space. Map to bowtie2 index created from FASTA of hg38-tRNAs gtRNAdb (10/20/18) and mitochondrial tRNAs from tRNAdb (10/20/18). Mitochondrial tRNAs references were modified to have “N” in positions where unknown nucleotides were to prevent gaps. tRNAs were also collapsed into single genes for tRNAs with multiple identical genes. Convert .sam to .bam files, then remove .sam files. Compress what is left over to save space.

for x in *.fastq.gz
do
(cutadapt --cut 2 -o temp.fastq.gz $x) 1>>processing.log
(cutadapt -g TCCAACTGGATACTGGN -e 0.2 -o temp2.fastq.gz temp.fastq.gz) 1>>processing.log
(cutadapt -a CCAGTATCCAGTTGGAATT -e 0.2 -o ${x%%.fastq.gz}.fastq.trimmed.gz temp2.fastq.gz) 1>>processing.log
rm temp.fastq.gz
rm temp2.fastq.gz
(bowtie2 --quiet --min-score G,1,8 --local -D 20 -R 3 -N 1 -L 10 -i S,1,0.5 -p 3 -x $REF -U ${x%%.fastq.gz}.fastq.trimmed.gz -S ${x%%.fastq.gz}.sam)
samtools view -b -@ 8 -o ${x%%.fastq.gz}.bam ${x%%.fastq.gz}.sam
rm ${x%%.fastq.gz}.sam
done
