-
Notifications
You must be signed in to change notification settings - Fork 5
/
gt-toplevelize
executable file
·94 lines (82 loc) · 3.26 KB
/
gt-toplevelize
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
#!/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 <options> <sequence> < infile.gff",
oneliner="Promote features of a given type to toplevel.",
version="0.1"})
op:option{"-t", action='store', dest='type',
help="type to toplevelize"}
options,args = op:parse({type = "LTR_retrotransposon"})
function usage()
op:help()
os.exit(1)
end
if #args < 1 then
usage()
end
rm = gt.region_mapping_new_seqfile_matchdescstart(args[1])
gff3v = gt.gff3_visitor_new()
outfile = assert(io.open("toplevelized_" .. options.type .. ".fasta", "w+"))
visitor = gt.custom_visitor_new()
function visitor:visit_feature(fn)
for node in fn:children() do
if node:get_type() == options.type then
outfile:write(">" .. node:get_attribute("ID") .. "\n")
local seq = node:extract_sequence(options.type, false, rm)
outfile:write(seq)
outfile:write("\n")
local start = node:get_range():get_start()
local endp = node:get_range():get_end()
for c in node:children() do
if c:get_range():get_start() < start or c:get_range():get_end() > endp then
io.stderr:write(fn:get_type() .. " with ID '"
.. tostring(fn:get_attribute("ID")) .. "' has a child "
.. c:get_type()
.. " outside its coordinates "
.. "(" .. tostring(c:get_range())
.. " vs. " .. tostring(start)
.. "-" .. tostring(endp) ..") on line "
.. c:get_line_number() .. ", skipping\n")
return 0
else
local new_rng = gt.range_new(c:get_range():get_start() - start + 1,
c:get_range():get_start() - start +
c:get_range():length())
c:change_seqid(node:get_attribute("ID"))
c:set_range(new_rng)
end
end
node:accept(gff3v)
end
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
outfile:close()