File size: 6,531 Bytes
eb028df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import pandas as pd
import sys
import numpy as np
import seaborn as sns
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(rc={"lines.linewidth": 2}, palette  = "deep", style = "ticks")
from sklearn.metrics import precision_recall_curve, roc_curve, auc
from itertools import product, permutations, combinations, combinations_with_replacement
from tqdm import tqdm
import networkx as nx

def pathAnalysis(dataDict, inputSettings):
    '''
    Computes "directed","feed-forward", 
    "cascade", and "mutual" motifs.
    '''
    
    # Read file for trueEdges
    trueEdgesDF = pd.read_csv(str(inputSettings.datadir)+'/'+ dataDict['name'] +
                                '/' +dataDict['trueEdges'],
                                sep = ',', 
                                header = 0, index_col = None)
            
    possibleEdges = list(permutations(np.unique(trueEdgesDF.loc[:,['Gene1','Gene2']]),
                                 r = 2))        
    EdgeDict = {'|'.join(p):0 for p in possibleEdges}

    refGraph = nx.DiGraph()

    for key in EdgeDict.keys():
        u = key.split('|')[0]
        v = key.split('|')[1]
        if len(trueEdgesDF.loc[(trueEdgesDF['Gene1'] == u) &
               (trueEdgesDF['Gene2'] == v)])>0:
                refGraph.add_edge(u,v)

    #refCC, refFB, refFF, refMI = getNetProp(refGraph)

    
    # set-up outDir that stores output directory name
    outDir = "outputs/"+str(inputSettings.datadir).split("inputs/")[1]+ '/' +dataDict['name']
    #print(dataDict['name'])

    ##################################################
    # Get counts of tp,fp with, and fp without paths #
    ##################################################
    collection = {}

    for algo in inputSettings.algorithms:
        # check if the output rankedEdges file exists
        if Path(outDir + '/' +algo[0]+'/rankedEdges.csv').exists() and algo[0] not in ['PPCOR','PIDC']:
            # Initialize Precsion
            predDF = pd.read_csv(outDir + '/' +algo[0]+'/rankedEdges.csv', \
                                        sep = '\t', header =  0, index_col = None)

            predDF.EdgeWeight = predDF.EdgeWeight.round(6)
            predDF.EdgeWeight = predDF.EdgeWeight.abs()
            predDF = predDF.loc[(predDF['EdgeWeight'] > 0)]
            predDF.drop_duplicates(keep = 'first', inplace=True)
            predDF.reset_index(drop = True,  inplace= True)
            predDF = predDF.loc[(predDF['Gene1'] != predDF['Gene2'])]
            if predDF.shape[0] != 0:
                maxk = min(predDF.shape[0], len(refGraph.edges()))
                edgeWeightTopk = predDF.iloc[maxk-1].EdgeWeight            
             
            newDF = predDF.loc[(predDF['EdgeWeight'] >= edgeWeightTopk)]
                
            predGraph = nx.DiGraph()
            

            for key in EdgeDict.keys():
                u = key.split('|')[0]
                v = key.split('|')[1]
                if len(newDF.loc[(newDF['Gene1'] == u) &
                       (newDF['Gene2'] == v)])>0:
                        predGraph.add_edge(u,v)

            dataDict = pathStats(predGraph, refGraph)
            collection[algo[0]] = dataDict
            if algo[0] == 'PPCOR' or algo[0] == 'PIDC':
                collection[algo[0]] = {}
        else:
            print(outDir + '/' +algo[0]+'/rankedEdges.csv', \
                  ' is an undirected graph. Skipping...')
            
    hMap = '/pathStats'
    largest = max([len(algopaths.keys()) for algo,algopaths in collection.items()])
    allpathsizes = set()
    for algo,algopaths in collection.items():
        allpathsizes.update(set(algopaths.keys()))
    
    for algo, algopaths in collection.items():
        notpresent = allpathsizes.difference(set(algopaths.keys()))
        collection[algo].update({np:0 for np in notpresent})
                   
    dataDF = pd.DataFrame(collection)
    dataDF = dataDF.T
    dataDF.to_csv(outDir+hMap+'.csv')

   
    
    
def getNetProp(inGraph):
    '''
    Function to compute properties
    of a given network.
    '''

    # number of weakly connected components in 
    # reference network
    numCC = len(list(nx.weakly_connected_components(inGraph)))
    
    # number of feedback loop 
    # in reference network
    allCyc = nx.simple_cycles(inGraph)
    cycSet = set()
    for cyc in allCyc:
        if len(cyc) == 3:
            cycSet.add(frozenset(cyc))
    
    numFB = len(cycSet)
    
    # number of feedfwd loops
    # in reference network
    allPaths = []
    allPathsSet = set()   
    for u,v in inGraph.edges():
        allPaths = nx.all_simple_paths(inGraph, u, v, cutoff=2)
        for p in allPaths:
            if len(p) >  2:
                allPathsSet.add(frozenset(p))
                
    numFF= len(allPathsSet)
    
    
    # number of mutual interactions
    numMI = 0.0
    for u,v in inGraph.edges():
        if (v,u) in inGraph.edges():
            numMI += 0.5

    return numCC, numFB, numFF, numMI

def getEdgeHistogram(inGraph, refGraph):
    falsePositives = set(inGraph.edges()).difference(refGraph.edges())
    edgeHistogramCounts = {0:0}
    
    for fe in falsePositives:
        u,v = fe
        try:
            path = nx.dijkstra_path(refGraph,u,v)
            pathlength = len(path) -1 
            if pathlength in edgeHistogramCounts.keys():
                edgeHistogramCounts[pathlength] +=1
            else:
                edgeHistogramCounts[pathlength] = 0
            
        except nx.exception.NetworkXNoPath:
            edgeHistogramCounts[0] +=1
    return edgeHistogramCounts



def pathStats(inGraph, refGraph):
    """
    Only returns TP, FP, numPredictions for each networks
    """
    falsePositives = set(inGraph.edges()).difference(refGraph.edges())
    truePositives = set(inGraph.edges()).intersection(refGraph.edges())
    numPredictions = len(inGraph.edges())
    nopath = 0
    yespath = 0
    edgeCounts = {0:0,2:0,3:0,4:0,5:0}    
    for fe in falsePositives:
        u,v = fe
        try:
            path = nx.dijkstra_path(refGraph,u,v)
            pathlength = len(path) -1
            yespath +=1
            if pathlength in edgeCounts.keys():
                edgeCounts[pathlength] +=1
            
        except nx.exception.NetworkXNoPath:
            nopath +=1

    edgeCounts['numPred'] = numPredictions
    edgeCounts['numTP'] = len(truePositives)
    edgeCounts['numFP_withPath'] = yespath
    edgeCounts['numFP_noPath'] = nopath
    return edgeCounts