-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbio.lua
More file actions
executable file
·227 lines (215 loc) · 5.24 KB
/
Copy pathbio.lua
File metadata and controls
executable file
·227 lines (215 loc) · 5.24 KB
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
local torch = require('torch')
local re = require('re')
local ftcsv = require('ftcsv')
local bio = {}
bio = {
fasta = (
function(f)
local file = io.open(f, "r")
local seqname = ""
local seqna = {}
local open = false
local seq = {}
local i = 0
for line in file:lines() do
if string.match(line, ">") == nil and open == true then
table.insert(seqna, line)
end
if #seqna >= 1 then
seq[i] = {
name = seqname,
na = table.concat(seqna)
}
end
if string.match(line, ">") ~= nil and open == true then
seqname = ""
seqna = {}
open = false
end
if string.match(line, ">") ~= nil and open == false then
seqname = string.gsub(line, ">(.+)", "%1")
open = true
i = i+1
end
end
return seq
end
),
flatten = (
--- ++++++ source: http://svn.wildfiregames.com/public/ps/trunk/build/premake/premake4/src/base/table.lua
function(arr)
local result = {}
local function toflat(arr)
for _, v in ipairs(arr) do
if type(v) == "table" then
toflat(v)
else
table.insert(result, v)
end
end
end
toflat(arr)
return result
end
),
select = (
--- ++++++ source: http://stackoverflow.com/questions/31304601/torch-lua-how-to-select-a-subset-of-an-array-or-tensor
function(t, first, last)
local sub = {}
for i=first,last do
sub[#sub + 1] = t[i]
end
return sub
end
),
win = (
function(seq, winsize)
local seql = string.len(seq)-winsize
local seqw = {}
local seqr = torch.range(1, seql)
seqr:apply(function(wstart)
local wend = wstart+winsize-1
seqw[wstart] = string.sub(seq, wstart, wend)
end
)
return seqw
end
),
transpose = (
function(t)
local tb = {}
for row = 1, #t[1] do
tb[row] = {}
for col = 1, #t do
tb[row][col] = t[col][row]
end
end
return tb
end
),
csv = (
function(inp)
local hotdatain = nil
if(inp.data == nil) then -- READ IN
if (inp.headers == nil) then
inp.headers = false
end
if (string.match(inp.file, ",") ~= nil) then -- MANY FILES
hotdatain = {}
local files = {}
local files = string.split(inp.file, ",")
for k, v in ipairs(files) do
hotdatain[k] = bio.csvin(v, inp.headers)
end
else -- SINGLE FILE
hotdatain = bio.csvin(inp.file, inp.headers)
end
else -- WRITE OUT
if (torch.type(inp.headers) == "string") then
bio.csvout(inp.data, inp.file, inp.headers) -- ASSIGN HEADERS
else
bio.csvout(inp.data, inp.file) -- NO HEADERS
end
end
if (hotdatain ~= nil) then
return hotdatain
end
end
),
csvin = (
function(file, headers)
local csvopt = { headers = headers }
local datain = ftcsv.parse(file, ",", csvopt)
return datain
end
),
csvout = (
function(data, saveas, headers)
if string.match(saveas, ".+%.csv") ~= nil then
saveas = saveas .. ".csv"
end
if string.match(torch.type(data), "torch.+") ~= nil then
if (headers ~= nil) then
bio.tensor2csv(data, saveas, headers) -- TENSOR TO CSV
else
bio.tensor2csv(data, saveas)
end
elseif torch.type(data) == "table" then
local file = assert(io.open(saveas, "w"))
if (headers ~= nil) then
file:write(headers .. "\n")
end
file:write(ftcsv.encode(data, ",")) -- LUA TABLE TO CSV
file:close()
end
end
),
tensor2csv = (
function(m, f, headers)
local ne = 0
local drillm = m
local c = 0
while string.match(torch.type(data), "torch.+") ~= nil do
print(torch.type(drillm))
c = c + 1
if torch.type(drillm) ~= "number" then
drillm = drillm[1]
end
if string.match(torch.type(data), "torch.+") == nil then
ne = c
end
end
if ne == 3 then
for x = 1, m:size()[1] do
local t = {}
for i = 1, m:size()[2] do
for j = 1, m:size()[3] do
if j ~= m:size()[3] then
t[(#t+1)] = m[x][i][j] .. ","
else
t[(#t+1)] = m[x][i][j] .. "\n"
end
end
end
file = assert(io.open(x .. "-" .. f, "w"))
if (headers ~= nil) then
file:write(headers .. "\n")
end
file:write(table.concat(t, ""))
file:close()
end
end
if ne == 2 then
local t = {}
for i = 1, m:size()[1] do
for j = 1, m:size()[2] do
if j ~= m:size()[2] then
t[(#t+1)] = m[i][j] .. ","
else
t[(#t+1)] = m[i][j] .. "\n"
end
end
end
file = assert(io.open(f, "w"))
if (headers ~= nil) then
file:write(headers .. "\n")
end
file:write(table.concat(t, ""))
file:close()
end
if ne == 1 then
local t = {}
for i = 1, m:size()[1] do
t[(#t+1)] = m[i] .. "\n"
end
file = assert(io.open(f, "w"))
if (headers ~= nil) then
file:write(headers .. "\n")
end
file:write(table.concat(t, ""))
file:close()
end
end
)
}
return bio