aertslab / pySCENIC

pySCENIC is a lightning-fast python implementation of the SCENIC pipeline (Single-Cell rEgulatory Network Inference and Clustering) which enables biologists to infer transcription factors, gene regulatory networks and cell types from single-cell RNA-seq data.
http://scenic.aertslab.org
GNU General Public License v3.0
420 stars 179 forks source link

Getting simplified regulons list #16

Closed topherconley closed 6 years ago

topherconley commented 6 years ago

I used the pyscenic command line interface (CLI) and it is a bit limited in that it won't return a simple list of pruned regulons with their targets. I believe this is important for interpreting the AUC matrix of activity scores and knowing what the targets are of the transcription factors. I cobbled together some python code to get this list for downstream analysis (see below). If you could make this standard output from the `pyscenic ctx' output that would be helpful. The current 'CTX' pruned file has a non-standard means of parsing and appears to have a lot of information that is of secondary interest.

import pandas as pd
import os
from pyscenic.transform import df2regulons as df2regs
import json

# use function from pyscenic for parsing the 'pyscenic ctx' output
def _df2regulons(fname):
  ext = os.path.splitext(fname,)[1]
  df = pd.read_csv(fname, sep=',', index_col=[0,1], header=[0,1], skipinitialspace=True)
  df[('Enrichment', 'Context')] = df[('Enrichment', 'Context')].apply(lambda s: eval(s))
  df[('Enrichment', 'TargetGenes')] = df[('Enrichment', 'TargetGenes')].apply(lambda s: eval(s))
  return df2regs(df)

# Form dictionary of { TF : Target } pairs from 'pyscenic ctx' output.
rdict = {}
regulons = _df2regulons('joint_ctx.csv')
for reg in regulons:
  targets = [ target for target in reg.gene2weight ]
  rdict[reg.name] = targets

# Write to JSON for import to R
rjson = json.dumps(rdict)
f = open("regulons.json","w")
f.write(rjson)
f.close()

Then export into R with :

library(jsonlite)
regs <- read_json('regulons.json', simplify = T)
bramvds commented 6 years ago

Dear Chris,

Thanks for the suggestion. I'll make JSON export of regulons available as an option in the CLI version of pySCENIC in the next release.

Kr, Bram

bramvds commented 6 years ago

Dear Chris,

Just released a new version (0.8.7). This version includes your suggestion. Thanks.

Kindest regards, Bram

topherconley commented 6 years ago

Thanks!

finjen commented 3 years ago

Hi, I am trying to export regulons using the above mentioned code, however, I obtain the following error:

TypeError: expected str, bytes or os.PathLike object, not DataFrame

Could anyone please let me know what might be going wrong here? Thank you.

Best.