microbetag.db ============= .. py:module:: microbetag.db .. autoapi-nested-parse:: Aim: Establishing connection and query on microbetagDB. Useful for the API and the on-the-fly version of microbetag, more specifically for the phenotrex-based phenotypic traits and the pathway complementarity steps. .. rubric:: Notes Needs a hidden file called .env_dev.json with the database credentials. Attributes ---------- .. autoapisummary:: microbetag.db.DB_CREDENTIALS Classes ------- .. autoapisummary:: microbetag.db.GetPhenotrexTraits Functions --------- .. autoapisummary:: microbetag.db.execute microbetag.db.execute_in_a_pool microbetag.db.init_connection_pool microbetag.db.gc_unify microbetag.db.alt_genome_prefix microbetag.db.get_genomes_for_ncbi_tax_id microbetag.db.get_ncbi_tax_id_for_genome microbetag.db.patric_from_gc_list microbetag.db.update_for_patric microbetag.db.get_path_compls_otf microbetag.db.get_path_compls_for_ncbi_ids microbetag.db.query_for_getting_compl_ids microbetag.db.build_complement_queries microbetag.db.get_complement_ids microbetag.db.map_queries_to_pairs microbetag.db.get_coloured_complements microbetag.db.build_pairs_complements microbetag.db.build_kegg_urls Module Contents --------------- .. py:data:: DB_CREDENTIALS .. py:function:: execute(phrase: str) -> list[tuple] Establish a database connection and perform an action .. py:function:: execute_in_a_pool(cursor: mysql.connector, query: str) Executes a query using a connection from the connection pool. .. py:function:: init_connection_pool() -> mysql.connector.pooling.MySQLConnectionPool Initiates a connection pool. A pool opens a number of connections and handles thread safety when providing connections to requesters. For more see: https://dev.mysql.com/doc/connector-python/en/connector-python-connection-pooling.html .. py:function:: gc_unify(gc_list) Removes duplicates of a genome that has entries both as GCA and GCF in the db. .. py:function:: alt_genome_prefix(gc) Switches GCA prefic of a genome accession id to GCF and vice-versa. .. py:function:: get_genomes_for_ncbi_tax_id(ncbi_tax_id: int) Get the genome IDs corresponding to a given NCBI Taxonomy ID from the microbetagDB. Example: 1281578 .. py:function:: get_ncbi_tax_id_for_genome(gc_id: str) .. py:function:: patric_from_gc_list(gc_accession_list: list) Gets a list of GC accession ids and returns a dictionary where the GC ids are the keys and their corresponding PATRIC ids are the values. ["GCA_003184265.1"] .. py:function:: update_for_patric(module_nonseeds_pkl, gc_to_patric) :param module_nonseeds_pkl: path to nonseeds KEGG MODULE related pickle file :param gc_to_patric: a dictionary with GTDB representative genomes as key and their corresponding PATRIC id as value. .. py:class:: GetPhenotrexTraits(config=None) .. py:method:: get_phen_traits() Returns predictions for a list of genomes repr_genomes_present, config.predictions_path .. py:method:: get_phendb_traits(gtdb_genome_id: str) Get phenotypical traits based on phenDB classes based on its GTDB representative genome "GCA_018819265.1" .. py:method:: phen_query(gtdb_id) :staticmethod: Builds query for microbetagDB .. py:method:: to_csv(df, output_dir=None) :staticmethod: Saves phenotypic trait predictions of a trait for a set of genomes in a file, in a 3-column format. Identifier, Trait present, Confidence The values the 'Trait present' column may get, is 'YES, 'NO', and 'N/A'. .. py:function:: get_path_compls_otf(config) On the fly way to get pathway complementarities. Builds the pathCompls.json and the pathway_complements_extended.json files. The first file, in the stand-alone version lools like this: {"bin_101": { "bin_101": [], "bin_151": [["md:M00019", ["K00826"], ["K01652", "K01653", "K00053", "K01687", "K00826"], "https://www.kegg.jp/kegg-bin/show_pathway?map00290/K00826%09%23EAD1DC/K01687%09%2300A898/"], ..] },.. } where the elements regarding the complement and the alternative are actually lists, and not strings, while the latter: {"bin_101": { "bin_101": [], "bin_151": {"0": ["M00019", "Valine/isoleucine biosynthesis", "Branched-chain amino acid metabolism", "K00826", "K01652;K01653;K00053;K01687;K00826", "https://...] } } To build those files for the on-the-fly version (otf), this function makes use of the config.otf_seq_tax_df attribute of the config, as returned by the taxonomy.py script. It creates a list of tuples with the NCBI Taxonomy Ids of found related taxa (in the edgelist) and using their corresponding GTDB representative genomes gets their complements. :param config: Insance of the microbetat'g config class but as edited in the app.py script where the otf_seq_tax_df attribute is added :returns: A pd.DataFrame with nodes names, NCBI Taxonomy ids and GTDB representative genomes for those edges of the network that were both mapped to at least a GTDB genome. :rtype: mspecies_map_df .. py:function:: get_path_compls_for_ncbi_ids(relative_genomes: Dict[str, Set[str]], pairs_of_interest: Set[Tuple[str, str]]) -> dict :param relative_genomes: A dictionary {"1260918": {"GCF_002102185.1"}, "1819566": {"GCF_009711525.1"}} :param pairs_of_interest={: :type pairs_of_interest={: "1260918", "1819566" .. py:function:: query_for_getting_compl_ids(beneficiary='GCA_003184265.1', donor='GCA_000015645.1') Gets 2 gc accession ids and returns a query for their pathway complementarities .. py:function:: build_complement_queries(relative_genomes, pairs_of_interest) .. py:function:: get_complement_ids(unique_queries) .. py:function:: map_queries_to_pairs(complements_ids_queries, unique_queries2comples) .. py:function:: get_coloured_complements(pairs_to_compl_ids: Dict[Tuple[str, str], dict[Tuple[str, str], list[str]]]) Gets thes actual complement using its unique complementId and builds its KEGG url. :param pairs_to_compl_ids: {('1260918', '1819566'): {('GCA_002102185.1', 'GCA_009711525.1'): ['180131', ..']}} :returns: [] :rtype: pairs_complements .. py:function:: build_pairs_complements(pairs_to_compl_ids, all_compl_ids2coloured_compls) .. py:function:: build_kegg_urls(genome_pair_compls) Takes as input the complements list between two genomes and build urls to colorify the related to the module kegg map based on the KO terms of the beneficiary (pink) and those it gets from the donor (green). .. rubric:: Notes Some modules do not belong to any map, e.g. https://www.kegg.jp/module/M00705. In these cases, we will have a N/A value in the url.