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
438 stars 181 forks source link

Extract regulons from a pickled file[results] #206

Closed deevdevil88 closed 4 years ago

deevdevil88 commented 4 years ago

Hi this is a continuation of the #202 .Where you suggest how to extract regulons from a csv file. Thank you, but what if i have a pickled file which i get after running df2regulons on the motif file generated from CLI version. As df2regulons output is directly saved as a pickled file and not a csv. So how would I extract the regulons and regulon genes from the pickled file or is there a way to save the df2regulon output as a csv.

Thanks Devika

cflerin commented 4 years ago

Hi @deevdevil88 ,

It should be the same solution as #202 , you can use the same function to load the regulon file in any format:

sig = load_signatures('signatures.dat')

Hope that helps. I'll close this but feel free to ask further if something isn't clear.

akramdi commented 4 years ago

Hi @deevdevil88 , @cflerin,

load_signatures does not seem to work for pickled files:

In [85]: sig = load_signatures('regulons.p')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-85-ff376c949042> in <module>
----> 1 sig = load_signatures('regulons.p')

~/ENV/env3/lib/python3.6/site-packages/pyscenic/cli/utils.py in load_signatures(fname)
    185             return pickle.load(f)
    186     else:
--> 187         raise ValueError("Unknown file format \"{}\".".format(fname))
    188 
    189 

Here's what I did to export regulons and target genes into a csv file after running df2regulons (inspired by #16):


with open("regulons.p", "rb") as f: regulons = pickle.load(f)   

#convert regulon to dict
rdict={}
for reg in regulons:
    targets = [ target for target in reg.gene2weight ]
    rdict[reg.name] = targets

#Optional: join list of target genes 
for key in rdict.keys(): rdict[key]=";".join(rdict[key])

#Write to csv file
with open("regulons.csv",'w') as f:
    w = csv.writer(f)
    w.writerow(["Regulons","Target_genes"])
    w.writerows(rdict.items())

The ouputs looks like this:

Regulons    Target_genes
ARID3A(+)   LINC01140;ATP6V1D;PCDHB5;NLE1;HIF1AN;SLC4A8;FAF2;SF3A3;IP6K1;RF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RDM4;ATF7IP;SPG21;KDM6B;FARSA;RCOR2;BROX;GPR176;ACAP3;NUDCD3;TCEAL9;JUN;ANO6;LAMTOR3;SUPT3H;POMGNT2;CBL;ELMSAN1;RNF185;TNRC6B;PLS1;CPNE2;APBA1;RRBP1;FRMD6;CYP2R1;MSANTD3;TP53BP2

This is the best I could come up with to have regulons and target genes in a readable file.. I hope it helps!

Amira

deevdevil88 commented 4 years ago

Hi Amira @akramdi , this is the code we used to get a csv file Transform reg.csv output from pipeline to regulons (in reg.csv, each TF can

be listed multiple times)

Create regulons from a dataframe of enriched features

df_motifs = load_motifs(args.ctx_output) regulons = df2regulons(df_motifs)

Pickle these regulons

with open("pyscenic_results.dir/regulons.P", 'wb') as f: pickle.dump(regulons, f)

Output regulons as a csv file

regulon_df = pd.DataFrame(columns = ["regulon_name", "transcription_factor", "genes", "weights", "score", "context"]) for i in range(len(regulons)): regulon = regulons[i] regulon_dict = dict({"regulon_name": regulon.name, "transcription_factor": regulon.transcription_factor, "genes": list(regulon.genes), "weights": list(regulon.weights), "score": regulon.score, "context": list(regulon.context)}) regulon_df = regulon_df.append(regulon_dict, ignore_index = True)

regulon_df.genes = regulon_df.genes.apply(lambda x: ", ".join(x)) regulon_df.weights = regulon_df.weights.apply(lambda x: str(x)) regulon_df.weights = regulon_df.weights.apply(lambda x: x.replace('[', '')) regulon_df.context = regulon_df.context.apply(lambda x: ", ".join(x))

regulon_df.to_csv("pyscenic_results.dir/regulons.csv", index = False)

hope this helps. Devika

akramdi commented 4 years ago

Great! Thanks for sharing!

Best,

wangjiawen2013 commented 2 years ago

Some time s the strings are too long to save as csv, and must be saved as xlsx. @deevdevil88