# Zvolení Working Directory
setwd("C:/VUT/BAKALARKA/RfilesBc")

################################
# Instalace a nacteni knihoven
BiocManager::install("Biostrings")
BiocManager::install("Rsamtools")
#
library(Biostrings)
library(Rsamtools)
################################


PoolFasta <- function(pool_seq, pool_name) {
  # Funkce pro vytvoreni pojmenovaneho FASTA souboru ze sekvence DNA
  # Vstup1: sekvence DNA
  # Vstup2: jmeno sekvence
  
  pool_string <- DNAStringSet(pool_seq)
  names(pool_string) <- pool_name
  writeXStringSet(pool_string, paste(pool_name, '.fasta', sep = ""))
}
###########

RefPools <- function(ref_seq) {
  # Funkce pro referencnich sekvenci pro jednotlive pooly ve formatu FASTA
  # a csv souboru s indexy zacatku a koncu sekvenci
  # Vstup: referencni sekvence genomu
  
  # Nacteni referencni sekvence
  ref_seq <- readDNAStringSet(ref_seq)
  ref_seq <- ref_seq[[1]]
  
  # Pocatecni a koncove sekvence poolu
  p1_start <- "CGGCTGTATTTCGTTACTGTCTTG"
  p1_end <- "TTGGACACGGACCTACGTACGCCAT"
  p2_start <- "GCGCAGTAAAAGAGGGACGACCTCT"
  p2_end <- "CCGGTTCAAGTGGTGCATAGCAAGC"
  p3_start <- "CCTGCAGGGTACGTAAGTAGAGGAC"
  p3_end <- "AAGCACGTAGCTCTTCCTCGCGTAT"
  p4_start <- "TCGATAAAGGCTGCGATACCTCCAC"
  p4_end <- "AACTTCTCCAAGGCAGGAGGAGTGT"
  tprC_start <- "TTATCAGCCTGAATCGTATGTCCCT"
  tprC_end <- "TCACCGAGACTGCAACAATGGCTCT"
  tprE_start <- "TTCCGAGCCATATCTGCGTACTGCG"
  tprE_end <- "AACACCGGCCGCAGAACATTAAACG"
  
  # Ziskani pozic zacatku a koncu poolu
  p1_indexS <- gregexpr(p1_start, ref_seq)
  p1_indexE <- gregexpr(p1_end, ref_seq)
  p2_indexS <- gregexpr(p2_start, ref_seq)
  p2_indexE <- gregexpr(p2_end, ref_seq)
  p3_indexS <- gregexpr(p3_start, ref_seq)
  p3_indexE <- gregexpr(p3_end, ref_seq)
  p4_indexS <- gregexpr(p4_start, ref_seq)
  p4_indexE <- gregexpr(p4_end, ref_seq)
  tprC_indexS <- gregexpr(tprC_start, ref_seq)
  tprC_indexE <- gregexpr(tprC_end, ref_seq)
  tprE_indexS <- gregexpr(tprE_start, ref_seq)
  tprE_indexE <- gregexpr(tprE_end, ref_seq)
  
  # Zaznamenani poolu do matice
  indices <- matrix(nrow = 6, ncol = 2)
  
  indices[1, 1] <- p1_indexS[[1]]
  indices[1, 2] <- p1_indexE[[1]]
  indices[2, 1] <- p2_indexS[[1]]
  indices[2, 2] <- p2_indexE[[1]]
  indices[3, 1] <- p3_indexS[[1]]
  indices[3, 2] <- p3_indexE[[1]]
  indices[4, 1] <- p4_indexS[[1]]
  indices[4, 2] <- p4_indexE[[1]]
  indices[5, 1] <- tprC_indexS[[1]]
  indices[5, 2] <- tprC_indexE[[1]]
  indices[6, 1] <- tprE_indexS[[1]]
  indices[6, 2] <- tprE_indexE[[1]][1]
  
  # Ziskani sekvenci jednotlivych poolu podle indexu
  pool1_seq <-
    c(ref_seq[indices[1, 1]:length(ref_seq)], ref_seq[1:indices[1, 2] + 24])
  pool2_seq <- ref_seq[indices[2, 1]:indices[2, 2] + 24]
  pool3_seq <- ref_seq[indices[3, 1]:indices[3, 2] + 24]
  pool4_seq <- ref_seq[indices[4, 1]:indices[4, 2] + 24]
  tprC_seq <- ref_seq[indices[5, 1]:indices[5, 2] + 24]
  tprE_seq <- ref_seq[indices[6, 1]:indices[6, 2] + 24]
  
  # Ulozeni sekvenci do souboru FASTA
  PoolFasta(pool1_seq, 'pool1')
  PoolFasta(pool2_seq, 'pool2')
  PoolFasta(pool3_seq, 'pool3')
  PoolFasta(pool4_seq, 'pool4')
  PoolFasta(tprC_seq, 'tprC')
  PoolFasta(tprE_seq, 'tprE')
  
  # Zaznamenaní indexu do souboru csv
  write.table(indices, file = "indices.csv", row.names = F, col.names = F)
}

###########################################


# Vytvoreni referencnich sekvenci pro jednotlive pooly pro vsechny zkoumane
# genomy pomoci funkce RefPools

setwd("C:/VUT/BAKALARKA/RfilesBc/TPA_Grady")
RefPools('reference.fasta')

setwd("C:/VUT/BAKALARKA/RfilesBc/TPA_HaitiB")
RefPools('reference.fasta')

setwd("C:/VUT/BAKALARKA/RfilesBc/TPA_Madras")
RefPools('reference.fasta')

setwd("C:/VUT/BAKALARKA/RfilesBc/TPA_Philadelphia")
RefPools('reference.fasta')

setwd("C:/VUT/BAKALARKA/RfilesBc/TPA_UZ1974")
RefPools('reference.fasta')

##########################################
