"""
Wrap RNAfold command
"""

import subprocess
import pybedtools

from bcbio.utils import splitext_plus
from bcbio.distributed.transaction import file_transaction

def run_rnafold(seqs):
    out = 0
    cmd = ("echo {seqs} | RNAfold").format(**locals())
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
    for line in iter(process.stdout.readline, ''):
        if line.find(" ") > -1:
            out = float(line.split(" (")[1][0:6].strip())
    return out

def calculate_structure(loci_file, genome):
    """
    Get fasta sequence for each loci and calculate structure
    """
    structure_file = splitext_plus(loci_file)[0] + "-fold.tsv"
    with file_transaction(structure_file) as out_tx:
        with open(out_tx, 'w') as out_handle:
            loci_bed = pybedtools.BedTool(loci_file)
            print >>out_handle, "id\tfold"
            for region in loci_bed:
                print region
                seq = pybedtools.BedTool(str(region), from_string=True).sequence(fi=genome)
                seq = open(seq.seqfn).read().split("\n")[1]
                g = run_rnafold(seq)
                print >>out_handle, "%s\t%s" % (region[3], g)
    return structure_file

@@ -158,16 +158,48 @@
 action%0A%0A
+from seqcluster.libs import do%0A%0A
 def run_
@@ -376,16 +376,76 @@
 l=True)%0A
+    assert do.find_cmd(%22RNAfold%22), %22Install RNAfold first.%22%0A
     for 
@@ -1012,37 +1012,8 @@
 ed:%0A
-                print region%0A
     

check if rnafold is installed