Skip to content

Commit 1e7d0c6

Browse files
committed
up - size in for derepliction in clustering
1 parent 9d4404d commit 1e7d0c6

2 files changed

Lines changed: 91 additions & 4 deletions

File tree

JAMP/R/Denoise.R

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Haplotyping v0.1
22

3-
Denoise <- function(files="latest", strategy="unoise", unoise_alpha=5, minsize=10, minrelsize=0.0001, OTUmin=0.01, minhaplosize=0.003, withinOTU=5, eachsampleOTUmin=NULL, minHaploPresence=1, minOTUPresence=1, renameSamples="(.*_.*)_cut.*", exe="usearch"){
3+
Denoise <- function(files="latest", strategy="unoise", unoise_alpha=5, minsize=10, minrelsize=0.0001, poolsamples=F, OTUmin=0.01, minhaplosize=0.003, withinOTU=5, eachsampleOTUmin=NULL, minHaploPresence=1, minOTUPresence=1, renameSamples="(.*_.*)_cut.*", exe="usearch"){
44

55

66

@@ -56,7 +56,7 @@ message(log)
5656

5757
# merge all files into one!
5858

59-
59+
if(poolsamples){
6060
cat(file=paste(folder, "/_stats/1_derep_logs.txt", sep=""), paste("\nCombining all files in a single file (samples_pooled.txt):\n", paste("cmd", cmd, collapse="", sep=""), collapse="", sep="") , append=T, sep="\n")
6161
cat(file="log.txt", "\nCombining all files in a single file (samples_pooled.txt)\n", append=T, sep="\n")
6262

@@ -160,10 +160,54 @@ message(info)
160160
cat(file="log.txt", info, append=T, sep="\n")
161161

162162
}
163+
}
163164

164165

165-
# Cluster into OTUs (for OTU table information)
166166

167+
# If denoising on individual sampels!
168+
if(!poolsamples){
169+
170+
dir.create(paste(folder, "/_data/2_denoised", sep=""))
171+
172+
denoised <- new_names
173+
denoised <- sub("1_derep", "2_denoised", denoised)
174+
175+
176+
cmd <- paste("-unoise3 \"", new_names, "\" -zotus \"", denoised,"\" -unoise_alpha ", unoise_alpha, " -sizein -sizeout", sep="")
177+
178+
179+
for (i in 1:length(denoised)){
180+
181+
A <- system2(exe, cmd[i], stdout=T, stderr=T)
182+
183+
cat(file=paste(folder, "/_stats/2_denoise_logs.txt", sep=""), paste("usearch", cmd[i], sep=""), append=T, sep="\n")
184+
cat(file=paste(folder, "/_stats/2_denoise_logs.txt", sep=""), A, append=T, sep="\n")
185+
186+
seqin <- Count_sequences(new_names[i], fastq=F)
187+
seqout <- Count_sequences(denoised[i], fastq=F)
188+
189+
temp <- paste("Sample ", sub(".*/(.*)_PE_.*", "\\1", new_names[i]), " denoised ", seqin, " sequences to ", seqout, " ESVs (", round(seqout/seqin*100, 2), "% keeped)", sep="")
190+
message(temp)
191+
cat(file="log.txt", temp, append=T, sep="\n")
192+
}
193+
194+
# Include abundance information again
195+
196+
for (i in 1:length(denoised)){
197+
de <- read.fasta(denoised[i], as.string=T, forceDNAtolower=F)
198+
fast <- read.fasta(new_names[i], as.string=T, forceDNAtolower=F)
199+
matched <- match(fast, de)
200+
matched <- matched[!is.na(matched)]
201+
202+
names(de) <- names(fast[matched])
203+
write.fasta(de, names(de), file.out= denoised[i])
204+
}
205+
206+
} # indiv renamed (for indiv processing).
207+
208+
209+
# Cluster into OTUs (for OTU table information)
210+
if(poolsamples){
167211
cmd <- paste(" -cluster_otus ", folder, "/_data/1_derep/samples_pooled_+_denoised_renamed.txt -otus ", folder, "/_data/1_derep/samples_pooled_+_denoised_renamed_OTUsequ.txt -uparseout ", folder, "/_data/1_derep/samples_pooled_+_denoised_renamed_OTUtable.txt -relabel OTU_ -strand plus", sep="")
168212

169213
A <- system2(exe, cmd, stdout=T, stderr=T) # cluster OTUs!
@@ -177,6 +221,49 @@ if(is.na(chimeras)){chimeras<-0}
177221
info <- paste("Clustered ", length(haplotypes), " haplotype sequences (cluster_otus, 3% simmilarity) into ", OTUs, " OTUs (+", chimeras, " chimeras).\nOTUs and (potentially) chimeric sequences will be included in the Haplotype table!\n", sep="" )
178222
message(info)
179223
cat(file="log.txt", info, append=T, sep="\n")
224+
} # end pooled processing
225+
226+
227+
# OTU clustering for indiv samples
228+
if(!poolsamples){
229+
230+
dir.create(paste(folder, "/_data/3_pooledESV", sep=""))
231+
232+
233+
234+
235+
cat(file=paste(folder, "/_stats/1_derep_logs.txt", sep=""), paste("\nCombining all files in a single file (samples_pooled.txt):\n", paste("cmd", cmd, collapse="", sep=""), collapse="", sep="") , append=T, sep="\n")
236+
cat(file="log.txt", "\nCombining all files in a single file (samples_pooled.txt)\n", append=T, sep="\n")
237+
238+
# dereplicating pooled file
239+
message("\nCombining all files in a single file (samples_pooled.txt)")
240+
cmd <- paste(paste(paste("\"", denoised, "\"", sep=""), collapse=" "), "> ", folder, "/_data/3_pooledESV/1_samples_pooled.txt", sep="")
241+
system2("cat", cmd)
242+
243+
# dereplicating files
244+
info <- "Dereplicating pooled sequences!"
245+
message(info)
246+
cat(file="log.txt", info, append=T, sep="\n")
247+
248+
cmd <- paste("-fastx_uniques \"", folder, "/_data/3_pooledESV/1_samples_pooled.txt\" -fastaout \"", folder, "/_data/3_pooledESV/2_samples_pooled_derep.txt\" -sizein -sizeout", sep="")
249+
A <- system2(exe, cmd, stdout=T, stderr=T)
250+
251+
cat(file=paste(folder, "/_stats/1_derep_logs.txt", sep=""), paste("usearch", cmd, sep=""), append=T, sep="\n")
252+
cat(file=paste(folder, "/_stats/1_derep_logs.txt", sep=""), A, append=T, sep="\n")
253+
254+
255+
# rename files for ESV
256+
ESVs <- read.fasta(paste(folder, "/_data/3_pooledESV/2_samples_pooled_derep.txt", sep=""), forceDNAtolower=F, as.string=T)
257+
258+
names(ESVs) <- paste("haplo_", 1:length(ESVs), sep="")
259+
write.fasta(ESVs, names=names(ESVs), paste(folder, "/_data/3_pooledESV/3_ESV_list.txt", sep=""))
260+
261+
262+
263+
# need to keep working on this
264+
265+
266+
} # end processing indiv denoised files
180267

181268

182269
# generate one united haplotype table!

JAMP/R/U_cluster_otus.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ new_names <- sub("_data", paste("_data/1_derep_minsize_", minuniquesize, sep="")
5656
new_names <- paste(folder, "/", new_names, sep="")
5757

5858

59-
cmd <- paste("-derep_fulllength \"", files[!empty], "\" -output \"", new_names, "\" -sizeout", if(! mapp_singletons){paste(" -minuniquesize ", minuniquesize, sep="")}, sep="")
59+
cmd <- paste("-derep_fulllength \"", files[!empty], "\" -output \"", new_names, "\" -sizeout -sizein", if(! mapp_singletons){paste(" -minuniquesize ", minuniquesize, sep="")}, sep="")
6060

6161
files_to_delete <- c(files_to_delete, new_names)
6262

0 commit comments

Comments
 (0)