---
title: 3D7 Telomere Repeat Analysis 
---

```{r setup, echo=FALSE, message=FALSE}
source("../common.R")
```

# Chromosome completion analysis  

## Tandem repeat analysis to determine end of chromsome

Run tandem repeat finder[@Benson1999-hx] to analyze the ends
```{bash, eval =F}
nohup elucidator runTRF --fasta /tank/data/genomes/plasmodium/genomes/pf/genomes/Pf3D7.fasta --supplement --dout trf_Pf3D7 --overWriteDir & 
nohup elucidator runTRF --fasta /tank/data/genomes/plasmodium/genomes/pf/genomes/PfHB3.fasta --supplement --dout trf_PfHB3 --overWriteDir & 

```

### TAREs  

The ends of the telomeres have six blocks of repetitive sequences that were designated [telomere-associated repetitive elements (TAREs 1–6) ](https://www.sciencedirect.com/science/article/pii/0166685188900552) arranged in specific structures[@Vernick1988-mu]. 

All plasmodium falciparum telomeres end with a 7bp tandem repeat termed TARE-1 
Subtelomeric block 1 (SB-1, equivalent to TARE-1), contains the 7-bp telomeric repeat in a variable number of near-exact copies 
represented by  the  consensus,  TT(T/C)AGGG at the ends and CCCT[AG]AA at the fronts  

```{r}
startTare1Pats = c(
  "^CCCT[AG]AA$", 
  "^CCT[AG]AAC$", 
  "^CT[AG]AACC$", 
  "^T[AG]AACCC$", 
  "^[AG]AACCCT$", 
  "^AACCCT[AG]$", 
  "^ACCCT[AG]A$"
)
endTare1Pats = c(
  "^TT[TC]AGGG$", 
  "^T[TC]AGGGT$", 
  "^[TC]AGGGTT$", 
  "^AGGGTT[TC]$", 
  "^GGGTT[TC]A$", 
  "^GGTT[TC]AG$", 
  "^GTT[TC]AGG$"
)

startTare1Pats_14 = c(
  "^CCCT[AG]AACCCT[AG]AA$", 
  "^CCT[AG]AACCCT[AG]AAC$", 
  "^CT[AG]AACCCT[AG]AACC$", 
  "^T[AG]AACCCT[AG]AACCC$", 
  "^[AG]AACCCT[AG]AACCCT$", 
  "^AACCCT[AG]AACCCT[AG]$", 
  "^ACCCT[AG]AACCCT[AG]A$"
)
endTare1Pats_14 = c(
  "^TT[TC]AGGGTT[TC]AGGG$", 
  "^T[TC]AGGGTT[TC]AGGGT$", 
  "^[TC]AGGGTT[TC]AGGGTT$", 
  "^AGGGTT[TC]AGGGTT[TC]$", 
  "^GGGTT[TC]AGGGTT[TC]A$", 
  "^GGTT[TC]AGGGTT[TC]AG$", 
  "^GTT[TC]AGGGTT[TC]AGG$", 
  "^AGTAAGTAAGTAAG$"
)

```


## Read in repeats  
```{r}
chromLengths = readr::read_tsv("surroundingRegionsMaterials/chromLengths/Pf3D7.txt", col_names = c("chrom", "length"))

repeats = readr::read_tsv("surroundingRegionsMaterials/alltrfs/trf_Pf3D7/repeats.bed", col_names = F) %>% 
  separate(X4, sep = "__", into = c("name", "repeatInfo"), remove = F) %>% 
  separate(repeatInfo, sep = "_x", into = c("repeatUnit", "repeatNumber"), convert = T, remove = F) %>% 
  mutate(repeatUnitLen = nchar(repeatUnit))%>% 
  mutate(tare1 = (grepl(paste0(c(startTare1Pats, startTare1Pats_14), collapse = "|"), repeatUnit) | 
                  grepl(paste0(c(endTare1Pats, endTare1Pats_14), collapse = "|"), repeatUnit))) %>% 
  mutate(SB3 = (repeatUnitLen == 21 | repeatUnitLen == 42) & X5 > 1000) %>% 
  left_join(
    chromLengths %>% 
      rename(X1 = chrom, 
             chromLen = length)
  )
  
```

```{r}
repeats_filt = repeats %>% 
  filter(X1 %!in% c("Pf3D7_API"))


repeats_filt_starts = repeats_filt %>% 
  filter(X2 <= 120000)

repeats_filt_ends = repeats_filt %>% 
  mutate(endPos = chromLen - 120000) %>% 
  filter(X2 >= endPos)



```

```{r}
repeats_filt_starts_ends = bind_rows(
  repeats_filt_starts, 
  repeats_filt_ends
)%>% 
    mutate(repeatType = case_when(
      tare1 ~ "TARE1", 
      SB3 ~ "SB3", 
      T ~ "other"
    ))

repeats_filt_starts_ends_tares = repeats_filt_starts_ends %>% 
  filter(repeatType != "other")

write_tsv(repeats_filt_starts_ends_tares, "pf3D7_repeats_filt_starts_ends_tares.tsv")

repeats_filt_starts_ends_rp21_42 = repeats_filt_starts_ends %>% 
  filter(X1 != "3D7_00_01") %>% 
  filter(repeatUnitLen == 21 | repeatUnitLen == 42) %>% 
  filter(X5 > 200, repeatNumber > 20)

```

```{r}
writeFasta<-function(data, filename, openMode = "w"){
  if(stringr::str_ends(filename, ".gz")){
    fileConn<-gzfile(filename, open = openMode)
  }else{
    fileConn<-file(filename, open = openMode)
  }
  for (rowNum in 1:nrow(data)){
    cat(paste(">", data[[rowNum,"name"]], sep = ""), file = fileConn, sep = "\n")
    cat(data[[rowNum,"seq"]], file = fileConn, sep = "\n")
  }
  close(fileConn)
}

writeFasta(repeats_filt_starts_ends_rp21_42 %>%
             filter(!is.na(endPos)) %>% 
             select(X4, repeatUnit) %>% 
             rename(name = X4, 
                    seq = repeatUnit), 
           "repeatUnits_21_42_end.fasta")

writeFasta(repeats_filt_starts_ends_rp21_42 %>%
             filter(is.na(endPos)) %>% 
             select(X4, repeatUnit) %>% 
             rename(name = X4, 
                    seq = repeatUnit), 
           "repeatUnits_21_42_starts.fasta")


```





## Starts 
```{r}
for(chr in unique(sort(repeats_filt_starts$X1))){
  repeats_filt_starts_chr = repeats_filt_starts %>% 
    filter(X1 == chr) %>% 
    mutate(row_id = row_number()) %>% 
    mutate(repeatType = case_when(
      tare1 ~ "TARE1", 
      SB3 ~ "SB3", 
      T ~ "other"
    ))

  print(  ggplot(repeats_filt_starts_chr) + 
    geom_rect(aes(
      xmin = X2, 
      xmax = X3-1, 
      ymin = row_id, 
      ymax = row_id + 1,
      fill = repeatType
    )) + 
    sofonias_theme + 
    labs(title = chr) + 
      scale_fill_tableau()) 
}
```


## Ends   

```{r}
for(chr in unique(sort(repeats_filt_ends$X1))){
  repeats_filt_ends_chr = repeats_filt_ends %>% 
    filter(X1 == chr) %>% 
    mutate(row_id = row_number()) %>% 
    mutate(repeatType = case_when(
      tare1 ~ "TARE1", 
      SB3 ~ "SB3", 
      T ~ "other"
    ))

  print(  ggplot(repeats_filt_ends_chr) + 
    geom_rect(aes(
      xmin = X2, 
      xmax = X3-1, 
      ymin = row_id, 
      ymax = row_id + 1, 
      fill = repeatType
    )) + 
    sofonias_theme + 
    labs(title = chr) + 
      scale_fill_tableau())
}
```

