Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os,csv
#parser for CSV download of https://spreadsheets1.google.com/ccc?key=toMz3Kp3T2EAUDF3MSGCquw&hl=en#gid=0
# extract pairwise combinations of compounds in the same ReactionID
# use this combinations to define edges in a graph viewable in Gephi
#recent versions of py include this as itertools.combinations()
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
fr3=open('csid2csid.gdf','w')
anon={}
reactionCompounds={}
reactions=csv.reader(open('ReactionAttempts.csv'))
fr3.write("nodedef> name VARCHAR,label VARCHAR, type VARCHAR, labbook VARCHAR\n")
unique=[]
firstrow=True
for r in reactions:
if firstrow is False:
ReactionID,hyperlink,CompoundName,CSID,SMILES,Role,Type,SolventPredict=r
if ReactionID not in unique:
reactionCompounds[ReactionID]=[]
unique.append(ReactionID)
if CSID not in unique:
unique.append(CSID)
if CompoundName !='':
name=CompoundName
else:
name=CSID
fr3.write('"'+CSID+'","'+CompoundName+'","Compound","'+hyperlink+'"\n')
if hyperlink not in unique:
unique.append(hyperlink)
reactionCompounds[ReactionID].append(CSID)
else:
firstrow=False
fr3.write("edgedef> node1 VARCHAR,node2 VARCHAR, reaction VARCHAR\n")
for r in reactionCompounds:
pairs=combinations(reactionCompounds[r],2)
for p in pairs:
fr3.write('"'+p[0]+'","'+p[1]+'","'+r+'"\n')
fr3.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.