Created
October 25, 2016 15:05
-
-
Save fawda123/47f44dc81188c8f75faf09cc115dcf4e to your computer and use it in GitHub Desktop.
moth_form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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