Ejemplo n.º 1
0
def remove_neighborhood(context, input_path, output_path, name):
    if input_path is None:
        input_stream = ctxstream(sys.stdin)
    else:
        input_stream = open(input_path, 'r')
    with input_stream:
        graph = GraphReader(input_stream).network
    try:
        graph.neighborhoods.remove(name)
    except KeyError:
        click.secho("No neighborhood with name %r was found" % name,
                    err=True,
                    fg='yellow')
    if output_path is None:
        output_stream = ctxstream(sys.stdout)
    else:
        output_stream = open(output_path, 'w')
    with output_stream:
        GraphWriter(graph, output_stream)
Ejemplo n.º 2
0
def glycan_network(context, database_connection, hypothesis_identifier,
                   edge_strategy, output_path):
    conn = DatabaseBoundOperation(database_connection)
    hypothesis = get_by_name_or_id(conn, GlycanHypothesis,
                                   hypothesis_identifier)
    if output_path is None:
        output_stream = ctxstream(sys.stdout)
    else:
        output_stream = open(output_path, 'wb')
    with output_stream:
        db = GlycanCompositionDiskBackedStructureDatabase(
            database_connection, hypothesis.id)
        glycans = list(db)
        graph = CompositionGraph(glycans)
        if edge_strategy == 'manhattan':
            graph.create_edges(1)
        else:
            raise click.ClickException("Could not find edge strategy %r" %
                                       (edge_strategy, ))
        GraphWriter(graph, output_stream)
Ejemplo n.º 3
0
def add_prebuild_neighborhoods_to_network(context, input_path, output_path,
                                          name):
    if input_path is None:
        input_stream = ctxstream(sys.stdin)
    else:
        input_stream = open(input_path, 'r')
    with input_stream:
        graph = GraphReader(input_stream).network
    if name == 'n-glycan':
        neighborhoods = make_n_glycan_neighborhoods()
    else:
        raise LookupError(name)
    for n in neighborhoods:
        graph.neighborhoods.add(n)
    if output_path is None:
        output_stream = ctxstream(sys.stdout)
    else:
        output_stream = open(output_path, 'w')
    with output_stream:
        GraphWriter(graph, output_stream)
Ejemplo n.º 4
0
def add_neighborhood_to_network(context, input_path, output_path, name,
                                range_rule, expression_rule, ratio_rule):
    if input_path is None:
        input_stream = ctxstream(sys.stdin)
    else:
        input_stream = open(input_path, 'r')
    with input_stream:
        graph = GraphReader(input_stream).network
    if name in graph.neighborhoods:
        click.secho(
            "This network already has a neighborhood called %s, overwriting" %
            name,
            fg='yellow',
            err=True)
    rules = []
    for rule in range_rule:
        expr, lo, hi, req = rule
        lo = int(lo)
        hi = int(hi)
        req = req.lower().strip() in ('true', 'yes', '1')
        rules.append(CompositionRangeRule(expr, lo, hi, req))
    for rule in expression_rule:
        expr, req = rule
        req = req.lower().strip() in ('true', 'yes', '1')
        rules.append(CompositionExpressionRule(expr, req))
    for rule in ratio_rule:
        numer, denom, threshold, req = rule
        threshold = float(threshold)
        req = req.lower().strip() in ('true', 'yes', '1')
        rules.append(CompositionRatioRule(numer, denom, threshold, req))
    graph.neighborhoods.add(CompositionRuleClassifier(name, rules))
    if output_path is None:
        output_stream = ctxstream(sys.stdout)
    else:
        output_stream = open(output_path, 'w')
    with output_stream:
        GraphWriter(graph, output_stream)