annotate tools/myTools/bin/SFA_virtscreen.py @ 1:7e5c71b2e71f draft default tip

Uploaded
author laurenmarazzi
date Wed, 22 Dec 2021 16:00:34 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
1 #!/usr/bin/env python3
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
2 import os
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
3 import numpy as np
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
4 import pandas as pd
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
5 import networkx as nx
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
6 import random
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
7 import sfa
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
8 import csv
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
9 from sys import argv
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
10 from sklearn.preprocessing import StandardScaler
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
11 import itertools
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
12 ########INPUTS############
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
13 fpath = os.path.join( argv[1]) # location of networkfile
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
14 samples=pd.read_csv(argv[2],index_col = 0) # input samples (normexp initial states)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
15 inits=argv[3] #the initial states of interest
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
16
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
17 phenotypes = pd.read_csv(argv[4],delim_whitespace=True, index_col = False)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
18 initz = phenotypes[phenotypes.isin([inits]).any(axis=1)]['name'].tolist()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
19 samples = samples[initz]
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
20
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
21 FC_nodes=open(argv[5]).read().strip().split('\n') # FC set
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
22 FC_perts=pd.read_csv(argv[6],delim_whitespace=True,index_col=0,header=0,names=FC_nodes)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
23
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
24
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
25 class ThreeNodeCascade(sfa.base.Data):
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
26 def __init__(self):
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
27 super().__init__()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
28 self._abbr = "TNC"
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
29 self._name = "A simple three node cascade"
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
30
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
31 signs = {'activates':1, 'inhibits':-1}
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
32 A, n2i, dg = sfa.read_sif(fpath, signs=signs, as_nx=True)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
33 self._A = A
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
34 self._n2i = n2i
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
35 self._dg = dg
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
36 self._i2n = {idx: name for name, idx in n2i.items()}
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
37
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
38 # end of def __init__
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
39 # end of def class
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
40
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
41 if __name__ == "__main__":
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
42 ## initalize parameters from SFA
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
43 data = ThreeNodeCascade()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
44 algs = sfa.AlgorithmSet()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
45 alg = algs.create('SP')
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
46 alg.data = data
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
47 alg.params.apply_weight_norm = True
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
48 alg.initialize()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
49 alg.params.exsol_forbidden=True
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
50 alg.params.alpha=0.9
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
51
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
52
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
53 netnodes= list(data.dg.nodes)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
54
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
55 n = data.dg.number_of_nodes() #the number of nodes
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
56 b = np.zeros((n,))
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
57
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
58 alllogss=pd.DataFrame()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
59 for name, item in samples.iteritems(): #for each simulated initial condition
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
60 logss=pd.DataFrame(index=FC_perts.index,columns=netnodes,copy=True) # create dataframe for the FC perturbations
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
61 enodes=item.index.tolist() # get expressed nodes
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
62 for node in enodes: # set initial state to simulated value
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
63 b[data.n2i[node]]=float(str(item.loc[node]))
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
64 for name2,item2 in FC_perts.iterrows(): # for each FCnode perturbation
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
65 pnode=item2.index.tolist()
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
66 pi = []
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
67 for node in pnode: #if logFC.loc[node].at['logFC']>0: # if the logfc is postive (res > veh)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
68 if int(item2.loc[node])==-1: #if the perturbation is 0
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
69 if node in enodes:
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
70 b[data.n2i[node]]=float(str(item.loc[node]))-2.5*(float(str(item.loc[node]))) #downregulate the expression. fix to 0
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
71 else:
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
72 b[data.n2i[node]]=-2.5 #downregulate the expression. fix to 0
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
73 pi.append(data.n2i[node])
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
74 if int(item2.loc[node])==1: #if the perturbation is 1
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
75 if node in enodes:
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
76 b[data.n2i[node]]=float(str(item.loc[node]))+2.5*(float(str(item.loc[node]))) #upregulate the expression
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
77 else:
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
78 b[data.n2i[node]]=2.5 #upregulate the expression
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
79 pi.append(data.n2i[node])
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
80 x = alg.compute(b,pi) # Run SFA calculation
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
81 logss.loc[name2,netnodes]=x[0]
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
82 logss=logss.astype(float).round(3)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
83 alllogss=pd.concat([alllogss,logss],axis=0)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
84 multi=pd.MultiIndex.from_product([samples.columns.tolist(),FC_perts.index.tolist()],names=['replicate', 'perturbation'])
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
85 alllogss.set_index(multi,inplace=True)
7e5c71b2e71f Uploaded
laurenmarazzi
parents:
diff changeset
86 alllogss.to_csv('pert_logss.txt', sep=' ',float_format='%.3f',chunksize=10000)