def func(x, y, checklist): url = "https://www.catalogueoflife.org/col/webservice" if checklist is None: pass else: checklist = str(checklist) if checklist in ["2012", "2011", "2010"]: url = re.sub("col", "annual-checklist/" + checklist, url) else: url = "https://www.catalogueoflife.org/annual-checklist/year/webservice" url = re.sub("year", checklist, url) payload = { "name": x, "id": y, "format": format, "response": "full", "start": start, } payload = {k: v for k, v in payload.items() if v is not None} tt = Refactor(url, payload, request="get").xml() childtaxa = tt.xpath("//child_taxa//taxon") if len(childtaxa) == 0: sys.exit("Please enter a valid search name") outlist = [] for i in range(len(childtaxa)): tt_ = childtaxa[i].getchildren() outlist.append( dict(zip(["id", "name", "rank"], [x.text for x in tt_[:3]]))) return outlist
def _itisterms(endpt, args={}, **kwargs): ''' Get itis terms Usage: pytaxize._itisterms(x="buya") ''' out = Refactor(itis_base + endpt, payload=args, request='get').xml(**kwargs) nodes = out.xpath("//ax21:itisTerms", namespaces=ns21) nodes2 = [x.getchildren() for x in nodes] allnodes = [[_get_text_single(y) for y in x] for x in nodes2] output = [] for x in allnodes: kyz = [y.keys()[0] for y in x] notuniq = set([v for v in kyz if kyz.count(v) > 1]) if len(notuniq) > 0: for z in notuniq: tt = ','.join([ m.values()[0] for m in x if m.keys()[0] == z ]) toadd = { z: tt } uu = [ v for v in x if v.keys()[0] not in z ] uu.append(toadd) output.append(uu) else: output.append(x) df = pd.concat([pd.DataFrame([y.values()[0] for y in x]).transpose() for x in output]) df.columns = [x.keys()[0] for x in allnodes[0]] return df
def currency(tsn, as_dataframe=False, **kwargs): """ Get currency from tsn :param tsn: (int) TSN for a taxonomic group :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis # currency data itis.currency(28727) # no currency dat itis.currency(526852) # as data_frame itis.currency(526852, as_dataframe=True) """ out = Refactor(itis_base + "getCurrencyFromTSN", payload={ "tsn": tsn }, request="get").json(**kwargs) out.pop("class") return _df(out, as_dataframe)
def searchforanymatchpaged(x, pagesize, pagenum, ascend, **kwargs): ''' Search for any matched page for descending (logical) Usage:: pytaxize.searchforanymatchpaged(x=202385, pagesize=100, pagenum=1, ascend=False) pytaxize.searchforanymatchpaged("Zy", pagesize=100, pagenum=1, ascend=False) ''' args = { 'srchKey': x, 'pageSize': pagesize, 'pageNum': pagenum, 'ascend': ascend } out = Refactor(itis_base + 'searchForAnyMatchPaged', payload=args, request='get').xml(**kwargs) tmp = out.getchildren()[0].getchildren() output = [] for v in tmp: tmp = v.getchildren() for w in tmp: output.append( dict( zip([gettag(e) for e in w.iter()], [e.text for e in w.iter()]))) return output
def _itisterms(endpt, args={}, **kwargs): ''' Get itis term Usage:: pytaxize._itisterms("buya") ''' out = Refactor(itis_base + endpt, payload=args, request='get').xml(**kwargs) nodes = out.xpath("//ax21:itisTerms", namespaces=ns21) nodes2 = [x.getchildren() for x in nodes] allnodes = [[_get_text_single(y) for y in x] for x in nodes2] output = [] for x in allnodes: kyz = [y.keys()[0] for y in x] notuniq = set([v for v in kyz if kyz.count(v) > 1]) if len(notuniq) > 0: for z in notuniq: tt = ','.join([m.values()[0] for m in x if m.keys()[0] == z]) toadd = {z: tt} uu = [v for v in x if v.keys()[0] not in z] uu.append(toadd) output.append(uu) else: output.append(x) df = pd.DataFrame([{k: v for d in R for k, v in d.items()} for R in output]) return df[[x.keys()[0] for x in allnodes[0]]]
def getacceptednamesfromtsn(tsn, **kwargs): ''' Get accepted names from tsn :param tsn: taxonomic serial number (TSN) (character or numeric) Usage:: # TSN accepted - good name pytaxize.getacceptednamesfromtsn('208527') # TSN not accepted - input TSN is old name pytaxize.getacceptednamesfromtsn('504239') ''' out = Refactor(itis_base + 'getAcceptedNamesFromTSN', payload={ 'tsn': tsn }, request='get').xml(**kwargs) temp = out.getchildren() if (temp[0].getchildren()[1].values()[0] == 'true'): dat = temp[0].getchildren()[0].text else: nodes = temp[0].getchildren()[1].getchildren() dat = _parse_nodes(nodes) dat.pop('author') dat['submittedTsn'] = temp[0].getchildren()[0].text return dat
def searchforanymatch(x, **kwargs): ''' Search for any match Usage:: pytaxize.searchforanymatch(x=202385) pytaxize.searchforanymatch(x="dolphin") ''' out = Refactor(itis_base + 'searchForAnyMatch', payload={ 'srchKey': x }, request='get').xml(**kwargs) # if isinstance(x, basestring): tmp = out.getchildren()[0].getchildren() output = [] for v in tmp: tmp = v.getchildren() for w in tmp: output.append( dict( zip([gettag(e) for e in w.iter()], [e.text for e in w.iter()]))) return output
def jurisdictional_origin(tsn, as_dataframe=False, **kwargs): """ Get jurisdictional origin from tsn :param tsn: (int) TSN for a taxonomic group :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis itis.jurisdictional_origin(180543) itis.jurisdictional_origin(180543, True) """ out = Refactor( itis_base + "getJurisdictionalOriginFromTSN", payload={ "tsn": tsn }, request="get", ).json(**kwargs) out.pop("class") if out["jurisdictionalOrigins"][0] is not None: [z.pop("class") for z in out["jurisdictionalOrigins"]] return _df(out["jurisdictionalOrigins"], as_dataframe)
def func(x, y): url = "http://www.catalogueoflife.org/col/webservice" if (checklist.__class__.__name__ == 'NoneType'): pass else: if (checklist in ['2012', '2011', '2010']): url = re.sub("col", "annual-checklist/" + checklist, url) else: url = "http://www.catalogueoflife.org/annual-checklist/year/webservice" url = re.sub("year", checklist, url) payload = { 'name': x, 'id': y, 'format': format, 'response': "full", 'start': start } tt = Refactor(url, payload, request='get').xml() childtaxa = tt.xpath('//child_taxa//taxon') if len(childtaxa) == 0: sys.exit('Please enter a valid search name') outlist = [] for i in range(len(childtaxa)): tt_ = childtaxa[i].getchildren() outlist.append( dict(zip(['id', 'name', 'rank'], [x.text for x in tt_[:3]]))) return outlist
def hierarchy_down(tsn, as_dataframe=False, **kwargs): """ Get hierarchy down from tsn :param tsn: (int) TSN for a taxonomic group :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis itis.hierarchy_down(tsn = 179913) itis.hierarchy_down(tsn = 161994) itis.hierarchy_down(tsn = 9999999) """ tt = Refactor(itis_base + "getHierarchyDownFromTSN", payload={ "tsn": tsn }, request="get").json(**kwargs) tt.pop("class") if tt["hierarchyList"]: pass [z.pop("class") for z in tt["hierarchyList"] if z is not None] return _df(tt["hierarchyList"], as_dataframe)
def func(x, y): url = "http://www.catalogueoflife.org/col/webservice" if(checklist.__class__.__name__ == 'NoneType'): pass else: if(checklist in ['2012','2011','2010']): url = re.sub("col", "annual-checklist/" + checklist, url) else: url = "http://www.catalogueoflife.org/annual-checklist/year/webservice" url = re.sub("year", checklist, url) payload = {'name': x, 'id': y, 'start': start} tt = Refactor(url, payload, request='get').xml() stuff = tt.xpath('//result') outlist = [] for i in range(len(stuff)): tt_ = stuff[i] each = {} for g in range(len(tt_)): for e in tt_[g].iter(): each.update({e.tag: e.text}) outlist.append(each) # values = [x.text for x in tt_[:4]] # tags = [x.tag for x in tt_[:4]] # mydict = dict(zip(tags, values)) df = pd.DataFrame(outlist) # tt_ = stuff[0].getchildren() # res = [x.text for x in tt_[:4]] return df
def getjurisdictionvalues(**kwargs): ''' Get possible jurisdiction values Usage: pytaxize.getjurisdictionvalues() ''' out= Refactor(itis_base + 'getJurisdictionValues', payload={}, request='get').xml(**kwargs) vals = [ x.text for x in out.getchildren()[0].getchildren() ] return pd.DataFrame(vals, columns = ['jurisdictionValues'])
def searchcol(x): payload = {'name':x, 'format':format, 'response':"full", 'start':start} tt = Refactor(url, payload, request='get').xml() childtaxa = tt.xpath('//child_taxa//taxon') outlist = [] for i in range(len(childtaxa)): tt_ = childtaxa[i].getchildren() outlist.append([x.text for x in tt_[:3]]) df = pd.DataFrame(outlist, columns=['id','name','rank']) return df
def _gnr_resolve( names="H**o sapiens", source=None, format="json", resolve_once="false", with_context="false", best_match_only="false", header_only="false", preferred_data_sources="false", http="get", ): url = "https://resolver.globalnames.org/name_resolvers" payload = { "data_source_ids": source, "format": format, "resolve_once": resolve_once, "with_context": with_context, "best_match_only": best_match_only, "header_only": header_only, "preferred_data_sources": preferred_data_sources, } if names.__class__.__name__ == "list": if len(names) > 300 and http == "get": http = "post" else: names = "|".join(names) payload["names"] = names else: payload["names"] = names if http == "get": result_json = Refactor(url, payload, request="get").json() else: if names.__class__.__name__ != "list": result_json = Refactor(url, payload, request="post").json() else: with open("names_list.txt", "w") as f: for name in names: f.write(name + "\n") f.close() result_json = Refactor(url, payload, request="post").json( files={"file": open("names_list.txt", "rb")} ) while result_json["status"] == "working": result_url = result_json["url"] time.sleep(10) result_json = Refactor(result_url, payload={}, request="get").json() os.remove("names_list.txt") data = [] for each_result in result_json["data"]: data.append(each_result["results"] if "results" in each_result else []) return data
def getanymatchcount(x, **kwargs): ''' Get any match count. :param x: text or taxonomic serial number (TSN) (character or numeric) :param **kwargs: optional additional curl options (debugging tools mostly) Usage: pytaxize.getanymatchcount(x=202385) pytaxize.getanymatchcount(x="dolphin") ''' out = Refactor(itis_base + 'getAnyMatchCount', payload={'srchKey': x}, request='get').xml(**kwargs) return int(out.getchildren()[0].text)
def searchbycommonnameendswith(x, **kwargs): ''' Search for tsn by common name ending with Usage: pytaxize.searchbycommonnameendswith("snake") ''' out= Refactor(itis_base + 'searchByCommonNameEndsWith', payload={'srchKey':x}, request='get').xml(**kwargs) matches = ["commonName","language","tsn"] tmp = out.xpath('//ax21:commonNames', namespaces=ns21) return _itisdf(tmp[0], ns21, matches, _tolower(matches))
def ncbi(self, x, **kwargs): if x is None: return [] key = os.environ.get("ENTREZ_KEY") if key is None: raise Exception("ENTREZ_KEY is not defined") query = {"db": "taxonomy", "ID": x, "api_key": key} url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi" res = Refactor(url, query, "get").xml(**kwargs) z = res.xpath("//TaxaSet/Taxon/OtherNames/GenbankCommonName") return [w.text for w in z]
def searchbycommonname(x, **kwargs): ''' Search for tsn by common name Usage: pytaxize.searchbycommonname(x="american bullfrog") pytaxize.searchbycommonname("ferret-badger") pytaxize.searchbycommonname("polar bear") ''' out= Refactor(itis_base + 'searchByCommonName', payload={'srchKey':x}, request='get').xml(**kwargs) matches = ["commonName","language","tsn"] tmp = out.xpath('//ax21:commonNames', namespaces=ns21) return _itisdf(tmp[0], ns21, matches, _tolower(matches))
def getcredibilityratings(**kwargs): ''' Get possible credibility ratings :param **kwargs: Curl options passed on to \code{\link[httr]{GET}} Usage: pytaxize.getcredibilityratings() ''' out = Refactor(itis_base + 'getCredibilityRatings', payload={}, request='get').xml(**kwargs) nodes = out.xpath("//ax23:credibilityValues", namespaces=ns23) credibilityValues = [x.text for x in nodes] df = pd.DataFrame(credibilityValues, columns=['credibilityValues']) return df
def getgeographicvalues(**kwargs): ''' Get all possible geographic values :param **kwargs: Curl options passed on to \code{\link[httr]{GET}} Usage pytaxize.getgeographicvalues() ''' out = Refactor(itis_base + 'getGeographicValues', payload={}, request='get').xml(**kwargs) ns = {'ax21':'http://metadata.itis_service.itis.usgs.gov/xsd'} nodes = out.xpath("//ax21:geographicValues", namespaces=ns) gv = [x.text for x in nodes] return pd.DataFrame(gv, columns=['geographicvalues'])
def getcredibilityratings(**kwargs): ''' Get possible credibility ratings :param **kwargs: Curl options passed on to `requests.get` Usage:: pytaxize.getcredibilityratings() ''' out = Refactor(itis_base + 'getCredibilityRatings', payload={}, request='get').xml(**kwargs) nodes = out.xpath("//ax23:credibilityValues", namespaces=ns23) return [x.text for x in nodes]
def getlastchangedate(**kwargs): ''' Provides the date the ITIS database was last updated. Usage: pytaxize.getlastchangedate() ''' out= Refactor(itis_base + 'getLastChangeDate', payload={}, request='get').xml(**kwargs) ns = {'ax23':"http://metadata.itis_service.itis.usgs.gov/xsd"} nodes = out.xpath("//ax23:updateDate", namespaces=ns) bb = nodes[0].text dt = time.strptime(bb.split()[0], "%Y-%m-%d") return dt
def getjurisdictionvalues(**kwargs): ''' Get possible jurisdiction values Usage:: pytaxize.getjurisdictionvalues() ''' out = Refactor(itis_base + 'getJurisdictionValues', payload={}, request='get').xml(**kwargs) vals = [x.text for x in out.getchildren()[0].getchildren()] return pd.DataFrame(vals, columns=['jurisdictionValues'])
def getgeographicvalues(**kwargs): ''' Get all possible geographic values :param **kwargs: Curl options passed on to `requests.get` Usage:: pytaxize.getgeographicvalues() ''' out = Refactor(itis_base + 'getGeographicValues', payload={}, request='get').xml(**kwargs) ns = {'ax21':'http://metadata.itis_service.itis.usgs.gov/xsd'} nodes = out.xpath("//ax21:geographicValues", namespaces=ns) return [x.text for x in nodes]
def itis_ping(**kwargs): ''' Ping the ITIS API Usage: >>> import pytaxize >>> pytaxize.itis_ping() u'<ns:getDescriptionResponse xmlns:ns="http://itis_service.itis.usgs.gov"><ns:return xmlns:ax21="http://data.itis_service.itis.usgs.gov/xsd" xmlns:ax26="http://itis_service.itis.usgs.gov/xsd" xmlns:ax23="http://metadata.itis_service.itis.usgs.gov/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax26:SvcDescription"><ax26:description>This is the ITIS Web Service, providing access to the data behind www.itis.gov. The database contains 641,468 scientific names (486,232 of them valid/accepted) and 118,145 common names.</ax26:description></ns:return></ns:getDescriptionResponse>' ''' tt = Refactor(itis_base + 'getDescription', payload={}, request='get').xml(**kwargs) ns = {'ax26':'http://itis_service.itis.usgs.gov/xsd'} nodes = tt.xpath('//ax26:description', namespaces=ns) text = [x.text for x in nodes][0] return text
def itis_ping(**kwargs): ''' Ping the ITIS API Usage:: import pytaxize pytaxize.itis_ping() ''' tt = Refactor(itis_base + 'getDescription', payload={}, request='get').xml(**kwargs) ns = {'ax26':'http://itis_service.itis.usgs.gov/xsd'} nodes = tt.xpath('//ax26:description', namespaces=ns) text = [x.text for x in nodes][0] return text
def gettsnfromlsid(lsid, **kwargs): ''' Gets the TSN corresponding to the LSID, or an empty result if there is no match. Usage: pytaxize.gettsnfromlsid(lsid="urn:lsid:itis.gov:itis_tsn:28726") pytaxize.gettsnfromlsid("urn:lsid:itis.gov:itis_tsn:0") ''' out= Refactor(itis_base + 'getTSNFromLSID', payload={'lsid': lsid}, request='get').xml(**kwargs) tt = out.getchildren()[0].text if tt is None: tt = "no match" else: pass return tt
def searchcol(x): payload = { 'name': x, 'format': format, 'response': "full", 'start': start } tt = Refactor(url, payload, request='get').xml() childtaxa = tt.xpath('//child_taxa//taxon') outlist = [] for i in range(len(childtaxa)): tt_ = childtaxa[i].getchildren() outlist.append([x.text for x in tt_[:3]]) df = pd.DataFrame(outlist, columns=['id', 'name', 'rank']) return df
def getlastchangedate(**kwargs): ''' Provides the date the ITIS database was last updated. Usage:: pytaxize.getlastchangedate() ''' out = Refactor(itis_base + 'getLastChangeDate', payload={}, request='get').xml(**kwargs) ns = {'ax23': "http://metadata.itis_service.itis.usgs.gov/xsd"} nodes = out.xpath("//ax23:updateDate", namespaces=ns) bb = nodes[0].text dt = time.strptime(bb.split()[0], "%Y-%m-%d") return dt
def itis_ping(**kwargs): ''' Ping the ITIS API Usage:: import pytaxize pytaxize.itis_ping() ''' tt = Refactor(itis_base + 'getDescription', payload={}, request='get').xml(**kwargs) ns = {'ax26': 'http://itis_service.itis.usgs.gov/xsd'} nodes = tt.xpath('//ax26:description', namespaces=ns) text = [x.text for x in nodes][0] return text
def getgeographicvalues(**kwargs): ''' Get all possible geographic values :param **kwargs: Curl options passed on to `requests.get` Usage:: pytaxize.getgeographicvalues() ''' out = Refactor(itis_base + 'getGeographicValues', payload={}, request='get').xml(**kwargs) ns = {'ax21': 'http://metadata.itis_service.itis.usgs.gov/xsd'} nodes = out.xpath("//ax21:geographicValues", namespaces=ns) return [x.text for x in nodes]
def searchforanymatch(x, **kwargs): ''' Search for any match pytaxize.searchforanymatch(x=202385) pytaxize.searchforanymatch(x="dolphin") ''' out= Refactor(itis_base + 'searchForAnyMatch', payload={'srchKey':x}, request='get').xml(**kwargs) # if isinstance(x, basestring): tmp = out.getchildren()[0].getchildren() output = [] for v in tmp: tmp = v.getchildren() for w in tmp: output.append(dict(zip([gettag(e) for e in w.iter()], [e.text for e in w.iter()]))) return output
def accepted_names(tsn, **kwargs): """ Get accepted names from tsn :param tsn: taxonomic serial number (TSN) (character or numeric) :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis # TSN accepted - good name itis.accepted_names(tsn=208527) # TSN not accepted - input TSN is old name itis.accepted_names(tsn=504239) """ out = Refactor(itis_base + "getAcceptedNamesFromTSN", payload={ "tsn": tsn }, request="get").json(**kwargs) if out["acceptedNames"][0] is None: return {} else: out["acceptedNames"][0].pop("class") return out["acceptedNames"][0]
def _itisterms(endpt, args={}, as_dataframe=False, **kwargs): out = Refactor(itis_base + endpt, payload=args, request="get").json(**kwargs) if out["itisTerms"][0] is None: return {} [w.pop("class") for w in out["itisTerms"]] return _df(out["itisTerms"], as_dataframe)
def searchbycommonnameendswith(x, **kwargs): ''' Search for tsn by common name ending with Usage:: pytaxize.searchbycommonnameendswith("snake") ''' out = Refactor(itis_base + 'searchByCommonNameEndsWith', payload={ 'srchKey': x }, request='get').xml(**kwargs) matches = ["commonName", "language", "tsn"] tmp = out.xpath('//ax21:commonNames', namespaces=ns21) return _itisdf(tmp[0], ns21, matches, _tolower(matches))
def vascan_search(q, format="json", raw=False): """ Search the CANADENSYS Vascan API. :param q: Taxonomic rank, one of species, genus (default), family, order. :param format: Number of names to get. Maximum depends on the rank. :param raw: Raw data or not (default) :param callopts: Further args passed to request Usage:: import pytaxize pytaxize.vascan_search(q = ["Helianthus annuus"]) pytaxize.vascan_search(q = ["Helianthus annuus"], raw=True) pytaxize.vascan_search(q = ["Helianthus annuus", "Crataegus dodgei"], raw=True) # format type ## json pytaxize.vascan_search(q = ["Helianthus annuus"], format="json", raw=True) ## xml pytaxize.vascan_search(q = ["Helianthus annuus"], format="xml", raw=True) # lots of names, in this case 50 splist = pytaxize.names_list(rank='species', size=50) pytaxize.vascan_search(q = splist) """ if format == "json": url = "http://data.canadensys.net/vascan/api/0.1/search.json" else: url = "http://data.canadensys.net/vascan/api/0.1/search.xml" if len(q) > 1: query = "\n".join(q) payload = {"q": query} if format == "json": out = Refactor(url, payload, request="post").json() else: out = Refactor(url, payload, request="post").raw() return out else: payload = {"q": q} if format == "json": out = Refactor(url, payload, request="get").json() else: out = Refactor(url, payload, request="get").raw() return out
def searchforanymatchpaged(x, pagesize, pagenum, ascend, **kwargs): ''' Search for any matched page for descending (logical) Usage: pytaxize.searchforanymatchpaged(x=202385, pagesize=100, pagenum=1, ascend=False) pytaxize.searchforanymatchpaged("Zy", pagesize=100, pagenum=1, ascend=False) ''' args = {'srchKey':x, 'pageSize':pagesize, 'pageNum':pagenum, 'ascend':ascend} out= Refactor(itis_base + 'searchForAnyMatchPaged', payload=args, request='get').xml(**kwargs) tmp = out.getchildren()[0].getchildren() output = [] for v in tmp: tmp = v.getchildren() for w in tmp: output.append(dict(zip([gettag(e) for e in w.iter()], [e.text for e in w.iter()]))) return output
def credibility_ratings(**kwargs): """ Get possible credibility ratings :param \*\*kwargs: Curl options passed on to `requests.get` :return: a dict Usage:: from pytaxize import itis itis.credibility_ratings() """ out = Refactor(itis_base + "getCredibilityRatings", payload={}, request="get").json(**kwargs) out.pop("class") return out["credibilityValues"]
def jurisdiction_values(**kwargs): """ Get possible jurisdiction values :param \*\*kwargs: Curl options passed on to `requests.get` :return: list Usage:: from pytaxize import itis itis.jurisdiction_values() """ out = Refactor(itis_base + "getJurisdictionValues", payload={}, request="get").json(**kwargs) out.pop("class") return out["jurisdictionValues"]
def jurisdiction_origin_values(as_dataframe=False, **kwargs): """ Get jurisdiction origin values :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis itis.jurisdiction_origin_values() """ out = Refactor(itis_base + "getJurisdictionalOriginValues", payload={}, request="get").json(**kwargs) out.pop("class") [z.pop("class") for z in out["originValues"]] return _df(out["originValues"], as_dataframe)
def getanymatchcount(x, **kwargs): ''' Get any match count. :param x: text or taxonomic serial number (TSN) (character or numeric) :param **kwargs: Curl options passed on to `requests.get` Usage:: pytaxize.getanymatchcount(x=202385) pytaxize.getanymatchcount(x="dolphin") ''' out = Refactor(itis_base + 'getAnyMatchCount', payload={ 'srchKey': x }, request='get').xml(**kwargs) return int(out.getchildren()[0].text)
def getlsidfromtsn(tsn, **kwargs): ''' Gets the unique LSID for the TSN, or an empty result if there is no match. Usage: # valid TSN pytaxize.getlsidfromtsn(155166) # invalid TSN, returns nothing pytaxize.getlsidfromtsn(0) ''' out = _itisGET("getLSIDFromTSN", {'tsn': tsn}, **kwargs) out= Refactor(itis_base + 'getLSIDFromTSN', payload={'tsn':tsn}, request='get').xml(**kwargs) tt = out.getchildren()[0].text if tt is None: tt = "no match" else: pass return tt
def searchbycommonname(x, **kwargs): ''' Search for tsn by common name Usage:: pytaxize.searchbycommonname(x="american bullfrog") pytaxize.searchbycommonname("ferret-badger") pytaxize.searchbycommonname("polar bear") ''' out = Refactor(itis_base + 'searchByCommonName', payload={ 'srchKey': x }, request='get').xml(**kwargs) matches = ["commonName", "language", "tsn"] tmp = out.xpath('//ax21:commonNames', namespaces=ns21) return _itisdf(tmp[0], ns21, matches, _tolower(matches))
def rank_name(tsn, as_dataframe=False, **kwargs): """ Returns the kingdom and rank information for the TSN. :param tsn: (int) TSN for a taxonomic group :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis itis.rank_name(tsn = 202385) """ tt = Refactor(itis_base + "getTaxonomicRankNameFromTSN", payload={ "tsn": tsn }, request="get").json(**kwargs) tt.pop("class") return _df(tt, as_dataframe)
def gettsnfromlsid(lsid, **kwargs): ''' Gets the TSN corresponding to the LSID, or an empty result if there is no match. Usage:: pytaxize.gettsnfromlsid(lsid="urn:lsid:itis.gov:itis_tsn:28726") pytaxize.gettsnfromlsid("urn:lsid:itis.gov:itis_tsn:0") ''' out = Refactor(itis_base + 'getTSNFromLSID', payload={ 'lsid': lsid }, request='get').xml(**kwargs) tt = out.getchildren()[0].text if tt is None: tt = "no match" else: pass return tt
def experts(tsn, as_dataframe=False, **kwargs): """ Get expert information for the TSN. :param tsn: (int) TSN for a taxonomic group :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis itis.experts(tsn=180544) """ out = Refactor(itis_base + "getExpertsFromTSN", payload={ "tsn": tsn }, request="get").json(**kwargs) out.pop("class") return _df(out["experts"], as_dataframe)
def getacceptednamesfromtsn(tsn, **kwargs): ''' Get accepted names from tsn :param tsn: taxonomic serial number (TSN) (character or numeric) Usage: # TSN accepted - good name pytaxize.getacceptednamesfromtsn('208527') # TSN not accepted - input TSN is old name pytaxize.getacceptednamesfromtsn('504239') ''' out = Refactor(itis_base + 'getAcceptedNamesFromTSN', payload={'tsn': tsn}, request='get').xml(**kwargs) temp = out.getchildren() if(temp[0].getchildren()[1].values()[0] == 'true'): dat = temp[0].getchildren()[0].text else: nodes = temp[0].getchildren()[1].getchildren() dat = _parse_nodes(nodes) dat.pop('author') dat['submittedTsn'] = temp[0].getchildren()[0].text return dat
def ubio_search(searchName = None, searchAuth = None, searchYear = None, order = None, sci = None, vern = None, keyCode = None): ''' ubio_search returns NameBankIDs that match search terms :param searchName: (string) - term to search within name string :param searchAuth: (string) - term to search within name authorship :param searchYear: (string) - term to search within name year :param order: (string) - (name or namebankID) field by which the results will be sorted (default is namebankID) :param sci: (int) - (sci, vern, or all) type of results to be returned (default is all) :param vern: (int) - (limit 1000) maximum number of results to be returned (default is 1000) :param keyCode: Your uBio API key; loads from .Rprofile. If you don't have one, obtain one at http://www.ubio.org/index.php?pagename=form. :param callopts: Parameters passed on to httr::GET call. Usage # A basic example >>> import pytaxize >>> >>> pytaxize.ubio_search(searchName = 'elephant', sci = 1, vern = 0) >>> pytaxize.ubio_search(searchName = 'Astragalus aduncus', sci = 1, vern = 0) ''' url = "http://www.ubio.org/webservices/service.php" ubioApiKey = "b052625da5f330e334471f8efe725c07bf4630a6" payload = {'function': 'namebank_search', 'searchName': searchName, 'searchAuth': searchAuth, 'searchYear': searchYear, 'order': order, 'sci': sci, 'vern': vern, 'keyCode': ubioApiKey} tt = Refactor(url, payload, request='get').xml() nodes = tt.xpath('//value') if (len(nodes) == 0): sys.exit('Please enter a valid searchName') outlist = [] for i in range(len(nodes)): tt_ = nodes[i].getchildren() outlist.append([x.text for x in tt_[:8]]) df = pd.DataFrame(outlist, columns=['namebankID','nameString','fullNameString','packageID','packageName','basionymunit','rankID','rankName']) return df
def hierarchy_up(tsn, as_dataframe=False, **kwargs): """ Get hierarchy up from tsn :param tsn: (int) TSN for a taxonomic group :param as_dataframe: (bool) specify return type, if pandas is available :param \*\*kwargs: Curl options passed on to `requests.get` Usage:: from pytaxize import itis itis.hierarchy_up(tsn = 36485) itis.hierarchy_up(tsn = 37906) """ tt = Refactor(itis_base + "getHierarchyUpFromTSN", payload={ "tsn": tsn }, request="get").json(**kwargs) tt.pop("class") return _df(tt, as_dataframe)
def func(x, y): url = "http://www.catalogueoflife.org/col/webservice" if(checklist.__class__.__name__ == 'NoneType'): pass else: if(checklist in ['2012','2011','2010']): url = re.sub("col", "annual-checklist/" + checklist, url) else: url = "http://www.catalogueoflife.org/annual-checklist/year/webservice" url = re.sub("year", checklist, url) payload = {'name':x, 'id':y, 'format':format, 'response':"full", 'start':start} tt = Refactor(url, payload, request='get').xml() childtaxa = tt.xpath('//child_taxa//taxon') if len(childtaxa) == 0: sys.exit('Please enter a valid search name') outlist = [] for i in range(len(childtaxa)): tt_ = childtaxa[i].getchildren() outlist.append([x.text for x in tt_[:3]]) df = pd.DataFrame(outlist, columns=['id','name','rank']) return df