view query.py @ 4:35593423c2e2 draft

Uploaded 20180131
author fabio
date Wed, 31 Jan 2018 11:28:53 -0500
parents
children 97dd57f81d77
line wrap: on
line source

#!/usr/bin/env python

# https://github.com/ross/requests-futures
# http://docs.python-requests.org/en/master/user/quickstart/#more-complicated-post-requests

import os, uuid, optparse, requests, json, time
#from requests_futures.sessions import FuturesSession

#### NN14 ####
service_url = "http://nn14.galaxyproject.org:8080/";
#service_url = "http://127.0.0.1:8082/";
query_url = service_url+"tree/0/query";
status_url = service_url+"status/<task_id>";
##############

def query_request( options, args, payload ):
    # add additional parameters to the payload
    #payload["tree_id"] = str(options.treeid);
    payload["search_mode"] = str(options.search);
    payload["exact_algorithm"] = int(options.exact);
    payload["search_threshold"] = float(options.sthreshold);
    # set the content type to application/json
    headers = {'Content-type': 'application/json'};

    # create a session
    session = requests.Session();
    # make a synchronous post request to the query route
    req = session.post(query_url, headers=headers, json=payload);
    resp_code = req.status_code;
    #print(str(req.content)+"\n\n");
    if resp_code == requests.codes.ok:
        resp_content = str(req.content);
        # convert out to json
        json_content = json.loads(resp_content);
        # retrieve task id
        task_id = json_content['task_id'];
        task_processed = False;
        # results json content
        json_status_content = None;
        task_status = None;
        while task_processed is False:
            # create a new session
            session = requests.Session();
            # make a synchronous get request to the status route
            status_query_url = status_url.replace("<task_id>", task_id);
            status_req = session.get(status_query_url);
            status_resp_content = str(status_req.content);
            #print(status_resp_content+"\n\n");
            # convert out to json
            json_status_content = json.loads(status_resp_content);
            # take a look at the state
            # state attribute is always available
            if json_status_content['state'] == 'SUCCESS':
                task_processed = True;
                break;
            elif json_status_content['state'] in ['FAILURE', 'REVOKED']:
                return "Task status: "+str(json_status_content['state']);
            else:
                time.sleep(60); # in seconds
        
        # get output dir (collection) path
        output_dir_path = options.outputdir;
        if not os.path.exists(output_dir_path):
            os.makedirs(output_dir_path);
        out_file_format = "txt";

        for block in json_status_content['results']:
            seq_id = block['sequence_id'];
            accessions = block['accession_numbers'];
            # put response block in the output collection
            output_file_path = os.path.join(output_dir_path, seq_id + "_" + out_file_format);
            accessions_list = "";
            for accession_number in accessions:
                accessions_list = accessions_list + accession_number + "\n";
            with open(output_file_path, 'w') as out:
                out.write(accessions_list.strip());
    else:
        return "Unable to query the remote server. Please try again in a while.";

def query( options, args ):
    multiple_data = {};
    comma_sep_file_paths = options.files;
    #print("files: "+str(comma_sep_file_paths)+" - "+str(type(comma_sep_file_paths)));
    # check if options.files contains at least one file path
    if comma_sep_file_paths is not None:
        # split file paths
        file_paths = comma_sep_file_paths.split(",");
        # split file names
        comma_sep_file_names = str(options.names);
        #print("names: "+str(comma_sep_file_names));
        file_names = comma_sep_file_names.split(",");
        for idx, file_path in enumerate(file_paths):
            #file_name = file_names[idx];
            with open(file_path, 'r') as content_file:
                for line in content_file:
                    if line.strip() != "":
                        line_split = line.strip().split("__tc__"); # split on tab
                        if len(line_split) == 2: # 0:id , 1:seq , otherwise skip line
                            seq_id = line_split[0];
                            seq_text = line_split[1];
                            if seq_id in multiple_data:
                                return "Error: the id '"+seq_id+"' is duplicated";
                            multiple_data[seq_id] = seq_text;
        if len(multiple_data) > 0:
            return async_request( options, args,  multiple_data );
            #return echo( options, args );
        else:
            return "An error has occurred. Please be sure that your input files are valid.";
    else:
        # try with the sequence in --sequence
        text_content = options.sequences;
        #print("sequences: "+text_content);
        # check if options.sequences contains a list of sequences (one for each row)
        if text_content is not None:
            text_content = str(text_content);
            if text_content.strip():
                # populate a dictionary with the files containing the sequences to query
                text_content = text_content.strip().split("__cn__"); # split on new line
                for line in text_content:
                    if line.strip() != "":
                        line_split = line.strip().split("__tc__"); # split on tab
                        if len(line_split) == 2: # 0:id , 1:seq , otherwise skip line
                            seq_id = line_split[0];
                            seq_text = line_split[1];
                            if seq_id in multiple_data:
                                return "Error: the id '"+seq_id+"' is duplicated";
                            multiple_data[seq_id] = seq_text;
                if len(multiple_data) > 0:
                    return async_request( options, args, multiple_data );
                    #return echo( options, args );
                else:
                    return "An error has occurred. Please be sure that your input files are valid.";
            else:
                return "You have to insert at least one row formatted as a tab delimited <id, sequence> touple";
    return -1;

def __main__():
    # Parse the command line options
    usage = "Usage: query.py --files comma_sep_file_paths --names comma_seq_file_names --sequences sequences_text --search search_mode --exact exact_alg --sthreshold threshold --outputdir output_dir_path";
    parser = optparse.OptionParser(usage = usage);
    parser.add_option("-f", "--files", type="string",
                    action="store", dest="files", help="comma separated files path");
    parser.add_option("-n", "--names", type="string",
                    action="store", dest="names", help="comma separated names associated to the files specified in --files");
    parser.add_option("-s", "--sequences", type="string",
                    action="store", dest="sequences", help="contains a list of sequences (one for each row)");
    parser.add_option("-a", "--fasta", type="string",
                    action="store", dest="fasta", help="contains the content of a fasta file");
    parser.add_option("-x", "--search", type="string", default=0,
                    action="store", dest="search", help="search mode");
    parser.add_option("-e", "--exact", type="int", default=0,
                    action="store", dest="exact", help="exact algorithm (required if search is 1 only)");
    parser.add_option("-t", "--sthreshold", type="float",
                    action="store", dest="sthreshold", help="threshold applied to the search algrithm");
    parser.add_option("-o", "--outputdir", type="string",
                    action="store", dest="outputdir", help="output directory (collection) path");

    #parser.add_option("-k", "--outfile", type="string",
                    #action="store", dest="outfile", help="output file");
    
    # TEST
    #--search 'rrr'
    #--sthreshold 0.5
    #--exact 0
    #--sequences 'id0__tc__CAATTAATGATAAATATTTTATAAGGTGCGGAAATAAAGTGAGGAATATCTTTTAAATTCAAGTTCAATTCTGAAAGC'
    #--outputdir 'collection_content'
    #sequences = 'id0__tc__CAATTAATGATAAATATTTTATAAGGTGCGGAAATAAAGTGAGGAATATCTTTTAAATTCAAGTTCAATTCTGAAAGC';
    #print(sequences);
    #(options, args) = parser.parse_args(['-x', 'rrr', '-t', 0.5, '-s', sequences, '-o', 'collection_content']);
    
    (options, args) = parser.parse_args();
    return query( options, args );

if __name__ == "__main__": __main__()