This function allows to read and reformat mothur .shared file such that columns are features (OTUs) and rows are samples

read.mothur.shared <- function(shared.file){
  otu_data <-read.table(shared.file, header=T)  %>%
    t(.) %>%
    .[2:nrow(.),] %>%
    .[-c(2),] %>%
    data.frame(.)
  colnames(otu_data) <- as.character(unlist(otu_data[1,]))
  otu_data <- otu_data[-1, ]
  write.csv(otu_data, file="otu_data.csv")
  otu_data <-read.csv("otu_data.csv", header=T, row.names = 1)
  otu_data <- data.frame(t(otu_data))
  return(otu_data)
}

Example

library(devtools)
install_github("ravinpoudel/myFunctions")
library(myFunctions)
library(igraph)
library(magrittr)
library(tidyverse)
library(data.table)
# import count data with NOT using read.mothur.shared function
otu_data.reg <- read.table(Sys.glob("*.shared"), sep = "\t")
otu_data.reg[1:4, 1:3]

# import count data with read.mothur.shared function
otu_data.fn <- read.mothur.shared(Sys.glob("*.shared"))
otu_data.fn[1:4, 1:3]

# upload taxanomy file without using read.mothur.taxonomy function
tax.reg <- read.delim(Sys.glob("*.taxonomy"), sep="\t", header=F)
head(tax.reg)

# upload taxanomy file using read.mothur.taxonomy function
tax.fn <- read.mothur.taxonomy(Sys.glob("*.taxonomy"))
head(tax.fn)

# if everything went smoothly then rownames of these two files should get you a true output
all.equal(colnames(otu_data.fn), rownames(tax.fn))


ravinpoudel/myFunctions documentation built on May 9, 2020, 7:39 a.m.