Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #647 from adamrp/update_metadata_getter
Browse files Browse the repository at this point in the history
Update metadata getter
  • Loading branch information
ElDeveloper committed Mar 20, 2014
2 parents 45c78ee + 5c863a3 commit 8931ca5
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions python_code/scripts/ag_get_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
script_info['script_description'] = (
"Connects to the QIIME metadata database using the web app's "
"ag_data_access.AGGetBarcodeMetadata. This function is called once per "
"barcodes in the input file."
"barcodes in the input file.\nNote that at least one of -b or -i must "
"be supplied. If both are supplied, the two lists will be concatenated."
)

script_info['script_usage'] = [("","","")]
Expand All @@ -42,16 +43,19 @@
)

script_info['required_options'] = [
make_option('-i', '--input_barcodes_file', type='existing_filepath',
help="The input file containing barcodes, one per line"),
make_option('-o', '--output_file', type='new_filepath',
help="The output file, to which metadata will be written")

]

script_info['optional_options'] = [
make_option('-H', '--omit_headers', action='store_true',
help="Do not print column headers as the first line")
help="Do not print column headers as the first line"),
make_option('-i', '--input_barcodes_file', type='existing_filepath',
help="The input file containing barcodes, one per line."),
make_option('-b', '--barcodes', type=str,
help="A comma separated list of barcodes to fetch. At least "
"one of -b or -i must be supplised.")
]

script_info['version'] = __version__
Expand Down Expand Up @@ -79,6 +83,7 @@ def main():
option_parser, opts, args = parse_command_line_parameters(**script_info)

input_fp = opts.input_barcodes_file
barcodes = opts.barcodes
output_fp = opts.output_file
print_headers = not opts.omit_headers

Expand Down Expand Up @@ -129,11 +134,22 @@ def main():
'NITROMIDAZOLE', 'PENICILLIN', 'SULFA_DRUG', 'CEPHALOSPORIN'
]

all_barcodes = []

if input_fp is None and barcodes is None:
option_parser.error("Must supply either -i or -b")

if input_fp is not None:
all_barcodes = [x.strip() for x in open(input_fp).readlines()]

if barcodes is not None:
all_barcodes.extend(barcodes.split(','))

with open(output_fp, 'w') as out_file:
if print_headers:
out_file.write('\t'.join(headers) + '\n')

for metadata in get_ag_metadata_bulk(open(input_fp, 'U')):
for metadata in get_ag_metadata_bulk(all_barcodes):
line = '\t'.join([str(metadata[header]) for header in headers])
line += '\n'
out_file.write(line)
Expand Down

0 comments on commit 8931ca5

Please sign in to comment.