Skip to content

Instantly share code, notes, and snippets.

@lmmx
Last active August 29, 2015 14:07
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 lmmx/dc8d4974c8d1eddb0a65 to your computer and use it in GitHub Desktop.
Save lmmx/dc8d4974c8d1eddb0a65 to your computer and use it in GitHub Desktop.
Script writer for PDBe PISA downloads and example to create the first output file: interfaces1-1.xml, and the download script to initiate proceedings once scripts were written.
#!/usr/bin/python
with open("interfaces.txt") as urllist:
sect = int(outputfile.name.split('-')[0][-1:])
subsect = int(outputfile.name.split('-')[1].split('.')[0])
pdbcodes = re.findall('pdb_code>(.*)<', outputfile.read())
num = len(pdbcodes)
print str(num)+" interfaces have been written to file"
startline = num/50 + (sect-1)*500 + (subsect-1)*50 + 1
subsectendline = (sect-1)*500 + (subsect-1)*50 + 50
now = datetime.now()
if num/50 < 50:
print str(now.hour)+":"+str(now.minute)+" - Starting at line "+str(startline)
urltup = tuple(urllist)
for line in urltup:
if urltup.index(line)+1 >= startline and urltup.index(line)+1 <= subsectendline:
url = line.rstrip()
try:
s = urllib2.urlopen(url)
except urllib2.HTTPError, err:
import sys
from subprocess import call
if sys.platform == 'linux2':
call(["mpg123","Pause-Mario.mp3"])
elif sys.platform == 'darwin':
call(["afplay","Pause-Mario.mp3"])
break
contents = s.read()
outputfile.write(contents)
now = datetime.now()
print str(now.hour)+":"+str(now.minute)+" - "+str(urltup.index(line)+1)+" Downloaded "+url.split('?')[1]
elif urltup.index(line) == subsectendline:
import sys
from subprocess import call
if sys.platform == 'linux2':
call(["mpg123","Coin-Mario.mp3"])
elif sys.platform == 'darwin':
call(["afplay","Coin-Mario.mp3"])
else:
print "All interfaces downloaded."
#!/usr/bin/python
try:
import urllib2
import re
from datetime import datetime
with open("interfaces1-1.xml","r+") as outputfile:
execfile("download.py")
except KeyboardInterrupt:
pass
import os
for filename in os.listdir('.'):
if len(filename.split('getxml'))>1:
with open(filename,'w') as outputfile:
spl = filename.split('-')
sect = spl[0][-1:]
subsect = spl[1].split('.')[0]
interfacefile = 'interfaces'+sect+'-'+subsect+'.xml'
scriptout = '#!/usr/bin/python\n\ntry:\n\timport urllib2\n\timport re\n\tfrom datetime import datetime\n\twith open("'+interfacefile+'","r+") as outputfile:\n\t\texecfile("download.py")\nexcept KeyboardInterrupt:\n\tpass\n'
outputfile.write(scriptout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment