view utils/__init__.py @ 1:21130153e729 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/rna_commander/tools/rna_tools/rna_commender commit d5a6f8df417e899092599e956e8033d502bea3c8
author rnateam
date Tue, 19 Jul 2016 07:20:29 -0400
parents 8918de535391
children
line wrap: on
line source

"""Util functions."""

import pandas as pd
import cPickle

__author__ = "Gianluca Corrado"
__copyright__ = "Copyright 2016, Gianluca Corrado"
__license__ = "MIT"
__maintainer__ = "Gianluca Corrado"
__email__ = "gianluca.corrado@unitn.it"
__status__ = "Production"


def feature_size(store_name):
    """Number of features."""
    store = pd.io.pytables.HDFStore(store_name)
    a = store.features
    store.close()
    return a.shape[0]


def save_serendipity_dic(y, filename):
    """Save the dictionary with the serendipity values."""
    store = pd.io.pytables.HDFStore(y)
    mat = store.matrix
    store.close()
    n = len(mat.columns)
    ser = 1 - mat.sum(axis=1) / n

    f = open(filename, "w")
    cPickle.dump(ser.to_dict(), f, protocol=2)
    f.close()


def get_serendipity_val(dic, key):
    """Return the serendipity of a RNA."""
    # The key was in the training set
    try:
        return dic[key]
    # The key wasn't in the training set, then the serendipity is 1
    except KeyError:
        return 1.