import urllib
fec=open("ec.txt","r")  # as a input gets ec number list in a text file
fecre=open("ec_re.txt","w") # as a output file returne a EC- reaction in a text file
fre=open("re.txt","w")# as a output file return all reactions in a text file
lre=[]
while 1:
	text=fec.readline().rstrip('\n')
	if text=="":
		break
	else:
		print(text)		
		url='http://www.genome.jp/dbget-bin/www_bget?ec:'+text
		#print(url)
		f = urllib.urlopen(url)
		myfile = f.read()
		h1=myfile.find('Reaction(IUBMB)')
		h2=myfile.find('Reaction(KEGG)')
		st=myfile[h1:h2]
		h3=st.find('href="/dbget-bin/www_bget?rn:')
		while h3>-1:
        		fecre.write(text)
			fecre.write('\t')	
			#print(st[h3+29:h3+35])
			fecre.write(st[h3+29:h3+35])
			fecre.write('\n')
			if st[h3+29:h3+35] not in lre: 
				lre.append(st[h3+29:h3+35])
			st=st[h3+35:len(st)]	
			h3=st.find('href="/dbget-bin/www_bget?rn:')
fec.close()
fecre.close()
lre.sort()
i=0
while i<len(lre):
	fre.write(lre[i])
	fre.write('\n')
	i+=1
fre.close()
