Skip to content

Instantly share code, notes, and snippets.

@cplaisier
Forked from ssstrike/TF_TestNetwork.py
Last active March 13, 2018 18:19
Show Gist options
  • Select an option

  • Save cplaisier/4e0572b7de9a15d0ec283cf58d8475c2 to your computer and use it in GitHub Desktop.

Select an option

Save cplaisier/4e0572b7de9a15d0ec283cf58d8475c2 to your computer and use it in GitHub Desktop.
Outputs list of associated Entrez ID to an input Entrez ID
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 12 18:16:24 2018
@author: Fuzzy
"""
import json
with open('tfbsDb_plus_and_minus_5000_entrez.json', 'r') as f:
data = json.load(f)
motif2Id = {}
id2Motif = {}
with open('id_conversion/humanTFs_All.CSV','r') as inFile:
header = inFile.readline().strip().split(',') #get rid of header, strip gets rid of whitespace, split each in element by comma
while 1:
inLine = inFile.readline()
if not inLine:
break
split = inLine.strip().split(',')#create an array with element 0 as key, element 2 as ID
motif2Id[split[0]] = split[2]#store element 0 as key and element 2 as the string
#creating Id to motif, mapping one motif to many Ids, create list inside dictonary
if not split[2] in id2Motif:
id2Motif[split[2]] = []
id2Motif[split[2]].append(split[0])
"""
#attempt to associate a gene Id to many other gene Ids using associated motifs
gene2GeneDB = {}
for geneIn in id2Motif.keys()[0:3]: # range(3):#run through unique gene IDs and set them as geneIn
#geneIn = id2Motif.keys()[i]
motifList = [] #empty list for motif hits
# for loop to search for any hits in humanTFs_All
for x in id2Motif: #range(len(id2Motif)):
if geneIn == x:
motifList = id2Motif[x]
#nested for loop to search for motifs in json file and appends all genes
geneList = []
for x in data:
for y in motifList:
if str(y) == str(x):
geneList += data[x]
#nested for loop to search origonal humanTF list for shared gene IDs
#humanGeneList = []
gene2GeneDB[str(geneIn)] = []
for x in geneList: #range(len(geneList)):
for y in id2Motif: #range(len(id2Motif)):
if x == y:
gene2GeneDB[str(geneIn)].append(str(y))
#humanGeneList.append(id2Motif.keys()[y])
"""
gene2GeneDB = {}
for geneIn in id2Motif.keys()[0:3]: #run through unique gene IDs and set them as geneIn
for motif in id2Motif[geneIn]:
if motif in data:
for geneOut in data[motif]:
if geneOut in id2Motif.keys():
if not geneIn in gene2GeneDB:
gene2GeneDB[geneIn] = []
gene2GeneDB[geneIn].append(geneOut)
#gene2GeneDB = dict(zip(id2Motif.keys()[0:3],[[[geneOut for geneOut in data[motif] if geneOut in id2Motif.keys()] for motif in id2Motif[geneIn] if motif in data] for geneIn in id2Motif.keys()[0:3]]))
print gene2GeneDB
#print (gene2GeneDB[gene2GeneDB.keys()[0:5]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment