Input and output¶
metator.io reads and writes the files of a run: the contig data and cluster
summary tables, FASTA files and indexes. metator.outputs names the folders
of the output directory of a run, and writes the FASTA files of its genomes
and MGE clusters and the cool files of its MAGs. metator.log holds the
logger every module writes to. File formats describes the
files.
metator.io ¶
Core I/O tools of metaTOR: FASTA checks, compressed files, restriction sites, the MGE list, miComplete's results and the summary of the clusters.
SummaryLayout ¶
Bases: NamedTuple
How write_bin_summary wrote the summary of the final clusters (cluster_summary.txt), which the map of the clusters records with its values (see metator.cli.validation_entry), to write it again: a header line, an empty field then the columns, then a line per row, its name then a field per column, tab-separated.
Attributes:
| Name | Type | Description |
|---|---|---|
rows |
list of str
|
The names of the clusters, in the order of the lines. |
columns |
list of str
|
The columns, in order, after the name of the cluster. |
formats |
dict
|
The format of each column, by name, as printf writes it: "%.2f" (a number with two decimals), "%.4f" (four: the abundances), "%d" (a whole number) or "%s" (text as it is: True or False for a boolean). |
Source code in src/metator/io.py
check_is_fasta ¶
Function from hicstuff.io (https://github.com/koszullab/hicstuff/) Checks whether input file is in fasta format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_file
|
str
|
Path to the input file. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the input is in fasta format, False otherwise |
Source code in src/metator/io.py
file_compression ¶
The compression of a file, found from its first bytes, whatever its name, as read_compressed opens it: "gz", "bz2", "zip", or "uncompressed" for any other file.
Source code in src/metator/io.py
generate_temp_dir ¶
Temporary directory generation
The name comes from the system's random numbers, not Python's random module, which igraph draws from: a run's partition does not depend on how many of these directories it made.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The path at which the temporary directory will be created. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The path of the newly created temporary directory. |
Source code in src/metator/io.py
get_restriction_site ¶
Function to return a regex which corresponds to all possible restriction sites given a set of enzyme.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enzyme
|
str
|
String that contains the names of the enzyme separated by a comma. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Regex that corresponds to all possible restriction sites given a set of enzyme. |
Examples:
Source code in src/metator/io.py
import_mges_contigs ¶
Import list of mges contigs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mges_file
|
str
|
Path to the mges file which contains the list of mges contigs. |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of the mges contigs names: the first word of each line, its blank lines left out. |
Source code in src/metator/io.py
micomplete_results_to_dict ¶
Read micomplte output file and transfrom it as a dictionnary with the bin name as keys and bin information as values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
micomplete_file
|
str
|
Path to the micomplete output file. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionnary of the output of miComplete as values and the bin id as keys. |
Source code in src/metator/io.py
read_compressed ¶
Read compressed file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
The path to the input file |
required |
Returns:
| Type | Description |
|---|---|
file-like object
|
The handle to access the input file's content |
Source code in src/metator/io.py
rename_1x_columns ¶
The table of contig data contig_data, its columns named after bins by metaTOR 1.x renamed as metaTOR 2.0 names them (CONTIG_DATA_1X_COLUMNS), to read the contig data files of a 1.x run as those of a 2.0 run: no command reads the tables of 1.x anymore (metator host reads the map).
A column already named as in 2.0 is kept, and the 1.x column of the same meaning left as it is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
contig_data
|
DataFrame
|
Table of contig data, as read from a contig data file. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The table, its 1.x columns renamed, or the table itself if it has none. |
Source code in src/metator/io.py
write_bin_summary ¶
Function to write the bin summary from dictionnary to table text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bin_summary
|
dict
|
Dictionnary with the output of the miComplete of the bins. |
required |
bin_summary_file
|
str
|
Path to the output file to write the summary informations of the bins. |
required |
full_file
|
str
|
Path to the same table, its values at full precision (see SUMMARY_FULL_FILE), where bin_summary_file writes its floats with 2 decimals, and the abundances with 4. |
None
|
Returns:
| Type | Description |
|---|---|
SummaryLayout
|
How the summary is written: its rows, its columns and their format, which the map of the clusters records (see metator.cli.validation_entry). |
Source code in src/metator/io.py
metator.outputs ¶
The files of a run beside its map: where they go in its output directory, the FASTA files of its genomes and MGE clusters, and the cool files of its MAGs.
Every command writes into its output directory (-o), under these names:
fasta/mags_with-mges/: the FASTA file of each final cluster, as the validation made it, with the MGE contigs that it holds (validation, pipeline);fasta/mags/: each MAG of the map of the run, unstitched and stitched (see write_fasta_files): its genomes but__unbinned__and the MGE genomes, which hold no MGE contig oncemetator mge --reassignmade the MGE clusters genomes (pipeline);fasta/mges/: each MGE cluster, unstitched and stitched (mge, pipeline --mges);fasta/mges_random/for the random clusters of mge --random;cool/: one cool file per MAG, with the MGE clusters that it hosts, balanced (see write_cool_files; pipeline);tables/: the tables of metator dump (pipeline), andmge_hosts.txt(host, pipeline --mges);plots/: the plots, as PDF files (see metator.figures and metator.host).
metator export writes them all again from the map of a run (the FASTA
files with the assembly): the clusters of its rounds final and mge (see
round_groups and mge_groups), its MAGs (see map_mags and hosted_mges), the
summary of its final clusters (see final_summary).
write_fasta_files ¶
write_fasta_files(
metacool,
assembly,
outdir,
genomes=None,
groups=None,
temp_dir=None,
stitched=True,
)
Write the FASTA files of genomes of a map, or of groups of its contigs, in outdir (made if missing), two for each, side by side (the first only, without stitched: the final clusters of fasta/mags_with-mges, as the validation writes them):
<name>.fa: its contigs, the records of the assembly as they are (headers and lines), in the order of the map for a genome, in the order given for a group;<name>.stitched.fa: one record named after it, its contigs (the longest first for a genome, ties in the order of the map; in the order given for a group) joined by SPACER N, in lines of 80 bases.
metacooler writes both (metacooler.write_fasta, once each: each reads the assembly); the stitched files are written in a directory of their own in outdir, then renamed. A file of that name in outdir is replaced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metacool
|
str
|
Path to the .metacool file, or URI of an entry of a .mmetacool collection: the map whose contigs they are. |
required |
assembly
|
str
|
Path to the assembly the map was made from: FASTA, plain or compressed with gzip. |
required |
outdir
|
str
|
The directory to write them in. |
required |
genomes
|
list of str
|
The genomes of the map to write, by name. |
None
|
groups
|
dict
|
Name -> the names of its contigs, in their order: groups of contigs to write in place of genomes (the clusters of a run that the map does not hold yet). |
None
|
temp_dir
|
str
|
Where a compressed assembly is copied, plain, as it is read: the temporary directory of the run. |
None
|
stitched
|
bool
|
Whether to write |
True
|
Returns:
| Type | Description |
|---|---|
list of str
|
The genomes or groups written, in order: none for none given. |
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If the files cannot be written: the assembly lacks a contig, gives one another length than the map, or is not FASTA (a ValueError, KeyError or OSError of metacooler, message kept). |
Source code in src/metator/outputs.py
round_groups ¶
The clusters of the clustering round name of a map (mc, opened), by name, in the order of the round: the names of their contigs, in the order of the map, as the round stores them. None for a map without that round.
Source code in src/metator/outputs.py
mge_groups ¶
The MGE clusters of the round name of a map (mc, opened: mge, or mge_random), by name (see round_groups): their contigs the longest first, ties by name, as metator mge writes them (see metator.mge.mge_binning). None for a map without that round.
Source code in src/metator/outputs.py
final_summary ¶
The final clusters of a map (mc, opened) as the plots of the validation read them (see metator.figures.plot_figures): by name, those of the summary of the run (rows, see metator.dump.summary_layout) that are genomes of the map, in its order, with their Length, Weighted completeness and Weighted redundancy, as its genomes table holds them: the values of the summary, which cluster_summary.txt rounds to 2 decimals, where the validation drew the values of miComplete.
Source code in src/metator/outputs.py
map_mags ¶
The MAGs of a map (mc, opened), in the order of its genomes table: its genomes but unbinned and the MGE genomes, those that its column mge says are and those named after a cluster of its round mge (see metator.host.map_bins).
Source code in src/metator/outputs.py
hosted_mges ¶
The MGE clusters that each MAG of a map hosts (mc, opened), by MAG, as metator host stores them in its genomes table (its column mges, names separated by commas): none for a map without hosts.
Source code in src/metator/outputs.py
plot_inputs ¶
What the plots of the final clusters of a map (mc, opened) are drawn from, as the validation draws them (see metator.figures.plot_figures): the length of its contigs, as a Size column, and the summary of its final clusters (rows: their names, see final_summary).
Source code in src/metator/outputs.py
write_cool_files ¶
Write the cool file of each MAG of a map in outdir (made if missing):
<MAG>.cool, the contacts of its contigs and of those of the MGE
clusters that it hosts (hosted: their names, by MAG, the MGE genomes of
the map), as metacooler extract writes them, at the bins of the map, then
balanced as cooler balance balances a cool file, the whole of it at once,
with its defaults (the first two diagonals left out, bins with fewer than
10 non-zero pixels masked...): the weight column of its bins. A cool file
that cannot be balanced is a warning, and is kept without weights; one
whose every bin is masked (too few bins, or contacts, for cooler balance)
is a warning too, its weights all NaN.
Returns:
| Type | Description |
|---|---|
list of str
|
The cool files written, in the order of mags. |
Raises:
| Type | Description |
|---|---|
MetacoolError
|
If metacooler cannot extract a MAG (a ValueError, KeyError or OSError of metacooler, message kept). |
Source code in src/metator/outputs.py
metator.log ¶
Basic logging setup for metator
Basic logging setup with two handlers (stdout and log file). By default the log file is disabled, but can be enabled or changed using set_file_handler. Python's warnings (warnings.warn, which cooler and pandas give theirs with) go to the same handlers, as metaTOR's own messages do.
The messages at INFO tell what a run does and finds; those at DEBUG, its bookkeeping (the files it writes on the way, the temporary directory it removes...), are not shown: CURRENT_LOG_LEVEL shows them.
set_file_handler ¶
Change the file handler for custom log file location: that of metaTOR's messages and of Python's warnings.
Source code in src/metator/log.py
duration ¶
A duration, in whole seconds, as hicstuff writes it in its log: 0h 17m 31s.