-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgene.lua
69 lines (59 loc) · 2.07 KB
/
gene.lua
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
-- examples/gene.lua
-- The alignment score of -405038 indicates that the two sequences
-- compared have high dissimilarity and little to no meaningful alignment.
-- Load the required packages
local nwunsch = require("nwunsch")
local http = require("socket.http")
local ltn12 = require("ltn12")
local coroutine = require("coroutine")
-- Function to retrieve DNA sequence from Ensembl REST API
local function get_sequence(gene_id)
local server = "http://rest.ensembl.org"
local ext = "/sequence/id/" .. gene_id .. "?content-type=text/plain"
local response_body = {}
local status_code = http.request {
url = server .. ext,
method = "GET",
sink = ltn12.sink.table(response_body)
}
if status_code ~= 200 then
error("Failed to retrieve sequence for " .. gene_id .. ". Status code: " .. status_code)
end
return table.concat(response_body)
end
-- Function to download sequences in parallel
local function download_sequences(gene_ids)
local sequences = {}
local threads = {}
for _, gene_id in ipairs(gene_ids) do
local thread = coroutine.create(function()
local sequence = get_sequence(gene_id)
sequences[gene_id] = sequence
end)
table.insert(threads, thread)
end
for _, thread in ipairs(threads) do
coroutine.resume(thread)
end
return sequences
end
-- Retrieve DNA sequences
local gene_ids = {"ENSG00000157764", "ENSG00000198804"}
local sequences = download_sequences(gene_ids)
-- Check if sequences for both gene IDs are retrieved
if not sequences["ENSG00000157764"] or not sequences["ENSG00000198804"] then
error("Sequences for both gene IDs are required for comparison.")
end
-- Assign sequences to variables
local seq1 = sequences["ENSG00000157764"]
local seq2 = sequences["ENSG00000198804"]
-- Set scoring parameters
local match = 2
local mismatch = -1
local gap = -2
-- Perform sequence alignment using nwunsch package
local score, align1, align2 = nwunsch.NeedlemanWunsch(seq1, seq2, match, mismatch, gap)
-- Print the alignment results
print("Alignment score:", score)
print("Alignment 1:", align1)
print("Alignment 2:", align2)