---
title: "Project"
output:
  html_document:
    df_print: paged
---


```{r setup, include=FALSE}
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
)
```

Make Bedgraph from top SNPS

```{r}
# Load necessary library
library(readr)

# Read CSV (assuming your file is called "input.csv")
df <- read_csv("/home/shared/8TB_HDD_02/thielkla/Karina-chinook/Karina-chinook-v2/project/Top_1__FST_Loci.csv", col_types = cols())

# Drop the first column
df <- df[, -1]

# Rename columns for clarity
colnames(df) <- c("chrom", "pos", "score")

# Convert to bedGraph format: chrom, start (0-based), end (1-based), score
df$start <- df$pos - 1
df$end <- df$start + 1

# Reorder columns
bedgraph <- df[, c("chrom", "start", "end", "score")]

# Write to tab-separated file with no header
write.table(bedgraph, file = "/home/shared/8TB_HDD_02/thielkla/Karina-chinook/Karina-chinook-v2/project/TopSNP.bedgraph", sep = "\t", quote = FALSE, row.names = FALSE, col.names = FALSE)
```

CLOSEST SNPs and genes

```{bash}
curl -O https://owl.fish.washington.edu/halfshell/genomic-databank/Olurida_v081-20190709.gene.gff
```

```{bash}
sort -k1,1 -k4,4n Olurida_v081-20190709.gene.gff > Olurida_v081-20190709.gene.sorted.gff
```

```{bash}
sort -k1,1 -k2,2n output/TopSNP.bedgraph > output/TopSNP.sorted.bedgraph
```


```{bash}
/home/shared/bedtools2/bin/bedtools closest \
-a output/TopSNP.sorted.bedgraph \
-b Olurida_v081-20190709.gene.sorted.gff \
> output/09-snp-gene-closet.out

head output/09-snp-gene-closet.out
```

