Mercurial > repos > fabio > sbtas_se
changeset 12:039e8e1e8b1f draft
Uploaded 20180201
author | fabio |
---|---|
date | Thu, 01 Feb 2018 16:23:17 -0500 |
parents | 0d0f7080b55c |
children | b5f070767ed4 |
files | ._.shed.yml ._example.tsv ._query.py ._query.xml query.py query.xml |
diffstat | 6 files changed, 40 insertions(+), 35 deletions(-) [+] |
line wrap: on
line diff
--- a/query.py Wed Jan 31 17:29:13 2018 -0500 +++ b/query.py Thu Feb 01 16:23:17 2018 -0500 @@ -3,7 +3,7 @@ # 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 +import sys, os, uuid, optparse, requests, json, time #from requests_futures.sessions import FuturesSession #### NN14 #### @@ -16,9 +16,17 @@ QUERY_DELAY = 30; ############## +__version__ = "1.0.0"; VALID_CHARS = '.-()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' +# in the case of collections, exitcodes equal to 0 and 1 are not considered errors +def raiseException( exitcode, message, errorfilepath ): + with open(errorfilepath, 'w') as out: + out.write(message); + sys.exit(exitcode); + def query_request( options, args, payload ): + output_dir_path = options.outputdir; # add additional parameters to the payload #payload["tree_id"] = str(options.treeid); payload["search_mode"] = str(options.search); @@ -32,7 +40,7 @@ # 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"); + #print(str(req.content)+"\n\n"); if resp_code == requests.codes.ok: resp_content = str(req.content); # convert out to json @@ -42,7 +50,6 @@ 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(); @@ -50,7 +57,7 @@ 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"); + #print(status_resp_content+"\n\n"); # convert out to json json_status_content = json.loads(status_resp_content); # take a look at the state @@ -59,16 +66,11 @@ task_processed = True; break; elif json_status_content['state'] in ['FAILURE', 'REVOKED']: - return "Task status: "+str(json_status_content['state']); + return raiseException( 1, "Task ID: "+str(task_id)+"\nTask status: "+str(json_status_content['state']), str(options.errorfile) ); else: time.sleep(QUERY_DELAY); # 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 = "tabular"; - for block in json_status_content['results']: seq_id = block['sequence_id']; accessions = block['accession_numbers']; @@ -79,10 +81,12 @@ accessions_list = accessions_list + accession_number + "\n"; with open(output_file_path, 'w') as out: out.write(accessions_list.strip()); + return sys.exit(0); else: - return "Unable to query the remote server. Please try again in a while."; + return raiseException( 1, "Unable to query the remote server. Please try again in a while.", str(options.errorfile) ); def query( options, args ): + output_dir_path = options.outputdir; multiple_data = {}; comma_sep_file_paths = options.files; #print("files: "+str(comma_sep_file_paths)+" - "+str(type(comma_sep_file_paths))); @@ -106,13 +110,13 @@ seq_id = ''.join(e for e in seq_id if e in VALID_CHARS) seq_text = line_split[1]; if seq_id in multiple_data: - return "Error: the id '"+seq_id+"' is duplicated"; + return raiseException( 1, "Error: the id '"+seq_id+"' is duplicated", str(options.errorfile) ); multiple_data[seq_id] = seq_text; if len(multiple_data) > 0: return query_request( options, args, multiple_data ); #return echo( options, args ); else: - return "An error has occurred. Please be sure that your input files are valid."; + return raiseException( 1, "An error has occurred. Please be sure that your input files are valid.", str(options.errorfile) ); else: # try with the sequence in --sequence text_content = options.sequences; @@ -132,21 +136,23 @@ seq_id = ''.join(e for e in seq_id if e in VALID_CHARS) seq_text = line_split[1]; if seq_id in multiple_data: - return "Error: the id '"+seq_id+"' is duplicated"; + return raiseException( 1, "Error: the id '"+seq_id+"' is duplicated", str(options.errorfile) ); multiple_data[seq_id] = seq_text; if len(multiple_data) > 0: return query_request( options, args, multiple_data ); #return echo( options, args ); else: - return "An error has occurred. Please be sure that your input files are valid."; + return raiseException( 1, "An error has occurred. Please be sure that your input files are valid.", str(options.errorfile) ); else: - return "You have to insert at least one row formatted as a tab delimited <id, sequence> touple"; - return -1; + return raiseException( 1, "You have to insert at least one row formatted as a tab delimited (ID, SEQUENCE) couple", str(options.errorfile) ); + 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("-v", "--version", action="store_true", dest="version", + default=False, help="display version and exit") parser.add_option("-f", "--files", type="string", action="store", dest="files", help="comma separated files path"); parser.add_option("-n", "--names", type="string", @@ -161,23 +167,24 @@ 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", + 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", + action="store", dest="errorfile", help="error file name containing error messages"); - #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 = 'NM_001169378.2__tc__atttcggatgctttggagggaggaactctagtgctgcattgattggggcgtgtgttaatgatattcccagttcgcatggcgagcatcgattcctggtacgtatgtgggccccttgactcccacttatcgcacttgtcgttcgcaatttgcatgaattccgcttcgtctgaaacgcacttgcgccagacttctccggctggtctgatctggtctgtgatccggtctggtggggcgccagttgcgtttcgagctcatcaccagtcactccgcagtcgcattctgccagaggtctccgatcaagagcgcttctccattcgagattcaaacgcagcgcggtctgacgccgccacatcgagtgaaatccatatcgatggccacattcacacaggacgagatcgacttcctgcgcagccatggcaacgagctgtgtgccaagacctggctgggattgtgggatccgaagcgggctgtgcaccagcaggagcagcgcgaactgatgatggacaagtatgagcggaagcgatactacctggagccggccagtcctcttaagtcgctggccaatgcggtcaacctgaagtcgtctgctccggcgacgaaccacactcagaatggccaccaaaatgggtatgccagcatccatttgacgcctcctgctgcccagcggacctcggccaatggattgcagaaggtggccaactcgtcgagtaactcttctggaaagacctcatcctcgatcagtaggccacactataatcaccagaacaacagccaaaacaacaatcacgatgcctttggcctgggtggcggattgagcagcctgaacagcgccggttccacatccactggagctctttccgacaccagcagttgtgctagcaatggcttcggtgcggactgcgactttgtggctgactttggctcggccaacattttcgacgccacatcggcgcgttccacaggatcgccggcggtgtcgtccgtgtcctcagtgggttccagcaatggctacgccaaggtgcagcccatccgggcagctcatctccagcagcaacagcagttgcagcagcagctgcatcagcagcagctcctcaatggcaatggtcatcagggcactgagaactttgccgacttcgatcacgctcccatctacaatgcagtggctccaccgacttttaacgattggatcagcgactggagcaggcggggcttccacgatcccttcgacgattgcgatgactcgccaccaggtgcccgccctccagcacctgcgccagctcctgctcaagttcccgcagtatcatcaccattgccaaccgtccgagaagaaccagagcttgcgtggaatttttgggaggacgagatgcgaatagaggcgcaggaaaaggagtcccaaactaaacagccggagttgggctactccttttcgattagtactactacgcccctttccccttcgaatcccttcctgccctaccttgtcagtgaggagcagcatcgaaatcatccagagaagccctccttttcgtattcgttgttcagctccatatcaaatagttcgcaagaagatcaggcggatgatcatgagatgaatgttttaaatgccaatttccatgatttctttacgtggagtgctcccttgcagaacggccatacgaccagtccgcccaagggcggaaatgcagcgatggcgcccagtgaggatcgatatgccgctcttaaggatctcgacgagcagctgcgagaactgaaggccagcgaaagcgccacagagacgcccacgcccaccagtggcaatgttcaggccacagatgcctttggtggagccctcaacaacaatccaaatcccttcaagggccagcaacagcagcagctcagcagccatgtggtgaatccattccagcagcagcaacagcagcagcaccagcagaatctctatggccagttgacgctcataccaaatgcctacggcagcagttcccagcagcagatggggcaccatctcctccagcagcagcagcagcaacagcagagcttcttcaacttcaacaacaacgggttcgccatctcgcagggtctgcccaacggctgcggcttcggcagcatgcaacccgctcctgtgatggccaacaatccctttgcagccagcggcgccatgaacaccaacaatccattcttatgagactcaacccgggagaatccgcctcgcgccacctggcagaggcgctgagccagcgaacaaagagcagacgcggaggaaccgaaccgaaattagtccattttactaacaatagcgttaatctatgtatacataatgcacgccggagagcactctttgtgtacatagcccaaatatgtacacccgaaaggctccacgctgacgctagtcctcgcggatggcggaggcggactggggcgttgatatattcttttacatggtaactctactctaacgtttacggatacggatatttgtatttgccgtttgccctagaactctatacttgtactaagcgcccatgaacacttcatccactaacatagctactaatcctcatcctagtggaggatgcagttggtccagacactctgttatttgttttatccatcctcgtacttgtctttgtcccatttagcactttcgttgcggataagaactttgtcagttattgattgtgtggccttaataagattataaaactaaatattataacgtacgactatacatatacggatacagatacagattcagacacagttagtacagatacagatatacatatacgcttttgtacctaatgaattgcttcttgtttccattgctaatcatctgcttttcgtgtgctaattttatacactagtacgtgcgatatcggccgtgcagatagattgctcagctcgcgagtcaagcctcttttggttgcacccacggcagacatttgtacatatactgtctgattgtaagcctcgtgtaatacctccattaacaccactcccccaccacccatccatcgaaccccgaatccatgactcaattcactgctcacatgtccatgcccatgccttaacgtgtcaaacattatcgaagccttaaagttatttaaaactacgaaatttcaataaaaacaaataagaacgctatc'; - #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 options.version: + print __version__; + else: + # create output dir (collection) + output_dir_path = options.outputdir; + if not os.path.exists(output_dir_path): + os.makedirs(output_dir_path); + + return query( options, args ); if __name__ == "__main__": __main__()
--- a/query.xml Wed Jan 31 17:29:13 2018 -0500 +++ b/query.xml Thu Feb 01 16:23:17 2018 -0500 @@ -34,17 +34,17 @@ <option value="1">By manually inserted text</option> </param> <when value="0"> - <param format="tabular" name="txtfiles" type="data" label="Select files" multiple="true" optional="true" help="Select one or more tabular files containing (ID, TRANSCRIPT) touples for each line. The content of these files will be merged and the result will represent a query to the AllSome Sequence Bloom Tree Search Engine that will return a collection containing a file for each ID. The content of these files as result of the tool will be a list of accession numbers." /> + <param format="tabular" name="txtfiles" type="data" label="Select files" multiple="true" optional="false" help="Select one or more tabular files containing (ID, TRANSCRIPT) couples for each line. The content of these files will be merged and the result will represent a query to the AllSome Sequence Bloom Tree Search Engine that will return a collection containing a file for each ID. The content of these files as result of the tool will be a list of accession numbers." /> </when> <when value="1"> - <param name="sequences" type="text" area="True" size="5x25" label="Manually insert sequences" optional="true" help="Insert a list of (ID, TRANSCRIPT) touples in a tab delimited format, one for each line. The content of this text box will represent a query to the AllSome Sequence Bloom Tree Search Engine that will return a collection containing a file for each ID. The content of these files as result of the tool will be a list of accession numbers." /> + <param name="sequences" type="text" area="True" size="5x25" label="Manually insert sequences" optional="false" help="Insert a list of (ID, TRANSCRIPT) couples in a tab delimited format, one for each line. The content of this text box will represent a query to the AllSome Sequence Bloom Tree Search Engine that will return a collection containing a file for each ID. The content of these files as result of the tool will be a list of accession numbers." /> </when> </conditional> <param name="sthreshold" size="3" type="float" value="0.5" min="0.0" max="1.0" label="Search threshold" help="This threshold controls the specificity. Lower values will produce more hits to the query. Higher values are more stringent and will produce fewer hits." /> </inputs> <outputs> <collection name="output_collect" type="list" label="AllSome Sequence Bloom Tree Search Collection"> - <discover_datasets pattern="(?P<identifier_0>[^_]+)_(?P<ext>[^_]+)" directory="collection_content" ext="tabular" /> + <discover_datasets pattern="(?P<identifier_0>[^_]+)_(?P<ext>[^_]+)" directory="collection_content" ext="auto" /> </collection> </outputs> @@ -54,9 +54,7 @@ ---- -**Example** - -The input for this tool is a list of (ID, TRANSCRIPT) touples, one for each line, +The input for this tool is a list of (ID, TRANSCRIPT) couples, one for each line, in a tab delimited format:: id0 CCAACCAAAGGGAAAACTTTTTTCCGACTTTGGCCTAAAGGGTTTAACGGCCAAGTCAGAAGGGAAAAAGTTGCGCCA