def insert_release_notes(p_entries: str, p_proteins: str, p_proteomes: str, p_structures: str, p_taxonomy: str, p_uniprot2matches: str, p_uniprot2proteome: str, rel_url: str, stg_url: str, relfile: str): logger.info("preparing data") uniprot2pdbe = {} for pdb_id, entry in loadobj(p_structures).items(): for uniprot_acc in entry["proteins"]: try: uniprot2pdbe[uniprot_acc].add(pdb_id) except KeyError: uniprot2pdbe[uniprot_acc] = {pdb_id} con = MySQLdb.connect(**url2dict(stg_url), charset="utf8mb4") cur = con.cursor() cur.execute(""" SELECT name_long, version FROM webfront_database WHERE name_long IN ('UniProtKB', 'UniProtKB/Swiss-Prot', 'UniProtKB/TrEMBL') """) uniprot = {} for name, version in cur: uniprot[name] = { "version": version, "count": 0, "signatures": 0, "integrated_signatures": 0 } cur.close() con.close() entries = loadobj(p_entries) proteins = Store(p_proteins) u2matches = Store(p_uniprot2matches) u2proteome = Store(p_uniprot2proteome) # Entities found in InterPro integrated_proteomes = set() integrated_structures = set() integrated_taxonomy = set() # Number of proteins with GO terms from InterPro uniprot2go = 0 logger.info("starting") i = 0 for uniprot_acc, info in proteins.items(): i += 1 if not i % 10000000: logger.info(f"{i:>12,}") if info["reviewed"]: database = uniprot["UniProtKB/Swiss-Prot"] else: database = uniprot["UniProtKB/TrEMBL"] database["count"] += 1 try: matches = u2matches[uniprot_acc] except KeyError: # No matches continue # Protein matched by at least one signature database["signatures"] += 1 is_integrated = False for entry_acc in matches: entry = entries[entry_acc] if entry.database == "interpro": """ Protein matched by at least one InterPro entry, i.e. at least one integrated signature """ is_integrated = True if entry.go_terms: uniprot2go += 1 break if is_integrated: database["integrated_signatures"] += 1 try: proteome_id = u2proteome[uniprot_acc] except KeyError: pass else: integrated_proteomes.add(proteome_id) try: pdb_ids = uniprot2pdbe[uniprot_acc] except KeyError: pass else: integrated_structures |= pdb_ids integrated_taxonomy.add(info["taxid"]) proteins.close() u2matches.close() u2proteome.close() logger.info(f"{i:>12,}") # Sum Swiss-Prot and TrEMBL counts for key in ["count", "signatures", "integrated_signatures"]: value_sp = uniprot["UniProtKB/Swiss-Prot"][key] value_tr = uniprot["UniProtKB/TrEMBL"][key] uniprot["UniProtKB"][key] = value_sp + value_tr logger.info("tracking changes since last releases") con = MySQLdb.connect(**url2dict(rel_url), charset="utf8mb4") cur = con.cursor() cur.execute(""" SELECT accession, source_database, integrated_id FROM webfront_entry WHERE is_alive = 1 """) public_entries = set() public_integrated = set() for entry_acc, database, integrated_in in cur: if database == "interpro": public_entries.add(entry_acc) elif integrated_in: # Signature already integrated in the previous release public_integrated.add(entry_acc) cur.execute(""" SELECT name, version FROM webfront_database WHERE type = 'entry' """) public_databases = dict(cur.fetchall()) cur.execute("SELECT * FROM webfront_release_note") prev_releases = cur.fetchall() cur.close() con.close() con = MySQLdb.connect(**url2dict(stg_url), charset="utf8mb4") cur = con.cursor() cur.execute("DROP TABLE IF EXISTS webfront_release_note") cur.execute(""" CREATE TABLE webfront_release_note ( version VARCHAR(20) PRIMARY KEY NOT NULL, release_date DATETIME NOT NULL, content LONGTEXT NOT NULL ) CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci """) cur.executemany( """ INSERT INTO webfront_release_note VALUES (%s, %s, %s) """, prev_releases) con.commit() prev_releases = None cur.execute(""" SELECT name, name_long, version, release_date FROM webfront_database WHERE type = 'entry' """) staging_databases = {row[0]: (row[1], row[2], row[3]) for row in cur} interpro_new = [] interpro_types = {} member_databases = {} pubmed_citations = set() interpro2go = 0 latest_entry = None for entry in sorted(loadobj(p_entries).values(), key=lambda e: e.creation_date): if entry.is_deleted: continue if entry.database == "interpro": for pub in entry.literature.values(): if pub["PMID"] is not None: pubmed_citations.add(pub["PMID"]) try: interpro_types[entry.type.lower()] += 1 except KeyError: interpro_types[entry.type.lower()] = 1 if entry.accession not in public_entries: interpro_new.append(entry.accession) interpro2go += len(entry.go_terms) latest_entry = entry.accession else: try: obj = member_databases[entry.database] except KeyError: database, version, _ = staging_databases[entry.database] is_new = is_updated = False if entry.database not in public_databases: is_new = True elif version != public_databases[entry.database]: is_updated = True obj = member_databases[entry.database] = { "name": database, "version": version, "signatures": 0, "integrated_signatures": 0, "recently_integrated": [], "is_new": is_new, "is_updated": is_updated, "sets": set() } obj["signatures"] += 1 if entry.integrated_in: obj["integrated_signatures"] += 1 if entry.accession not in public_integrated: # Recent integration obj["recently_integrated"].append(entry.accession) if entry.clan: obj["sets"].add(entry.clan["accession"]) # Transform sets of clans to counts: for obj in member_databases.values(): obj["sets"] = len(obj["sets"]) structures = list(loadobj(p_structures).values()) proteomes = set(loadobj(p_proteomes).keys()) errors = integrated_proteomes - proteomes if errors: raise RuntimeError(f"{len(errors)} invalid proteomes") taxa = set(loadobj(p_taxonomy).keys()) errors = integrated_taxonomy - taxa if errors: raise RuntimeError(f"{len(errors)} invalid taxa") content = { "notes": [], # TODO implement way to pass custom notes "interpro": { "entries": sum(interpro_types.values()), "new_entries": interpro_new, "latest_entry": latest_entry, "types": interpro_types, "go_terms": interpro2go }, "member_databases": member_databases, "proteins": uniprot, "structures": { "total": len(structures), "integrated": len(integrated_structures), "version": max(entry["date"] for entry in structures).strftime("%Y-%m-%d") }, "proteomes": { "total": len(proteomes), "integrated": len(integrated_proteomes), "version": uniprot["UniProtKB"]["version"] }, "taxonomy": { "total": len(taxa), "integrated": len(integrated_taxonomy), "version": uniprot["UniProtKB"]["version"] }, "citations": len(pubmed_citations) } _, version, date = staging_databases["interpro"] cur.execute( """ SELECT COUNT(*) FROM webfront_release_note WHERE version = %s """, (version, )) n_rows, = cur.fetchone() if n_rows: cur.execute( """ UPDATE webfront_release_note SET content = %s WHERE version = %s """, (json.dumps(content), version)) else: cur.execute( """ INSERT INTO webfront_release_note VALUES (%s, %s, %s) """, (version, date, json.dumps(content))) con.commit() cur.close() con.close() with open(relfile, "wt") as fh: new_integrated = 0 dbs_integrated = [] for db in sorted(member_databases.values(), key=lambda x: x["name"]): cnt = len(db["recently_integrated"]) if cnt: new_integrated += cnt dbs_integrated.append(f"{db['name']} ({cnt})") if new_integrated: integr_str = (f" integrates {new_integrated} new methods from " f"the {', '.join(dbs_integrated)} databases, and") else: integr_str = "" u_ver = uniprot["UniProtKB"]["version"] u_integ = uniprot["UniProtKB"]["integrated_signatures"] u_total = uniprot["UniProtKB"]["count"] u_cov = round(u_integ / u_total * 100, 1) fh.write(f"""\ Title ----- New releases: InterPro {version} and InterProScan 5.??-{version} Image: alternate text --------------------- InterPro: protein sequence analysis & classification Image: title ------------ InterPro: protein sequence analysis & classification Summary ------- InterPro version {version} and InterProScan 5.??-{version} are now available! \ InterPro now features hundreds of new methods integrated \ from partner databases, and InterProScan draws on over \ {sum(interpro_types.values())//1000*1000} entries. Body ---- <h3> <a href="http://www.ebi.ac.uk/interpro/">InterPro version {version}</a> </h3> <p> <a href="http://www.ebi.ac.uk/interpro/">InterPro {version}</a>\ {integr_str} covers {u_cov}% of UniProt Knowledgebase release {u_ver}. \ It predicts <a href="http://www.geneontology.org/">Gene Ontology</a> \ (GO) terms for over {uniprot2go/1e6:.0f} million UniProt proteins \ via the InterPro2GO pipeline. </p> <p> The new release includes an update to UniParc (uniparc_match.tar.gz) \ matches to InterPro methods. You can find this on our ftp site: \ <a href="ftp://ftp.ebi.ac.uk/pub/databases/interpro">ftp://ftp.ebi.ac.uk/pub/databases/interpro</a>. </p> <p> For full details, see <a href="//www.ebi.ac.uk/interpro/release_notes/">the latest InterPro Release Notes</a>. </p> <h3> <a href="https://github.com/ebi-pf-team/interproscan">InterProScan 5.??-{version}</a> </h3> <p> InterProScan 5.??-{version} uses data from the newly released InterPro {version}, \ which contains {sum(interpro_types.values()):,} entries. \ You can find the <a href="https://interproscan-docs.readthedocs.io/en/latest/ReleaseNotes.html">full release notes here</a>. </p> <p> If you need help with InterPro or InterProScan, please contact us using \ <a href="http://www.ebi.ac.uk/support/interpro">our support form</a> - \ your message will reach everyone on the team. </p> Meta fields: description ------------------------ We are pleased to announce the release of InterPro {version} \ and InterProScan 5.??-{version}! Meta fields: tags ----------------- Protein families, InterProScan, InterPro, Protein, \ protein family, protein motif URL alias --------- about/news/service-news/InterPro-{version} """) logger.info("complete")
def insert_proteomes(p_entries: str, p_proteins: str, p_proteomes: str, p_structures: str, p_uniprot2ida: str, p_uniprot2matches: str, p_uniprot2proteome: str, stg_url: str): logger.info("preparing data") proteomes = loadobj(p_proteomes) uniprot2pdbe = {} for pdb_id, entry in loadobj(p_structures).items(): for uniprot_acc in entry["proteins"]: try: uniprot2pdbe[uniprot_acc].add(pdb_id) except KeyError: uniprot2pdbe[uniprot_acc] = {pdb_id} # Init all proteomes xrefs = {} for proteome_id in proteomes: xrefs[proteome_id] = { "domain_architectures": set(), "entries": {}, "proteins": 0, "sets": set(), "structures": set(), "taxa": set() } entries = loadobj(p_entries) proteins = Store(p_proteins) u2ida = Store(p_uniprot2ida) u2matches = Store(p_uniprot2matches) u2proteome = Store(p_uniprot2proteome) logger.info("starting") i = 0 for uniprot_acc, proteome_id in u2proteome.items(): proteome = xrefs[proteome_id] proteome["proteins"] += 1 info = proteins[uniprot_acc] proteome["taxa"].add(info["taxid"]) try: dom_members, dom_arch, dom_arch_id = u2ida[uniprot_acc] except KeyError: pass else: proteome["domain_architectures"].add(dom_arch_id) for entry_acc in u2matches.get(uniprot_acc, []): entry = entries[entry_acc] try: proteome["entries"][entry.database].add(entry_acc) except KeyError: proteome["entries"][entry.database] = {entry_acc} if entry.clan: proteome["sets"].add(entry.clan["accession"]) try: pdb_ids = uniprot2pdbe[uniprot_acc] except KeyError: pass else: proteome["structures"] |= pdb_ids i += 1 if not i % 10000000: logger.info(f"{i:>12,}") logger.info(f"{i:>12,}") proteins.close() u2ida.close() u2matches.close() u2proteome.close() con = MySQLdb.connect(**url2dict(stg_url), charset="utf8mb4") cur = con.cursor() cur.execute("DROP TABLE IF EXISTS webfront_proteome") cur.execute(""" CREATE TABLE webfront_proteome ( accession VARCHAR(20) PRIMARY KEY NOT NULL, name VARCHAR(215) NOT NULL, is_reference TINYINT NOT NULL, strain VARCHAR(512), assembly VARCHAR(512), taxonomy_id VARCHAR(20) NOT NULL, counts LONGTEXT NOT NULL ) CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci """) cur.close() sql = """ INSERT INTO webfront_proteome VALUES (%s, %s, %s, %s, %s, %s, %s) """ with Table(con, sql) as table: for proteome_id, info in proteomes.items(): counts = reduce(xrefs[proteome_id]) counts["entries"]["total"] = sum(counts["entries"].values()) table.insert( (proteome_id, info["name"], 1 if info["is_reference"] else 0, info["strain"], info["assembly"], info["taxon_id"], jsonify(counts))) con.commit() con.close() logger.info("complete")
def insert_proteins(p_entries: str, p_proteins: str, p_structures: str, p_taxonomy: str, p_uniprot2comments: str, p_uniprot2name: str, p_uniprot2evidences: str, p_uniprot2ida: str, p_uniprot2matches: str, p_uniprot2proteome: str, p_uniprot2sequence: str, pro_url: str, stg_url: str): logger.info("loading CATH/SCOP domains") uniprot2cath = pdbe.get_cath_domains(pro_url) uniprot2scop = pdbe.get_scop_domains(pro_url) logger.info("preparing data") proteins = Store(p_proteins) u2comments = Store(p_uniprot2comments) u2descriptions = Store(p_uniprot2name) u2evidences = Store(p_uniprot2evidences) u2ida = Store(p_uniprot2ida) u2matches = Store(p_uniprot2matches) u2proteome = Store(p_uniprot2proteome) u2sequence = Store(p_uniprot2sequence) taxonomy = {} for taxid, info in loadobj(p_taxonomy).items(): taxonomy[taxid] = jsonify({ "taxId": taxid, "scientificName": info["sci_name"], "fullName": info["full_name"] }) uniprot2pdbe = {} for pdb_id, entry in loadobj(p_structures).items(): for uniprot_acc in entry["proteins"]: try: uniprot2pdbe[uniprot_acc].append(pdb_id) except KeyError: uniprot2pdbe[uniprot_acc] = [pdb_id] logger.info("counting proteins/IDA") ida_count = {} for dom_members, dom_arch, dom_arch_id in u2ida.values(): try: ida_count[dom_arch_id] += 1 except KeyError: ida_count[dom_arch_id] = 1 logger.info("inserting proteins") entries = loadobj(p_entries) con = MySQLdb.connect(**url2dict(stg_url), charset="utf8mb4") cur = con.cursor() cur.execute(""" SELECT protein_acc, COUNT(*) FROM webfront_varsplic GROUP BY protein_acc """) isoforms = dict(cur.fetchall()) cur.execute("DROP TABLE IF EXISTS webfront_protein") cur.execute(""" CREATE TABLE webfront_protein ( accession VARCHAR(15) PRIMARY KEY NOT NULL, identifier VARCHAR(16) NOT NULL, organism LONGTEXT NOT NULL, name VARCHAR(255) NOT NULL, description LONGTEXT, sequence LONGBLOB NOT NULL, length INT(11) NOT NULL, proteome VARCHAR(20), gene VARCHAR(70), go_terms LONGTEXT, evidence_code INT(11) NOT NULL, source_database VARCHAR(10) NOT NULL, is_fragment TINYINT NOT NULL, structure LONGTEXT, tax_id VARCHAR(20) NOT NULL, ida_id VARCHAR(40), ida TEXT, counts LONGTEXT NOT NULL ) CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci """) cur.close() i = 0 sql = """ INSERT into webfront_protein VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) """ with Table(con, sql) as table: for uniprot_acc, protein_info in proteins.items(): taxid = protein_info["taxid"] try: taxon = taxonomy[taxid] except KeyError: table.close() con.close() raise RuntimeError(f"{uniprot_acc}: invalid taxon {taxid}") try: name = u2descriptions[uniprot_acc] except KeyError: table.close() con.close() raise RuntimeError(f"{uniprot_acc}: missing name") try: evidence, gene = u2evidences[uniprot_acc] except KeyError: table.close() con.close() raise RuntimeError(f"{uniprot_acc}: missing evidence") try: sequence = u2sequence[uniprot_acc] except KeyError: table.close() con.close() raise RuntimeError(f"{uniprot_acc}: missing sequence") proteome_id = u2proteome.get(uniprot_acc) clans = [] databases = {} go_terms = {} for entry_acc in u2matches.get(uniprot_acc, []): entry = entries[entry_acc] try: databases[entry.database] += 1 except KeyError: databases[entry.database] = 1 if entry.clan: clans.append(entry.clan["accession"]) for term in entry.go_terms: go_terms[term["identifier"]] = term protein_structures = {} domains = uniprot2cath.get(uniprot_acc) if domains: protein_structures["cath"] = {} for dom in domains.values(): dom_id = dom["id"] protein_structures["cath"][dom_id] = { "domain_id": dom["superfamily"]["id"], "coordinates": dom["locations"] } domains = uniprot2scop.get(uniprot_acc) if domains: protein_structures["scop"] = {} for dom in domains.values(): dom_id = dom["id"] protein_structures["scop"][dom_id] = { "domain_id": dom["superfamily"]["id"], "coordinates": dom["locations"] } try: dom_members, dom_arch, dom_arch_id = u2ida[uniprot_acc] except KeyError: dom_arch = dom_arch_id = None dom_count = 0 else: dom_count = ida_count[dom_arch_id] table.insert( (uniprot_acc, protein_info["identifier"], taxon, name, jsonify(u2comments.get(uniprot_acc)), gzip.compress(sequence.encode("utf-8")), protein_info["length"], proteome_id, gene, jsonify(list(go_terms.values())), evidence, "reviewed" if protein_info["reviewed"] else "unreviewed", 1 if protein_info["fragment"] else 0, jsonify(protein_structures), protein_info["taxid"], dom_arch_id, dom_arch, jsonify({ "domain_architectures": dom_count, "entries": databases, "isoforms": isoforms.get(uniprot_acc, 0), "proteomes": 1 if proteome_id else 0, "sets": len(set(clans)), "structures": len(uniprot2pdbe.get(uniprot_acc, [])), "taxa": 1 }))) i += 1 if not i % 10000000: logger.info(f"{i:>12,}") logger.info(f"{i:>12,}") con.commit() proteins.close() u2comments.close() u2descriptions.close() u2evidences.close() u2ida.close() u2matches.close() u2proteome.close() u2sequence.close() logger.info("indexing") cur = con.cursor() cur.execute(""" CREATE UNIQUE INDEX ui_protein_identifier ON webfront_protein (identifier) """) cur.execute(""" CREATE INDEX i_protein_proteome ON webfront_protein (proteome) """) cur.execute(""" CREATE INDEX i_protein_database ON webfront_protein (source_database) """) cur.execute(""" CREATE INDEX i_protein_taxon ON webfront_protein (tax_id) """) cur.execute(""" CREATE INDEX i_protein_ida ON webfront_protein (ida_id) """) cur.execute(""" CREATE INDEX i_protein_fragment ON webfront_protein (is_fragment) """) cur.close() con.close() logger.info("complete")
def insert_taxonomy(p_entries: str, p_proteins: str, p_structures: str, p_taxonomy: str, p_uniprot2matches: str, p_uniprot2proteome: str, stg_url: str, p_interpro2taxonomy: str, tmpdir: Optional[str] = None): logger.info("preparing data") dt = DirectoryTree(tmpdir) entries = loadobj(p_entries) taxonomy = loadobj(p_taxonomy) uniprot2pdbe = {} for pdb_id, entry in loadobj(p_structures).items(): for uniprot_acc, chains in entry["proteins"].items(): try: uniprot2pdbe[uniprot_acc][pdb_id] = chains except KeyError: uniprot2pdbe[uniprot_acc] = {pdb_id: chains} proteins = Store(p_proteins) u2matches = Store(p_uniprot2matches) u2proteome = Store(p_uniprot2proteome) logger.info("starting") i = 0 xrefs = {} files = [] for uniprot_acc, info in proteins.items(): taxon_id = info["taxid"] try: taxon = xrefs[taxon_id] except KeyError: taxon = xrefs[taxon_id] = init_xrefs() try: proteome_id = u2proteome[uniprot_acc] except KeyError: pass else: taxon["proteomes"].add(proteome_id) taxon["proteins"]["all"] += 1 protein_structures = uniprot2pdbe.get(uniprot_acc, {}) # Add structures to taxon, regardless of entry matches taxon["structures"]["all"] |= set(protein_structures.keys()) databases = set() for entry_acc, locations in u2matches.get(uniprot_acc, {}).items(): entry = entries[entry_acc] database = entry.database try: taxon["entries"][database].add(entry_acc) except KeyError: taxon["entries"][database] = {entry_acc} if database not in databases: # Counting the protein *once* per database databases.add(database) try: taxon["proteins"]["databases"][database] += 1 except KeyError: taxon["proteins"]["databases"][database] = 1 try: taxon["proteins"]["entries"][entry_acc] += 1 except KeyError: taxon["proteins"]["entries"][entry_acc] = 1 for pdb_id, chains in protein_structures.items(): for chain_id, segments in chains.items(): if overlaps_pdb_chain(locations, segments): try: taxon["structures"]["entries"][entry_acc].add( pdb_id) except KeyError: taxon["structures"]["entries"][entry_acc] = { pdb_id } break # Skip other chains i += 1 if not i % 1000000: output = dt.mktemp() dump_xrefs(xrefs, taxonomy, output) files.append(output) xrefs = {} if not i % 10000000: logger.info(f"{i:>12,}") if xrefs: output = dt.mktemp() dump_xrefs(xrefs, taxonomy, output) files.append(output) xrefs = {} logger.info(f"{i:>12,}") logger.info(f"temporary files: " f"{sum(map(os.path.getsize, files))/1024/1024:.0f} MB") proteins.close() u2matches.close() u2proteome.close() logger.info("populating taxonomy tables") con = MySQLdb.connect(**url2dict(stg_url), charset="utf8mb4") cur = con.cursor() cur.execute("DROP TABLE IF EXISTS webfront_taxonomy") cur.execute(""" CREATE TABLE webfront_taxonomy ( accession VARCHAR(20) PRIMARY KEY NOT NULL, scientific_name VARCHAR(255) NOT NULL, full_name VARCHAR(512) NOT NULL, lineage LONGTEXT NOT NULL, parent_id VARCHAR(20), rank VARCHAR(20) NOT NULL, children LONGTEXT, counts LONGTEXT NOT NULL ) CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci """) cur.execute("DROP TABLE IF EXISTS webfront_taxonomyperentry") cur.execute(""" CREATE TABLE webfront_taxonomyperentry ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, tax_id VARCHAR(20) NOT NULL, entry_acc VARCHAR(25) NOT NULL, counts LONGTEXT NULL NULL ) CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci """) cur.execute("DROP TABLE IF EXISTS webfront_taxonomyperentrydb") cur.execute(""" CREATE TABLE webfront_taxonomyperentrydb ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, tax_id VARCHAR(20) NOT NULL, source_database VARCHAR(10) NOT NULL, counts LONGTEXT NOT NULL ) CHARSET=utf8mb4 DEFAULT COLLATE=utf8mb4_unicode_ci """) cur.close() table = Table(con, query=""" INSERT INTO webfront_taxonomy VALUES (%s, %s, %s, %s, %s, %s, %s, %s) """) per_entry = Table(con, query=""" INSERT INTO webfront_taxonomyperentry (tax_id,entry_acc,counts) VALUES (%s, %s, %s) """) per_database = Table(con, query=""" INSERT INTO webfront_taxonomyperentrydb (tax_id,source_database,counts) VALUES (%s, %s, %s) """) with DumpFile(p_interpro2taxonomy, compress=True) as interpro2taxonomy: interpro_entries = { entry.accession for entry in entries.values() if entry.database == "interpro" and not entry.is_deleted } i = 0 for taxon_id, taxon_xrefs in merge_dumps(files): taxon = taxonomy[taxon_id] protein_counts = taxon_xrefs.pop("proteins") structure_counts = taxon_xrefs.pop("structures") counts = reduce(taxon_xrefs) # Add total protein count (not grouped by database/entry) counts["proteins"] = protein_counts["all"] # Add total structure count counts["structures"] = len(structure_counts["all"]) # Add total entry count (not grouped by database) counts["entries"]["total"] = sum(counts["entries"].values()) table.insert( (taxon_id, taxon["sci_name"], taxon["full_name"], f" {' '.join(taxon['lineage'])} ", taxon["parent"], taxon["rank"], jsonify(taxon["children"]), jsonify(counts))) # Remove the 'entry' property # (no needed for webfront_taxonomyperentry) entry_counts = counts.pop("entries") database_structures = {} for entry_acc, count in protein_counts["entries"].items(): if entry_acc in interpro_entries: interpro2taxonomy.dump((entry_acc, taxon_id, count)) counts["proteins"] = count try: entry_structures = structure_counts["entries"][entry_acc] except KeyError: counts["structures"] = 0 else: counts["structures"] = len(entry_structures) database = entries[entry_acc].database try: database_structures[database] |= entry_structures except KeyError: database_structures[database] = entry_structures.copy() finally: per_entry.insert((taxon_id, entry_acc, jsonify(counts))) for database, count in protein_counts["databases"].items(): counts.update({ "entries": entry_counts[database], "proteins": count, "structures": len(database_structures.get(database, [])) }) per_database.insert((taxon_id, database, jsonify(counts))) i += 1 if not i % 100000: logger.info(f"{i:>12,}") logger.info(f"{i:>12,}") table.close() per_entry.close() per_database.close() con.commit() dt.remove() logger.info("indexing") cur = con.cursor() cur.execute(""" CREATE INDEX i_webfront_taxonomyperentry_tax ON webfront_taxonomyperentry (tax_id) """) cur.execute(""" CREATE INDEX i_webfront_taxonomyperentry_entry ON webfront_taxonomyperentry (entry_acc) """) cur.execute(""" CREATE INDEX i_webfront_taxonomyperentrydb_tax ON webfront_taxonomyperentrydb (tax_id) """) cur.execute(""" CREATE INDEX i_webfront_taxonomyperentrydb_database ON webfront_taxonomyperentrydb (source_database) """) cur.close() con.close() logger.info("complete")
def export_documents(src_proteins: str, src_entries: str, src_proteomes: str, src_structures: str, src_taxonomy: str, src_uniprot2ida: str, src_uniprot2matches: str, src_uniprot2proteomes: str, outdirs: Sequence[str], version: str, cache_size: int = 100000): logger.info("preparing data") os.umask(0o002) organizers = [] for path in outdirs: try: shutil.rmtree(path) except FileNotFoundError: pass os.makedirs(path, mode=0o775) organizers.append(DirectoryTree(path)) open(os.path.join(path, f"{version}{LOAD_SUFFIX}"), "w").close() logger.info("loading domain architectures") domains = {} with Store(src_uniprot2ida) as u2ida: for dom_members, dom_arch, dom_arch_id in u2ida.values(): try: dom = domains[dom_arch_id] except KeyError: domains[dom_arch_id] = { "ida_id": dom_arch_id, "ida": dom_arch, "counts": 1 } else: dom["counts"] += 1 logger.info("writing IDA documents") num_documents = 0 domains = list(domains.values()) for i in range(0, len(domains), cache_size): documents = [] for dom in domains[i:i + cache_size]: documents.append(( IDA_INDEX + version, dom["ida_id"], dom )) num_documents += len(documents) for org in organizers: filepath = org.mktemp() dumpobj(filepath, documents) os.rename(filepath, f"{filepath}{EXTENSION}") domains = None proteins = Store(src_proteins) uniprot2ida = Store(src_uniprot2ida) uniprot2matches = Store(src_uniprot2matches) uniprot2proteomes = Store(src_uniprot2proteomes) entries = loadobj(src_entries) # mem: ~1.5 GB proteomes = loadobj(src_proteomes) # mem: <1 GB structures = loadobj(src_structures) # mem: ~ 4GB taxonomy = loadobj(src_taxonomy) # mem: ~ 2.5GB uniprot2pdbe = {} # mem: <1 GB for pdb_id, entry in structures.items(): for uniprot_acc in entry["proteins"]: try: uniprot2pdbe[uniprot_acc].append(pdb_id) except KeyError: uniprot2pdbe[uniprot_acc] = [pdb_id] logger.info("writing relationship documents") i = 0 documents = [] used_entries = set() used_taxa = set() for uniprot_acc, info in proteins.items(): taxid = info["taxid"] taxon = taxonomy[taxid] used_taxa.add(taxid) # remember that this taxon has been used try: dom_members, dom_arch, dom_arch_id = uniprot2ida[uniprot_acc] except KeyError: dom_members = [] dom_arch = dom_arch_id = None # Create an empty document (all properties set to None) doc = init_rel_doc() doc.update({ "protein_acc": uniprot_acc.lower(), "protein_length": info["length"], "protein_is_fragment": info["fragment"], "protein_db": "reviewed" if info["reviewed"] else "unreviewed", "text_protein": join(uniprot_acc, info["identifier"]), # Taxonomy "tax_id": taxid, "tax_name": taxon["sci_name"], "tax_lineage": taxon["lineage"], "tax_rank": taxon["rank"], "text_taxonomy": join(taxid, taxon["full_name"], taxon["rank"]) }) proteome_id = uniprot2proteomes.get(uniprot_acc) if proteome_id: proteome = proteomes[proteome_id] doc.update({ "proteome_acc": proteome_id.lower(), "proteome_name": proteome["name"], "proteome_is_reference": proteome["is_reference"], "text_proteome": join(proteome_id, proteome["name"], proteome["assembly"], proteome["taxon_id"], proteome["strain"]), }) # Adding PDBe structures/chains pdb_chains = {} # mapping PDB-chain ID -> chain segments pdb_documents = {} # mapping PDB-chain ID -> ES document for pdb_id in uniprot2pdbe.get(uniprot_acc, []): pdb_entry = structures[pdb_id] chains = pdb_entry["proteins"][uniprot_acc] pdb_doc = doc.copy() pdb_doc.update({ "structure_acc": pdb_id.lower(), "structure_resolution": pdb_entry["resolution"], "structure_date": pdb_entry["date"], "structure_evidence": pdb_entry["evidence"], "protein_structure": chains, "text_structure": join(pdb_id, pdb_entry["evidence"], pdb_entry["name"]) }) for chain_id, segments in chains.items(): pdb_chain_id = f"{pdb_id}-{chain_id}" locations = [] for segment in segments: locations.append({ "fragments": [{ "start": segment["protein_start"], "end": segment["protein_end"], }] }) chain_doc = pdb_doc.copy() chain_doc.update({ "structure_chain_acc": chain_id, "structure_protein_locations": locations, "structure_chain": pdb_chain_id }) pdb_documents[pdb_chain_id] = chain_doc pdb_chains[pdb_chain_id] = segments # Adding entries overlapping_chains = set() # chains associated to at least one entry matches = uniprot2matches.get(uniprot_acc, {}) num_protein_docs = 0 for entry_acc, locations in matches.items(): used_entries.add(entry_acc) # this entry has been used entry = entries[entry_acc] if entry.integrated_in: interpro_acc = entry.integrated_in.lower() else: interpro_acc = None entry_obj = { "entry_acc": entry_acc.lower(), "entry_db": entry.database, "entry_type": entry.type.lower(), "entry_date": entry.creation_date.strftime("%Y-%m-%d"), "entry_protein_locations": locations, "entry_go_terms": [t["identifier"] for t in entry.go_terms], "entry_integrated": interpro_acc, "text_entry": join(entry_acc, entry.short_name, entry.name, entry.type.lower(), interpro_acc), } if entry.clan: entry_obj.update({ "set_acc": entry.clan["accession"].lower(), "set_db": entry.database, "text_set": join(entry.clan["accession"], entry.clan["name"]), }) if entry_acc in dom_members: entry_obj.update({ "ida_id": dom_arch_id, "ida": dom_arch, }) # Test if the entry overlaps PDB chains entry_chains = set() for pdb_chain_id, segments in pdb_chains.items(): if overlaps_pdb_chain(locations, segments): # Entry overlaps chain: associate entry to struct/chain chain_doc = pdb_documents[pdb_chain_id] entry_doc = chain_doc.copy() entry_doc.update(entry_obj) documents.append(( entry.database + version, get_rel_doc_id(entry_doc), entry_doc )) entry_chains.add(pdb_chain_id) num_protein_docs += 1 if entry_chains: # Entry overlaps at least one chain overlapping_chains |= entry_chains else: # Associate entry to protein directly entry_doc = doc.copy() entry_doc.update(entry_obj) documents.append(( entry.database + version, get_rel_doc_id(entry_doc), entry_doc )) num_protein_docs += 1 # Add chains not overlapping any entry for chain_id, chain_doc in pdb_documents.items(): if chain_id in overlapping_chains: continue chain_doc.update({ "ida_id": dom_arch_id, "ida": dom_arch, }) documents.append(( # Not overlapping any entry -> not associated to a member DB REL_INDEX + version, get_rel_doc_id(chain_doc), chain_doc )) num_protein_docs += 1 if not num_protein_docs: # No relationships for this protein: fallback to protein doc documents.append(( REL_INDEX + version, get_rel_doc_id(doc), doc )) while len(documents) >= cache_size: for org in organizers: filepath = org.mktemp() dumpobj(filepath, documents[:cache_size]) os.rename(filepath, f"{filepath}{EXTENSION}") del documents[:cache_size] num_documents += cache_size i += 1 if not i % 10000000: logger.info(f"{i:>12,}") logger.info(f"{i:>12,}") logger.info("writing remaining documents") # Add unused entries for entry in entries.values(): if entry.accession in used_entries or entry.is_deleted: continue if entry.integrated_in: interpro_acc = entry.integrated_in.lower() else: interpro_acc = None doc = init_rel_doc() doc.update({ "entry_acc": entry.accession.lower(), "entry_db": entry.database, "entry_type": entry.type.lower(), "entry_date": entry.creation_date.strftime("%Y-%m-%d"), "entry_protein_locations": [], "entry_go_terms": [t["identifier"] for t in entry.go_terms], "entry_integrated": interpro_acc, "text_entry": join(entry.accession, entry.short_name, entry.name, entry.type.lower(), interpro_acc), }) if entry.clan: doc.update({ "set_acc": entry.clan["accession"].lower(), "set_db": entry.database, "text_set": join(entry.clan["accession"], entry.clan["name"]), }) documents.append(( entry.database + version, get_rel_doc_id(doc), doc )) # Add unused taxa for taxon in taxonomy.values(): if taxon["id"] in used_taxa: continue doc = init_rel_doc() doc.update({ "tax_id": taxon["id"], "tax_name": taxon["full_name"], "tax_lineage": taxon["lineage"], "tax_rank": taxon["rank"], "text_taxonomy": join(taxon["id"], taxon["full_name"], taxon["rank"]) }) documents.append(( REL_INDEX + version, get_rel_doc_id(doc), doc )) num_documents += len(documents) while documents: for org in organizers: filepath = org.mktemp() dumpobj(filepath, documents[:cache_size]) os.rename(filepath, f"{filepath}{EXTENSION}") del documents[:cache_size] proteins.close() uniprot2ida.close() uniprot2matches.close() uniprot2proteomes.close() for path in outdirs: open(os.path.join(path, f"{version}{DONE_SUFFIX}"), "w").close() logger.info(f"complete ({num_documents:,} documents)")
def export_entries(url: str, p_metacyc: str, p_clans: str, p_proteins: str, p_structures: str, p_uniprot2matches: str, p_uniprot2proteome: str, p_uniprot2ida: str, p_entry2xrefs: str, p_entries: str, **kwargs): min_overlap = kwargs.get("overlap", 0.2) processes = kwargs.get("processes", 1) min_similarity = kwargs.get("similarity", 0.75) tmpdir = kwargs.get("tmpdir") con = cx_Oracle.connect(url) cur = con.cursor() entries = {} logger.info("loading active InterPro entries") for entry in _get_interpro_entries(cur): entries[entry.accession] = entry logger.info("enriching entries with IntAct data") for accession, interactions in intact.get_interactions(cur).items(): try: entry = entries[accession] except KeyError: continue else: entry.ppi = interactions logger.info("loading deleted InterPro entries") for entry in _get_retired_interpro_entries(cur): if entry.accession in entries: cur.close() con.close() raise RuntimeError(f"entry cannot be active " f"and deleted {entry.accession}") entries[entry.accession] = entry logger.info("loading member database signatures") for entry in _get_signatures(cur): if entry.integrated_in and entry.integrated_in not in entries: cur.close() con.close() raise RuntimeError(f"{entry.accession} integrated " f"in missing entry ({entry.integrated_in})") entries[entry.accession] = entry logger.info("loading past entry names") past_names = _get_name_history(cur) logger.info("loading past signature integrations") past_integrations = _get_integration_history(cur) logger.info("loading ENZYME") u2enzyme = uniprot.get_swissprot2enzyme(cur) logger.info("loading Reactome pathways") u2reactome = uniprot.get_swissprot2reactome(cur) cur.close() con.close() logger.info("loading MetaCyc pathways") ec2metacyc = metacyc.get_ec2pathways(p_metacyc) # Updating entry history for entry in entries.values(): try: names = past_names[entry.accession] except KeyError: pass else: entry.history["names"] = names try: signatures = past_integrations[entry.accession] except KeyError: pass else: entry.history["signatures"] = signatures # Updating entry clan info for clan in loadobj(p_clans).values(): for entry_acc, score, seq_length in clan["members"]: try: entry = entries[entry_acc] except: continue else: entry.clan = { "accession": clan["accession"], "name": clan["name"] } inqueue = Queue(maxsize=processes) outqueue = Queue() workers = [] for _ in range(max(1, processes - 1)): dt = DirectoryTree(tmpdir) p = Process(target=_process_proteins, args=(inqueue, entries, min_overlap, dt, outqueue)) p.start() workers.append((p, dt)) logger.info("processing") uniprot2pdbe = {} for pdb_id, entry in loadobj(p_structures).items(): for uniprot_acc, chains in entry["proteins"].items(): try: uniprot2pdbe[uniprot_acc][pdb_id] = chains except KeyError: uniprot2pdbe[uniprot_acc] = {pdb_id: chains} proteins = Store(p_proteins) u2matches = Store(p_uniprot2matches) u2proteome = Store(p_uniprot2proteome) i = 0 for uniprot_acc, matches in u2matches.items(): inqueue.put(( uniprot_acc, proteins[uniprot_acc], matches, u2proteome.get(uniprot_acc), uniprot2pdbe.get(uniprot_acc, {}), set(u2enzyme.get(uniprot_acc, [])), set(u2reactome.get(uniprot_acc, [])) )) i += 1 if not i % 10000000: logger.info(f"{i:>15,}") proteins.close() u2matches.close() u2proteome.close() logger.info(f"{i:>15,}") # Send sentinel for _ in workers: inqueue.put(None) # Merge results from workers logger.info("exporting domain architectures") entries_with_xrefs = set() xref_files = [] entry_counts = {} entry_intersections = {} interpro2enzyme = {} interpro2reactome = {} with Store(p_uniprot2ida, u2matches.get_keys(), tmpdir) as u2ida: for _ in workers: obj = outqueue.get() xref_files.append(obj[0]) # str entries_with_xrefs |= obj[1] # set ida_file = obj[2] # str deepupdate(obj[3], entry_counts, replace=False) # dict deepupdate(obj[4], entry_intersections, replace=False) # dict deepupdate(obj[5], interpro2enzyme) # dict deepupdate(obj[6], interpro2reactome) # dict with DumpFile(ida_file) as df: i = 0 for uniprot_acc, dom_members, dom_str, dom_id in df: u2ida[uniprot_acc] = ( dom_members, dom_str, dom_id ) i += 1 if not i % 1000: u2ida.sync() u2ida.sync() size = u2ida.merge(processes=processes) # Adding empty EntryXrefs objects for entries without xrefs xref_files.append(workers[0][1].mktemp()) with DumpFile(xref_files[-1], compress=True) as df: for entry_acc in sorted(set(entries.keys()) - entries_with_xrefs): df.dump((entry_acc, EntryXrefs().asdict())) logger.info("exporting cross-references") with DumpFile(p_entry2xrefs, compress=True) as df: for entry_acc, xrefs in merge_dumps(xref_files): df.dump((entry_acc, xrefs)) entry = entries[entry_acc] # Reactome pathways if entry_acc in interpro2reactome: pathways = interpro2reactome[entry_acc] entry.pathways["reactome"] = [ dict(zip(("id", "name"), pthw)) for pthw in sorted(pathways) ] # EC numbers if entry_acc in interpro2enzyme: ecnos = sorted(interpro2enzyme[entry_acc]) entry.cross_references["ec"] = ecnos # MetaCyc pathways pathways = set() for ecno in ecnos: pathways |= set(ec2metacyc.get(ecno, [])) if pathways: entry.pathways["metacyc"] = [ dict(zip(("id", "name"), pthw)) for pthw in sorted(pathways) ] for p, dt in workers: size += dt.size dt.remove() logger.info(f"temporary files: {size / 1024 / 1024:.0f} MB") logger.info("calculating overlapping relationships") supfam = "homologous_superfamily" types = (supfam, "domain", "family", "repeat") for entry_acc, overlaps in entry_intersections.items(): entry1 = entries[entry_acc] entry_cnt = entry_counts[entry_acc] type1 = entry1.type.lower() for other_acc, overlap_counts in overlaps.items(): o1 = overlap_counts["1"] o2 = overlap_counts["2"] other_cnt = entry_counts[other_acc] # Independent coefficients coef1 = o1 / (entry_cnt + other_cnt - o1) coef2 = o2 / (entry_cnt + other_cnt - o2) # Final coefficient: average of independent coefficients coef = (coef1 + coef2) * 0.5 # Containment indices c1 = o1 / entry_cnt c2 = o2 / other_cnt if all([item < min_similarity for item in (coef, c1, c2)]): continue # Entries are similar enough entry2 = entries[other_acc] type2 = entry2.type.lower() if ((type1 == supfam and type2 in types) or (type1 in types and type2 == supfam)): # e1 -> e2 relationship entry1.overlaps_with.append({ "accession": other_acc, "name": entry2.name, "type": type2 }) # e2 -> e1 relationship entry2.overlaps_with.append({ "accession": entry_acc, "name": entry1.name, "type": type1 }) dumpobj(p_entries, entries) logger.info("populating ENTRY2PATHWAY") con = cx_Oracle.connect(url) cur = con.cursor() cur.execute("TRUNCATE TABLE INTERPRO.ENTRY2PATHWAY") cur.close() sql = "INSERT INTO INTERPRO.ENTRY2PATHWAY VALUES (:1, :2, :3, :4)" with Table(con, sql) as table: for e in entries.values(): for database, pathways in e.pathways.items(): code = PATHWAY_DATABASE[database] for pthw in pathways: table.insert(( e.accession, code, pthw["id"], pthw["name"] )) con.commit() con.close() logger.info("complete")