---
title: "Untitled"
output: html_document
date: "2024-06-06"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}
library(tidyverse)
library(dplyr)
```

```{r}
COG_cat <- read.table("COG_categories.tsv", sep="\t",  quote="") 
colnames(COG_cat) <- c("cat_code","color","cat_name")

COG_desc <- read.table("COG_descriptions.tsv", sep="\t",  quote="")
colnames(COG_desc) <- c("gene_family", "cat_code","Name","Gene","Path","Pubmed","PDB")

COG_desc_reformat <- merge(COG_desc %>% separate_rows(cat_code, sep = '(?<=.)(?=.)'), COG_cat, all.x = TRUE) %>% select(cat_name, Path, Name, gene_family) %>% rename("level1"="cat_name","level2"="Path") %>% mutate(level3=NA, .after=level2)
COG_desc_reformat[COG_desc_reformat == ""] <- NA
COG_desc_reformat <- COG_desc_reformat %>% group_by(gene_family) %>% mutate(level1 = paste0(level1, collapse = "; ")) %>% unique()

write.table(COG_desc_reformat,"COG_annot.tsv", quote = FALSE, sep="\t", row.names = FALSE)
```

