diff query.py @ 4:69a3505ac594 draft

Uploaded 20180405
author fabio
date Wed, 04 Apr 2018 18:54:40 -0400
parents 315246810bfa
children 300e4932bad6
line wrap: on
line diff
--- a/query.py	Tue Apr 03 20:39:33 2018 -0400
+++ b/query.py	Wed Apr 04 18:54:40 2018 -0400
@@ -9,7 +9,7 @@
 #### NN14 ####
 SERVICE_URL = "http://nn14.galaxyproject.org:8080/";
 #service_url = "http://127.0.0.1:8082/";
-QUERY_URL = SERVICE_URL+"tree/1/query";
+QUERY_URL = SERVICE_URL+"tree/<tree_id>/query";
 STATUS_URL = SERVICE_URL+"status/<query_id>";
 ##############
 # query delay in seconds
@@ -41,7 +41,8 @@
     # create a session
     session = requests.Session();
     # make a synchronous post request to the query route
-    req = session.post(QUERY_URL, headers=headers, json=payload);
+    QUERY_URL.replace("<tree_id>", str(options.treeid));
+    req = session.post(QUERY_URL.replace("<tree_id>", str(options.treeid)), headers=headers, json=payload);
     resp_code = req.status_code;
     #print(str(req.content)+"\n\n");
     if resp_code == requests.codes.ok:
@@ -80,11 +81,22 @@
             output_file_path = os.path.join(output_dir_path, seq_id + "_" + out_file_format);
             accessions_list = "";
             hits_block = block['hits'];
+            accessions_dict = { };
+            is_sabutan = False;
             for hit in hits_block:
                 if type(hit) is dict: # sabutan
-                    accessions_list = accessions_list + hit['accession_number'] + "\t" + hit['score'] + "\n";
+                    #accessions_list = accessions_list + str(hit['accession_number']) + "\t" + str(hit['score']) + "\n";
+                    accession_number = hit['accession_number'];
+                    score = hit['score'];
+                    score_split = score.split("/");
+                    accessions_dict[accession_number] = "{0:.6f}".format(float(score_split[0])/float(score_split[1]));
+                    is_sabutan = True;
                 else: # all-some
                     accessions_list = accessions_list + str(hit) + "\n";
+            if is_sabutan:
+                sorted_accessions = sorted(accessions_dict, key=lambda i: float(accessions_dict[i]));
+                for acc in sorted_accessions:
+                    accessions_list = accessions_list + str(acc) + "\t" + str(accessions_dict[acc]) + "\n";
             with open(output_file_path, 'w') as out:
                 out.write(accessions_list.strip());
         return sys.exit(OK_EXIT_CODE);
@@ -167,15 +179,17 @@
                     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,
+    parser.add_option("-x", "--search", type="string", default="rrr",
                     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("-k", "--tree", type="int", default=0,
+                    action="store", dest="treeid", help="the id of the tree that will be queried");
     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", default="output",
                     action="store", dest="outputdir", help="output directory (collection) path");
-    parser.add_option("-r", "--errorfile", type="string", default="error.log",
+    parser.add_option("-r", "--errorfile", type="string", default="error_txt",
                     action="store", dest="errorfile", help="error file name containing error messages");
 
     # TEST