---
title: "13=igv"
author: "Steven Roberts"
date: "`r format(Sys.time(), '%d %B, %Y')`"  
output: 
  github_document:
    toc: true
    toc_depth: 3
    number_sections: true
    html_preview: true
  html_document:
    theme: readable
    highlight: zenburn
    toc: true
    toc_float: true
    number_sections: true
    code_folding: show
    code_download: true
editor_options: 
  markdown: 
    wrap: sentence
---

```{r setup, include=FALSE}
library(knitr)
library(tidyverse)
library(kableExtra)
library(DT)
library(Biostrings)
library(tm)
library(pheatmap)
library(DESeq2)
knitr::opts_chunk$set(
  echo = TRUE,         # Display code chunks
  eval = FALSE,         # Evaluate code chunks
  warning = FALSE,     # Hide warnings
  message = FALSE,     # Hide messages
  fig.width = 6,       # Set plot width in inches
  fig.height = 4,      # Set plot height in inches
  fig.align = "center", # Align plots to the center
  comment = ""         # Prevents appending '##' to beginning of lines in code output
)
```


Need to index genome


```{r, engine='bash'}
/home/shared/samtools-1.12/samtools faidx ../data/ncbi_dataset/data/GCF_031168955.1/GCF_031168955.1_ASM3116895v1_genomic.fna
```

```{r, engine='bash'}
/home/shared/samtools-1.12/samtools faidx ../data/ncbi_dataset/data/GCF_031168955.1/GCF_031168955.1_ASM3116895v1_genomic.fa
```



start of IGV (save as xml)


```
<Session genome="https://gannet.fish.washington.edu/seashell/bu-github/project-cod-temperature/data/ncbi_dataset/data/GCF_031168955.1/GCF_031168955.1_ASM3116895v1_genomic.fa" hasGeneTrack="false" hasSequenceTrack="true" locus="NC_082382.1:6171230-6590510" version="8">
    <Resources>
        <Resource path="https://gannet.fish.washington.edu/seashell/bu-github/project-cod-temperature/data/ncbi_dataset/data/GCF_031168955.1/genomic.gff" type="gff"/>
        <Resource path="https://gannet.fish.washington.edu/seashell/bu-github/project-cod-temperature/data/ncbi_dataset/data/GCF_031168955.1/genomic.gtf" type="gtf"/>
    </Resources>
    <Panel height="807" name="FeaturePanel" width="1500">
        <Track attributeKey="Reference sequence" clazz="org.broad.igv.track.SequenceTrack" fontSize="10" id="Reference sequence" name="Reference sequence" sequenceTranslationStrandValue="POSITIVE" shouldShowTranslation="false" visible="true"/>
        <Track attributeKey="genomic.gtf" clazz="org.broad.igv.track.FeatureTrack" fontSize="10" groupByStrand="false" id="https://gannet.fish.washington.edu/seashell/bu-github/project-cod-temperature/data/ncbi_dataset/data/GCF_031168955.1/genomic.gtf" name="genomic.gtf" visible="true"/>
        <Track attributeKey="genomic.gff" clazz="org.broad.igv.track.FeatureTrack" fontSize="10" groupByStrand="false" id="https://gannet.fish.washington.edu/seashell/bu-github/project-cod-temperature/data/ncbi_dataset/data/GCF_031168955.1/genomic.gff" name="genomic.gff" visible="true"/>
    </Panel>
    <PanelLayout dividerFractions="0.007334963325183374"/>
    <HiddenAttributes>
        <Attribute name="DATA FILE"/>
        <Attribute name="DATA TYPE"/>
        <Attribute name="NAME"/>
    </HiddenAttributes>
</Session>
```


![](https://gannet.fish.washington.edu/seashell/snaps/IGV_2024-05-24_10-33-08.png)
# Converting bams to bedgraph


RUN OVERNIGHT

```{r, engine='bash'}
# Directory containing your BAM files
bam_directory="../output/10-hisat-deseq2"

# Directory to store the resulting BEDGraph files
output_directory="../output/13-IGV"

# Create the output directory if it does not exist
if [ ! -d "$output_directory" ]; then
    mkdir -p "$output_directory"
fi

# Loop over each BAM file in the directory
for bam_file in "$bam_directory"/*sorted.bam
do
    # Generate output filename by replacing .bam with .bedgraph
    bam_filename=$(basename "$bam_file")
    output_file="${output_directory}/${bam_filename%.bam}.bedgraph"

    # Run bedtools genomecov
    /home/shared/bedtools2/bin/bedtools genomecov -ibam "$bam_file" -bg > "$output_file"

    echo "Generated BEDGraph for $bam_file at $output_file"
done

echo "All BEDGraph files have been generated in $output_directory."



```

