def update_db(project): log = task.logger projects_out_port = task.ports("projects_out") project_id = project["id"] log.info("--- [{0}] --------------------------------------------".format(project_id)) oclust = project["oncodriveclust"] del project["oncodriveclust"] if not os.path.exists(oclust["results"]): log.warn("No results have been found. Skipping it.") return log.info("Updating the project database ...") projdb = ProjectDb(project["db"]) exc_path = os.path.join(project["temp_path"], "oncodriveclust-excluded-cause.tsv") log.info(" Excluded gene causes ...") log.debug(" > {0}".format(exc_path)) count = 0 with tsv.open(exc_path, "r") as exf: for gene, cause in tsv.lines(exf, (str, str), header=True): projdb.update_gene(Gene(id=gene, clust_exc_cause=cause)) count += 1 log.debug(" {0} genes excluded".format(count)) log.info(" OncodriveCLUST results ...") with tsv.open(oclust["results"], "r") as f: types = (str, str, float, float, float) columns = ("GENE", "CLUST_COORDS", "ZSCORE", "PVALUE", "QVALUE") for gene, coords, zscore, pvalue, qvalue in tsv.lines(f, types, columns=columns, header=True, null_value="NA"): projdb.update_gene(Gene(id=gene, clust_coords=coords, clust_zscore=zscore, clust_pvalue=pvalue, clust_qvalue=qvalue, clust_exc_cause=ProjectDb.NO_GENE_EXC)) projdb.commit() projdb.close() projects_out_port.send(project)
def end(): log = task.logger projects_out_port = task.ports("projects_out") log.info("Updating the projects database ...") for project_id, projects in task.context.items(): log.info("[{0}]".format(project_id)) for index, project in enumerate(projects): projdb = ProjectDb(project["db"]) if index == 0: log.info(" Functional impact ...") projdb.delete_sample_gene_fimpact() with tsv.open(project["sample_gene_fi_data"], "r") as f: types = (int, str, float, float, int, float, float, int, float, float, int) for fields in tsv.lines(f, types, header=True, null_value="-"): projdb.add_sample_gene_fimpact(*fields) ofm = project["oncodrivefm"] del project["oncodrivefm"] exc_path = os.path.join(project["temp_path"], "oncodrivefm-excluded-cause.tsv") log.info(" Excluded gene causes ...") log.debug(" > {0}".format(exc_path)) count = 0 with tsv.open(exc_path, "r") as exf: for gene, cause in tsv.lines(exf, (str, str), header=True): projdb.update_gene(Gene(id=gene, fm_exc_cause=cause)) count += 1 log.debug(" {0} genes excluded".format(count)) for feature, results_path in ofm: log.info(" {0} ...".format(feature)) log.debug(" > {0}".format(results_path)) if feature == "genes": with tsv.open(results_path, "r") as f: count = 0 for gene, pvalue, qvalue in tsv.lines(f, (str, float, float), header=True): projdb.update_gene(Gene(id=gene, fm_pvalue=pvalue, fm_qvalue=qvalue, fm_exc_cause=ProjectDb.NO_GENE_EXC)) count += 1 log.info(" {0} genes".format(count)) elif feature == "pathways": with tsv.open(results_path, "r") as f: count = 0 for pathway, zscore, pvalue, qvalue in tsv.lines(f, (str, float, float, float), header=True): projdb.update_pathway(Pathway(id=pathway, fm_zscore=zscore, fm_pvalue=pvalue, fm_qvalue=qvalue)) count += 1 log.info(" {0} pathways".format(count)) projdb.commit() projdb.close() projects_out_port.send(projects[0])
def update_db(project): log = task.logger config = GlobalConfig(task.conf) projects_port = task.ports("projects_out") log.info("--- [{0}] --------------------------------------------".format(project["id"])) partitions = project["partitions"] if not os.path.exists(config.vardb_path): log.warn("Database for variation external references not found") log.debug("> {0}".format(conf["vardb_path"])) varxdb = VarXrefsDb(config.vardb_path) varxdb.open() projdb = ProjectDb(project["db"]) updated_variants = set() plen = len(partitions) gene_xrefs = defaultdict(set) for part in partitions: log.info("Updating database with partition data ({0} out of {1}) ...".format(part["index"] + 1, plen)) log.info(" VEP results ...") ctype = lambda v: v.split(",") with open(part["vep_path"], "r") as vf: for fields in tsv.lines(vf, (int, str, str, ctype, str, str, str, float, float), null_value="-"): ( var_id, gene, transcript, consequences, protein_pos, aa_change, protein, sift_score, pph2_score, ) = fields var = projdb.get_variant(var_id) xrefs = varxdb.get_xrefs(var.chr, var.start, var.ref, var.alt, var.strand) if xrefs is not None: xrefs = ["{0}:{1}".format(source, xref) for source, xref in xrefs] gene_xrefs[gene].update(xrefs) if len(xrefs) == 0: xrefs = None projdb.update_variant(Variant(id=var_id, xrefs=xrefs)) projdb.add_consequence( Consequence( var=Variant(id=var_id), transcript=transcript, gene=gene, ctypes=consequences, protein_pos=protein_pos, aa_change=aa_change, protein=protein, ) ) log.info(" Transcript functional impacts ...") with open(part["tfi_path"], "r") as f: types = (int, str, str, int, float, float, int, float, float, int, float, float, int) columns = [0, 1, 3, 6, 7, 8, 9, 11, 12, 13, 15, 16, 17] for fields in tsv.lines(f, types, columns=columns, null_value="-"): ( var_id, transcript, uniprot, impact, sift_score, sift_tfic, sift_class, pph2_score, pph2_tfic, pph2_class, ma_score, ma_tfic, ma_class, ) = fields print fields projdb.update_consequence( Consequence( var=Variant(id=var_id), transcript=transcript, uniprot=uniprot, sift_score=sift_score, sift_tfic=sift_tfic, sift_tfic_class=sift_class, pph2_score=pph2_score, pph2_tfic=pph2_tfic, pph2_tfic_class=pph2_class, ma_score=ma_score, ma_tfic=ma_tfic, ma_tfic_class=ma_class, impact=impact, ) ) log.info("Updating variant-gene functional impacts ...") with open(project["gfi_path"], "r") as f: types = (int, str, float, int, str) for var_id, gene, impact, coding_region, prot_changes in tsv.lines(f, types, null_value="-"): projdb.add_affected_gene( AffectedGene( var=Variant(id=var_id), gene_id=gene, impact=impact, coding_region=coding_region, prot_changes=prot_changes, ) ) log.info("Updating database with gene external variant references ...") for gene, xrefs in gene_xrefs.items(): projdb.update_gene(Gene(id=gene, xrefs=xrefs)) projdb.commit() projdb.close() varxdb.close() del project["partitions"] projects_port.send(project)