---
title: "05-Alignment data"
author: "Hannia Larino"
date: "2025-04-29"
output: html_document
---
Purpose: Create and inspect and alignment files. Including visualizing and capturing “outside” graphics. Publish notebook in rpubs and provide link at top of code.

**Task 1**: Looking at alignment files
*Downloading alignment data 
**Reminder - these are BIG files, be sure to IGNORE on commit.**


```{r, engine='bash'}

cd /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data
curl -O https://gannet.fish.washington.edu/seashell/bu-mox/scrubbed/120321-cvBS/19F_R1_val_1_bismark_bt2_pe.deduplicated.sorted.bam
curl -O https://gannet.fish.washington.edu/seashell/bu-mox/scrubbed/120321-cvBS/19F_R1_val_1_bismark_bt2_pe.deduplicated.sorted.bam.bai
```

```{r, engine='bash'}         
cd /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data
curl -O https://gannet.fish.washington.edu/seashell/bu-mox/data/Cvirg-genome/GCF_002022765.2_C_virginica-3.0_genomic.fa
curl -O https://gannet.fish.washington.edu/seashell/bu-mox/data/Cvirg-genome/GCF_002022765.2_C_virginica-3.0_genomic.fa.fai
```


**Visualize with tview**
*Run the following below in Terminal as is interactive

```{r, engine='bash'}
/home/shared/samtools-1.12/samtools tview \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data/19F_R1_val_1_bismark_bt2_pe.deduplicated.sorted.bam \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data/GCF_002022765.2_C_virginica-3.0_genomic.fa
```



*INSER IMAGE OF TERMINAL**

```{r}
knitr::include_graphics("/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/tview results.png")
```


**TASK 2**
* 2 chunks below: Aligning WGS data and visualizing in IGV


```{r, engine='bash'}
cd /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data
curl -O https://owl.fish.washington.edu/nightingales/C_gigas/F143n08_R2_001.fastq.gz
curl -O https://owl.fish.washington.edu/nightingales/C_gigas/F143n08_R1_001.fastq.gz
```

```{r, engine='bash'}
cd /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data
curl -O https://gannet.fish.washington.edu/panopea/Cg-roslin/cgigas_uk_roslin_v1_genomic-mito.fa
curl -O https://gannet.fish.washington.edu/panopea/Cg-roslin/cgigas_uk_roslin_v1_genomic-mito.fa.fai
curl -O https://gannet.fish.washington.edu/panopea/Cg-roslin/GCF_902806645.1_cgigas_uk_roslin_v1_genomic-mito.gtf
```
**Alignment**

```{r, engine='bash'}
/home/shared/hisat2-2.2.1/hisat2-build \
-f /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data/cgigas_uk_roslin_v1_genomic-mito.fa \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/cgigas_uk_roslin_v1_genomic-mito.index
```

```{r, engine='bash'}
/home/shared/hisat2-2.2.1/hisat2 \
-x /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/cgigas_uk_roslin_v1_genomic-mito.index \
-p 4 \
-1 /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data/F143n08_R1_001.fastq.gz \
-2 /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data/F143n08_R2_001.fastq.gz \
-S /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas.sam
```


```{bash}
tail -1 /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas.sam
```

```{r, engine='bash'}
# Convert SAM to BAM, using 4 additional threads
/home/shared/samtools-1.12/samtools view -@ 4 -bS \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas.sam > /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas.bam
```

```{r, engine='bash'}
# Sort the BAM file, using 4 additional threads
/home/shared/samtools-1.12/samtools sort -@ 4 \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas.bam -o /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas_sorted.bam

# Index the sorted BAM file (multi-threading is not applicable to this operation)
/home/shared/samtools-1.12/samtools index \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas_sorted.bam
```




```{r, engine='bash'}
/home/shared/bcftools-1.14/bcftools mpileup --threads 4 --no-BAQ \
--fasta-ref /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/data/cgigas_uk_roslin_v1_genomic-mito.fa \
/home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_cgigas_sorted.bam > /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_mpileup_output.txt
```

```{r, engine='bash'}
tail /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_mpileup_output.txt
```

```{r, engine='bash'}
cat /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_mpileup_output.txt \
| /home/shared/bcftools-1.14/bcftools call -mv -Oz \
> /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_mpile.vcf.gz
```


```{r, engine='bash'}
zgrep "^##" -v /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_mpile.vcf.gz | \
awk 'BEGIN{OFS="\t"} {split($8, a, ";"); print $1,$2,$4,$5,$6,a[1],$9,$10}' | head

```


The code below might not work. That is fine. The VCF in the above chunk can be used for visualization in IGV.

```{r, engine='bash'}
/home/shared/bcftools-1.14/bcftools call \
-v -c /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/F143_mpile.vcf.gz \
> /home/shared/8TB_HDD_02/hannia/Hannia-jelly/assignments/output/output/F143_mpile_calls.vcf
```



