Network¶
metator.network measures the contigs of the assembly, sums the contacts of a
.metacool contact map by pair of contigs, and reads the network that
metator network stores in the map: its edges weighed with the normalisation
of its contigs. See Building the network.
metator.network ¶
The metaHiC network of the contigs of a .metacool contact map.
The network sums the map's contacts by pair of contigs (contig_contacts) and weighs each edge with the normalization (map_network): the count times one weight per contig, which metator network stores in the map as weights of its bins, next to what it measures on each contig in the contigs table (create_contig_data, map_contig_table). Its nodes are the contigs of the map, by their position in it.
StoredNetwork ¶
Bases: NamedTuple
The network that metator network stored in a map, as the steps after it read it (see stored_network): the contig data of the contigs of the map (table, see map_contig_table), the network (see map_network), its normalization, and its identity (digest, see network_digest).
Source code in src/metator/network.py
contig_contacts ¶
Contacts of a .metacool contact map summed by pair of contigs.
metacooler sums the pixels between the bins of each two contigs, by contig id, whatever the bin size, chunksize pixels at a time: memory grows with the contig pairs in contact, not with the pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool
collection: |
required |
chunksize
|
int
|
Number of pixels read at a time. |
METACOOL_CHUNKSIZE
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per pair of contigs in contact, in the order of the map: columns id1 and id2, the positions of the two contigs in the contigs table of the map, from 0 (id1 <= id2: a contact within a contig has id1 == id2), and count. |
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map cannot be read (a missing file, a file that is not a .metacool one, a .mmetacool without the entry), stores both triangles of the matrix (storage-mode square), or holds counts that are not integers. |
Source code in src/metator/network.py
contig_hits ¶
Hits of each contig of a map, by its position in the map: the sum of its contacts (see contig_contacts), a contact within the contig counted twice, as a pair with both reads on the contig counts twice in the pairs file that the map was made of. A contig in no contact has 0.
Source code in src/metator/network.py
log_contacts ¶
Count the contacts of a map (see contig_contacts), within and between contigs: a map without any is refused (MetacoolError).
Source code in src/metator/network.py
map_network ¶
The network of the contigs of a .metacool contact map: one edge between each two contigs of the map in contact, weighed with the normalization of the network.
An edge weighs count * w[i] * w[j], as cooler applies balancing weights, with the map's stored weights by default, the weights given, or none (the counts) for None. Edges of weight 0 or NaN are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool
collection: |
required |
weights
|
(stored, ndarray or None)
|
"stored" (the default): the weights that metator network stored in the map. |
'stored'
|
contacts
|
DataFrame
|
The contacts of the map (see contig_contacts), when they are read already: the map is not read again. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per edge, in the order of the map: columns contig1 and contig2, the IDs of the two contigs (their positions in the map as it is read, from 1: the ID column of the contig data that a run writes from it, see contig_table; contig1 < contig2), and weight. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If weights is a string other than "stored". |
MetacoolError
|
If the map cannot be read or give its contacts (see contig_contacts), or, for its stored weights, holds no network, or not the weights of its normalization (see stored_weights). |
Source code in src/metator/network.py
relabel_network ¶
The network of a map (see map_network) with other IDs for its contigs, those of a contig data table of the map: the IDs of the network are the positions of the contigs in the map now, and those of the table the positions they had when it was written (by partition), which differ once a run has written the map of its clusters (grouped by cluster). The edges stay those of the network, listed as map_network lists them: each from the contig of the lower ID, in the order of their IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
DataFrame
|
The network (columns contig1, contig2 and weight). |
required |
names
|
sequence of str
|
The names of the contigs of the map, in its order. |
required |
ids
|
dict
|
The ID of each contig of the map in the table, by name. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The network, with the IDs of the table. |
Source code in src/metator/network.py
network_normalization ¶
The normalization of the network that metator network stored in a map (see metator.metacool.write_map): that of the entry network of the metator key of its metadata, which a pipeline run writes too. "None" for the raw counts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool collection, for the message. |
required |
mc
|
MetaCooler
|
The map, opened: by default, metacool is opened. |
None
|
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map holds no network: metator network has not run on it. |
Source code in src/metator/network.py
stored_network ¶
The network that metator network stored in a map, read in full, as partition and validation read it: the contig data of its contigs (see map_contig_table), the network of its contacts weighed with the stored weights (see map_network and stored_weights), its normalization (see network_normalization), and its identity (see network_digest), which partition records with the partition that it stores, and validation checks.
Given mc, every table is read through it, so a map replaced since is refused as replaced before its Hit column is checked. Hit must still be the hits of the contacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool
collection: |
required |
mc
|
MetaCooler
|
The map, opened: by default, metacool is opened. |
None
|
Returns:
| Type | Description |
|---|---|
StoredNetwork
|
The contig data, the network, its normalization and its digest. |
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map cannot be read, holds no network, lacks a column or the weights that metator network stores (an edit of the map dropped them since), holds a value that is not a number in one of these columns, or weights that differ between the bins of a contig, or hits that are not those of its contacts: each time, with the step to run again. |
Source code in src/metator/network.py
stored_weights ¶
The weights of the contigs of a map for the normalization of its network,
which metator network stored in it as the weights metator_<normalization>
of its bins (see network_normalization and metator.metacool.write_map):
one per contig, in the order of the map, the weight of each of its bins.
None for the normalization None, whose network is the counts.
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map cannot be read, holds no network, lacks the weights of its normalization (an edit of the map dropped them since metator network stored them, as metacooler drops weights whose columns it changes), or holds weights that differ between the bins of a contig. |
Source code in src/metator/network.py
contig_table ¶
The contig data of the contigs of a map, in its order: one row per contig, with the columns of CONTIG_TABLE_COLUMNS. ID is the position of the contig in the map, from 1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
sequences
|
The name, length, GC content and Hi-C hits of each contig. |
required |
lengths
|
sequences
|
The name, length, GC content and Hi-C hits of each contig. |
required |
gc
|
sequences
|
The name, length, GC content and Hi-C hits of each contig. |
required |
hits
|
sequences
|
The name, length, GC content and Hi-C hits of each contig. |
required |
coverage
|
sequences
|
The shotgun coverage and the restriction sites (plus one, as create_contig_data counts them) of each contig. |
None
|
sites
|
sequences
|
The shotgun coverage and the restriction sites (plus one, as create_contig_data counts them) of each contig. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The contig data. |
Source code in src/metator/network.py
contig_data_table ¶
The contig data of the contigs of a map, in its order (see contig_table), from what a run measured on the assembly and read from the map (contig_data, by contig name: see create_contig_data and contig_data_from_map) and the hits of the contigs of the map (see contig_hits). The contigs of the assembly that the map lacks (a map made with a contig filter, for example) are left out: they have no contact, and no node in the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contig_data
|
dict
|
The data of each contig of the assembly, by name. |
required |
names
|
sequence of str
|
The names of the contigs of the map, in its order. |
required |
hits
|
ndarray
|
The hits of the contigs of the map, in its order. |
required |
metacool
|
str
|
The map, for the message. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The contig data. |
Source code in src/metator/network.py
map_contig_table ¶
The contig data of the contigs of a map, in its order (see contig_table), as metator network stored them in its contigs table (see metator.metacool.write_map): GC_content and Hit, and Shotgun_coverage and Restriction_site when the run measured them. Given mc, the map opened, it is read through it.
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map cannot be read, holds no network (see network_normalization), or lacks the GC_content or Hit column (an edit of the map dropped it since metator network stored it). |
Source code in src/metator/network.py
network_weights ¶
The weights of the contigs of a contig data table (see contig_table) for a normalization of the network: those of normalization_weights, from its sizes, hits, shotgun coverage and restriction sites, which metator network computes from the columns it stores in the map. None for the normalization None, whose network is the counts.
Source code in src/metator/network.py
check_map_contigs ¶
Refuse a .metacool map that is not a map of the contigs of the assembly: one that holds contigs that the assembly lacks, or that gives them other lengths. It may lack contigs of the assembly.
Raises:
| Type | Description |
|---|---|
MetacoolError
|
With the contigs that are not in the assembly, or that have another length in it. |
Source code in src/metator/network.py
read_depth ¶
Read the depth file of jgi_summarize_bam_contig_depths (MetaBAT2): the length and the mean coverage of each contig, by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth_file
|
str
|
Path to the depth file: a header line, then one line per contig, with its name, length and mean coverage in its first three columns. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The length and the coverage (float) of each contig, by name: the length is an int, or a float when the file writes it in scientific notation (see depth_length_matches). |
Source code in src/metator/network.py
depth_length_matches ¶
Whether the length of a contig in the depth file is that of a contig of the assembly.
jgi_summarize_bam_contig_depths writes the lengths of a million bases or more in scientific notation, to 6 significant digits (1217543 as 1.21754e+06): such a length matches the length of the contig rounded so, or the length itself. A length written as an integer must be the length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
depth_length
|
int or float
|
The length of the contig in the depth file (see read_depth). |
required |
length
|
int
|
The length of the contig in the assembly. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Whether the lengths match. |
Source code in src/metator/network.py
create_contig_data ¶
Create a dictionnary with data on each Contig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assembly
|
str
|
Path to the assembly fasta file, plain or compressed. |
required |
depth_file
|
str or None
|
Path to the depth.txt file from jgi_summarize_bam_contig_depths from Metabat2 Software, read by contig name: it must hold every contig of the assembly, with its length. |
None
|
enzyme
|
str or None
|
String that contains the names of the enzyme separated by a comma. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The data of each contig of the assembly, by name, in the order of the
assembly: "length", "GC", "coverage" ( |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the assembly names a contig by several records, or the depth file lacks a contig of the assembly or gives it another length (the reason is logged as an error). |
Source code in src/metator/network.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |
map_columns ¶
The columns of MAP_COLUMNS that the contigs table of a .metacool map gives
values in, in that order (see _map_values: a column of -, as metaTOR
writes what it did not measure, gives none).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool
collection: |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
The columns of MAP_COLUMNS that give values in the contigs table of the map. |
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map cannot be read, or is replaced or written as it is read. |
Source code in src/metator/network.py
contig_data_from_map ¶
Read the restriction sites and the shotgun coverage of the contigs from a .metacool map that a run starts from, where it measures none: the Restriction_site and Shotgun_coverage columns of its contigs table (MAP_COLUMNS), which metaTOR stores in the maps it writes. --enzyme and --depth measure them on the assembly instead (create_contig_data): they fill in a column that the map lacks, and replace one that it has, with a warning that counts the contigs whose value is not the map's.
A column is read only if it gives every contig a value: an integer of at least 1 for the restriction sites, a number of at least 0 for the coverage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool
collection: |
required |
contig_data
|
dict
|
Dictionary of the all the contigs from the assembly (see create_contig_data): the "RS" and "coverage" of each contig are set from the columns of the map that are read. |
required |
enzyme
|
str or None
|
The enzymes given to --enzyme, if any. |
None
|
depth
|
str or None
|
The depth file given to --depth, if any. |
None
|
Returns:
| Type | Description |
|---|---|
list of str
|
The columns of MAP_COLUMNS read from the map, in that order. |
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the map cannot be read, or is not a map of the contigs of the assembly. |
Source code in src/metator/network.py
normalization_weights ¶
One weight per contig whose products give the normalization of the network.
Each normalization divides the contacts of two contigs by the geometric mean of one value per contig, so the normalized contact is count * w[i] * w[j]; a contig whose value makes that factor 0 gets no weight (NaN).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lengths
|
ndarray
|
Length of each contig. |
required |
hits
|
ndarray or None
|
Hi-C hits of each contig (contig data "hit"), for length, RS and empirical_hit. |
required |
coverage
|
ndarray or None
|
Shotgun coverage of each contig (contig data "coverage"), for abundance and theoritical_hit. |
required |
sites
|
ndarray or None
|
Restriction sites of each contig, plus one, as create_contig_data counts them (contig data "RS"), for RS and theoritical_hit. |
required |
normalization
|
str
|
Mode of normalization: abundance, length, RS, empirical_hit or theoritical_hit. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
One weight per contig, NaN for a contig that has none. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the normalization is unknown ("None" among them: it has no weight), or a value it uses is not given. |
Source code in src/metator/network.py
network_digest ¶
The identity of a network: the SHA-256 of what map_network computes it from, by the names of the contigs. It is that of the network, not of a run: two runs that store the same network give the same digest, and a run that regroups the map (validation, pipeline), which moves the contigs but keeps their names, their contacts and their weights, leaves it as it was. metator partition records the digest of the network that it partitioned, which validation checks against the network stored in the map (see metator.metacool.read_partition), and metator network against the network that it stores.
It covers the normalization, the contig names, each contig's weight and each pair's count, by name: the inputs of map_network, which survive a regrouping of the map where the edges' last bits may not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contacts
|
DataFrame
|
The contacts of the map (see contig_contacts). |
required |
weights
|
ndarray or None
|
The weight of each contig of the map, in its order (see stored_weights and network_weights), or None for the normalization None. |
required |
names
|
sequence of str
|
The names of the contigs of the map, in its order. |
required |
normalization
|
str
|
The normalization of the network ("None" for the counts). |
required |
Returns:
| Type | Description |
|---|---|
str
|
The SHA-256, in hexadecimal. |