-
Notifications
You must be signed in to change notification settings - Fork 6
/
rnaplfold.py
55 lines (41 loc) · 1.59 KB
/
rnaplfold.py
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
from subprocess import Popen, PIPE, STDOUT
import os
import tempfile
def rnaplfold(seq, prob_threshold=0.0, rnaplfold_binary="RNAplfold"):
"""
Arguments:
- `seq`: FIXME
- `prob_threshold`: FIXME
"""
# There are two ways to call RNAplfold:
#
# echo seq | RNAplfold; produces plfold_dp.ps
# or
# RNAplfold < seq.file; produces seqid__dp.ps
#
# We use the first to avoid issues with messy seq-ids
RNAPLFOLD_DEFAULT_DPPS_NAME = "plfold_dp.ps"
# RNAplfold produces a file in current dir, so move to temp dir
# before calling
orig_workdir = os.getcwd()
os.chdir(tempfile.tempdir)
p = Popen([rnaplfold_binary], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
# gives OSError if not installed
(stdoutdata, stderrdata) = p.communicate(input=seq + "\n")[0]
# FIXME test stderrdata and stdoutdata?
# FIXME test if file exists
fid_dpps = open(RNAPLFOLD_DEFAULT_DPPS_NAME, 'r')
for line in fid_dpps:
# base pairs are stored in the following form:
# % i j sqrt(p(i,j)) ubox
line_split = line.split()
if len(line_split) == 4 and line_split[3] == "ubox":
print "DEBUG: got a bp (FIXME: unit offset?) at %s" % ':'.join(line.split()[0:2])
#(bp_i, bp_j) = [int(x) for x in line_split[0:2]
#bp_prob = float(line_split[2])**2
# FIXME store as list of lists
# FIXME zero-/unit-offset?
# FIXME apply prob_threshold
fid_dpps.close()
os.remove(RNAPLFOLD_DEFAULT_DPPS_NAME)
os.chdir(orig_workdir)