Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/networkx/readwrite/leda.py @ 5:9b1c78e6ba9c draft default tip
"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
| author | shellac |
|---|---|
| date | Mon, 01 Jun 2020 08:59:25 -0400 |
| parents | 79f47841a781 |
| children |
comparison
equal
deleted
inserted
replaced
| 4:79f47841a781 | 5:9b1c78e6ba9c |
|---|---|
| 1 """ | |
| 2 Read graphs in LEDA format. | |
| 3 | |
| 4 LEDA is a C++ class library for efficient data types and algorithms. | |
| 5 | |
| 6 Format | |
| 7 ------ | |
| 8 See http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html | |
| 9 | |
| 10 """ | |
| 11 # Original author: D. Eppstein, UC Irvine, August 12, 2003. | |
| 12 # The original code at http://www.ics.uci.edu/~eppstein/PADS/ is public domain. | |
| 13 __author__ = """Aric Hagberg (hagberg@lanl.gov)""" | |
| 14 # Copyright (C) 2004-2019 by | |
| 15 # Aric Hagberg <hagberg@lanl.gov> | |
| 16 # Dan Schult <dschult@colgate.edu> | |
| 17 # Pieter Swart <swart@lanl.gov> | |
| 18 # All rights reserved. | |
| 19 # BSD license. | |
| 20 | |
| 21 __all__ = ['read_leda', 'parse_leda'] | |
| 22 | |
| 23 import networkx as nx | |
| 24 from networkx.exception import NetworkXError | |
| 25 from networkx.utils import open_file, is_string_like | |
| 26 | |
| 27 | |
| 28 @open_file(0, mode='rb') | |
| 29 def read_leda(path, encoding='UTF-8'): | |
| 30 """Read graph in LEDA format from path. | |
| 31 | |
| 32 Parameters | |
| 33 ---------- | |
| 34 path : file or string | |
| 35 File or filename to read. Filenames ending in .gz or .bz2 will be | |
| 36 uncompressed. | |
| 37 | |
| 38 Returns | |
| 39 ------- | |
| 40 G : NetworkX graph | |
| 41 | |
| 42 Examples | |
| 43 -------- | |
| 44 G=nx.read_leda('file.leda') | |
| 45 | |
| 46 References | |
| 47 ---------- | |
| 48 .. [1] http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html | |
| 49 """ | |
| 50 lines = (line.decode(encoding) for line in path) | |
| 51 G = parse_leda(lines) | |
| 52 return G | |
| 53 | |
| 54 | |
| 55 def parse_leda(lines): | |
| 56 """Read graph in LEDA format from string or iterable. | |
| 57 | |
| 58 Parameters | |
| 59 ---------- | |
| 60 lines : string or iterable | |
| 61 Data in LEDA format. | |
| 62 | |
| 63 Returns | |
| 64 ------- | |
| 65 G : NetworkX graph | |
| 66 | |
| 67 Examples | |
| 68 -------- | |
| 69 G=nx.parse_leda(string) | |
| 70 | |
| 71 References | |
| 72 ---------- | |
| 73 .. [1] http://www.algorithmic-solutions.info/leda_guide/graphs/leda_native_graph_fileformat.html | |
| 74 """ | |
| 75 if is_string_like(lines): | |
| 76 lines = iter(lines.split('\n')) | |
| 77 lines = iter([line.rstrip('\n') for line in lines | |
| 78 if not (line.startswith('#') or line.startswith('\n') or line == '')]) | |
| 79 for i in range(3): | |
| 80 next(lines) | |
| 81 # Graph | |
| 82 du = int(next(lines)) # -1=directed, -2=undirected | |
| 83 if du == -1: | |
| 84 G = nx.DiGraph() | |
| 85 else: | |
| 86 G = nx.Graph() | |
| 87 | |
| 88 # Nodes | |
| 89 n = int(next(lines)) # number of nodes | |
| 90 node = {} | |
| 91 for i in range(1, n + 1): # LEDA counts from 1 to n | |
| 92 symbol = next(lines).rstrip().strip('|{}| ') | |
| 93 if symbol == "": | |
| 94 symbol = str(i) # use int if no label - could be trouble | |
| 95 node[i] = symbol | |
| 96 | |
| 97 G.add_nodes_from([s for i, s in node.items()]) | |
| 98 | |
| 99 # Edges | |
| 100 m = int(next(lines)) # number of edges | |
| 101 for i in range(m): | |
| 102 try: | |
| 103 s, t, reversal, label = next(lines).split() | |
| 104 except: | |
| 105 raise NetworkXError('Too few fields in LEDA.GRAPH edge %d' % (i + 1)) | |
| 106 # BEWARE: no handling of reversal edges | |
| 107 G.add_edge(node[int(s)], node[int(t)], label=label[2:-2]) | |
| 108 return G |
