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
|
#!/usr/bin/env ruby
require 'ostruct'
require 'optparse'
require 'gtruby'
def assert
raise "Assertion failed !" unless yield
end
Lcpinterval = Struct.new("Lcpinterval",:lcp, :lb, :rb, :brchildlist)
class LcpSufstream
def initialize(indexname)
@lcpfile = File.new(indexname + ".lcp","r")
@lcpfile.read(1)
@llvfile = File.new(indexname + ".llv","r")
@suffile = File.new(indexname + ".suf","r")
@totallength = nil
@specialcharacters = nil
@intbytes=nil
@mirrored=nil
@lastsuftabvalue = nil
File.new(indexname + ".prj","r").each_line do |line|
m = line.match(/^totallength=(\d+)/)
if m
@totallength=m[1].to_i
end
m = line.match(/^specialcharacters=(\d+)/)
if m
@specialcharacters=m[1].to_i
end
m = line.match(/^integersize=(\d+)/)
if m
if m[1].to_i == 64
@intbytes=8
else
@intbytes=4
end
end
m = line.match(/^mirrored=(\d+)/)
if m
if m[1].to_i == 0
@mirrored=false
else
@mirrored=true
end
end
end
el = GT::EncseqLoader.new
@encseq = el.load(indexname)
if @mirrored
@encseq.mirror()
end
end
def getencseq()
return @encseq
end
def next()
@lcpfile.each_byte do |cc|
suftabvalue = @suffile.read(@intbytes).unpack("L")[0]
if cc == 255
idxvalue = @llvfile.read(@intbytes)
lcpvalue = @llvfile.read(@intbytes)
yield lcpvalue.unpack("L")[0],suftabvalue
else
yield cc,suftabvalue
end
end
@lastsuftabvalue = @suffile.read(@intbytes).unpack("L")[0]
end
def numofnonspecials()
return @totallength - @specialcharacters
end
def lastsuftabvalue_get()
return @lastsuftabvalue
end
end
def processlcpinterval(itv)
puts "N #{itv.lcp} #{itv.lb} #{itv.rb}"
end
def enumlcpintervals(lcpsufstream)
stack = Array.new()
stack.push(Lcpinterval.new(0,0,nil,[]))
idx = 0
lcpsufstream.next() do |lcpvalue,previoussuffix|
lb = idx
while lcpvalue < stack.last.lcp
lastinterval = stack.pop
lastinterval.rb = idx
processlcpinterval(lastinterval)
lb = lastinterval.lb
end
if lcpvalue > stack.last.lcp
stack.push(Lcpinterval.new(lcpvalue,lb,nil,[]))
end
idx += 1
end
lastinterval = stack.pop
lastinterval.rb = idx
processlcpinterval(lastinterval)
end
def showbool(b)
if b
return "1"
else
return "0"
end
end
Edge = Struct.new("Edge",:kind,:firstedge,:parent,:goal)
def enumlcpintervaltree(lcpsufstream)
stack = Array.new()
lastinterval = nil
firstedgefromroot = true
firstedge = false
prevlcpvalue = 0
storesuffix = nil
stack.push(Lcpinterval.new(0,0,nil,[]))
nonspecials = lcpsufstream.numofnonspecials()
idx=0
lcpsufstream.next() do |lcpvalue,previoussuffix|
storesuffix = previoussuffix
if idx >= nonspecials
break
end
if lcpvalue <= stack.last.lcp
if stack.last.lcp > 0 or not firstedgefromroot
firstedge = false
else
firstedge = true
firstedgefromroot = false
end
assert{stack.last.lcp == [prevlcpvalue,lcpvalue].max}
yield Edge.new(0,firstedge,stack.last,previoussuffix)
end
assert {lastinterval.nil?}
while lcpvalue < stack.last.lcp
lastinterval = stack.pop
lastinterval.rb = idx
yield Edge.new(2,nil,lastinterval,nil)
if lcpvalue <= stack.last.lcp
firstedge = false
if stack.last.lcp > 0 or not firstedgefromroot
firstedge = false
else
firstedge = true
firstedgefromroot = false
end
yield Edge.new(1,firstedge,stack.last,lastinterval)
stack.last.brchildlist.push(lastinterval)
lastinterval = nil
end
end
if not lastinterval.nil?
assert{lcpvalue > stack.last.lcp}
end
if lastinterval.nil?
assert{lcpvalue>=stack.last.lcp}
end
if lcpvalue > stack.last.lcp
if not lastinterval.nil?
stack.push(Lcpinterval.new(lcpvalue,lastinterval.lb,nil,[lastinterval]))
yield Edge.new(1,true,stack.last,lastinterval)
lastinterval = nil
else
stack.push(Lcpinterval.new(lcpvalue,idx,nil,[]))
yield Edge.new(0,true,stack.last,previoussuffix)
end
end
assert {lcpvalue == stack.last.lcp}
prevlcpvalue = lcpvalue
idx += 1
end
assert {stack.length > 0}
if stack.last.lcp > 0
yield Edge.new(0,false,stack.last,lcpsufstream.lastsuftabvalue_get())
yield Edge.new(2,nil,stack.last,nil)
end
end
def parseargs(argv)
options = OpenStruct.new
options.itv = false
options.tree = false
options.minlen = nil
options.indexname = nil
opts = OptionParser.new
opts.on("-i","--itv","output lcp-intervals") do |x|
options.itv = true
end
opts.on("-t","--tree","output lcp-intervaltree") do |x|
options.tree = true
end
opts.on("-m","--m NUM","output suffix-prefix matches of given length") do |x|
options.minlen = x.to_i
end
rest = opts.parse(argv)
if rest.empty?
STDERR.puts "Usage: #{$0}: missing indexname"
exit 1
elsif rest.length != 1
STDERR.puts "Usage: #{$0}: too many indexnames"
exit 1
else
options.indexname = rest[0]
end
return options
end
def showleafedge(firstedge,parent,suffix)
puts "L #{showbool(firstedge)} #{parent.lcp} #{parent.lb} #{suffix}"
end
def showbranchedge(firstedge,parent,toitv)
print "B #{showbool(firstedge)} #{parent.lcp} #{parent.lb} "
puts "#{toitv.lcp} #{toitv.lb}"
end
Resource = Struct.new("Resource",:encseq,:minlen,:firstinW,:wset,:lset)
def itv2key(itv)
return "#{itv.lcp} #{itv.lb}"
end
def spmleafedge(res,firstedge,itv,pos)
if itv.lcp >= res.minlen
# puts "leafedge from #{itv.lcp} #{itv.lb} to #{pos}"
if firstedge
res.firstinW[itv2key(itv)] = res.wset.length
end
if pos == 0 or res.encseq.get_encoded_char(pos-1) == 255
idx = res.encseq.seqnum(pos)
res.wset.push(idx)
end
if pos + itv.lcp == res.encseq.total_length or
res.encseq.get_encoded_char(pos + itv.lcp) == 255
idx = res.encseq.seqnum(pos)
res.lset.push(idx)
end
end
end
def spmbranchedge(res,firstedge,itv,itvprime)
if itv.lcp >= res.minlen and firstedge
# print "branch from #{itv.lcp} #{itv.lb} #{itv.rb} to "
# puts "#{itvprime.lcp} #{itvprime.lb} #{itvprime.rb}"
res.firstinW[itv2key(itv)] = res.firstinW[itv2key(itvprime)]
end
end
def spmlcpinterval(res,itv)
if itv.lcp >= res.minlen
# puts "itv #{itv.lcp} #{itv.lb}"
firstpos = res.firstinW[itv2key(itv)]
res.lset.each do |l|
firstpos.upto(res.wset.length-1) do |i|
puts "#{l} #{res.wset[i]} #{itv.lcp}"
end
end
res.lset = []
else
res.wset = []
end
end
options = parseargs(ARGV)
lcpsufstream = LcpSufstream.new(options.indexname)
if options.itv
enumlcpintervals(lcpsufstream)
elsif options.tree
enumlcpintervaltree(lcpsufstream) do |item|
if item.kind == 0
showleafedge(item.firstedge,item.parent,item.goal)
elsif item.kind == 1
showbranchedge(item.firstedge,item.parent,item.goal)
end
end
elsif not options.minlen.nil?
res = Resource.new(lcpsufstream.getencseq(),
options.minlen,Hash.new(),Array.new(),Array.new())
enumlcpintervaltree(lcpsufstream) do |item|
if item.kind == 0
spmleafedge(res,item.firstedge,item.parent,item.goal)
elsif item.kind == 1
spmbranchedge(res,item.firstedge,item.parent,item.goal)
else
spmlcpinterval(res,item.parent)
end
end
end
|