Skip to content

Commit 6cc775b

Browse files
author
Will Trimble
committed
style refactoring
1 parent dca84d6 commit 6cc775b

1 file changed

Lines changed: 34 additions & 31 deletions

File tree

scripts/mg-download.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ def file_download(auth, info, dirpath="."):
4343
fhandle = open(os.path.join(dirpath, info['file_name']), 'w')
4444
file_from_url(info['url'], fhandle, auth=auth)
4545
fhandle.close()
46-
else: # don't open empty file is no download
47-
sys.stderr.write("WARNING Download info does not contain url. Possibly datasets suppressed")
46+
else: # Don't open empty file if download doesn't have url
47+
sys.stderr.write("WARNING Download info does not contain url. Possibly datasets pre- human screening?\n" + repr(info)+"\n")
4848
sys.stdout.write("Done\n")
4949

5050
def main(args):
@@ -60,39 +60,44 @@ def main(args):
6060
parser.add_argument("--file", dest="file", default=None, help="file ID for given project or metagenome")
6161
parser.add_argument("--dir", dest="dir", default=".", help="directory to do downloads")
6262
parser.add_argument("--list", dest="list", action="store_true", default=False, help="list files and their info for given ID")
63-
63+
6464
# get inputs
6565
opts = parser.parse_args()
66-
if not (opts.project or opts.metagenome):
66+
PROJECT = opts.project
67+
DOWNDIR = opts.dir
68+
METAGENOME = opts.metagenome
69+
LIST = opts.list
70+
FILE = opts.file
71+
URL = opts.url
72+
73+
if not (PROJECT or METAGENOME):
6774
sys.stderr.write("ERROR: a project or metagenome id is required\n")
6875
return 1
69-
if not os.path.isdir(opts.dir):
70-
sys.stderr.write("ERROR: dir '%s' does not exist\n"%opts.dir)
76+
if not os.path.isdir(DOWNDIR):
77+
sys.stderr.write("ERROR: dir '%s' does not exist\n"%DOWNDIR)
7178
return 1
72-
downdir = opts.dir
73-
79+
7480
# get auth
7581
token = get_auth_token(opts)
76-
82+
7783
# get metagenome list
7884
mgs = []
79-
if opts.project:
80-
url = opts.url+'/project/'+opts.project+'?verbosity=full'
85+
if PROJECT:
86+
url = URL +'/project/'+PROJECT+'?verbosity=full'
8187
data = obj_from_url(url, auth=token)
8288
for mg in data['metagenomes']:
8389
mgs.append(mg["metagenome_id"])
84-
elif opts.metagenome:
85-
mgs.append(opts.metagenome)
86-
90+
elif METAGENOME:
91+
mgs.append(METAGENOME)
8792
# get file lists
8893
all_files = {}
8994
for mg in mgs:
90-
url = opts.url+'/download/'+mg
95+
url = URL + '/download/' + mg
9196
data = obj_from_url(url, auth=token)
9297
all_files[mg] = data['data']
93-
98+
9499
# just list
95-
if opts.list:
100+
if LIST:
96101
pt = PrettyTable(["Metagenome", "File Name", "File ID", "Checksum", "Byte Size"])
97102
for mg, files in all_files.items():
98103
for f in files:
@@ -102,32 +107,30 @@ def main(args):
102107
pt.align['Byte Size'] = "r"
103108
print(pt)
104109
return 0
105-
110+
106111
# download all in dirs by ID
107-
if opts.project:
108-
downdir = os.path.join(downdir, opts.project)
109-
if not os.path.isdir(downdir):
110-
os.mkdir(downdir)
112+
if PROJECT:
113+
DOWNDIR = os.path.join(DOWNDIR, PROJECT)
114+
if not os.path.isdir(DOWNDIR):
115+
os.mkdir(DOWNDIR)
111116
for mg, files in all_files.items():
112-
mgdir = os.path.join(downdir, mg)
117+
mgdir = os.path.join(DOWNDIR, mg)
113118
if not os.path.isdir(mgdir):
114119
os.mkdir(mgdir)
115120
for f in files:
116-
if opts.file:
121+
if FILE:
117122
filecount = 0
118-
if f['file_id'] == opts.file:
119-
filecount +=1
123+
if f['file_id'] == FILE:
124+
filecount += 1
120125
file_download(token, f, dirpath=mgdir)
121-
elif f['file_name'] == opts.file:
122-
filecount +=1
126+
elif f['file_name'] == FILE:
127+
filecount += 1
123128
file_download(token, f, dirpath=mgdir)
124129
else:
125130
file_download(token, f, dirpath=mgdir)
126131
if filecount == 0:
127-
sys.exit("Can't find file number "+opts.file)
128-
132+
sys.exit("Didn't find file number " + FILE)
129133
return 0
130134

131-
132135
if __name__ == "__main__":
133136
sys.exit(main(sys.argv))

0 commit comments

Comments
 (0)