Skip to content

Instantly share code, notes, and snippets.

@fawda123
Created October 25, 2016 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fawda123/47f44dc81188c8f75faf09cc115dcf4e to your computer and use it in GitHub Desktop.
Save fawda123/47f44dc81188c8f75faf09cc115dcf4e to your computer and use it in GitHub Desktop.
moth_form
#' Format a text file for mothur
#'
#' @param fl chr string for input file
#' @param rev_cmp logical if file needs reverse complemeNT
#' @param savefl logical if output file is saved, otherwise the output is returned in the console
moth_form <- function(fl, rev_cmp = FALSE, savefl = TRUE, flnm = 'myfile.txt'){
# import file
dat <- read.table(fl, sep = '\t')
# format
dat <- mutate(dat,
V1 = 'barcode',
V3 = 'NONE'
) %>%
select(V1, V2, V3, V6)
# take reverse comp if T
if(rev_cmp){
# take reverse complement
revcd <- as.character(dat$V2) %>%
strsplit(., '') %>%
lapply(., function(x) paste(rev(x), collapse = '')) %>%
unlist %>%
chartr("ATGC", "TACG", .)
# replace columns
dat <- mutate(dat,
V2 = 'NONE',
V3 = revcd
)
}
# return the data if savefl F
if(!savefl) return(dat)
write.table(dat, flnm, sep = '\t', quote = F, row.names = F, col.names = F)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment