-
Notifications
You must be signed in to change notification settings - Fork 5
/
gt-stripstop
executable file
·122 lines (110 loc) · 4.22 KB
/
gt-stripstop
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
#!/usr/bin/env gt
--[[
Copyright (c) 2015 Sascha Steinbiss <[email protected]>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
package.path = gt.script_dir .. "/?.lua;" .. package.path
require("optparse")
op = OptionParser:new({usage="%prog <sequence file> < infile.gff",
oneliner="Converts all genes with internal stop codons "
.. "to pseudogenes.",
version="0.1"})
op:option{"-r", action='store_true', dest='remove',
help="skip feature instead of making it a pseudogene"}
options,args = op:parse({remove = false})
function usage()
op:help()
os.exit(1)
end
region_mapping = gt.region_mapping_new_seqfile_matchdescstart(args[1])
outvis = gt.gff3_visitor_new()
visitor = gt.custom_visitor_new()
visitor.last_seqid = nil
function visitor:visit_feature(fn)
local has_stop = false
local protseq = nil
for node in fn:children() do
if node:get_type() == "mRNA" then
protseq = node:extract_and_translate_sequence("CDS", true,
region_mapping)
if protseq:sub(1, -2):match("[*+#]") then
has_stop = true
break
end
end
end
-- make this a pseudogene
if has_stop then
if not options.remove then
local npseudogene = nil
local nptranscript = nil
local npexon = nil
for node in fn:children() do
local rng = node:get_range()
if node:get_type() == "gene" then
npseudogene = gt.feature_node_new(node:get_seqid(), "pseudogene",
rng:get_start(), rng:get_end(),
node:get_strand())
for k,v in node:attribute_pairs() do
npseudogene:set_attribute(k, v)
end
npseudogene:set_attribute("has_internal_stop", "true")
local rorth = npseudogene:get_attribute("ratt_ortholog")
if rorth and protseq then
npseudogene:set_attribute("Target", rorth .. " 1 " .. string.len(protseq))
end
elseif node:get_type() == "mRNA" then
assert(npseudogene)
nptranscript = gt.feature_node_new(node:get_seqid(),
"pseudogenic_transcript",
rng:get_start(), rng:get_end(),
node:get_strand())
npseudogene:add_child(nptranscript)
for k,v in node:attribute_pairs() do
nptranscript:set_attribute(k, v)
end
elseif node:get_type() == "CDS" then
assert(nptranscript)
npexon = gt.feature_node_new(node:get_seqid(),
"pseudogenic_exon",
rng:get_start(), rng:get_end(),
node:get_strand())
nptranscript:add_child(npexon)
for k,v in node:attribute_pairs() do
npexon:set_attribute(k, v)
end
end
end
if npseudogene then
npseudogene:accept(outvis)
end
end
else
fn:accept(outvis)
end
return 0
end
-- make simple visitor stream, just applies given visitor to every node
visitor_stream = gt.custom_stream_new_unsorted()
function visitor_stream:next_tree()
local node = self.instream:next_tree()
if node then
node:accept(self.vis)
end
return node
end
visitor_stream.instream = gt.gff3_in_stream_new_sorted()
visitor_stream.vis = visitor
local gn = visitor_stream:next_tree()
while (gn) do
gn = visitor_stream:next_tree()
end