# column 0; but the column really ought to be chosen by a command line # argument, either by position or by column name (in header). name_column = 0 # Extract just the taxon names from the table, as a vector, one entry per row. ids = [] for row in rows: ids.append(row[name_column]) # Invoke the Open Tree TNRS service. # TBD: consider doing fuzzy matches as well (with a high threshold). Not # clear what the user should do for names that don't uniquely resolve. tnrs_return = opentreelib.tnrs_match_names(ids, do_approximate_matching=False) # import pprint # pprint.PrettyPrinter().pprint(tnrs_return) # Process the wad of stuff that the TNRS returned. First, index the # results by taxon name. name_to_matches = {} for lump in tnrs_return['results']: name_to_matches[lump['id']] = lump['matches'] # Now add the OTT id as a new column at the right side of the table. for row in rows: name = row[name_column] if name in name_to_matches:
def unique_ott_id(name): tnrs_result = opentreelib.tnrs_match_names([name]) ottid = tnrs_result['results'][0]['matches'][0]['ot:ottId'] return ottid