bin/bactopia/bactopia-datasets.py
def validate_species(species):
    """Query input species against ENA to determine if it exists."""
    import requests
    ENDPOINT = 'https://www.ebi.ac.uk/ena/data/taxonomy/v1/taxon/any-name'
    checks = []

    if os.path.exists(species):
        with open(species, 'r') as handle:
            for line in handle:
                line = line.rstrip()
                if line:
                    checks.append(line)
    elif "," in species:
        checks = species.split(',')
    else:
        checks.append(species)
    
    species_key = {}
    for species in checks:
        species = species.strip()
        r = requests.get(f'{ENDPOINT}/{species}?limit=1')
        if r.status_code == requests.codes.ok:
            try:
                json_data = r.json()
                scientific_name = json_data[0]['scientificName'].replace('[', '').replace(']', '')
                if scientific_name.lower() != species.lower():
                    # Error! Species/Organism found, but doesn't match input. This shouldn't
                    # (query is case-insensitive exact match) happen, but my grandma could "
                    # probably trigger it, so here it is!
                    logging.error((f'Input species ({species}) does not match return result '
                                f'({scientific_name}), please check spelling.'))
                    sys.exit(1)
                
                species_key[species.lower()] = scientific_name
                logging.info(f'{species} verified in ENA Taxonomy database')
            except json.decoder.JSONDecodeError:
                if r.text == "No results.":
                    logging.error(f'Input species ({species}) not found, please check spelling.')
                    sys.exit(1)
        else:
            # Error! Species/Organism not found. Check spelling?
            # TODO: Implement"Did you mean?" function
            logging.error(f'Input species ({species}) not found, please check spelling.')
            sys.exit(1)

    return species_key
Query input species against ENA to determine if it exists.
Query input species against ENA to determine if it exists.
Query input species against ENA to determine if it exists .
def validate_species(species):
    import requests
    ENDPOINT = 'https://www.ebi.ac.uk/ena/data/taxonomy/v1/taxon/any-name'
    checks = []
    if os.path.exists(species):
        with open(species, 'r') as handle:
            for line in handle:
                line = line.rstrip()
                if line:
                    checks.append(line)
    elif "," in species:
        checks = species.split(',')
    else:
        checks.append(species)
    species_key = {}
    for species in checks:
        species = species.strip()
        r = requests.get(f'{ENDPOINT}/{species}?limit=1')
        if r.status_code == requests.codes.ok:
            try:
                json_data = r.json()
                scientific_name = json_data[0]['scientificName'].replace('[', '').replace(']', '')
                if scientific_name.lower() != species.lower():
                    logging.error((f'Input species ({species}) does not match return result '
                                f'({scientific_name}), please check spelling.'))
                    sys.exit(1)
                species_key[species.lower()] = scientific_name
                logging.info(f'{species} verified in ENA Taxonomy database')
            except json.decoder.JSONDecodeError:
                if r.text == "No results.":
                    logging.error(f'Input species ({species}) not found, please check spelling.')
                    sys.exit(1)
        else:
            logging.error(f'Input species ({species}) not found, please check spelling.')
            sys.exit(1)
    return species_key
def validate_species ( species ) : import requests ENDPOINT = 'https://www.ebi.ac.uk/ena/data/taxonomy/v1/taxon/any-name' checks = [ ] if os . path . exists ( species ) : with open ( species , 'r' ) as handle : for line in handle : line = line . rstrip ( ) if line : checks . append ( line ) elif "," in species : checks = species . split ( ',' ) else : checks . append ( species ) species_key = { } for species in checks : species = species . strip ( ) r = requests . get ( f'{ENDPOINT}/{species}?limit=1' ) if r . status_code == requests . codes . ok : try : json_data = r . json ( ) scientific_name = json_data [ 0 ] [ 'scientificName' ] . replace ( '[' , '' ) . replace ( ']' , '' ) if scientific_name . lower ( ) != species . lower ( ) : logging . error ( ( f'Input species ({species}) does not match return result ' f'({scientific_name}), please check spelling.' ) ) sys . exit ( 1 ) species_key [ species . lower ( ) ] = scientific_name logging . info ( f'{species} verified in ENA Taxonomy database' ) except json . decoder . JSONDecodeError : if r . text == "No results." : logging . error ( f'Input species ({species}) not found, please check spelling.' ) sys . exit ( 1 ) else : logging . error ( f'Input species ({species}) not found, please check spelling.' ) sys . exit ( 1 ) return species_key
Query input species against ENA to determine if it exists.
Query input species against ENA to determine if it exists .
"""Query input species against ENA to determine if it exists.""" # Error! Species/Organism found, but doesn't match input. This shouldn't # (query is case-insensitive exact match) happen, but my grandma could " # probably trigger it, so here it is! # Error! Species/Organism not found. Check spelling? # TODO: Implement"Did you mean?" function
species