[Back]

Calling the WoRMS webservice from Python (example 1)


Step 1: Install Python 2
Go to www.python.org, download & install Python (Windows/Mac OS X/Linux).

Step 2: Install SOAPpy
Go to http://sourceforge.net/projects/pywebsvcs/files/SOAP.py, download & install SOAPpy

Step 3: Copy/paste the following code in a file 'worms.py'
#!/usr/bin/python

from SOAPpy import WSDL
import re
import sys

def get_Records(name, offset_number):
	a = wsdlObjectWoRMS.getAphiaRecords(name, like='true', fuzzy='true', marine_only='false', offset=offset_number)
	return(a)

def process_worms_output(records):
    for b in records:
            record = str(b)
            t_family = re.compile("family': '[A-Z][a-z]+idae")
            m_family = t_family.search(record)
            if m_family:
                family = m_family.group().replace("family': '","")
                t_name = re.compile("scientificname': '[A-Z][a-z]+[ a-z]*")
                m_name = t_name.search(record)
                if m_name:
                   name = m_name.group().replace("scientificname': '","")
                   t_authority = re.compile("authority': '[\(\)A-Za-z ]+")
                   m_authority = t_authority.search(record)
                   if m_authority:
                       authority = m_authority.group().replace("authority': '","")
                       t_valid = re.compile("valid_name': '[\(\)A-Za-z ]+")
                       m_valid = t_valid.search(record)
                       if m_valid:
                           valid = m_valid.group().replace("valid_name': '","")
                           print '\n', name, authority
                           print 'Accepted name:', valid
                           print family
                       else:
                           print '\n', name, authority
                           print 'Accepted name: None'
                           print family

def get_all_worms_records(taxon_name):
    start = 1
    max_capacity = 50
    records = []
    print 'get_all_worms_records: fetching records', start, 'to', max_capacity, 'for taxon', taxon_name
    a = get_Records(str(taxon_name), start)
    if not a == None:
        for i in a:
            records.append(i)
        while len(records) == max_capacity:
            start = start + 50
            max_capacity = max_capacity + 50
            print 'get_all_worms_records: fetching records', start, 'to', max_capacity, 'for taxon', taxon_name
            b = get_Records(str(taxon_name), start)
            if not b == None:
                for i in b:
                    records.append(i)
        print 'get_all_worms_records: returning', len(records), 'records for taxon', taxon_name
        process_worms_output(records)

wsdlObjectWoRMS = WSDL.Proxy('http://marinespecies.org/aphia.php?p=soap&wsdl=1')

if len(sys.argv) == 1:
    print '  USAGE: ./worms.py taxon_name1 taxon_name2 ... taxon_nameN'
    print '  EXAMPLE: ./worms.py Mytilus\ edulis Tellinidae'
    print '  ERROR: Enter one or more taxon names'

target_names = sys.argv[1:]
for a in target_names:
    get_all_worms_records(a)
    print '\n'


Step 4: Run the script from the command line [followed by taxon name(s)]:
./worms.py Mollusca

Download this example.
Download example with suds package.

Credits for this tutorial go to Andre F. Sartori (Muséum National d'Histoire Naturelle, France) and Jonathan Pye (Ocean Tracking Network, Canada)

Calling the matchAphiaRecordsByNames function from Python (example 2)

Since some users experienced issue while implementing the call 'matchAphiaRecordsByNames' with suds-py3, we have added another python example that covers this case. Enjoy!


Step 1: Install python 3
Go to www.python.org, download & install Python (Windows/Mac OS X/Linux).

Step 2: Install the suds-py3 library
Go to https://github.com/cackharot/suds-py3, follow the installation instructions.

Step 3: Copy/paste the following code in a file 'match_taxa.py' (and adapt as desired)

from suds import null, WebFault
from suds.client import Client
cl = Client('http://marinespecies.org/aphia.php?p=soap&wsdl=1')

scinames = cl.factory.create('scientificnames')
scinames["_arrayType"] = "string[]"
scinames["scientificname"] = ["Buccinum fusiforme", "Abra alba"]

array_of_results_array = cl.service.matchAphiaRecordsByNames(scinames, like="true", fuzzy="false", marine_only="false")
    for results_array in array_of_results_array:
        for aphia_object in results_array:
            print('%s %s %s' % (aphia_object.AphiaID, aphia_object.scientificname, aphia_object.genus))


Step 4: Run the script from the command line

./match_taxa.py