annotate p_chunks.R @ 3:e6fc152a2462 draft default tip

Uploaded
author greg
date Tue, 28 Mar 2023 14:17:59 +0000
parents f45c65e3fd18
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
1 #!/bin/env Rscript
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
2
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
3 library(parallel)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
4 library(hash)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
5 library(stringr)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
6 library(grid)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
7 library(gridExtra)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
8 library(optparse)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
9
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
10 options(width = 180)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
11
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
12 printif = function(string = NULL, condition){
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
13 if (condition) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
14 print(string)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
15 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
16 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
17
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
18 findPlasmids = function(plasmidPSLFile = NULL, plasmidDatabase = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
19 amrPSLFile = NULL, amrDatabase, noAMR = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
20 incPSLFile = NULL, incDatabase, noInc = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
21 outputDirectory = NA, overwrite = TRUE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
22 maxTargetLength = 300000,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
23 minQueryLength = 500,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
24 makeCircos = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
25 minQueryCoverage = 1/2, minTargetCoverage = 1/2,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
26 searchDepth = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
27 verbosity = 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
28
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
29 ## Verify the arguments
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
30 argumentsGood = TRUE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
31 if (minQueryCoverage < .1 || minQueryCoverage > 1) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
32 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
33 message(paste('Minimum query coverage', minQueryCoverage, 'is outside of the range 0.1 <= x <= 1'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
34 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
35 if (minTargetCoverage < 0.02 || minTargetCoverage > 1) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
36 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
37 message(paste('Minimum target coverage', minTargetCoverage, 'is outside of the range 0.1 <= x <= 1'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
38 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
39 if (!argumentsGood){
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
40 message('There is a problem with the arguments')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
41 return()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
42 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
43
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
44 printif(paste('Finding plasmids in', plasmidPSLFile), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
45
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
46 ## Keep track of the total score in case we doing a grid search
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
47 totalPlasmidScore = 0
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
48
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
49 ## Check for the existence of the output directory, remove if it exists
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
50 if (file.exists(outputDirectory)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
51 printif(paste('Removing existing output directory', outputDirectory), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
52 unlink(outputDirectory, recursive = TRUE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
53 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
54 printif(paste('Making output directory', outputDirectory), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
55 dir.create(outputDirectory)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
56 outputPrefix = paste0(outputDirectory, "/plasmids")
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
57
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
58 ## Read in and filter the plasmid hits
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
59 plasmidHits = read.table(plasmidPSLFile, row.names = NULL, header = FALSE, sep = '\t', stringsAsFactors = FALSE, skip = 5)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
60 colnames(plasmidHits) = c('match', 'mismatch', 'rep_m', 'Ns', 'tgap_c', 'tgap_b',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
61 'qgap_c', 'qgap_b', 'strand',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
62 'target', 'tlength', 'tstart', 'tstop',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
63 'query', 'qlength', 'qstart', 'qstop',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
64 'blocks', 'block_sizes', 'tstarts', 'qstarts')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
65 printif(paste("Sequence-plasmid hits:", nrow(plasmidHits)), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
66
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
67 plasmidHits = plasmidHits[order(plasmidHits[,'target'], -plasmidHits[,'qlength']), ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
68
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
69 ## Toss out any hits missing information
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
70 plasmidHits = plasmidHits[complete.cases(plasmidHits),]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
71
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
72 ## Toss out very long plasmid sequences -- probably actually genome chunks labeled incorrectly
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
73 veryLongHits = sum(plasmidHits[,'tlength'] >= maxTargetLength)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
74 printif(paste('Removing', veryLongHits, 'hits greater than', maxTargetLength), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
75 plasmidHits = plasmidHits[plasmidHits[,'tlength'] <= maxTargetLength, ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
76 printif(paste("Sequence-plasmid hits after removing very long plasmids:", nrow(plasmidHits)), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
77
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
78 ## Toss out very short query sequences -- probably junk or repeats
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
79 veryShortQuery = sum(plasmidHits[,'qlength'] >= minQueryLength)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
80 printif(paste('Removing', veryShortQuery, 'queries less than', minQueryLength), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
81 plasmidHits = plasmidHits[plasmidHits[,'qlength'] >= minQueryLength, ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
82 printif(paste("Sequence-plasmid hits after removing very short queries:", nrow(plasmidHits)), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
83
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
84 ## Toss out sequece-plasmid pairs below the coverage cutoff
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
85 sequenceMatches = aggregate(x = plasmidHits[,'match',drop = FALSE],
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
86 by = list(plasmidHits[,'query'], plasmidHits[,'target']), FUN = sum)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
87 printif(head(sequenceMatches), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
88 printif(paste('Sequence-plasmid pair matches:', paste(dim(sequenceMatches), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
89
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
90 sequenceLengths = aggregate(x = plasmidHits[,'qlength', drop = FALSE],
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
91 by = list(plasmidHits[,'query'], plasmidHits[,'target']), FUN = max)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
92 printif(head(sequenceLengths), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
93 printif(paste('Sequence-plasmid pair lengths:', paste(dim(sequenceLengths), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
94
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
95 matchingFractions = cbind(sequenceMatches[,c(1,2)], sequenceMatches[,3] / sequenceLengths[,3])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
96 colnames(matchingFractions) = c('query', 'target', 'fraction')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
97 printif(head(matchingFractions), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
98 printif(paste('Sequence-plasmid pair fractions:', paste(dim(matchingFractions), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
99
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
100 matchingFractions = matchingFractions[matchingFractions[,'fraction'] >= minQueryCoverage,]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
101 printif(head(matchingFractions), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
102 printif(paste('Passing sequence-plasmid pair fractions:', paste(dim(matchingFractions), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
103
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
104 aboveMinCoverage = apply(matchingFractions, 1, function(i){paste0(i['query'], '|', i['target'])})
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
105 plasmidHits = plasmidHits[apply(plasmidHits, 1, function(i){paste0(i['query'], '|', i['target'])}) %in% aboveMinCoverage, ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
106 printif(paste("Sequence-plasmid hits after removing low-coverage hits:", nrow(plasmidHits)), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
107
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
108 ## Toss out plasmid sequences below the coverage cutoff
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
109 targetMatches = aggregate(x = plasmidHits[,'match',drop = FALSE],
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
110 by = list(plasmidHits[,'target']), FUN = sum)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
111 printif(head(targetMatches), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
112 printif(paste('Plasmid matches:', paste(dim(targetMatches), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
113
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
114 targetLengths = aggregate(x = plasmidHits[,'tlength', drop = FALSE],
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
115 by = list(plasmidHits[,'target']), FUN = max)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
116 printif(head(targetLengths), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
117 printif(paste('Plasmid lengths:', paste(dim(targetLengths), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
118
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
119 matchingFractions = cbind(targetMatches[,1], targetMatches[,2] / targetLengths[,2])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
120 colnames(matchingFractions) = c('target', 'fraction')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
121 printif(head(matchingFractions), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
122 printif(paste('Plasmid fractions:', paste(dim(matchingFractions), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
123
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
124 matchingFractions = matchingFractions[matchingFractions[,'fraction'] >= minTargetCoverage,]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
125 printif(head(matchingFractions), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
126 printif(paste('Passing plasmid fractions:', paste(dim(matchingFractions), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
127
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
128 aboveMinCoverage = matchingFractions[, 'target']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
129 plasmidHits = plasmidHits[plasmidHits[, 'target'] %in% aboveMinCoverage, ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
130 printif(paste("Sequence-plasmid hits after removing low-coverage hits:", nrow(plasmidHits)), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
131
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
132
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
133 ## If we're out of sequece-plasmid hits, then stop here
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
134 if (nrow(plasmidHits) == 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
135 message(paste('Not hits found'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
136 return
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
137 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
138
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
139 ## Find out how much of each query (contig) is covered by each target (plasmid).
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
140 ## Query coverage is constant and does not change as we assign contigs to plasmids
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
141 queryCoverage = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
142 queryMismatches = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
143 for (i in 1:nrow(plasmidHits)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
144 if (!(i %% 1000)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
145 printif(paste('Processing hit', i, '/', nrow(plasmidHits)), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
146 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
147
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
148 query = plasmidHits[i,'query']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
149 target = plasmidHits[i, 'target']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
150
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
151 ## Represent each sequence-plasmid hit as a series of 0/1 vectors that
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
152 if (!has.key(query, queryCoverage)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
153 queryCoverage[[query]] = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
154 queryMismatches[[query]] = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
155 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
156 if (!has.key(target, queryCoverage[[query]])) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
157 queryCoverage[[query]][[target]] = rep(0, times = plasmidHits[i, 'qlength'])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
158 queryMismatches[[query]][[target]] = 0
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
159 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
160
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
161 blockSizes = as.numeric(unlist(strsplit(x = plasmidHits[i,'block_sizes'], ',')))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
162 qBlockStarts = as.numeric(unlist(strsplit(x = plasmidHits[i,'qstarts'], ',')))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
163
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
164 for (j in 1:length(blockSizes)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
165 queryCoverage[[query]][[target]][qBlockStarts[j]:(qBlockStarts[j]+blockSizes[j])] = 1
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
166 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
167 queryMismatches[[query]][[target]] = queryMismatches[[query]][[target]] + plasmidHits[i,'mismatch']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
168 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
169
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
170
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
171 ## Pull the full plasmid names from the blast database because BLAT/minimap2 doesn't report them, just the ID's
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
172 targetIDs = plasmidHits[,'target']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
173 targetIDs = gsub("\\|$", "", targetIDs)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
174 targetIDs = gsub(".*(\\|.*)$", "\\1", targetIDs)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
175 noDotIDs = gsub("\\|", "", targetIDs)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
176 noDotIDs = gsub("(^H[^.]+).[0-9]+$", "\\1", noDotIDs)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
177 noDotIDs = cbind(noDotIDs)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
178
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
179 targetFile = paste0(outputDirectory, '/targets.tsv', sep = '')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
180 write.table(file = targetFile, x = noDotIDs, quote = FALSE, row.names = FALSE, col.names = FALSE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
181 command = paste('blastdbcmd -db', plasmidDatabase,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
182 '-entry_batch', targetFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
183 '| grep ">"')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
184 targetNames = system(command, intern = TRUE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
185 printif(paste('Found', length(targetNames), 'target names for', length(targetIDs), 'targets.'), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
186
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
187 targetNames = gsub('^>.*\\| ', '', targetNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
188 targetNames = gsub('^>[^ ]+', '', targetNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
189 plasmidHits = cbind(plasmidHits, targetIDs, targetNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
190 printif(paste('Named hits:', nrow(plasmidHits)), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
191
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
192 #Pull just the plasmids out of the larget set of hits, i.e, make sure it has the word 'plasmid' in the description.
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
193 plasmidHits = plasmidHits[grep('plasmid|vector', plasmidHits[,'targetNames'], ignore.case = TRUE), ,drop = FALSE]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
194 plasmidHits = plasmidHits[!grepl('tig0000|unnamed', plasmidHits[,'targetNames'], ignore.case = TRUE), , drop = FALSE]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
195 printif(paste("Sequece-plasmid hits after screening by name:", paste(dim(plasmidHits), collapse = 'x')), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
196
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
197 ## Stop if there is nothing left
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
198 if (is.null(plasmidHits)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
199 message('Not hits found')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
200 return()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
201 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
202 if (nrow(plasmidHits) == 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
203 message('Not hits found')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
204 return()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
205 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
206
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
207 ## Clean up the plasmid names -- they look like crap by default.
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
208 plasmidNames = plasmidHits[,'targetNames']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
209 plasmidNames = gsub(', comp.*', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
210 plasmidNames = gsub(', contig.*', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
211 plasmidNames = gsub(', partial.*', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
212 plasmidNames = gsub('strain ', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
213 plasmidNames = gsub('^ *', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
214 plasmidNames = sub('^(cl\\|)(.*?) ', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
215 plasmidNames = sub('subsp. (.*?) ', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
216 plasmidNames = sub('serovar (.*?) ', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
217 plasmidNames = sub('strain ', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
218 plasmidNames = sub('plasmid$', '', plasmidNames)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
219 plasmidHits[,'targetNames'] = plasmidNames
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
220
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
221 ## Just take the best hit for each plasmid, hence the head, 1 in the agg
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
222 plasmidNames = aggregate(plasmidHits, by = list(plasmidHits[,'query']), FUN = head, 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
223 plasmidNames = plasmidNames[, 'targetNames', drop = FALSE]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
224
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
225 ## Find the set of plasmid coverage hits for each itteration
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
226 usedContigs = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
227 plasmidMismatches = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
228
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
229 ## Order hits by the plasmid ID and the query length
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
230 plasmidHits = plasmidHits[order(plasmidHits[,'target'], -plasmidHits[,'qlength']), ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
231
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
232 ## Iterate, finding plasmids until we run out of usable sequence-plasmid its
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
233 plasmidNumber = 0
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
234 plasmidResults = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
235 while (1) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
236
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
237 ## Keep track of how many plasmids we have gone over
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
238 plasmidNumber = plasmidNumber + 1
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
239
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
240 printif(paste('Sequence-plasmid hits left:', nrow(plasmidHits)), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
241 contigToPlasmid = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
242 plasmidToContig = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
243 plasmidCoverage = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
244 plasmidCoverageWithRepeats = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
245 contigCoverage = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
246
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
247 ##Find contig/plasmid plasmid/contig pairs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
248 if (is.null(plasmidHits)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
249 break
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
250 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
251 if (nrow(plasmidHits) == 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
252 break
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
253 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
254 repLengths = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
255
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
256
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
257 ## Find the coverage of each plasmid in the possible set by the contigs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
258 for (i in 1:nrow(plasmidHits)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
259
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
260 query = plasmidHits[i,'query']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
261 target = plasmidHits[i,'target']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
262 matches = plasmidHits[i,'match']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
263 mismatches = plasmidHits[i,'mismatch']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
264 score = matches - mismatches
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
265 queryLength = plasmidHits[i,'qlength']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
266 blockSizes = as.numeric(unlist(strsplit(plasmidHits[i, 'block_sizes'], ',')))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
267 queryStarts = as.numeric(unlist(strsplit(plasmidHits[i, 'qstarts'], ',')))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
268 targetStarts = as.numeric(unlist(strsplit(plasmidHits[i, 'tstarts'], ',')))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
269
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
270 ## Skip matches which have less than 50% of the bases from the contig on the plasmid -- probably not a good match
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
271 if ((sum(queryCoverage[[query]][[target]]) - queryMismatches[[query]][[target]]) / queryLength <= minQueryCoverage) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
272 next
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
273 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
274
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
275 targetLength = plasmidHits[i, 'tlength']; targetStart = plasmidHits[i, 'tstart']; targetStop = plasmidHits[i, 'tstop']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
276
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
277 ## Relate this contig to this plasmid
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
278 if (!has.key(query, contigToPlasmid)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
279 contigToPlasmid[[query]] = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
280 contigCoverage[[query]] = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
281 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
282 if (!has.key(target, contigToPlasmid[[query]])) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
283 contigToPlasmid[[query]][[target]] = score
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
284 contigCoverage[[query]][[target]] = rep(0, queryLength)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
285 } else {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
286 contigToPlasmid[[query]][[target]] = contigToPlasmid[[query]][[target]] + score
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
287 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
288
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
289 ## Keep track of target(plasmid) coverage by the contigs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
290 if (!has.key(target, plasmidCoverage)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
291 plasmidCoverage[[target]] = rep(0, targetLength)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
292 plasmidCoverageWithRepeats[[target]] = rep(0, targetLength)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
293 plasmidToContig[[target]] = hash()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
294 plasmidMismatches[target] = 0
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
295 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
296
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
297 penalized = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
298 for (j in 1:length(blockSizes)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
299
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
300 ## Keep track of all contig alignments to this plasmid, even with repeats
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
301 plasmidCoverageWithRepeats[[target]][targetStarts[j]:(targetStarts[j] + blockSizes[j])] = 1
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
302
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
303 ## Skip if this region of the query sequence has already been assigned to this plasmid
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
304 if (sum(contigCoverage[[query]][[target]][queryStarts[j]:(queryStarts[j] + blockSizes[j])] == 0) <= 50) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
305 printif(paste('Sequence', query, 'already used for', target,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
306 '. ', paste0(queryStarts[j], '-', queryStarts[j] + blockSizes[j])), verbosity > 2)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
307 next
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
308 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
309 if (!penalized) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
310 ## Penalty for every gap, only penalize once per match
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
311 ## TODO: Penalize for gap length, not just once per gap
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
312 #plasmidMismatches[target] = plasmidMismatches[target] + (length(blockSizes) - 1) * 100
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
313 plasmidMismatches[target] = plasmidMismatches[target] + mismatches * 5
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
314 penalized = TRUE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
315 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
316
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
317 plasmidCoverage[[target]][targetStarts[j]:(targetStarts[j] + blockSizes[j])][contigCoverage[[query]][[target]][queryStarts[j]:(queryStarts[j] + blockSizes[j])] == 0] =
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
318 plasmidCoverage[[target]][targetStarts[j]:(targetStarts[j] + blockSizes[j])][contigCoverage[[query]][[target]][queryStarts[j]:(queryStarts[j] + blockSizes[j])] == 0] + 1
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
319 contigCoverage[[query]][[target]][queryStarts[j]:(queryStarts[j] + blockSizes[j])] = 1
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
320 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
321
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
322 ## Relate this plasmid to this contig
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
323 if (!has.key(query, plasmidToContig[[target]])) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
324 plasmidToContig[[target]][[query]] = score
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
325 } else {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
326 plasmidToContig[[target]][[query]] = plasmidToContig[[target]][[query]] + score
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
327 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
328 if (target == 'NZ_GG692894.1' && query == 'contig_3_0') {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
329 print('NZ_GG692894.1')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
330 print(sum(plasmidCoverage[[target]]))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
331 print(sum(contigCoverage[[query]][[target]]))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
332 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
333
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
334 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
335
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
336 ## Get the best set of plasmids out, i.e., the set with the most bases matching between the contig and plasmid
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
337 plasmidScores = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
338 for (thisPlasmid in keys(plasmidCoverage)){
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
339 thisPlasmidScore = sum(plasmidCoverage[[thisPlasmid]])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
340 plasmidScores = c(plasmidScores, thisPlasmidScore)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
341 names(plasmidScores)[length(plasmidScores)] = thisPlasmid
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
342 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
343 plasmidScores = sort(plasmidScores - plasmidMismatches[names(plasmidScores)], dec = TRUE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
344
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
345 if (length(plasmidScores) > 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
346 printif('Highest scoring plasmids', verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
347 printif(head(cbind(plasmidScores), 20), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
348 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
349
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
350 ## Stop searching for plasmids if nothing matches well or we're out of hits
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
351 if (length(plasmidScores) == 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
352 printif('Out of plasmids', verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
353 break
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
354 } else if (max(plasmidScores) < 500) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
355 printif('Out of min-scoring plasmids', verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
356 break
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
357 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
358
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
359 ## For each matching plasmid, ordered by total bases matching the assembly, find the set of corresponding contigs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
360 plasmidToUse = 1
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
361 if (!is.null(searchDepth) && plasmidNumber <= length(searchDepth)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
362 plasmidToUse = searchDepth[plasmidNumber]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
363 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
364 if (plasmidToUse > length(plasmidScores)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
365 plasmidToUse = length(plasmidScores)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
366 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
367
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
368 plasmid = names(plasmidScores)[plasmidToUse]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
369 print(paste('Plasmid picked', plasmid))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
370 totalPlasmidScore = totalPlasmidScore + plasmidScores[plasmid]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
371 printif(paste("Pulling sequences for", plasmid), verbosity > 0)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
372
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
373 ## Find contigs what haven't already been given to another plasmid so we can assign them next round
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
374 plasmidContigs = keys(plasmidToContig[[plasmid]])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
375 unusedContigs = plasmidContigs[!(plasmidContigs %in% usedContigs)]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
376
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
377 ## If no unused contigs that also map to this plasmid then skip it
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
378 if (length(unusedContigs) == 0) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
379 printif(paste("No unused sequences for", plasmid), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
380 next
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
381 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
382
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
383 ## Keep just the rows for this plasmid and which haven't already been used by another plasmid
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
384 plasmidRows = plasmidHits[plasmidHits[,'target'] == plasmid,]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
385 plasmidRows = plasmidRows[plasmidRows[,'query'] %in% unusedContigs,,drop = FALSE]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
386 plasmidName = plasmidRows[1, 'targetNames']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
387 plasmidID = plasmidRows[1, 'targetIDs']
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
388 ## Get the plasmid length
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
389 command = paste('blastdbcmd -db', plasmidDatabase,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
390 '-entry', plasmidID,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
391 '-outfmt "%l"')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
392 plasmidLength = system(command, intern = TRUE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
393 plasmidLength = rep(plasmidLength, nrow(plasmidRows))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
394
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
395 ## How many bases from the plasmid are uncovered?
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
396 plasmidMissing = rep(sum(plasmidCoverageWithRepeats[[as.character(plasmidID)]] == 0), nrow(plasmidRows))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
397
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
398 ## Keep track of all of the contigs included
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
399 usedContigs = c(usedContigs, unusedContigs)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
400
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
401 ## How many matching bases for each query sequence?
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
402 thisPlasmidQuerySizes = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
403 thisPlasmidMatches = c()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
404 for (contig in unusedContigs) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
405 thisPlasmidQuerySizes = c(thisPlasmidQuerySizes, length(queryCoverage[[contig]][[plasmid]]))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
406 thisPlasmidMatches = c(thisPlasmidMatches, sum(queryCoverage[[contig]][[plasmid]]))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
407 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
408
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
409 ## Add this plasmid's hits onto the growing list of sequence-plasmid hits
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
410 thisPlasmidResults = cbind(unusedContigs, plasmidName, as.character(plasmidID), thisPlasmidQuerySizes, thisPlasmidMatches, plasmidLength, plasmidMissing)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
411 colnames(thisPlasmidResults) = c('query.name', 'plasmid.name', 'plasmid.accession', 'query.size', 'aligned.bases', 'plasmid.size', 'plasmid.missing')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
412 thisPlasmidResults = thisPlasmidResults[order(-thisPlasmidMatches), ]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
413 plasmidResults = rbind(plasmidResults, thisPlasmidResults)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
414
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
415 ## Remove the contigs added to this plasmid from the list of plasmid/contig BLAT hits
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
416 plasmidHits = plasmidHits[!(plasmidHits[,'query'] %in% usedContigs),]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
417 plasmidHits = plasmidHits[!(plasmidHits[,'target'] == plasmid),]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
418 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
419
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
420 rownames(plasmidResults) = plasmidResults[,1]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
421
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
422 ## Check for the presence of AMR genes in this file
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
423 if (!noAMR) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
424 amrBEDFile = paste0(outputDirectory, '/amrMapping.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
425 command = paste('cat', amrPSLFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
426 '| awk -F \'\\t\' \'($3 >= 80) && ($4 / $14 >= .95){OFS = "\t"; print $2,($9 < $10 ? $9 : $10),($9 < $10 ? $10 : $9),$1,$3/100,($9 < $10 ? "+" : "-")}\'',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
427 '| sort -k 1,1 -k 2,2n >', amrBEDFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
428 printif(command, verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
429 system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
430 ## Find local overlapping regions
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
431 amrMergedBEDFile = paste0(outputDirectory, '/amrMergedMapping.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
432 command = paste('bedtools merge -d -30 -i', amrBEDFile, '>', amrMergedBEDFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
433 printif(command, verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
434 system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
435 ## Find the best AMR gene for each region
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
436 amrFinalBEDFile = paste0(outputDirectory, '/amrFinal.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
437 command = paste('bedtools intersect',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
438 '-a', amrBEDFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
439 '-b', amrMergedBEDFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
440 '-f .9 -F .9',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
441 '-wao',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
442 '| awk \'$7 != "."\'',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
443 '| awk \'{OFS="\t";locus=$7"\t"$8"\t"$9; if($5 > s[locus]){s[locus]=$5;b[locus] = $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}}END{for(i in b){print i,b[i]}}\'',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
444 '>', amrFinalBEDFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
445 printif(command, verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
446 system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
447
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
448 ## Read the AMR results in and add them to the plasmid contigs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
449 amrResults = read.table(file = amrFinalBEDFile, header = FALSE, row.names = NULL, stringsAsFactors = FALSE, quote = '')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
450 amrResults[,7] = gsub('(_.*$)|(.*\\|)', '', amrResults[,7])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
451 amrResults = aggregate(amrResults[ , 7, drop = FALSE], by = list(amrResults[,1]), function(i){paste(i, collapse = ', ')})
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
452 rownames(amrResults) = amrResults[,1]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
453 amrResults = amrResults[ , 2, drop = FALSE]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
454 print(amrResults)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
455
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
456 plasmidResults = cbind(plasmidResults, rep('', nrow(plasmidResults)))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
457 colnames(plasmidResults)[ncol(plasmidResults)] = 'amr'
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
458 plasmidResults[rownames(plasmidResults) %in% rownames(amrResults), 'amr'] =
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
459 amrResults[rownames(plasmidResults)[rownames(plasmidResults) %in% rownames(amrResults)], 1]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
460
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
461 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
462
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
463 ## Check for the presence of incompatibility groups in this file
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
464 if (!noInc) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
465 incBEDFile = paste0(outputDirectory, '/incMapping.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
466 command = paste('cat', incPSLFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
467 '| awk -F \'\\t\' \'($3 >= 80) && ($4 / $14 >= .95){OFS = "\t"; print $2,($9 < $10 ? $9 : $10),($9 < $10 ? $10 : $9),$1,$3/100,($9 < $10 ? "+" : "-")}\'',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
468 '| sort -k 1,1 -k 2,2n >', incBEDFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
469 printif(command, verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
470 system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
471 ## Find local overlapping regions
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
472 incMergedBEDFile = paste0(outputDirectory, '/incMergedMapping.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
473 command = paste('bedtools merge -d -30 -i', incBEDFile, '>', incMergedBEDFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
474 printif(command, verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
475 system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
476 ## Find the best INC group for each region
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
477 incFinalBEDFile = paste0(outputDirectory, '/incFinal.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
478 command = paste('bedtools intersect',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
479 '-a', incBEDFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
480 '-b', incMergedBEDFile,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
481 '-f .9 -F .9',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
482 '-wao',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
483 '| awk \'$7 != "."\'',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
484 '| awk \'{OFS="\t";locus=$7"\t"$8"\t"$9; if($5 > s[locus]){s[locus]=$5;b[locus] = $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}}END{for(i in b){print i,b[i]}}\'',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
485 '>', incFinalBEDFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
486 printif(command, verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
487 system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
488
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
489 ## Read the inc group results in and add them to the plasmid contigs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
490 incResults = read.table(file = incFinalBEDFile, header = FALSE, row.names = NULL, stringsAsFactors = FALSE, quote = '')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
491 incResults[,7] = gsub('(_.*$)|(.*\\|)', '', incResults[,7])
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
492 incResults = aggregate(incResults[ , 7, drop = FALSE], by = list(incResults[,1]), function(i){paste(i, collapse = ', ')})
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
493 rownames(incResults) = incResults[,1]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
494 incResults = incResults[ , 2, drop = FALSE]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
495 print(incResults)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
496
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
497 plasmidResults = cbind(plasmidResults, rep('', nrow(plasmidResults)))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
498 colnames(plasmidResults)[ncol(plasmidResults)] = 'inc'
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
499 plasmidResults[rownames(plasmidResults) %in% rownames(incResults), 'inc'] =
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
500 incResults[rownames(plasmidResults)[rownames(plasmidResults) %in% rownames(incResults)], 1]
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
501 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
502
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
503 ## Write the plasmid results to file
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
504 plasmidChunksFile = paste0(outputDirectory, '/plasmids.tsv')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
505 write.table(file = plasmidChunksFile, x = plasmidResults, quote = FALSE, sep = '\t', row.names = FALSE, col.names = TRUE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
506
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
507 ## Dump a sequence file of potential plasmid contigs
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
508 plasmidSequenceFile = paste0(outputPrefix, '.fna')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
509 system(paste0('echo "" >', plasmidSequenceFile))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
510 for (contig in plasmidResults[,'query.name']) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
511 command = paste('faidx', paste0('assembly.fasta'), contig, '>>', plasmidSequenceFile)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
512 print(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
513 #system(command)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
514 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
515
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
516
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
517 ## Return the total score of this round, in case we are doing a search
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
518 return(totalPlasmidScore)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
519
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
520 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
521
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
522 pChunks = function(plasmidPSLFile = NULL, plasmidDatabase = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
523 amrPSLFile = NULL, amrDatabase, noAMR = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
524 incPSLFile = NULL, incDatabase, noInc = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
525 outputDirectory = NA, overwrite = TRUE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
526 maxTargetLength = 300000,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
527 minQueryLength = 200,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
528 makeCircos = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
529 minQueryCoverage = 1/2, minTargetCoverage = 1/2,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
530 searchDepth = c(1),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
531 threads = 1,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
532 verbosity = 2) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
533
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
534 print(plasmidDatabase)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
535
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
536 ## Verify the arguments
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
537 argumentsGood = TRUE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
538 if (!file.exists(plasmidPSLFile)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
539 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
540 message(paste('Plasmid PSL file', plasmidPSLFile, 'not found'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
541 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
542 if (is.na(outputDirectory)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
543 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
544 message('Output directory not given')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
545 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
546 if (file.exists(outputDirectory) && !overwrite) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
547 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
548 message(paste('Output directory', outputDirectory, 'already exists. Add overwrite = TRUE'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
549 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
550 if (minQueryCoverage < .1 || minQueryCoverage > 1) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
551 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
552 message(paste('Minimum query coverage', minQueryCoverage, 'is outside of the range 0.1 <= x <= 1'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
553 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
554 if (minTargetCoverage < 0.02 || minTargetCoverage > 1) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
555 argumentsGood = FALSE
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
556 message(paste('Minimum target coverage', minTargetCoverage, 'is outside of the range 0.1 <= x <= 1'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
557 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
558 if (!argumentsGood){
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
559 message('There is a problem with the arguments')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
560 return()
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
561 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
562
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
563 ## Check for the existence of the output directory, remove if it exists
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
564 if (file.exists(outputDirectory)) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
565 printif(paste('Removing existing output directory', outputDirectory), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
566 unlink(outputDirectory, recursive = TRUE)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
567 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
568 printif(paste('Making output directory', outputDirectory), verbosity > 1)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
569 dir.create(outputDirectory)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
570
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
571 ## Default to c(1) for the plasmid search depth
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
572 searchDepths = lapply(searchDepth, function(i){seq(i, 1)})
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
573 searchDepths = as.matrix(expand.grid(searchDepths))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
574 print(searchDepths)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
575
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
576 plasmidScores = mclapply(1:nrow(searchDepths), function(i) {
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
577 findPlasmids(plasmidPSLFile = plasmidPSLFile, plasmidDatabase = plasmidDatabase,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
578 amrPSLFile = amrPSLFile, amrDatabase, noAMR = noAMR,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
579 incPSLFile = incPSLFile, incDatabase, noInc = noInc,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
580 outputDirectory = paste0(outputDirectory, '/plasmids_', paste(searchDepths[i,], collapse = '_')), overwrite = overwrite,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
581 maxTargetLength = 300000,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
582 minQueryLength = 200,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
583 makeCircos = makeCircos,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
584 minQueryCoverage = 1/2, minTargetCoverage = 1/2,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
585 searchDepth = searchDepths[i,], ## Search depth i
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
586 verbosity = verbosity)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
587 },
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
588 mc.cores = threads)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
589 plasmidScores = unlist(plasmidScores)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
590
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
591 print(cbind(searchDepths, plasmidScores))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
592
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
593 ## Pick out the best set, penalizing for not taking the first
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
594 penalties = (unlist(apply(searchDepths, 1, sum)) - ncol(searchDepths)) * 2500
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
595 print(penalties)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
596 plasmidScores = plasmidScores - penalties
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
597 print(cbind(searchDepths, plasmidScores))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
598 bestScoring = which.max(plasmidScores)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
599 bestScoringDirectory = paste0(outputDirectory, '/plasmids_', paste(searchDepths[bestScoring,], collapse = '_'))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
600
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
601 ## Link to the best scoring files
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
602 files = c('plasmids.tsv', 'amrFinal.bed')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
603 commands = paste('ln -s ', paste0('"', bestScoringDirectory, '/', files, '"'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
604 paste0(outputDirectory, '/', files))
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
605 printif(commands, verbosity >= 2)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
606 lapply(commands, system)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
607
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
608 }
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
609
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
610
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
611 optionList = list(
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
612 make_option('--plasmid_psl', type = 'character', default = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
613 help = 'Plasmid PSL database-v-contig output', metavar = '<PSL_FILE>'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
614 make_option('--plasmid_database', type = 'character', default = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
615 help = 'Plasmid database', metavar = '<PLASMID_FASTA>'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
616 make_option('--amr_database', type = 'character', default = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
617 help = 'AMR database', metavar = '<AMR_FASTA>'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
618 make_option('--amr_blast', type = 'character', default = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
619 help = 'AMR blast output', metavar = '<BLAST_6>'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
620 make_option('--output', type = 'character', default = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
621 help = 'Output dir', metavar = '<OUTPUT_DIR>'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
622 make_option('--threads', type = 'numeric', default = NULL,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
623 help = 'Output dir', metavar = '<OUTPUT_DIR>'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
624 make_option('--no_amr', action = 'store_true', default = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
625 help = 'Don\'t run AMR'),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
626 make_option('--no_inc', action = 'store_true', default = FALSE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
627 help = 'Don\'t run incompatibility groups')
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
628 )
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
629
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
630 optParser = OptionParser(option_list = optionList)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
631 opt = parse_args(optParser)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
632 print(opt)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
633 pChunks(plasmidPSLFile = opt$'plasmid_psl', plasmidDatabase = opt$'plasmid_database',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
634 amrPSLFile = opt$'amr_blast', amrDatabase = opt$'amr_database',
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
635 outputDirectory = opt$output,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
636 threads = opt$threads,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
637 # searchDepth = c(1,1),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
638 searchDepth = c(5,5),
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
639 noAMR = TRUE, noInc = TRUE,
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
640 verbosity = 2)
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
641
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
642
f45c65e3fd18 Uploaded
greg
parents:
diff changeset
643