Load Packages

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(stringr)

Read in Data

BP <- read.table(
  file = "labels_BP.csv",
  header = FALSE,
  sep = "\n",
  stringsAsFactors = FALSE,
  quote = "",
  fill = TRUE,
  comment.char = ""
)

CC <- read.table(
  file = "labels_CC.csv",
  header = FALSE,
  sep = "\n",
  stringsAsFactors = FALSE,
  quote = "",
  fill = TRUE,
  comment.char = ""
)

MF <- read.table(
  file = "labels_MF.csv",
  header = FALSE,
  sep = "\n",
  stringsAsFactors = FALSE,
  quote = "",
  fill = TRUE,
  comment.char = ""
)

Split into 3 columns

BP <- as.data.frame(
  str_split_fixed(BP$V1, ",", 3),
  stringsAsFactors = FALSE
)

CC <- as.data.frame(
  str_split_fixed(CC$V1, ",", 3),
  stringsAsFactors = FALSE
)

MF <- as.data.frame(
  str_split_fixed(MF$V1, ",", 3),
  stringsAsFactors = FALSE
)

Remove unwanted rows

BP <- BP[!grepl("_", BP$V1), ]
BP <- BP[!grepl("gene", BP$V1, ignore.case = TRUE), ]

CC <- CC[!grepl("_", CC$V1), ]
CC <- CC[!grepl("gene", CC$V1, ignore.case = TRUE), ]

MF <- MF[!grepl("_", MF$V1), ]
MF <- MF[!grepl("gene", MF$V1, ignore.case = TRUE), ]

Rename the Columns

names(BP) <- c("gene", "validation", "BP")
names(CC) <- c("gene", "validation", "CC")
names(MF) <- c("gene", "validation", "MF")

Check

head(BP)
##      gene validation                      BP
## 2 YAL008W   Verified                     ion
## 3 YBR255W   Verified      biological_process
## 4 YGR164W    Dubious      biological_process
## 5 YGR131W   Verified             protein,ion
## 6 YNL003C   Verified           transport,ion
## 7 YBR135W   Verified regulation,ion,positive
head(CC)
##      gene validation                 CC
## 2 YAL008W   Verified membrane,mitochond
## 3 YBR255W   Verified                   
## 4 YGR164W    Dubious cellular_component
## 5 YGR131W   Verified           membrane
## 6 YNL003C   Verified mitochond,membrane
## 7 YBR135W   Verified
head(MF)
##      gene validation                        MF
## 1 YAL008W   Verified        molecular_function
## 2 YBR255W   Verified        molecular_function
## 3 YGR164W    Dubious        molecular_function
## 4 YGR131W   Verified        molecular_function
## 5 YNL003C   Verified transmembrane,transporter
## 6 YBR135W   Verified            kinase,histone

lapply function

lapply(
  list(BP = BP, CC = CC, MF = MF),
  dim
)
## $BP
## [1] 6011    3
## 
## $CC
## [1] 6011    3
## 
## $MF
## [1] 6011    3

Remove extra columns and merge

CC <- CC[, -2]
MF <- MF[, -2]

mergedLabels <- left_join(
  BP,
  CC,
  by = "gene"
)

mergedLabels <- left_join(
  mergedLabels,
  MF,
  by = "gene"
)

head(mergedLabels)
##      gene validation                      BP                 CC
## 1 YAL008W   Verified                     ion membrane,mitochond
## 2 YBR255W   Verified      biological_process                   
## 3 YGR164W    Dubious      biological_process cellular_component
## 4 YGR131W   Verified             protein,ion           membrane
## 5 YNL003C   Verified           transport,ion mitochond,membrane
## 6 YBR135W   Verified regulation,ion,positive                   
##                          MF
## 1        molecular_function
## 2        molecular_function
## 3        molecular_function
## 4        molecular_function
## 5 transmembrane,transporter
## 6            kinase,histone

#Export

write.csv(
  mergedLabels,
  file = "mergedLabels.csv",
  row.names = FALSE
)