-
Notifications
You must be signed in to change notification settings - Fork 0
/
nex2tbl.R
384 lines (288 loc) · 11.3 KB
/
nex2tbl.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
## Script to convert NEXUS-alignment to GenBank feature table
## Intron positions should be encoded in the following format "charset intron = 202-256 394-451;"
nex2tbl <- function(INPUT_NEX,
OUTPUT_TBL,
GENE = "gene_name",
PRODUCT = "product_name",
CODON_START = 1,
TRANSL_TABLE = 1,
FULL_GENE = FALSE
){
# ## Specify input and output files
# INPUT_NEX <- "test/exons-introns_CODON_START-1_TEF1_simple.nex"
# OUTPUT_TBL <- "test/exons-introns_CODON_START-1_TEF1_simple.nex.tbl"
# # OUTPUT_TBL <- NULL # print the results to screen
#
# ## Specify user-defined variables
# GENE <- "gene_name"
# PRODUCT <- "product_name"
# CODON_START <- 1
# TRANSL_TABLE <- 1
# FULL_GENE <- FALSE
library(ape)
library(plyr)
############################################################
############################################################ Data validation
############################################################
if(!CODON_START %in% c(1,2,3) | length(CODON_START) != 1){
warning("Please provide valid CODON_START paramerter.\n")
}
if(is.na(GENE) | is.null(GENE) | length(GENE) != 1){
warning("Please provide valid GENE name.\n")
}
if(is.na(PRODUCT) | is.null(PRODUCT) | length(PRODUCT) != 1){
warning("Please provide valid PRODUCT description.\n")
}
if(is.na(TRANSL_TABLE) | is.null(TRANSL_TABLE) | length(TRANSL_TABLE) != 1){
warning("Please provide valid translation table definition.\n")
}
if(FULL_GENE & CODON_START != 1){
warning("If the sequence covers the whole coding region of a protein, GenBank expects CODON_START to be 1.\n")
}
############################################################
############################################################ Region coordinates
############################################################
## Load alignment in NEXUS format
nex <- read.nexus.data(INPUT_NEX)
## Verify that all sequence names are unique
if(length(names(nex)) != length(unique(names(nex)))){
stop("Sequence names are not unique in the Nexus file!\n")
}
## Parse coordinates of intronic regions
introns <- grep(
pattern = "charset\\s+intron\\s*=\\s*",
x = readLines(INPUT_NEX),
ignore.case = TRUE,
value = TRUE)
introns <- gsub(
pattern = "charset\\s+intron\\s*=\\s*|;",
replacement = "",
ignore.case = TRUE,
perl = T,
x = introns)
## Calculate alignment length
aln_len <- length(nex[[1]])
## If there are no introns
if(length(introns) == 0){
introns <- list(
Exon_1 = c(1, aln_len)
)
} else {
## If there are some introns
introns <- strsplit(x = introns, split = " ")[[1]]
introns <- llply(.data = as.list(introns),
.fun = function(z){
z <- strsplit(z, split = "-")[[1]]
z <- as.numeric(z)
return(z)
})
names(introns) <- paste("Intron_", 1:length(introns), sep = "")
} # end of introns
############ Add exon segments
## Collapse runs of consecutive numbers to ranges
collapseConsecutive <- function(x){
## Find ranges of numbers
rg <- cumsum(c(TRUE, diff(x) > 1))
## Split numbers into ranges
rs <- split(x = x, f = rg)
## Preserve only the first and last number in a range
rs <- llply(.data = rs, .fun = function(z){ z[c(1, length(z))] })
return(rs)
}
# collapseConsecutive(c(1,2,3,4,5,6,8,9,10,22,23))
# collapseConsecutive(c(1, 3:5,20:25,26, 28))
## Expand regions
ii <- llply(.data = introns, .fun = function(z){ z[1]:z[2] })
ii <- sort(unlist(ii))
## Find exon regions
oth <- 1:aln_len
oth <- oth[!oth %in% ii]
if(length(oth) > 0){
ex <- collapseConsecutive(oth)
## Rename exons
names(ex) <- paste("Exon_", 1:length(ex), sep = "")
## Add regions to the main list
introns <- c(introns, ex)
## Sort regions by position
introns <- introns[ order(laply(.data = introns, .fun = function(z){ z[1] })) ]
}
## Check if there are any exon regions
if(!sum(grepl(pattern = "Exon", x = names(introns))) > 0){
stop("There are no exon regions in the alignment!\n")
}
############################################################
############################################################ Seq analysis
############################################################
## Gap symbols
gaps <- c(".", "?", "-")
## Sequence length
SeqLen <- ldply(
.data = nex,
.fun = function(z){ data.frame(SeqLen = sum(!z %in% gaps)) },
.id = "SeqID")
## Enumerate non-gap charactes
numb <- llply(
.data = nex,
.fun = function(z){
seqlen <- sum(!z %in% gaps)
z[!z %in% gaps] <- 1:seqlen
z[z %in% gaps] <- NA
z <- as.numeric(z)
return(z)
})
## Split alignment into regions
extract_region <- function(x, coords){ x[ coords[1]:coords[2] ] }
numb_reg <- llply(.data = numb,
.fun = function(z){
llply(.data = introns, .fun = function(cc){
extract_region(x = z, coords = cc) })
})
## Extract length of each region for each sequence
reg_len <- ldply(.data = numb_reg, .fun = function(z){
ldply(.data = z, .fun = function(r){
r <- na.omit(r)
if(length(r) > 0){
mi <- min(r, na.rm = TRUE)
ma <- max(r, na.rm = TRUE)
} else {
mi <- ma <- NA
}
rez <- data.frame(Start = mi, End = ma)
return(rez)
}, .id = "Region")
}, .id = "SeqID")
## Remove missing regions
reg_len <- reg_len[!is.na(reg_len$Start), ]
## Find if sequence starts with an intron
reg_len <- ddply(.data = reg_len, .variables = "SeqID",
.fun = function(x){
if(grepl(pattern = "Intron", x = x$Region[1])){
x$StartsWithIntron <- TRUE
} else {
x$StartsWithIntron <- FALSE
}
return(x)
})
## Find codon position for each sequence
get_codon <- function(z, CDSTART = NULL){
## Sequence name
seqid <- as.character( z$SeqID[1] )
## Get sequence
seq <- numb_reg[[ seqid ]]
## Get sequence of all exons
seq <- seq[ grep(pattern = "Exon", x = names(seq)) ]
seq <- unlist(seq)
## Find start postion of the first exon basepair
frst <- which.min(seq)
if(CDSTART == 1){
cdn <- rep(c(1,3,2), times = length(seq))
}
if(CDSTART == 2){
cdn <- rep(c(2,1,3), times = length(seq))
}
if(CDSTART == 3){
cdn <- rep(c(3,2,1), times = length(seq))
}
rz <- cdn[ frst ]
rz <- data.frame(codon_start = rz)
return(rz)
}
## e.g.,
# get_codon(z = subset(reg_len, SeqID == "AP2508"), CDSTART = 1)
# get_codon(z = subset(reg_len, SeqID == "AP2654"), CDSTART = 1)
codons <- ddply(.data = reg_len,
.variables = "SeqID",
.fun = function(z, ...){ get_codon(z, ...) },
CDSTART = CODON_START)
############################################################
############################################################ Feature table
############################################################
## Prepare data for feature table construction
prep_for_tbl <- function(x){
# x = part of a data.frame with region coordinates
# e.g., x <- subset(reg_len, SeqID == "SS1302")
## Extract exons
ex <- x[ grep(pattern = "Exon_", x = x$Region), ]
if(nrow(ex) >= 1){
## Collapse consequtive ranges
res <- sort(unlist(alply(.data = ex, .margins = 1, .fun = function(z){ z$Start : z$End })))
res <- collapseConsecutive(res)
## Convert to list
# res <- alply(.data = ex, .margins = 1, .fun = function(z){ c(z$Start, z$End) })
## Add sequence attributes
attr(res, which = "SeqID") <- as.character( x$SeqID[1] )
attr(res, which = "codon") <- codons[ which(codons$SeqID %in% x$SeqID[1]), "codon_start" ]
attr(res, which = "seqlen") <- SeqLen[ which(SeqLen$SeqID %in% x$SeqID[1]), "SeqLen" ]
} else {
## The case with intron-only sequence
cat("WARNING: Intron-only sequence - ", as.character( x$SeqID[1] ), "\n")
res <- NULL
}
return(res)
}
## Function to construct feature table (for single sequence)
make_tbl <- function(x,
gene = "placeholder_gene_name",
product = "placeholder_product_name",
transl_table = 1,
full_gene = FALSE){
# x = output of `prep_for_tbl`
# list with exon coordinates + attributes
## Skip intron-only sequences (no output returned)
if(is.null(x)){ return(NULL) }
min_len <- 1
max_len <- attr(x, which = "seqlen")
if(full_gene == FALSE){
min_len <- paste("<", min_len, sep = "")
max_len <- paste(">", max_len, sep = "")
x[[1]][1] <- paste("<", x[[1]][1], sep = "")
x[[length(x)]][2] <- paste(">", x[[length(x)]][2], sep = "")
}
## CDS
CDS <- laply(.data = x, .fun = paste, collapse = "\t")
## Print feature table
cat(">Features ", attr(x, which = "SeqID"), "\n", sep = "")
cat(paste(min_len, max_len, "gene", sep = "\t"), "\n", sep = "")
cat("\t", "\t", "\t", "gene", "\t", gene, "\n", sep = "")
cat(CDS[1], "\t", "CDS", "\n", sep = "")
if(length(CDS) > 1){
for(i in 2:length(CDS)){
cat(CDS[i], "\n", sep = "")
}}
cat("\t", "\t", "\t", "product", "\t", product, "\n", sep = "")
cat("\t", "\t", "\t", "codon_start", "\t", attr(x, which = "codon"), "\n", sep = "")
cat("\t", "\t", "\t", "transl_table", "\t", transl_table, "\n", sep = "")
}
## Test:
# make_tbl( prep_for_tbl(subset(reg_len, SeqID == "SS1308")) )
## Batch processing of seqs
For_tbl <- dlply(
.data = reg_len,
.variables = "SeqID",
.fun = prep_for_tbl
)
attr(For_tbl, "split_type") <- NULL
attr(For_tbl, "split_labels") <- NULL
## Remove NULL (no exons) instances
nulls <- laply(.data = For_tbl, .fun = is.null)
if(any(nulls)){ For_tbl <- For_tbl[ -which(nulls) ] }
## Remove NAs (empty sequence)
# nas <- ...
# if(any(nas)){ For_tbl <- For_tbl[ -which(nas) ] }
## Export feature tables to the file
if(!is.null(OUTPUT_TBL)){ sink(file = OUTPUT_TBL) }
l_ply(
.data = For_tbl,
.fun = make_tbl,
gene = GENE,
product = PRODUCT,
transl_table = TRANSL_TABLE,
full_gene = FULL_GENE)
## Stop writing to the file
if(!is.null(OUTPUT_TBL)){ sink() }
results <- list(
tabular = reg_len,
codons = codons
)
invisible(results)
}