klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
| 1 | -- bioinformatics routines |
| 2 | |
| 3 | -- Description: read a fasta/fastq file |
| 4 | local function readseq(fp) |
| 5 | local finished, last = false, nil; |
| 6 | return function() |
| 7 | local match; |
| 8 | if finished then return nil end |
| 9 | if (last == nil) then -- the first record or a record following a fastq |
| 10 | for l in fp:lines() do |
| 11 | if l:byte(1) == 62 or l:byte(1) == 64 then -- ">" || "@" |
| 12 | last = l; |
| 13 | break; |
| 14 | end |
| 15 | end |
| 16 | if last == nil then |
| 17 | finished = true; |
| 18 | return nil; |
| 19 | end |
| 20 | end |
| 21 | local tmp = last:find("%s"); |
| 22 | name = (tmp and last:sub(2, tmp-1)) or last:sub(2); -- sequence name |
| 23 | local seqs = {}; |
| 24 | local c; -- the first character of the last line |
| 25 | last = nil; |
| 26 | for l in fp:lines() do -- read sequence |
| 27 | c = l:byte(1); |
| 28 | if c == 62 or c == 64 or c == 43 then |
| 29 | last = l; |
| 30 | break; |
| 31 | end |
| 32 | table.insert(seqs, l); |
| 33 | end |
| 34 | if last == nil then finished = true end -- end of file |
| 35 | if c ~= 43 then return name, table.concat(seqs) end -- a fasta record |
| 36 | local seq, len = table.concat(seqs), 0; -- prepare to parse quality |
| 37 | seqs = {}; |
| 38 | for l in fp:lines() do -- read quality |
| 39 | table.insert(seqs, l); |
| 40 | len = len + #l; |
| 41 | if len >= #seq then |
| 42 | last = nil; |
| 43 | return name, seq, table.concat(seqs); |
| 44 | end |
| 45 | end |
| 46 | finished = true; |
| 47 | return name, seq; |
| 48 | end |
| 49 | end |
| 50 | |
| 51 | -- extract subsequence from a fasta file indexe by samtools faidx |
| 52 | local function faidxsub(fn) |
| 53 | local fpidx = io.open(fn .. ".fai"); |
| 54 | if fpidx == nil then |
| 55 | io.stderr:write("[faidxsub] fail to open the FASTA index file.\n"); |
| 56 | return nil |
| 57 | end |
| 58 | local idx = {}; |
| 59 | for l in fpidx:lines() do |
| 60 | local name, len, offset, line_blen, line_len = l:match("(%S+)%s(%d+)%s(%d+)%s(%d+)%s(%d+)"); |
| 61 | if name then |
| 62 | idx[name] = {tonumber(len), offset, line_blen, line_len}; |
| 63 | end |
| 64 | end |
| 65 | fpidx:close(); |
| 66 | local fp = io.open(fn); |
| 67 | return function(name, beg_, end_) -- 0-based coordinate |
| 68 | if name == nil then fp:close(); return nil; end |
| 69 | if idx[name] then |
| 70 | local a = idx[name]; |
| 71 | beg_ = beg_ or 0; |
| 72 | end_ = end_ or a[1]; |
| 73 | end_ = (end_ <= a[1] and end_) or a[1]; |
| 74 | local fb, fe = math.floor(beg_ / a[3]), math.floor(end_ / a[3]); |
| 75 | local qb, qe = beg_ - fb * a[3], end_ - fe * a[3]; |
| 76 | fp:seek("set", a[2] + fb * a[4] + qb); |
| 77 | local s = fp:read((fe - fb) * a[4] + (qe - qb)):gsub("%s", ""); |
| 78 | return s; |
| 79 | end |
| 80 | end |
| 81 | end |
| 82 | |
| 83 | --Description: Index a list of intervals and test if a given interval overlaps with the list |
| 84 | --Example: lua -lbio -e 'a={{100,201},{200,300},{400,600}};f=bio.intvovlp(a);print(f(600,700))' |
| 85 | --[[ |
| 86 | By default, we keep for each tiling 8192 window the interval overlaping the |
| 87 | window while having the smallest start position. This method may not work |
| 88 | well when most intervals are small but few intervals span a long distance. |
| 89 | ]]-- |
| 90 | local function intvovlp(intv, bits) |
| 91 | bits = bits or 13 -- the default bin size is 8192 = 1<<13 |
| 92 | table.sort(intv, function(a,b) return a[1] < b[1] end) -- sort by the start |
| 93 | -- merge intervals; the step speeds up testing, but can be skipped |
| 94 | local b, e, k = -1, -1, 1 |
| 95 | for i = 1, #intv do |
| 96 | if e < intv[i][1] then |
| 97 | if e >= 0 then intv[k], k = {b, e}, k + 1 end |
| 98 | b, e = intv[i][1], intv[i][2] |
| 99 | else e = intv[i][2] end |
| 100 | end |
| 101 | if e >= 0 then intv[k] = {b, e} end |
| 102 | while #a > k do table.remove(a) end -- truncate the interval list |
| 103 | -- build the index for the list of intervals |
| 104 | local idx, size, max = {}, math.pow(2, bits), 0 |
| 105 | for i = 1, #a do |
| 106 | b = math.modf(intv[i][1] / size) |
| 107 | e = math.modf(intv[i][2] / size) |
| 108 | if b == e then idx[b] = idx[b] or i |
| 109 | else for j = b, e do idx[j] = idx[j] or i end end |
| 110 | max = (max > e and max) or e |
| 111 | end |
| 112 | -- return a function (closure) |
| 113 | return function(_beg, _end) |
| 114 | local x = math.modf(_beg / size) |
| 115 | if x > max then return false end |
| 116 | local off = idx[x]; -- the start bin |
| 117 | if off == nil then -- the following is not the best in efficiency |
| 118 | for i = x - 1, 0, -1 do -- find the minimum bin with a value |
| 119 | if idx[i] ~= nil then off = idx[i]; break; end |
| 120 | end |
| 121 | if off == nil then return false end |
| 122 | end |
| 123 | for i = off, #intv do -- start from off and search for overlaps |
| 124 | if intv[i][1] >= _end then return false |
| 125 | elseif intv[i][2] > _beg then return true end |
| 126 | end |
| 127 | return false |
| 128 | end |
| 129 | end |
| 130 | |
| 131 | bio = { |
| 132 | readseq = readseq, |
| 133 | faidxsub = faidxsub, |
| 134 | intvovlp = intvovlp |
| 135 | } |
| 136 | |
| 137 | bio.nt16 = { |
| 138 | [0]=15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
| 139 | 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
| 140 | 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15, 15,15, 5, 6, 8,15, 7, 9, 0,10,15,15, 15,15,15,15, |
| 141 | 15, 1,14, 2, 13,15,15, 4, 11,15,15,12, 15, 3,15,15, 15,15, 5, 6, 8,15, 7, 9, 0,10,15,15, 15,15,15,15, |
| 142 | 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
| 143 | 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
| 144 | 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, |
| 145 | 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15, 15,15,15,15 |
| 146 | } |
| 147 | bio.ntcnt = { [0]=4, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 } |
| 148 | bio.ntcomp = { [0]=0, 8, 4, 12, 2, 10, 9, 14, 1, 6, 5, 13, 3, 11, 7, 15 } |
| 149 | bio.ntrev = 'XACMGRSVTWYHKDBN' |