<commit_before>#!/usr/bin/env python

import networkx as nx
import subprocess as sp
from eden.modifier.fasta import fasta_to_fasta
from eden.converter.fasta import seq_to_networkx
import math
from eden.util import is_iterable


def rnashapes_wrapper(sequence, shape_type=None, energy_range=None, max_num=None):
    #command line
    cmd = 'echo "%s" | RNAshapes -t %d -c %d -# %d' % (sequence,shape_type,energy_range,max_num)
    out = sp.check_output(cmd, shell = True)
    text = out.strip().split('\n')
    seq_info = text[0]
    #shape:
    shape_list = []
    #extract the shape bracket notation
    shape_list += [line.split()[2] for line in text[1:-1]] 
    #energy:
    energy_list = []
    #convert negative energy into positive integer
    #create a string of length equal to the energy
    for line in text[1:-1]:
        energy = int(math.floor(-float(line.split()[0])))
        energy_dec = int( energy / 10 )
        reminder = energy % 10
        energy_string = ""
        for i in range(energy_dec):
            energy_string += str(i)
        if reminder > 5:
            energy_string += 'x'
        energy_list += [energy_string] 
    #dotbracket:
    dotbracket_list = []
    #extract the dot bracket notation
    dotbracket_list += [line.split()[1] for line in text[1:-1]] 
    #make a list of triplets
    seq_struct_list = []
    for shape,energy,dotbracket in zip(shape_list,energy_list,dotbracket_list): 
        seq_struct_list.append((shape,energy,dotbracket))
    return seq_info, seq_struct_list


def string_to_networkx(header, sequence, **options):
    #defaults
    shape_type =  options.get('shape_type',5)
    energy_range =  options.get('energy_range',10)
    max_num =  options.get('max_num',3)
    shape = options.get('shape',False)
    energy = options.get('energy',False)
    dotbracket = options.get('dotbracket',True)
    split_components = options.get('split_components', False)
    seq_info, seq_struct_list = rnashapes_wrapper(sequence, shape_type=shape_type, energy_range=energy_range, max_num=max_num)
    if split_components:
        for shape_str,energy_str,dotbracket_str in seq_struct_list:
            G = nx.Graph()
            if shape:
                G_shape=seq_to_networkx('',shape_str)
                G = nx.disjoint_union(G, G_shape)
            if energy:
                G_energy=seq_to_networkx('',energy_str)
                G = nx.disjoint_union(G, G_energy)
            if dotbracket:
                G_dotbracket=seq_to_networkx('',dotbracket_str)
                G = nx.disjoint_union(G, G_dotbracket)
            G.graph['id'] = header + '_' + shape_str
            G.graph['info'] = 'RNAshapes shape_type=%s energy_range=%s max_num=%s shape=%s energy=%s dotbracket=%s' % (shape_type, energy_range, max_num, shape, energy, dotbracket)
            G.graph['sequence'] = sequence
            yield G
    else:
        G_global = nx.Graph()
        for shape_str,energy_str,dotbracket_str in seq_struct_list:
            G = nx.Graph()
            if shape:
                G_shape=seq_to_networkx('',shape_str)
                G = nx.disjoint_union(G, G_shape)
            if energy:
                G_energy=seq_to_networkx('',energy_str)
                G = nx.disjoint_union(G, G_energy)
            if dotbracket:
                G_dotbracket=seq_to_networkx('',dotbracket_str)
                G = nx.disjoint_union(G, G_dotbracket)
            G_global = nx.disjoint_union(G_global, G)
        G_global.graph['id'] = header
        G_global.graph['info'] = 'RNAshapes shape_type=%s energy_range=%s max_num=%s shape=%s energy=%s dotbracket=%s' % (shape_type, energy_range, max_num, shape, energy, dotbracket)
        G_global.graph['sequence'] = sequence
        yield G_global


def rnashapes_struct_to_eden(iterable, **options):    
    assert(is_iterable(iterable)), 'Not iterable'
    for header, seq in iterable:
        try:
            for G in string_to_networkx(header, seq, **options):
                yield G
        except Exception as e:
            print e.__doc__
            print e.message
            print 'Error in: %s' % seq
            G = seq_to_networkx(header, seq, **options)
            yield G<commit_msg>BUGFIX: rnashapes_struct had a bug for energy less than 5Kcal<commit_after>#!/usr/bin/env python

import networkx as nx
import subprocess as sp
from eden.modifier.fasta import fasta_to_fasta
from eden.converter.fasta import seq_to_networkx
import math
from eden.util import is_iterable


def rnashapes_wrapper(sequence, shape_type=None, energy_range=None, max_num=None):
    #command line
    cmd = 'echo "%s" | RNAshapes -t %d -c %d -# %d' % (sequence,shape_type,energy_range,max_num)
    out = sp.check_output(cmd, shell = True)
    text = out.strip().split('\n')
    seq_info = text[0]
    #shape:
    shape_list = []
    #extract the shape bracket notation
    shape_list += [line.split()[2] for line in text[1:-1]] 
    #energy:
    energy_list = []
    #convert negative energy into positive integer
    #create a string of length equal to the energy
    for line in text[1:-1]:
        energy = int(math.floor(-float(line.split()[0])))
        energy_dec = int( energy / 10 )
        reminder = energy % 10
        energy_string = ""
        for i in range(energy_dec + 1):
            energy_string += str(i)
        if reminder > 5:
            energy_string += 'x'
        energy_list += [energy_string] 
    #dotbracket:
    dotbracket_list = []
    #extract the dot bracket notation
    dotbracket_list += [line.split()[1] for line in text[1:-1]] 
    #make a list of triplets
    seq_struct_list = []
    for shape,energy,dotbracket in zip(shape_list,energy_list,dotbracket_list): 
        seq_struct_list.append((shape,energy,dotbracket))
    return seq_info, seq_struct_list


def string_to_networkx(header, sequence, **options):
    #defaults
    shape_type =  options.get('shape_type',5)
    energy_range =  options.get('energy_range',10)
    max_num =  options.get('max_num',3)
    shape = options.get('shape',False)
    energy = options.get('energy',False)
    dotbracket = options.get('dotbracket',True)
    split_components = options.get('split_components', False)
    seq_info, seq_struct_list = rnashapes_wrapper(sequence, shape_type=shape_type, energy_range=energy_range, max_num=max_num)
    if split_components:
        for shape_str,energy_str,dotbracket_str in seq_struct_list:
            G = nx.Graph()
            if shape:
                G_shape=seq_to_networkx('',shape_str)
                G = nx.disjoint_union(G, G_shape)
            if energy:
                G_energy=seq_to_networkx('',energy_str)
                G = nx.disjoint_union(G, G_energy)
            if dotbracket:
                G_dotbracket=seq_to_networkx('',dotbracket_str)
                G = nx.disjoint_union(G, G_dotbracket)
            G.graph['id'] = header + '_' + shape_str
            G.graph['info'] = 'RNAshapes shape_type=%s energy_range=%s max_num=%s shape=%s energy=%s dotbracket=%s' % (shape_type, energy_range, max_num, shape, energy, dotbracket)
            G.graph['sequence'] = sequence
            yield G
    else:
        G_global = nx.Graph()
        for shape_str,energy_str,dotbracket_str in seq_struct_list:
            G = nx.Graph()
            if shape:
                G_shape=seq_to_networkx('',shape_str)
                G = nx.disjoint_union(G, G_shape)
            if energy:
                G_energy=seq_to_networkx('',energy_str)
                G = nx.disjoint_union(G, G_energy)
            if dotbracket:
                G_dotbracket=seq_to_networkx('',dotbracket_str)
                G = nx.disjoint_union(G, G_dotbracket)
            G_global = nx.disjoint_union(G_global, G)
        G_global.graph['id'] = header
        G_global.graph['info'] = 'RNAshapes shape_type=%s energy_range=%s max_num=%s shape=%s energy=%s dotbracket=%s' % (shape_type, energy_range, max_num, shape, energy, dotbracket)
        G_global.graph['sequence'] = sequence
        yield G_global


def rnashapes_struct_to_eden(iterable, **options):    
    assert(is_iterable(iterable)), 'Not iterable'
    for header, seq in iterable:
        try:
            for G in string_to_networkx(header, seq, **options):
                yield G
        except Exception as e:
            print e.__doc__
            print e.message
            print 'Error in: %s %s' % (header, seq)
            G = seq_to_networkx(header, seq, **options)
            yield G
{'fraction_non_alphanumeric': 0.10855457227138643, 'fraction_numerical': 0.00471976401179941, 'mean_word_length': 2.6346483704974273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3297080', 'n_tokens_mistral': 2799, 'n_tokens_neox': 2685, 'n_words': 601}