annotate lib/tarean/tarean_batch_mode.R @ 0:1d1b9e1b2e2f draft

Uploaded
author petr-novak
date Thu, 19 Dec 2019 10:24:45 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
1 #!/usr/bin/env Rscript
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
2 library(optparse, quiet = TRUE)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
3 library(parallel)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
4 initial.options <- commandArgs(trailingOnly = FALSE)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
5 file.arg.name <- "--file="
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
6 script.name <- sub(file.arg.name,"",
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
7 initial.options[grep(file.arg.name, initial.options)]
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
8 )
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
9 script.dir <- normalizePath(dirname(script.name))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
10 oridir=getwd()
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
11 options(OGDF = paste0(script.dir,"/OGDF/runOGDFlayout2015.5"))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
12 CPU = detectCores()
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
13 source(paste(script.dir,"/","methods.R", sep=''))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
14 source(paste(script.dir,"/","logo_methods.R", sep=''))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
15 source(paste(script.dir,"/","htmlheader.R", sep=''))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
16
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
17 option_list = list(
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
18 make_option(c('-i', '--input_sequences_list'),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
19 action='store',type='character',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
20 help='list of fasta sequences file for tarean analysis'
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
21 ),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
22 make_option(c('-o', '--output_dir'),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
23 action='store',type='character',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
24 help='output directory',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
25 default="./kmer_analysis"),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
26 make_option(c('-t', '--tRNA_database'),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
27 action='store',type='character',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
28 help='path to tRNA database',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
29 default=NULL),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
30 make_option(c('-p', '--parallel'),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
31 action='store_true',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
32 type='logical',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
33 help='run in parallel (faster but can exhaust RAM)',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
34 default=FALSE),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
35 make_option(c('-N', '--not_paired'),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
36 action='store_true',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
37 type='logical',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
38 help='reads are not paired',
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
39 default=FALSE)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
40
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
41 )
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
42
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
43 description = paste (strwrap(" put decription here"), collapse ="\n")
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
44 epilogue = paste (strwrap(" put epilogue here"), collapse ="\n")
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
45 parser=OptionParser(
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
46 option_list=option_list,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
47 epilogue=epilogue,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
48 description=description,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
49 )
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
50
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
51 opt = parse_args(parser, args=commandArgs(TRUE))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
52 paired = !opt$not_paired
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
53 print(opt)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
54 dir.create(opt$output_dir)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
55 fl = readLines(opt$input_sequences_list)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
56 ## reorder to avoid running large top graphs at once
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
57 ord = sample(seq_along(fl), length(fl))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
58
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
59
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
60 index=0
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
61 info=list()
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
62 save.image(paste0(opt$output_dir,"/info.RData")) # for debugin purposes
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
63 if (opt$parallel){
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
64 cat("processing in parallel")
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
65 info=mcmapply(
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
66 FUN=tarean,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
67 input_sequences = fl[ord],
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
68 output_dir = paste0(opt$output_dir,"/",sprintf("%04d",ord)),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
69 min_kmer_length = 11,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
70 max_kmer_length = 27,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
71 CPU = CPU,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
72 sample_size = 30000,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
73 reorient_reads = TRUE,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
74 tRNA_database_path = opt$tRNA_database,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
75 paired = paired,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
76 include_layout=FALSE,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
77 mc.cores=round(1+detectCores()/9),
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
78 mc.set.seed = TRUE,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
79 mc.preschedule = FALSE,
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
80 SIMPLIFY = FALSE
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
81 )
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
82 }else{
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
83 for (i in fl){
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
84 index = index + 1
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
85 dirout=paste0(opt$output_dir,"/",sprintf("%04d",index))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
86 try({
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
87 info[[i]] = tarean(i, dirout, 11, 27, CPU, 30000, TRUE, opt$tRNA_database, include_layout=FALSE)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
88 cat("-----------------------------------------------------\n")
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
89 print(info[[i]])
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
90 })
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
91 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
92 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
93 save(info, file = paste0(opt$output_dir,"/info.RData"))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
94 save.image("tmp.RData")
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
95 ## export as csv table
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
96 ## 'graph_info' is always include:
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
97
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
98 tr_info = data.frame(do.call(rbind, info[sapply(info,length)>1]))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
99 if (nrow(tr_info)>0){
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
100 ## TR detected
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
101 graph_info = data.frame (do.call(rbind, lapply(info, "[[", "graph_info")))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
102 graph_info$source=rownames(graph_info)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
103 tr_info$graph_info=NULL
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
104 tr_info$source = rownames(tr_info)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
105 graph_tr_info = merge(graph_info, tr_info, all=TRUE, by='source')
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
106 if (any(sapply(graph_tr_info,class)=='list')){
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
107 for (i in colnames(graph_tr_info)){
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
108 graph_tr_info[,i] = unname(unlist(graph_tr_info[,i]))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
109 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
110 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
111 write.table(graph_tr_info, file=paste0(opt$output_dir,"/info.csv"), row.names=FALSE,sep="\t", quote= TRUE)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
112 }else{
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
113 ## TR not detected
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
114 graph_info = data.frame (do.call(rbind, lapply(info, function(x) unlist(x[['graph_info']]))))
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
115 graph_info$source=rownames(graph_info)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
116 write.table(graph_info, file=paste0(opt$output_dir,"/info.csv"), row.names=FALSE,sep="\t", quote = FALSE)
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
117 }
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
118
1d1b9e1b2e2f Uploaded
petr-novak
parents:
diff changeset
119