*This document details visualization in anvio*

Anvio is run in a dedicated environment.

```{bash}
conda activate anvio-7.1
```

Get bin info into a format that anvio can use. This means concatenating the bin files for each method, so there's a list of which contig/read goes in which bin

```{r}
path <- list.dirs("../data/Bins")

for (i in 2:7){
  
  DF <- NULL
  
  pathname <- path[i]
  filelist <- list.files(paste0(pathname, "/"))
  
  for (filename in filelist){
    df <- read.csv(paste0(pathname, "/", filename), header = F)
    df <- as.data.frame(df)
    colnames(df) <- "read"
    df$bin <- str_replace(filename, "[.]", "_")
    if (basename(pathname) == "24_sample_bam_bins"){
      df$bin <- str_replace(df$bin, "24", "twentyfour")
    }
    if (basename(pathname) == "47_sample_bam_bins"){
      df$bin <- str_replace(df$bin, "47", "fortyseven")
    }
    DF <- rbind(DF, df) 
  }
  
  write.table(DF, paste0("../output/all_bins/", basename(pathname), ".tsv"), row.names = F, col.names = F, quote = F, sep = "\t")
  
}
```

Import all bins into anvio

```{bash}
# Example for one bin import, change import and -C for each

anvi-import-collection "./github/jordan-marinimicrobia/output/all_bins/47_sample_bam_bins_secondhalf.tsv" -p "./Downloads/plus_PROFILE.db" -c "./Library/CloudStorage/GoogleDrive-jwinter2@uw.edu/Shared drives/Rocap Lab/Project_ODZ_Marinimicrobia_Jordan/Anvio/assembly_plus/1058_P1_2018_585_0.2um_assembly_plus.db" --contigs-mode -C fortyseven_otherhalf
```

Run interactive browser

```{bash}
anvi-interactive -p "./Downloads/plus_PROFILE.db" -c "./Library/CloudStorage/GoogleDrive-jwinter2@uw.edu/Shared drives/Rocap Lab/Project_ODZ_Marinimicrobia_Jordan/Anvio/assembly_plus/1058_P1_2018_585_0.2um_assembly_plus.db"
```

Dig into "contaminated" bins to see how/why they are contaminated. Reminder that "." is changed to "_" and 24 and 47 are written out in the anvi bin database.

```{bash}
anvi-refine -p "./Downloads/assembly_PROFILE.db" -c "./Library/CloudStorage/GoogleDrive-jwinter2@uw.edu/Shared drives/Rocap Lab/Project_ODZ_Marinimicrobia_Jordan/Anvio/assembly_only/1058_P1_2018_585_0.2um_assembly.db" -C assembly -b assembly_bin_35
```

Get summary statistics

```{r}
summary <- read.table("../output/anvio_outputs/assembly_plus_summary.txt", sep = "\t", header = T)
summary(summary)
```

Creating anvio dbs for my bins
```{bash}
anvi-gen-contigs-database -f sulf_genomes/assembly_plus_bin_4.fa -o sulfbin4.db

anvi-run-hmms -c sulf_genomes/dbs/sulfbin4.db
anvi-run-scg-taxonomy -c sulf_genomes/dbs/sulfbin4.db
anvi-scan-trnas -c sulf_genomes/dbs/sulfbin4.db
anvi-run-ncbi-cogs -c sulf_genomes/dbs/sulfbin4.db
anvi-run-kegg-kofams -c sulf_genomes/dbs/sulfbin4.db

anvi-gen-genomes-storage -e sulf-external-genomes.txt \
                         -o sulf-GENOMES.db

anvi-pan-genome -g sulf-GENOMES.db -n sulfitobacter

anvi-display-pan -g sulf-GENOMES.db -p sulfitobacter/sulfitobacter-PAN.db
```

