Exemple #1
0
def search(args):
    data = {}
    # Converting the given taxon id into the KEGG organism code
    tid = ''
    if 'taxon_id' in args.keys():
        tid = args['taxon_id']
        orgcode = tools.taxon_to_kegg(tid)
        if orgcode is None:
            raise Exception("Not a valid taxon id")

    # If a pathway id is specified
    if 'identifier' in args.keys():
        # Sets the default organism code to map
        org = 'map'
        if 'taxon_id' in args.keys():
            org = orgcode
        # If the pathway given is not in the form of a KEGG pathway id, raises
        # exception.
        if not tools.valid_pathway_id(args['identifier']):
            raise Exception('Not a valid identifier')
        id = org + args['identifier']

        # If a field of the specific pathway is requested. Can still be used,
        # but is not displayed in any documentation.
        if 'field' in args.keys():
            url = vars.url + 'get/' + id
            text = tools.openurl(url)
            data = tools.find_cat(text, args['field'])

        # No field is specified
        else:
            url = vars.url + 'list/' + id
            text = tools.openurl(url)
            data = tools.two_col_path(text, tid)

    # If the user has provided a search term to search pathways for
    elif 'term' in args.keys():
        term = args['term']

        # If no species is provided, just gets from the KEGG find function
        if 'taxon_id' not in args.keys():
            url = vars.url + 'find/pathway/' + term
            text = tools.openurl(url)
            data = tools.two_col_path(text, '')

        # If a species is specified, has to confirm that the pathway exists in
        # the given species
        else:
            org = orgcode
            return_data = []

            thread = Thread(target = pathway_set, args = (org, return_data))
            thread.start()
            # First searches all pathways
            url = vars.url + 'find/pathway/' + term
            text = tools.openurl(url)
            tempdata = tools.two_col_path(text, tid)
            # Then lists all pathway in given organism

            thread.join()
            data2 = return_data[0]

            data = []

            # Takes the intersection of both lists
            for element in tempdata:
                if element['KEGG_pathway_id'] in data2:
                    data.append(element)




    # No pathway is specified. Lists all pathways in Arabidopsis
    else:
        org = ''
        if 'taxon_id' in args.keys():
                org = orgcode

        url = vars.url + 'list/pathway/' + org
        text = tools.openurl(url)
        data = tools.two_col_path(text, tid)

    # Prints the data as a dict for Adama to return
    print json.dumps(data)
def search(args):
    # Converting the given taxon id into the KEGG organism code
    orgcode = ''
    taxon_id = None
    taxon_name = None
    if 'taxon_id' in args.keys():
        taxon_id = args['taxon_id']
        orgcode, taxon_name = tools.taxon_to_kegg(taxon_id)
        if orgcode is None:
            raise Exception("Not a valid taxon id")

    # If a pathway id is specified, it will get fields of the specific pathway
    if 'pathway_id' in args.keys():
        # Sets the default organism code to map if no organism is specified
        org = 'map'
        if 'taxon_id' in args.keys():
            org = orgcode
        # If the pathway given is not in the form of a KEGG pathway id, raises
        # exception.
        if not tools.valid_pathway_id(args['pathway_id']):
            raise Exception('Not a valid identifier')

        # Creates the full KEGG pathway ID with organism code and pathway ID
        id = org + args['pathway_id']

        # Accesses the KEGG API
        url = vars.url + 'get/' + id
        text = tools.openurl(url)

        # Parses the text received from the KEGG API
        data = parser.parse(text)

        # Adds the pathway ID to the returned information
        data['pathway_id'] = args['pathway_id']
        data['taxon_id'] = taxon_id
        data['taxon_name'] = taxon_name

        # Removes the organism name from the pathway
        if org != '' and 'name' in data:
            data['name'] = data['name'].rsplit(' - ', 1)[0]

        # Prints the data for ADAMA to return to the user
        print json.dumps(data)

    else: #No pathway specified

        # If an organism is given, set it to search through that organism
        org = ''
        if 'taxon_id' in args.keys():
            org = orgcode

        # Accesses the KEGG API
        url = vars.url + 'list/pathway/' + org
        text = tools.openurl(url)

        # Parses the text received into a list of pathways
        data = tools.two_col_path(text, taxon_id, taxon_name)

        # Prints the data in JSON form with elements in the list separated by
        # three dashes
        for element in data:
            print json.dumps(element)
            print '---'