<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import sys
from collections import *
import csv
import copy
import networkx as nx
import json
from networkx.readwrite import json_graph

IdDict = defaultdict() #Gives you the ID number with a gene input
GeneDict = defaultdict() #gives you a gene with ID number input
AlreadyCreated = set() #All the nodes created in the graph
graph = nx.DiGraph() # creates the graph object in NetworkX

def Read_And_Generate_Graph_Object():
    FileReader = open('../data/PPI_File.csv')
    readObject = csv.reader(FileReader, delimiter = ',')
    for x in xrange(0, 8):
        next(readObject) # skips first line
    #reads and scans file, creating dictionary
    ReactionGeneDictionary = defaultdict(set) # holds all the matches and type of reaction two genes have
    InteractionDict = defaultdict()
    #OutlierList = Create_Outliers_List()
    IDNum = 0 # id num
    for row in readObject: # reads through and fills accordingly
        gene1 = row[0] # The first gene
        gene2 = row[2] # the second gene
        value = row[4] # the kind of reaction it is (ex. PPI)
        if ((gene1 not in AlreadyCreated)):
            graph.add_node(IDNum,label=str(gene1),type="gene") # creates the respective node
            AlreadyCreated.add(gene1)
            IdDict[gene1] = IDNum
            GeneDict[IDNum] = gene1
            IDNum += 1
        if ((gene2 not in AlreadyCreated)):
            graph.add_node(IDNum,label=str(gene2),type="gene") # creates the respective node
            AlreadyCreated.add(gene2)
            IdDict[gene2] = IDNum
            GeneDict[IDNum] = gene2
            IDNum += 1
        ReactionGeneDictionary[gene1].add(gene2)
        InteractionDict[(gene1,gene2)] = value
    made = set()
    keyList = ReactionGeneDictionary.keys()
    for key in keyList: #creates all the edges
        for otherGene in ReactionGeneDictionary[key]:
            if((otherGene in AlreadyCreated) and (key in AlreadyCreated)):
                graph.add_edge(IdDict[key], IdDict[otherGene])
                if(int(InteractionDict[(key,otherGene)])%2 != 0):
                    graph.add_edge(IdDict[otherGene], IdDict[key])
                made.add((IdDict[key], IdDict[otherGene]))			
    # gmlFile = open("CheckIt.gml", 'w')
    # nx.write_gml(graph, gmlFile)
    # gmlFile.close()

#reads in an puts into a list the outliers of the data source
def Create_Outliers_List():
    tempListOfGenes=[]
    try:
        FileReader = open("../data/OutlierGenes.txt", 'r')
        #reads and scans file, creating dictionary
        for row in FileReader: # reads thro=ugh and fills accordingly
            tempListOfGenes.append(row.strip("\n"))
            
        FileReader.close()
    except IOError:   
       print "\nSorry, no such file exists" 	   
    return tempListOfGenes
    
#generates the shortest paths between two genes
def Create_Path_Shortest(geneOI1, geneOI2):
    geneList = []
    tempone = temptwo = -1
    try: # try except used to prevent breaking of program
        tempone = IdDict[geneOI1]
    except KeyError:
        print "\n" + geneOI1 + " is not in this data source."
    try: # try except used to prevent breaking of program
        temptwo = IdDict[geneOI2]
    except KeyError:
        print "\n" + geneOI2 + " is not in this data source."
    if(tempone &gt;= 0 and temptwo &gt;= 0):
        try: # using try to prevent crashes
            pathlist = nx.all_shortest_paths(graph, tempone, temptwo) # obtains the pathways and creates a list of genes
            for path in pathlist:
                templist = []
                for node in path:
                    templist.append(GeneDict[node])
                geneList.append(templist)
        except nx.NetworkXException:
            print ("\nNo path exists between given genes.")
    print geneList
    return geneList
	
def Create_Path_Shortest_With_Key(gene1, gene2, key):
    length = 0	
    try:
	section1 = nx.shortest_path_length(graph, gene1, IdDict[key])
	section2 = nx.shortest_path_length(graph, gene2, IdDict[key])
	length = section1+section2
    except:
	return 0
    return length
		
#generates all possible paths between two genes
def Create_Path_Simple(geneOI1, geneOI2):
    geneList = []
    tempone = temptwo = -1
    try: # try except used to prevent breaking of program
        tempone = IdDict[geneOI1]
    except KeyError:
        print "\n" + geneOI1 + " is not in this data source."
    try: # try except used to prevent breaking of program
        temptwo = IdDict[geneOI2]
    except KeyError:
        print "\n" + geneOI2 + " is not in this data source."
    if(tempone &gt;= 0 and temptwo &gt;= 0):
        try: # using try to prevent crashes
            pathlist = nx.all_simple_paths(graph, tempone, temptwo) # obtains the pathways and creates a list of genes
            for path in pathlist:
                templist = []
                for node in path:
                    templist.append(GeneDict[node])
                geneList.append(templist)
        except nx.NetworkXException:
            return ("\nNo path exists between given genes.")
    return geneList

#creates paths between two genes given a maximum path length	
def Create_Path_Given_Length(geneOI1, geneOI2, n):
    geneList = []
    tempone = temptwo = -1
    try: # try except used to prevent breaking of program
        tempone = IdDict[geneOI1]
    except KeyError:
        print "\n" + geneOI1 + " is not in this data source."
    try: # try except used to prevent breaking of program
        temptwo = IdDict[geneOI2]
    except KeyError:
        print "\n" + geneOI2 + " is not in this data source."
    if(tempone &gt;= 0 and temptwo &gt;= 0):
        try: # using try to prevent crashes
            size = nx.shortest_path_length(graph, tempone, temptwo) # obtains the pathways and creates a list of genes
            pathlist = nx.all_simple_paths(graph, tempone, temptwo, size+n) # obtains the pathways and creates a list of genes
            for path in pathlist:
                templist = []
                for node in path:
                    templist.append(GeneDict[node])
                geneList.append(templist)
        except nx.NetworkXException:
            return ("\nNo path exists between given genes.")
    return geneList
	    
def Create_Path_Shortest_With_Key(gene1, gene2, key):
    length = 0
    try:	
	section1 = nx.shortest_path_length(graph, gene1, key)
	section2 = nx.shortest_path_length(graph, gene2, key)
	length = section1+section2
    
    except:
	return 0
    
    return length
    
#gets rid of 5 genes that are not part of
#the bigger connected component
def Discard_Genes(listOfGenes):
    temp = []
    endResult = ""
    for gene in listOfGenes:
        if gene in AlreadyCreated:
	        temp.append(gene)
        else:
            endResult += (gene + " is not in the data source.\n")
    print endResult

    return temp
Read_And_Generate_Graph_Object()
print "Connected components: ", nx.number_weakly_connected_components(graph)
print "\n\n"
temp = 1
for sub in nx.weakly_connected_component_subgraphs(graph):
    print "Connected component ", temp, ":", nx.number_of_nodes(sub)
    temp += 1</pre></body></html>