Skip to content

Instantly share code, notes, and snippets.

@MridulS
Last active July 26, 2016 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MridulS/2a406ed5c2a465531b5e924af41e3eca to your computer and use it in GitHub Desktop.
Save MridulS/2a406ed5c2a465531b5e924af41e3eca to your computer and use it in GitHub Desktop.
from urllib import urlopen
import subprocess
import pandas as pd
from xml.etree import ElementTree as ET
from time import sleep
import datetime
import base64
root = ET.parse(urlopen('http://webservice.wikipathways.org/listPathways')).getroot()
list_of_pathways = [list(i)[0].text for i in root]
pathway_names = [list(i)[2].text.replace(" ", "_") for i in root]
pathway_species = [list(i)[3].text.replace(" ", "_") for i in root]
api_out = []
api = []
fails = []
more = []
more1 = []
trash = []
trash1 = []
count = 0
for pathway, pathway_name, specie in zip(list_of_pathways, pathway_names, pathway_species):
if pathway == 'WP3617':
try:
temp = ET.parse(urlopen('http://webservice.wikipathways.org/getPathway?pwId={}'.format(pathway))).getroot()
for i in temp:
gpml_text = i[0].text
gpml_text = base64.b64decode(gpml_text)
name = i[1].text
with open("output.gpml", "w") as text_file:
text_file.write(gpml_text)
file = "http://159.203.176.220/api/users/upload2@test.com/graph/add/{}_{}_{}_api.gpml/".format(name, pathway_name, specie)
location = 'graphname=@"/home/test/output.gpml"'
proc = subprocess.Popen(["curl", "-X", "POST", file, "-F", "username=upload2@test.com", "-F", "password=test" , "-F", location], stdout=subprocess.PIPE)
(out, err) = proc.communicate()
print(out)
if out[-6:] == b' 400\n}':
more.append(out)
more1.append(pathway)
elif out[-6:] == b' 201\n}':
api_out.append(out)
api.append(pathway)
else:
trash.append(out)
trash1.append(pathway)
except:
fails.append(pathway)
print pathway, count
count += 1
print datetime.datetime.now()
sleep(0.3)
# success = pd.DataFrame({'WP': api, 'Return Message': api_out})
# failure = pd.DataFrame({'WP': more1, 'Return Message': more})
# trash = pd.DataFrame({'WP': trash1, 'Return Message': trash})
# api_fail = pd.DataFrame({'WP':fails, 'Return Message': 'API fail'})
# result = pd.concat([success, failure, trash, api_fail])
# result.to_csv('testing.csv')
# print '========================'
# with open("list.txt", "w") as text_file:
# text_file.write("{0}".format(list_of_pathways))
# with open("failure_due_toerror.txt", "w") as text_file:
# text_file.write("{0} \n {1}".format(more, more1))
# with open("something.txt", "w") as text_file:
# text_file.write("{0} \n {1}".format(trash, trash1))
# with open("notabletoparse.txt", "w") as text_file:
# text_file.write("{0} ".format(fails))
# with open("api_out.txt", "w") as text_file:
# text_file.write("{0} \n {1}".format(api_out, api))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment