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]
##      V1        V2      V3
## 1 label     Group numOtus
## 2  0.03 SRh.2p.A1    9405
## 3  0.03 SRh.2p.A2    9405
## 4  0.03 SRh.2p.A3    9405
# import count data with read.mothur.shared function
otu_data.fn <- read.mothur.shared(Sys.glob("*.shared"))
otu_data.fn[1:4, 1:3]
##           Otu00001 Otu00002 Otu00003
## SRh.2p.A1        1        0      510
## SRh.2p.A2      692      201        3
## SRh.2p.A3      208       14        0
## SRh.2p.A4        4      151        0
# upload taxanomy file without using read.mothur.taxonomy function
tax.reg <- read.delim(Sys.glob("*.taxonomy"), sep="\t", header=F)
head(tax.reg)
##         V1     V2
## 1      OTU   Size
## 2 Otu00001 130638
## 3 Otu00002 119471
## 4 Otu00003 108083
## 5 Otu00004  82435
## 6 Otu00005  50921
##                                                                                                                                                                   V3
## 1                                                                                                                                                           Taxonomy
## 2                               k__Fungi(100);p__Ascomycota(100);c__Pezizomycetes(100);o__Pezizales(100);f__Pyronemataceae(100);f__Pyronemataceae_unclassified(100);
## 3                               k__Fungi(100);p__Ascomycota(100);c__Pezizomycetes(100);o__Pezizales(100);f__Pyronemataceae(100);f__Pyronemataceae_unclassified(100);
## 4 k__Fungi(100);p__Basidiomycota(100);c__Agaricomycetes(100);o__Cantharellales(100);f__Ceratobasidiaceae(100);g__Thanatephorus(100);s__Thanatephorus_cucumeris(100);
## 5                   k__Fungi(100);p__Ascomycota(100);c__Dothideomycetes(100);o__Pleosporales(100);f__Pleosporaceae(100);g__Alternaria(100);s__Alternaria_porri(100);
## 6     k__Fungi(100);p__Ascomycota(100);c__Pezizomycetes(100);o__Pezizales(100);f__Pyronemataceae(100);g__unclassified_Pyronemataceae(100);s__Pyronemataceae_sp(100);
# upload taxanomy file using read.mothur.taxonomy function
tax.fn <- read.mothur.taxonomy(Sys.glob("*.taxonomy"))
head(tax.fn)
##           Count  Kingdom           Phylum              Class
## Otu00001 130638 k__Fungi    p__Ascomycota   c__Pezizomycetes
## Otu00002 119471 k__Fungi    p__Ascomycota   c__Pezizomycetes
## Otu00003 108083 k__Fungi p__Basidiomycota  c__Agaricomycetes
## Otu00004  82435 k__Fungi    p__Ascomycota c__Dothideomycetes
## Otu00005  50921 k__Fungi    p__Ascomycota   c__Pezizomycetes
## Otu00006  49323 k__Fungi    p__Ascomycota c__Dothideomycetes
##                      Order                                Family
## Otu00001      o__Pezizales                     f__Pyronemataceae
## Otu00002      o__Pezizales                     f__Pyronemataceae
## Otu00003 o__Cantharellales                  f__Ceratobasidiaceae
## Otu00004   o__Pleosporales                      f__Pleosporaceae
## Otu00005      o__Pezizales                     f__Pyronemataceae
## Otu00006   o__Pleosporales f__Pleosporales_family_Incertae_sedis
##                                   Genus                    Species
## Otu00001 f__Pyronemataceae_unclassified                       <NA>
## Otu00002 f__Pyronemataceae_unclassified                       <NA>
## Otu00003               g__Thanatephorus s__Thanatephorus_cucumeris
## Otu00004                  g__Alternaria        s__Alternaria_porri
## Otu00005 g__unclassified_Pyronemataceae       s__Pyronemataceae_sp
## Otu00006                       g__Phoma      s__Phoma_sp_UASWS0872
# 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))
## [1] TRUE