view create.py @ 17:f02c2c58a6f9 draft

Deleted selected files
author fabio
date Fri, 18 Jan 2019 10:12:40 -0500
parents ba9d0fc8657f
children
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 sys, 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/";
CREATE_URL = SERVICE_URL+"tree/create";
STATUS_URL = SERVICE_URL+"status/<query_id>";
##############
# query delay in seconds
QUERY_DELAY = 30;
##############

__version__ = "1.0.0";
ERR_EXIT_CODE = 1;
OK_EXIT_CODE = 0;

def raiseException( exitcode, message, errorfilepath ):
    with open(errorfilepath, 'w') as out:
        out.write(message);
    sys.exit(exitcode);

def create_request( options, args, data ):
    outfilepath = options.outfile;
    cluster_id_2_query_id = { };

    for cluster_id in data:
        payload = { };
        payload["accessions"] = data[cluster_id];
        # add additional parameters to the payload
        payload["qualitycontrol"] = int(options.qualitycontrol);
        payload["qualitythreshold"] = float(options.qualitythreshold);
        payload["klen"] = int(options.klen);
        payload["minabundance"] = int(options.minabundance);
        # 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 create route
        req = session.post(CREATE_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 query id
            query_id = json_content['query_id'];
            cluster_id_2_query_id[cluster_id] = query_id;
        else:
            with open(outfilepath, 'a+') as outfile:
                outfile.write( "An error has occurred while submitting data to the /tree/create endpoint for the cluster " + cluster_id + "\n\n" );

    build_flags = [ ]
    while len(build_flags) < len(cluster_id_2_query_id):
        for idx, cluster_id in enumerate( cluster_id_2_query_id ):
            if cluster_id not in build_flags:
                query_id = cluster_id_2_query_id[ cluster_id ];
                # create a new session
                session = requests.Session();
                # make a synchronous get request to the status route
                status_query_url = STATUS_URL.replace("<query_id>", query_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':
                    build_flags.append( cluster_id );
                    built_tree_id = json_status_content['results']['tree_id'];
                    with open(outfilepath, 'a+') as outfile:
                        outfile.write( "Query ID: " + str(query_id) + "\n" + "Query status: " + str(json_status_content['state']) + "\n" + "Cluster ID: " + cluster_id + "\n" + "Sequence Bloom Tree ID: " + built_tree_id + "\n\n" );
                elif json_status_content['state'] in ['FAILURE', 'REVOKED']:
                    build_flags.append( cluster_id );
                    with open(outfilepath, 'a+') as outfile:
                        outfile.write( "Query ID: " + str(query_id) + "\n" + "Query status: " + str(json_status_content['state']) + "\n" + "Cluster ID: " + cluster_id + "\n\n" );
        if len(build_flags) < len(cluster_id_2_query_id):
            time.sleep(QUERY_DELAY); # in seconds
    return sys.exit(OK_EXIT_CODE);

def create( options, args ):
    multiple_data = {};
    experiment_list_file_path = options.explist;
    with open(experiment_list_file_path) as explist:
        for line in explist:
            if line.strip() != "":
                line_split = line.strip().split("\t"); # split on tab
                if len(line_split) == 2: # 0:accession , 1:cluster_id , otherwise skip line
                    accession = line_split[0];
                    cluster_id = line_split[1];
                    if cluster_id in multiple_data:
                        multiple_data[cluster_id].append( accession );
                    else:
                        multiple_data[cluster_id] = [ accession ];
    if len(multiple_data) > 0:
        return create_request( options, args, multiple_data );
    else:
        return raiseException( ERR_EXIT_CODE, "An error has occurred. Please be sure that your input file is valid.", options.outfile );

def __main__():
    # Parse the command line options
    usage = "Usage: create.py --explist experiment_list --qualitycontrol quality_control --qualitythreshold quality_threshold --klen kmer_len --minabundance min_abundance --outfile output_file_path";
    parser = optparse.OptionParser(usage = usage);
    parser.add_option("-v", "--version", action="store_true", dest="version",
                    default=False, help="display version and exit")
    parser.add_option("-l", "--explist", type="string",
                    action="store", dest="explist", help="tabular file with a list of SRA accessions and their cluster label");
    parser.add_option("-q", "--qualitycontrol", type="int", default=0
                    action="store", dest="qualitycontrol", help="flag to enable or disable the experiment quality control");
    parser.add_option("-t", "--qualitythreshold", type="float", default=0.0
                    action="store", dest="qualitythreshold", help="quality threshold, if quality control is enabled only");
    parser.add_option("-k", "--klen", type="int", default=21,
                    action="store", dest="klen", help="k-mer length");
    parser.add_option("-m", "--minabundance", type="int", default=2,
                    action="store", dest="minabundance", help="minimum abundance");
    parser.add_option("-o", "--outfile", type="string", default="outfile_txt",
                    action="store", dest="outfile", help="output file path");

    (options, args) = parser.parse_args();
    if options.version:
        print __version__;
    else:
        return create( options, args );

if __name__ == "__main__": __main__()