graphein.ml

Conversion

Utilities for converting Graphein Networks to Geometric Deep Learning formats.

class graphein.ml.conversion.GraphFormatConvertor(src_format: str, dst_format: str, verbose: ['gnn', 'default', 'all_info'] = 'gnn', columns: List[str] | None = None)[source]

Provides conversion utilities between NetworkX Graphs and geometric deep learning library destination formats. Currently, we provide support for converstion from nx.Graph to dgl.DGLGraph and pytorch_geometric.Data. Supported conversion formats can be retrieved from SUPPORTED_FORMATS.

Parameters:
  • src_format (Literal["nx", "pyg", "dgl"]) – The type of graph you’d like to convert from. Supported formats are available in SUPPORTED_FORMATS

  • dst_format (Literal["nx", "pyg", "dgl"]) – The type of graph format you’d like to convert to. Supported formats are available in: graphein.ml.conversion.SUPPORTED_FORMATS

  • verbose (graphein.ml.conversion.SUPPORTED_VERBOSITY) – Select from "gnn", "default", "all_info" to determine how much information is preserved (features) as some are unsupported by various downstream frameworks

  • columns (List[str], optional) – List of columns in the node features to retain

__init__(src_format: str, dst_format: str, verbose: ['gnn', 'default', 'all_info'] = 'gnn', columns: List[str] | None = None)[source]
static convert_dgl_to_nx(G: dgl.DGLGraph) → nx.Graph[source]

Converts a DGL Graph (dgl.DGLGraph) to a NetworkX (nx.Graph) object. Preserves node and edge attributes.

Parameters:

G (dgl.DGLGraph) – dgl.DGLGraph to convert to NetworkX graph.

Returns:

NetworkX graph object.

Return type:

nx.Graph

convert_nx_to_dgl(G: nx.Graph) → dgl.DGLGraph[source]

Converts NetworkX graph to DGL

Parameters:

G (nx.Graph) – nx.Graph to convert to DGLGraph

Returns:

DGLGraph object version of input NetworkX graph

Return type:

dgl.DGLGraph

static convert_nx_to_nx(G: Graph) → Graph[source]

Converts NetworkX graph (nx.Graph) to NetworkX graph (nx.Graph) object. Redundant - returns itself.

Parameters:

G (nx.Graph) – NetworkX Graph

Returns:

NetworkX Graph

Return type:

nx.Graph

convert_nx_to_pyg(G: Graph) → Data[source]

Converts NetworkX graph to pytorch_geometric.data.Data object. Requires PyTorch Geometric (https://pytorch-geometric.readthedocs.io/en/latest/) to be installed.

Parameters:

G (nx.Graph) – nx.Graph to convert to PyTorch Geometric Data object

Returns:

Data object containing networkx graph data

Return type:

pytorch_geometric.data.Data

static convert_pyg_to_nx(G: Data) → Graph[source]

Converts PyTorch Geometric Data object to NetworkX graph (nx.Graph).

Parameters:

G (torch_geometric.data.Data) – Pytorch Geometric Data.

Returns:

NetworkX graph.

Return type:

nx.Graph

graphein.ml.conversion.SUPPORTED_FORMATS = ['nx', 'pyg', 'dgl']

Supported conversion formats.

"nx": NetworkX graph

"pyg": PyTorch Geometric Data object

"dgl": DGL graph

graphein.ml.conversion.SUPPORTED_VERBOSITY = ['gnn', 'default', 'all_info']

Supported verbosity levels for preserving graph features in conversion.

Diffusion Matrices

Generate diffusion matrices based on NetworkX graph objects.

Author: Arian Jamasb & Eric Ma

Each of the functions here accepts a NetworkX graph object and returns a 2D xarray. These arrays can then be stacked to generate a diffusion tensor.

graphein.ml.diffusion.adjacency_matrix_power(G: Graph, amat_kwargs: Dict = {}, with_identity: bool = True, power: float = 1) → DataArray[source]

Return the matrix power of the adjacency matrix.

Parameters:
  • amat_kwargs (Dict[str, Any]) – Keyword arguments to configure NetworkX’s adjacency_matrix function. Defaults to {}

  • with_identity (bool) – Whether or not to add in the identity matrix to the adjacency matrix. Effectively adding “self loops”. Defaults to True.

  • power (float) – Matrix power to raise the adjacency matrix, defaults to 1.

Returns:

the matrix power of the adjacency matrix

Return type:

xr.DataArray

graphein.ml.diffusion.identity_matrix(G: Graph) → DataArray[source]

Return the identity diffusion matrix.

This is nothing more than the identity matrix with diagonals of 1.

Parameters:

G (nx.Graph) – Graph to retrieve identity diffusion matrix from

Returns:

Identity diffusion matrix

Return type:

xr.DataArray

graphein.ml.diffusion.inverse_distance_matrix(G: Graph, power: float) → DataArray[source]

Return the inverse distance matrix.

Using the coordinates present on the graph object, calculate the inverse power distance matrix.

The resultant array will be symmetric, with entries [i, j] corresponding to 1 / (distance**power), where distance == euclidean distance between the (x, y, z) coordinates of the two graphs. Diagonals (entries [i, i]) are set to 0.

Parameters:
  • G (nx.Graph) – NetworkX Graph object. Assumes that each node has x_coord, y_coord, and z_coord as node attributes.

  • power (float) – The power for the distance calculation.

Returns:

the inverse distance matrix.

Type:

xr.DataArray

Datasets

Pytorch Geometric Dataset classes for Protein Graphs.

class graphein.ml.datasets.torch_geometric_dataset.InMemoryProteinGraphDataset(root: str, name: str, paths: ~typing.List[str] | None = None, pdb_codes: ~typing.List[str] | None = None, uniprot_ids: ~typing.List[str] | None = None, graph_label_map: ~typing.Dict[str, ~torch.Tensor] | None = None, node_label_map: ~typing.Dict[str, ~torch.Tensor] | None = None, chain_selection_map: ~typing.Dict[str, ~typing.List[str]] | None = None, graphein_config: ~graphein.protein.config.ProteinGraphConfig = ProteinGraphConfig(granularity='CA', keep_hets=[], insertions=True, alt_locs='max_occupancy', pdb_dir=None, verbose=False, exclude_waters=True, deprotonate=False, protein_df_processing_functions=None, edge_construction_functions=[<function add_peptide_bonds>], node_metadata_functions=[<function meiler_embedding>], edge_metadata_functions=None, graph_metadata_functions=None, get_contacts_config=None, dssp_config=None), graph_format_convertor: ~graphein.ml.conversion.GraphFormatConvertor = <graphein.ml.conversion.GraphFormatConvertor object>, graph_transformation_funcs: ~typing.List[~typing.Callable] | None = None, transform: ~typing.Callable | None = None, pdb_transform: ~typing.List[~typing.Callable] | None = None, pre_transform: ~typing.Callable | None = None, pre_filter: ~typing.Callable | None = None, num_cores: int = 16, af_version: int = 6)[source]
__init__(root: str, name: str, paths: ~typing.List[str] | None = None, pdb_codes: ~typing.List[str] | None = None, uniprot_ids: ~typing.List[str] | None = None, graph_label_map: ~typing.Dict[str, ~torch.Tensor] | None = None, node_label_map: ~typing.Dict[str, ~torch.Tensor] | None = None, chain_selection_map: ~typing.Dict[str, ~typing.List[str]] | None = None, graphein_config: ~graphein.protein.config.ProteinGraphConfig = ProteinGraphConfig(granularity='CA', keep_hets=[], insertions=True, alt_locs='max_occupancy', pdb_dir=None, verbose=False, exclude_waters=True, deprotonate=False, protein_df_processing_functions=None, edge_construction_functions=[<function add_peptide_bonds>], node_metadata_functions=[<function meiler_embedding>], edge_metadata_functions=None, graph_metadata_functions=None, get_contacts_config=None, dssp_config=None), graph_format_convertor: ~graphein.ml.conversion.GraphFormatConvertor = <graphein.ml.conversion.GraphFormatConvertor object>, graph_transformation_funcs: ~typing.List[~typing.Callable] | None = None, transform: ~typing.Callable | None = None, pdb_transform: ~typing.List[~typing.Callable] | None = None, pre_transform: ~typing.Callable | None = None, pre_filter: ~typing.Callable | None = None, num_cores: int = 16, af_version: int = 6)[source]

In Memory dataset for protein graphs.

Dataset base class for creating graph datasets which easily fit into CPU memory. Inherits from torch_geometric.data.InMemoryDataset, which inherits from torch_geometric.data.Dataset. See here for the accompanying tutorial.

Parameters:
  • root (str) – Root directory where the dataset should be saved.

  • name (str) – Name of the dataset. Will be saved to data_$name.pt.

  • paths (Optional[List[str]], optional) – List of full path of PDB or MMTF files to load. Defaults to None.

  • pdb_codes (Optional[List[str]], optional) – List of PDB codes to download and parse from the PDB. Defaults to None.

  • uniprot_ids (Optional[List[str]], optional) – List of Uniprot IDs to download and parse from Alphafold Database. Defaults to None.

  • graph_label_map (Optional[Dict[str, Tensor]], optional) – Dictionary mapping PDB/Uniprot IDs to graph-level labels. Defaults to None.

  • node_label_map (Optional[Dict[str, torch.Tensor]], optional) – Dictionary mapping PDB/Uniprot IDs to node-level labels. Defaults to None.

  • chain_selection_map (Optional[Dict[str, List[str]]], optional) – Dictionary mapping, defaults to None.

  • graphein_config (ProteinGraphConfig, optional) – Protein graph construction config, defaults to ProteinGraphConfig().

  • graph_format_convertor (GraphFormatConvertor, optional) – Conversion handler for graphs, defaults to GraphFormatConvertor(src_format="nx", dst_format="pyg").

  • pdb_transform (Optional[List[Callable]], optional) – List of functions that consume a list of paths to the downloaded structures. This provides an entry point to apply pre-processing from bioinformatics tools of your choosing. Defaults to None.

  • graph_transformation_funcs (Optional[List[Callable]], optional) – List of functions that consume a nx.Graph and return a nx.Graph. Applied to graphs after construction but before conversion to pyg. Defaults to None.

  • transform (Optional[Callable], optional) – A function/transform that takes in a torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before every access. Defaults to None.

  • pre_transform (Optional[Callable], optional) – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before being saved to disk. Defaults to None.

  • pre_filter (Optional[Callable], optional) – A function that takes in a torch_geometric.data.Data object and returns a boolean value, indicating whether the data object should be included in the final dataset. Optional, defaults to None.

  • num_cores (int, optional) – Number of cores to use for multiprocessing of graph construction, defaults to 16.

  • af_version (int, optional) – Version of AlphaFoldDB structures to use, defaults to 6.

download()[source]

Download the PDB files from RCSB or Alphafold.

process()[source]

Process structures into PyG format and save to disk.

property processed_file_names: List[str]

Name of the processed file.

property raw_file_names: List[str]

Name of the raw files in the dataset.

transform_pdbs()[source]

Performs pre-processing of PDB structures before constructing graphs.

class graphein.ml.datasets.torch_geometric_dataset.ProteinGraphDataset(root: str, paths: ~typing.List[str] | None = None, pdb_codes: ~typing.List[str] | None = None, uniprot_ids: ~typing.List[str] | None = None, graph_labels: ~typing.List[~torch.Tensor] | None = None, node_labels: ~typing.List[~torch.Tensor] | None = None, chain_selections: ~typing.List[str] | None = None, graphein_config: ~graphein.protein.config.ProteinGraphConfig = ProteinGraphConfig(granularity='CA', keep_hets=[], insertions=True, alt_locs='max_occupancy', pdb_dir=None, verbose=False, exclude_waters=True, deprotonate=False, protein_df_processing_functions=None, edge_construction_functions=[<function add_peptide_bonds>], node_metadata_functions=[<function meiler_embedding>], edge_metadata_functions=None, graph_metadata_functions=None, get_contacts_config=None, dssp_config=None), graph_format_convertor: ~graphein.ml.conversion.GraphFormatConvertor = <graphein.ml.conversion.GraphFormatConvertor object>, graph_transformation_funcs: ~typing.List[~typing.Callable] | None = None, pdb_transform: ~typing.List[~typing.Callable] | None = None, transform: ~typing.Callable | None = None, pre_transform: ~typing.Callable | None = None, pre_filter: ~typing.Callable | None = None, num_cores: int = 16, af_version: int = 6)[source]
__init__(root: str, paths: ~typing.List[str] | None = None, pdb_codes: ~typing.List[str] | None = None, uniprot_ids: ~typing.List[str] | None = None, graph_labels: ~typing.List[~torch.Tensor] | None = None, node_labels: ~typing.List[~torch.Tensor] | None = None, chain_selections: ~typing.List[str] | None = None, graphein_config: ~graphein.protein.config.ProteinGraphConfig = ProteinGraphConfig(granularity='CA', keep_hets=[], insertions=True, alt_locs='max_occupancy', pdb_dir=None, verbose=False, exclude_waters=True, deprotonate=False, protein_df_processing_functions=None, edge_construction_functions=[<function add_peptide_bonds>], node_metadata_functions=[<function meiler_embedding>], edge_metadata_functions=None, graph_metadata_functions=None, get_contacts_config=None, dssp_config=None), graph_format_convertor: ~graphein.ml.conversion.GraphFormatConvertor = <graphein.ml.conversion.GraphFormatConvertor object>, graph_transformation_funcs: ~typing.List[~typing.Callable] | None = None, pdb_transform: ~typing.List[~typing.Callable] | None = None, transform: ~typing.Callable | None = None, pre_transform: ~typing.Callable | None = None, pre_filter: ~typing.Callable | None = None, num_cores: int = 16, af_version: int = 6)[source]

Dataset class for protein graphs.

Dataset base class for creating graph datasets. See here for the accompanying tutorial.

Parameters:
  • root (str) – Root directory where the dataset should be saved.

  • paths (Optional[List[str]], optional) – List of full path of PDB or MMTF files to load. Defaults to None.

  • pdb_codes (Optional[List[str]], optional) – List of PDB codes to download and parse from the PDB. Defaults to None.

  • uniprot_ids (Optional[List[str]], optional) – List of Uniprot IDs to download and parse from Alphafold Database. Defaults to None.

  • graph_label_map (Optional[Dict[str, Tensor]], optional) – Dictionary mapping PDB/Uniprot IDs to graph-level labels. Defaults to None.

  • node_label_map (Optional[Dict[str, torch.Tensor]], optional) – Dictionary mapping PDB/Uniprot IDs to node-level labels. Defaults to None.

  • chain_selection_map (Optional[Dict[str, List[str]]], optional) – Dictionary mapping, defaults to None.

  • graphein_config (ProteinGraphConfig, optional) – Protein graph construction config, defaults to ProteinGraphConfig().

  • graph_format_convertor (GraphFormatConvertor, optional) – Conversion handler for graphs, defaults to GraphFormatConvertor(src_format="nx", dst_format="pyg").

  • graph_transformation_funcs (Optional[List[Callable]], optional) – List of functions that consume a nx.Graph and return a nx.Graph. Applied to graphs after construction but before conversion to pyg. Defaults to None.

  • pdb_transform (Optional[List[Callable]], optional) – List of functions that consume a list of paths to the downloaded structures. This provides an entry point to apply pre-processing from bioinformatics tools of your choosing. Defaults to None.

  • transform (Optional[Callable], optional) – A function/transform that takes in a torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before every access. Defaults to None.

  • pre_transform (Optional[Callable], optional) – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before being saved to disk. Defaults to None.

  • pre_filter (Optional[Callable], optional) – A function that takes in a torch_geometric.data.Data object and returns a boolean value, indicating whether the data object should be included in the final dataset. Optional, defaults to None.

  • num_cores (int, optional) – Number of cores to use for multiprocessing of graph construction, defaults to 16.

  • af_version (int, optional) – Version of AlphaFoldDB structures to use, defaults to 6.

download()[source]

Download the PDB files from RCSB or Alphafold.

get(idx: int)[source]

Returns PyTorch Geometric Data object for a given index.

Parameters:

idx (int) – Index to retrieve.

Returns:

PyTorch Geometric Data object.

len() → int[source]

Returns length of data set (number of structures).

process()[source]

Processes structures from files into PyTorch Geometric Data.

property processed_file_names: List[str]

Names of processed files to look for

property raw_file_names: List[str]

Names of raw files in the dataset.

transform_pdbs()[source]

Performs pre-processing of PDB structures before constructing graphs.

class graphein.ml.datasets.torch_geometric_dataset.ProteinGraphListDataset(root: str, data_list: List[Data], name: str, transform=None)[source]
__init__(root: str, data_list: List[Data], name: str, transform=None)[source]

Creates a dataset from a list of PyTorch Geometric Data objects.

Parameters:
  • root (str) – Root directory where the dataset is stored.

  • data_list (List[Data]) – List of protein graphs as PyTorch Geometric Data objects.

  • name (str) – Name of dataset. Data will be saved as data_{name}.pt.

  • transform (Optional[Callable], optional) – A function/transform that takes in an torch_geometric.data.Data object and returns a transformed version. The data object will be transformed before every access. (default: None)

process()[source]

Saves data files to disk.

property processed_file_names

The name of the files in the self.processed_dir folder that must be present in order to skip processing.

Utilities for loading FoldComp databases for deep learning.

graphein.ml.datasets.foldcomp_dataset.FOLDCOMP_DATABASE_TYPES: List[str] = ['afdb_swissprot_v4', 'afdb_uniprot_v4', 'afdb_rep_v4', 'afdb_rep_dark_v4', 'esmatlas', 'esmatlas_v2023_02', 'highquality_clust30']

Currently supported FoldComp databases. See: https://github.com/steineggerlab/foldcomp

class graphein.ml.datasets.foldcomp_dataset.FoldCompDataset(root: str, database: Literal['afdb_swissprot_v4', 'afdb_uniprot_v4', 'afdb_rep_v4', 'afdb_rep_dark_v4', 'esmatlas', 'esmatlas_v2023_02', 'highquality_clust30'], ids: List[str] | None = None, exclude_ids: List[str] | None = None, fraction: float = 1.0, use_graphein: bool = True, transform: BaseTransform | None = None)[source]
__init__(root: str, database: Literal['afdb_swissprot_v4', 'afdb_uniprot_v4', 'afdb_rep_v4', 'afdb_rep_dark_v4', 'esmatlas', 'esmatlas_v2023_02', 'highquality_clust30'], ids: List[str] | None = None, exclude_ids: List[str] | None = None, fraction: float = 1.0, use_graphein: bool = True, transform: BaseTransform | None = None)[source]

Dataset class for FoldComp databases.

Parameters:
  • root (str) – Directory where the dataset should be saved.

  • database (Literal) – Name of the database. See: FOLDCOMP_DATABASE_TYPES.

  • ids (Optional[List[str]]) – List of protein IDs to include in the dataset. If None, all proteins are included. Default is None.

  • exclude_ids (Optional[List[str]]) – List of protein IDs to exclude from the dataset. If None, all proteins are included. Default is None.

  • fraction (float, optional) – Fraction of database to use, defaults to 1.0.

  • use_graphein (bool, optional) – Whether or not to use Graphein’s Protein objects or to use standard PyG Data, defaults to True.

  • transform (Optional[List[GraphTransform]]) – List of functions/transform that take in a Data/Protein object and return a transformed version. The data object will be transformed before every access. (default: None).

async async_setup()[source]

Asynchronous setup of foldcomp database (for jupyter support).

download()[source]

Downloads foldcomp database if not already downloaded.

get(idx) → Data | Protein[source]

Retrieves a protein from the dataset. Can idx on either the protein ID or its index.

len() → int[source]

Returns length of the dataset

process()[source]

Initialises the database.

property processed_file_names

Returns a token to force self.process() to be called.

property raw_file_names: List[str]

Return a list of raw file names expected to be found in the database.

Returns empty string as we don’t expect any raw files.

Returns:

List of raw file names

Return type:

List[str]

class graphein.ml.datasets.foldcomp_dataset.FoldCompLightningDataModule(data_dir, database: str, batch_size: int, use_graphein: bool = True, train_split: List[str] | float | None = None, val_split: List[str] | float | None = None, test_split: List[str] | float | None = None, transform: Iterable[Callable] | None = None, num_workers: int = 4, pin_memory: bool = True)[source]
__init__(data_dir, database: str, batch_size: int, use_graphein: bool = True, train_split: List[str] | float | None = None, val_split: List[str] | float | None = None, test_split: List[str] | float | None = None, transform: Iterable[Callable] | None = None, num_workers: int = 4, pin_memory: bool = True) → None[source]

Creates a PyTorch Lightning DataModule for FoldComp datasets.

Parameters:
  • data_dir (str) – Path to dataset directory.

  • database (str) – Name of the FoldComp database to use. See: FOLDCOMP_DATABASE_TYPES.

  • batch_size (int) – Batch size to use.

  • use_graphein (bool, optional) – Whether to use Graphein Protein objects or PyTorch Geometric Data objects, defaults to True.

  • train_split (Optional[Union[List[str], float]], optional) – List of IDs or a float specifying fraction of the dataset to use for training, defaults to None (whole dataset).

  • val_split (Optional[Union[List[str], float]], optional) – List of IDs or a float specifying fraction of the dataset to use for training, defaults to None (whole dataset).

  • test_split (Optional[Union[List[str], float]], optional) – List of IDs or a float specifying fraction of the dataset to use for training, defaults to None (whole dataset).

  • transform (Optional[Iterable[Callable]]) – List of functions/transform that take in a Data/Protein object and return a transformed version. The data object will be transformed before every access. (default: None).

  • num_workers (int, optional) – Number of workers to use for data loading, defaults to 4.

  • pin_memory (bool, optional) – Whether to pin memory for data loading, defaults to True

setup(stage: str | None = None)[source]

Called at the beginning of fit (train + validate), validate, test, or predict. This is a good hook when you need to build models dynamically or adjust something about them. This hook is called on every process when using DDP.

Parameters:

stage – either 'fit', 'validate', 'test', or 'predict'

Example:

class LitModel(...):
    def __init__(self):
        self.l1 = None

    def prepare_data(self):
        download_data()
        tokenize()

        # don't do this
        self.something = else

    def setup(self, stage):
        data = load_data(...)
        self.l1 = nn.Linear(28, data.num_classes)
test_dataloader() → DataLoader[source]

Returns a test dataloader.

Returns:

Test dataloader.

Return type:

DataLoader

test_dataset()[source]

Initialises the test dataset.

train_dataloader() → DataLoader[source]

Returns a training dataloader.

Returns:

Training dataloader.

Return type:

DataLoader

train_dataset()[source]

Initialises the training dataset.

val_dataloader() → DataLoader[source]

Returns a validation dataloader.

Returns:

Validation dataloader.

Return type:

DataLoader

val_dataset()[source]

Initialises the validation dataset.

Dataset Splitting

Utilities for splitting a protein dataset based on sequence homology.

NB. These functions require Blast+ for proper execution. On Linux, this can be installed with:

sudo apt install ncbi-blast+

Otherwise, please see: https://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download

graphein.ml.clustering.blast(sequence, database, ungapped=False, add_sseq=False, add_qseq=False, tmp_folder='/tmp/', blast_path='', sequence_name='', blast_filename='blast2606.txt', max_target_seqs=100, n_cpu=1, delete_blast_file=True)[source]

Given a sequence and a database file (e.g. a .fasta file with some sequences) blasts the sequence on the database

Parameters:
  • sequence – sequence of reference

  • database – database against which to blast the sequence

  • return_csv_data_class – Returns a dict with as keys subject_id and as value the list (without subject_id). This also contain a header under .hd

  • ungapped

  • add_sseq – append to the returned list the keywords sseq (aligned part of subject sequence)

  • add_qseq – append to the returned list the keywords qseq (aligned part of query sequence)

  • tmp_folder – temporary folder

  • blast_path – path to blast installation folder

  • sequence_name – name of the sequence

  • blast_filename – filename for the blast file

  • max_target_seqs – maximum number of target sequences

  • n_cpu – number of cpus

  • delete_blast_file – if True deletes the blast file at the end

Returns:

a list of lists. - each list is a possible match (best is at index 0) - if there is no matching it returns None - The inner part is: [subject_id (name of the match), identity (percentage), alignment_length, mismatches (number within alignment length), gaps (number within alignment length), q_start (start index in the query sequence), q_end (end index), s_start (start index in the matching sequence), s_end (end index), evalue, nident (number of identical amino acids in aligned region), slen (sublect length)]

Notes

Check that NCBI blast is correctly installed in the folder blast_path

graphein.ml.clustering.build_fasta_file_from_mapping(pdb_sequence_mapping: Dict[str, str], fasta_out: str)[source]

Builds a fasta database from a mapping of PDBs (or other identifier) to sequences.

Parameters:
  • pdb_sequence_mapping (Dict[str, str]) – dictionary with the sequence of each pdb file

  • fasta_out (str) – file name of the fasta database

graphein.ml.clustering.clustering_from_pairs(pairs, out_file=None)[source]

Clustering of a set of elements given a list of ‘homologous’ with possibly false negatives :param pairs: pair of sequences :param out_file: output file :return: the clusters

graphein.ml.clustering.create_pairs_for_clustering(fasta_file: str, seq_id_low_thresh: float = 25.0, use_very_loose_condition: bool = False, out_file: str | None = None, n_cpu: int = 1, max_target_seqs: int = 200, delete_blast_databases_when_done: bool = False)[source]

Create sequences pairs for clustering [sequence names MUST be the ids!].

Parameters:
  • fasta_file (str) – file with fastas

  • seq_id_low_thresh (float) – sequence identity of lower threshold

  • use_very_loose_condition (bool) –

    ??

  • out_file (str) – name of the output file

  • n_cpu (int) – number of cpus to be used

  • max_target_seqs (int) –

    ??

  • delete_blast_databases_when_done (bool) – delete the db once the process is finished

Returns:

graphein.ml.clustering.generate_random_sets(clusters, number_of_sets=10, fraction_in_test=0.1, train_set_key='LR', test_set_key='TS', early_break=True)[source]

Generate random sets from clusters.

Parameters:
  • clusters – input clusters

  • number_of_sets – total number of sets to generate

  • fraction_in_test – fraction of sequences to be put in test

  • train_set_key – Prefix to add to train set

  • test_set_key – Prefix to add to test set

  • early_break – if True, break after a suitable combination of training-test set is generated to feed the model

Returns:

graphein.ml.clustering.get_seq_records(filename, alphabet=None, check_sequences=False, file_format='fasta', return_as_dictionary=False)[source]

Get the sequence records from a file.

Parameters:
  • filename (str) – File with sequences [fasta in our case].

  • alphabet (Optional[str]) – String of allowed sequence letters, e.g. "ACDEFGHIKLMNPQRSTVWY". Used when check_sequences=True.

  • check_sequences (bool) – Sanity check on the sequences.

  • file_format (str) – We use fasta format (don’t change this).

  • return_as_dictionary (bool) – if True uses records.to_dict() to convert the record into a dictionary and return it.

Returns:

a list of the SeqRecord of the sequences. - .seq: access the records (sequence, i.e. Seq, for actual string use str(records[].seq)) - .id: access the ids in the file - .name: access names (sometimes equal to id, depends on format) - .description: is the description (when included) - .dbxref which (if present) is a list with the cross-referenced database

Notes:

For a list of available file_format see http://biopython.org/wiki/SeqIO#File_Formats .aln is generally ‘clustal’

graphein.ml.clustering.process_blast_results(blast_results_list, len_of_query, return_dictionary_of_numpy_matrices=False, filter_best_match_for_same_subject=True, add_identity_on_shortest=True)[source]

If filter_best_match_for_same_subject is True than only the first among the possible alignments of the query with that subject sequence is kept. Blast results are sorted according to subject sequences and then to evalue :param blast_results_list: :param len_of_query: :param return_dictionary_of_numpy_matrices: :param filter_best_match_for_same_subject: :param add_identity_on_shortest: :return:

graphein.ml.clustering.train_and_test_from_fasta(fasta_file: str, number_of_sets: int = 10, fraction_in_test: float = 0.1, cluster_file_name: str = 's2d_clusters.txt', seq_id_low_thresh: float = 25.0, use_very_loose_condition: bool = False, n_cpu: int = 2, max_target_seqs: int = 200, delete_db_when_done: bool = True, train_set_key: str = 'LR', test_set_key: str = 'TS', early_break: bool = True)[source]

Use a fasta database to generate test and training sets based on user defined parameters.

Parameters:
  • fasta_file (str) – Fasta file name.

  • number_of_sets (int) – Number of sets to generate.

  • fraction_in_test (float) – Fraction of proteins to be put in test set.

  • cluster_file_name (str) – Name of the cluster file.

  • seq_id_low_thresh (float) – Sequence identity lower threshold.

  • use_very_loose_condition (bool) – ignore

  • n_cpu (int) – Number of cpus.

  • max_target_seqs (int) – Maximum number of target sequences.

  • delete_db_when_done (bool) – if True delete the blast database when done.

  • train_set_key (str) – Prefix to append to training files.

  • test_set_key (str) – Prefix to append to test files.

  • early_break (bool) – if True, break after a suitable combination of training-test set is generated to feed the model.

Returns:

save clusters in files (or return the 1st suitable one).

Utils

ML utility Functions for working with graphs.

graphein.ml.utils.add_labels_to_graph(graphs: List[Graph], labels: List[Any], name: str) → List[Graph][source]

Adds labels to a graph.

Parameters:
  • graphs (List[nx.Graph]) – A list of graphs to add labels to.

  • labels (List[Any]) – A list of labels to add to the graphs.

  • name (str) – The name to add the label under.

Returns:

A list of graphs with labels added.

Return type:

List[nx.Graph]