Ejemplo n.º 1
0
def load_chromosome(in_f, fast=2):
    """
    Load a Chromosome object from a file. A Chromosome object can be saved with
    the :func:`Chromosome.save_chromosome` function. 
    
    :param in_f: path to a saved Chromosome object file
    :param 2 fast: if fast=2 do not load the Hi-C data (in the case that they 
       were saved in a separate file see :func:`Chromosome.save_chromosome`).
       If fast is equal to 1, the weights will be skipped from load to save 
       memory. Finally if fast=0, both the weights and Hi-C data will be loaded
    
    :returns: a Chromosome object

    TODO: remove first try/except type error... this is loading old experiments
    """
    dico = load(open(in_f))
    name = ''
    crm = Chromosome(dico['name'])
    for name in dico['experiments']:
        xpr = Experiment(name, dico['experiments'][name]['resolution'], 
                         no_warn=True)
        xpr.tads       = dico['experiments'][name]['tads']
        xpr.norm       = dico['experiments'][name]['wght']
        xpr.hic_data   = dico['experiments'][name]['hi-c']
        xpr.conditions = dico['experiments'][name]['cond']
        xpr.size       = dico['experiments'][name]['size']
        try:
            crm.experiments.append(xpr)
        except TypeError:
            continue
    crm.size            = dico['size']
    crm.r_size          = dico['r_size']
    crm.max_tad_size    = dico['max_tad_size']
    crm.forbidden       = dico['forbidden']
    crm._centromere     = dico['_centromere']
    if type(dico['experiments'][name]['hi-c']) == str and fast!= int(2):
        try:
            dicp = load(open(in_f + '_hic'))
        except IOError:
            raise Exception('ERROR: file %s not found\n' % (
                dico['experiments'][name]['hi-c']))
        for name in dico['experiments']:
            crm.get_experiment(name).hic_data = dicp[name]['hi-c']
            if fast != 1:
                crm.get_experiment(name).norm = dicp[name]['wght']
    elif not fast:
        warn('WARNING: data not saved correctly for fast loading.\n')
    return crm
Ejemplo n.º 2
0
def load_chromosome(in_f, fast=2):
    """
    Load Chromosome from file. Chromosome might have been saved through the
    :func:`Chromosome.save_chromosome`.
    
    :param in_f: path to a saved Chromosome file
    :param 2 fast: if fast=2 do not load Hi-C data (in the case that they were
       saved in a separate file see :func:`Chromosome.save_chromosome`). If fast
       is equal to 1, weight would be skipped from load in order to save memory.
       Finally if fast=0, both weights and Hi-C data will be loaded.
    
    :returns: Chromosome object

    TODO: remove first try/except type error... this is loading old experiments
    """
    dico = load(open(in_f))
    name = ""
    crm = Chromosome(dico["name"])
    for name in dico["experiments"]:
        xpr = Experiment(name, dico["experiments"][name]["resolution"], no_warn=True)
        xpr.tads = dico["experiments"][name]["tads"]
        xpr.wght = dico["experiments"][name]["wght"]
        xpr.hic_data = dico["experiments"][name]["hi-c"]
        xpr.conditions = dico["experiments"][name]["cond"]
        xpr.size = dico["experiments"][name]["size"]
        try:
            crm.experiments.append(xpr)
        except TypeError:
            continue
    crm.size = dico["size"]
    crm.r_size = dico["r_size"]
    crm.max_tad_size = dico["max_tad_size"]
    crm.forbidden = dico["forbidden"]
    crm._centromere = dico["_centromere"]
    if type(dico["experiments"][name]["hi-c"]) == str and fast != int(2):
        try:
            dicp = load(open(in_f + "_hic"))
        except IOError:
            raise Exception("ERROR: file {} not found\n".format(dico["experiments"][name]["hi-c"]))
        for name in dico["experiments"]:
            crm.get_experiment(name).hic_data = dicp[name]["hi-c"]
            if fast != 1:
                crm.get_experiment(name).wght = dicp[name]["wght"]
    elif not fast:
        warn("WARNING: data not saved correctly for fast loading.\n")
    return crm
Ejemplo n.º 3
0
def load_chromosome(in_f, fast=2):
    """
    Load a Chromosome object from a file. A Chromosome object can be saved with
    the :func:`Chromosome.save_chromosome` function.

    :param in_f: path to a saved Chromosome object file
    :param 2 fast: if fast=2 do not load the Hi-C data (in the case that they
       were saved in a separate file see :func:`Chromosome.save_chromosome`).
       If fast is equal to 1, the weights will be skipped from load to save
       memory. Finally if fast=0, both the weights and Hi-C data will be loaded

    :returns: a Chromosome object

    TODO: remove first try/except type error... this is loading old experiments
    """
    dico = load(open(in_f))
    name = ''
    crm = Chromosome(dico['name'])
    try:
        exp_order = dico['experiment_order']
    except KeyError:
        exp_order = dico['experiments'].keys()
    for name in exp_order:
        xpr = Experiment(name, dico['experiments'][name]['resolution'], 
                         no_warn=True)
        xpr.tads        = dico['experiments'][name]['tads']
        xpr.norm        = dico['experiments'][name]['wght']
        xpr.hic_data    = dico['experiments'][name]['hi-c']
        xpr.conditions  = dico['experiments'][name]['cond']
        xpr.size        = dico['experiments'][name]['size']
        xpr._zeros      = dico['experiments'][name].get('zero', {})
        try:  # new in version post-CSDM13
            xpr.identifier  = dico['experiments'][name]['iden']
            xpr.cell_type   = dico['experiments'][name]['cell']
            xpr.exp_type    = dico['experiments'][name]['expt']
            xpr.enzyme      = dico['experiments'][name]['enzy']
            xpr.description = dico['experiments'][name]['desc']
        except KeyError:
            xpr.identifier  = None
            xpr.cell_type   = None
            xpr.exp_type    = None
            xpr.enzyme      = None
            xpr.description = {}
        try:
            crm.experiments.append(xpr)
        except TypeError:
            continue
    crm.size            = dico['size']
    crm.r_size          = dico['r_size']
    crm.max_tad_size    = dico['max_tad_size']
    crm.forbidden       = dico['forbidden']
    crm._centromere     = dico['_centromere']
    try:  # new in version post-CSDM13
        crm.species         = dico['species']
        crm.assembly        = dico['assembly']
        crm.description     = dico['description']
    except KeyError:
        crm.species         = None
        crm.assembly        = None
        crm.description     = {}
    if isinstance(dico['experiments'][name]['hi-c'], str) or fast != int(2):
        try:
            dicp = load(open(in_f + '_hic'))
        except IOError:
            raise Exception('ERROR: file %s not found\n' % (
                dico['experiments'][name]['hi-c']))
        for name in dico['experiments']:
            crm.get_experiment(name).hic_data = dicp[name]['hi-c']
            if fast != 1:
                crm.get_experiment(name).norm = dicp[name]['wght']
    elif not fast:
        stderr.write('WARNING: data not saved correctly for fast loading.\n')
    return crm
Ejemplo n.º 4
0
def load_chromosome(in_f, fast=2):
    """
    Load a Chromosome object from a file. A Chromosome object can be saved with
    the :func:`Chromosome.save_chromosome` function.

    :param in_f: path to a saved Chromosome object file
    :param 2 fast: if fast=2 do not load the Hi-C data (in the case that they
       were saved in a separate file see :func:`Chromosome.save_chromosome`).
       If fast is equal to 1, the weights will be skipped from load to save
       memory. Finally if fast=0, both the weights and Hi-C data will be loaded

    :returns: a Chromosome object

    TODO: remove first try/except type error... this is loading old experiments
    """
    with open(in_f,'rb') as f_in_f:
        dico = load(f_in_f)
    name = ''
    crm = Chromosome(dico['name'])
    try:
        exp_order = dico['experiment_order']
    except KeyError:
        exp_order = list(dico['experiments'].keys())
    for name in exp_order:
        xpr = Experiment(name, dico['experiments'][name]['resolution'],
                         no_warn=True)
        xpr.tads        = dico['experiments'][name]['tads']
        xpr.norm        = dico['experiments'][name]['wght']
        xpr.hic_data    = dico['experiments'][name]['hi-c']
        xpr.conditions  = dico['experiments'][name]['cond']
        xpr.size        = dico['experiments'][name]['size']
        xpr._zeros      = dico['experiments'][name].get('zero', {})
        try:  # new in version post-CSDM13
            xpr.identifier  = dico['experiments'][name]['iden']
            xpr.cell_type   = dico['experiments'][name]['cell']
            xpr.exp_type    = dico['experiments'][name]['expt']
            xpr.enzyme      = dico['experiments'][name]['enzy']
            xpr.description = dico['experiments'][name]['desc']
        except KeyError:
            xpr.identifier  = None
            xpr.cell_type   = None
            xpr.exp_type    = None
            xpr.enzyme      = None
            xpr.description = {}
        try:
            crm.experiments.append(xpr)
        except TypeError:
            continue
    crm.size            = dico['size']
    crm.r_size          = dico['r_size']
    crm.max_tad_size    = dico['max_tad_size']
    crm.forbidden       = dico['forbidden']
    crm._centromere     = dico['_centromere']
    try:  # new in version post-CSDM13
        crm.species         = dico['species']
        crm.assembly        = dico['assembly']
        crm.description     = dico['description']
    except KeyError:
        crm.species         = None
        crm.assembly        = None
        crm.description     = {}
    if isinstance(dico['experiments'][name]['hi-c'], basestring) or fast != int(2):
        try:
            dicp = load(open(in_f + '_hic','rb'))
            for name in dico['experiments']:
                crm.get_experiment(name).hic_data = dicp[name]['hi-c']
                if fast != 1:
                    crm.get_experiment(name).norm = dicp[name]['wght']
        except IOError:
            try:
                for name in dico['experiments']:
                    crm.get_experiment(name).hic_data = dico['experiments'][name]['hi-c']
                    if fast != 1:
                        crm.get_experiment(name).norm = dico['experiments'][name]['wght']
            except KeyError:
                raise Exception('ERROR: file %s not found\n' % (
                    dico['experiments'][name]['hi-c']))
    elif not fast:
        stderr.write('WARNING: data not saved correctly for fast loading.\n')
    return crm