| 31 | 1 #! /usr/bin/python | 
|  | 2 # -*- coding: utf8 -*- | 
|  | 3 """#Convert Matrix - developed by Jocelyn Brayet <jocelyn.brayet@curie.fr> | 
|  | 4 #Copyright (C) 2015  Institut Curie | 
|  | 5 # | 
|  | 6 #This program is free software: you can redistribute it and/or modify | 
|  | 7 #it under the terms of the GNU General Public License as published by | 
|  | 8 #the Free Software Foundation, either version 3 of the License, or | 
|  | 9 #(at your option) any later version. | 
|  | 10 # | 
|  | 11 #This program is distributed in the hope that it will be useful, | 
|  | 12 #but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 14 #GNU General Public License for more details. | 
|  | 15 # | 
|  | 16 #You should have received a copy of the GNU General Public License | 
|  | 17 #along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|  | 18 # | 
|  | 19 ###########################################################' | 
|  | 20 # | 
|  | 21 #Client to download convert matrix results from RSAT server. | 
|  | 22 # | 
|  | 23 # | 
|  | 24 #usage: convertMatrix_wrapper.py [-h] -matrix <MATRIX> -input_form <INPUT_FORM> | 
|  | 25 #                                -output_form <OUTPUT_FORM> | 
|  | 26 #                                [-return_type <RETURN_TYPE>] [-perm <PERM>] | 
|  | 27 #                                [-rc <RC>] [-decimals <DECIMALS>] -server | 
|  | 28 #                                <SERVEUR> -outGalaxy <OUT_GALAXY> | 
|  | 29 # | 
|  | 30 #optional arguments: | 
|  | 31 #  -h, --help            show this help message and exit | 
|  | 32 #  -matrix <MATRIX>, --matrix <MATRIX> | 
|  | 33 #                        Matrix (or assembly or features) you want to convert. | 
|  | 34 #  -input_form <INPUT_FORM>, --input_form <INPUT_FORM> | 
|  | 35 #                        Input matrix format. Supported: alignace, assembly, | 
|  | 36 #                        cb, clustal, consensus, feature, gibbs, infogibbs, | 
|  | 37 #                        meme, motifsampler, tab, transfac. | 
|  | 38 #  -output_form <OUTPUT_FORM>, --output_form <OUTPUT_FORM> | 
|  | 39 #                        Output matrix format. Supported: consensus, patser, | 
|  | 40 #                        tab, transfac. | 
|  | 41 #  -return_type <RETURN_TYPE>, --return_type <RETURN_TYPE> | 
|  | 42 #                        Result type (matrix content). Supported: consensus, | 
|  | 43 #                        counts, frequencies, info, logo, margins, parameters, | 
|  | 44 #                        profile, sites, wdistrib, weights. | 
|  | 45 #  -perm <PERM>, --perm <PERM> | 
|  | 46 #                        Number of permuted matrices to return. | 
|  | 47 #  -rc <RC>, --rc <RC>   Convert the matrix to its reverse complement if value | 
|  | 48 #                        = 1. | 
|  | 49 #  -decimals <DECIMALS>, --decimals <DECIMALS> | 
|  | 50 #                        Number of decimals to print for real matrices | 
|  | 51 #                        (frequencies, weights, information) or to compute | 
|  | 52 #                        score distributions. | 
|  | 53 #  -server <SERVEUR>, --server <SERVEUR> | 
|  | 54 #  -outGalaxy <OUT_GALAXY>, --outGalaxy <OUT_GALAXY> | 
|  | 55 # | 
|  | 56 ###########################################################""" | 
|  | 57 __author__ =  'Jocelyn Brayet' | 
|  | 58 convertMatrixVersion = '0.1 - 06/03/2015' | 
|  | 59 | 
|  | 60 | 
|  | 61 ###########################################################' | 
|  | 62 ## Import | 
|  | 63 | 
|  | 64 import argparse | 
|  | 65 import os | 
|  | 66 import urllib | 
|  | 67 from suds.client import Client | 
|  | 68 import platform | 
|  | 69 | 
|  | 70 ###########################################################' | 
|  | 71 | 
|  | 72 ################################ functions ############################################################ | 
|  | 73 ## Define a function to make a service perform the desired request using provided arguments | 
|  | 74 def call_run_service(service, args): | 
|  | 75 	""" | 
|  | 76 	Run job in RSAT server. | 
|  | 77 		service -> RSAT web service | 
|  | 78 		args -> web service request | 
|  | 79 	""" | 
|  | 80 | 
|  | 81 	result = rsat_service.convert_matrix(args) | 
|  | 82 	return result | 
|  | 83 | 
|  | 84 def testNone(argument): | 
|  | 85 	""" | 
|  | 86 	Test if argument is None or not. | 
|  | 87 		argument -> argument give by user | 
|  | 88 	""" | 
|  | 89 | 
|  | 90 	if not argument is None: | 
|  | 91 		variable = argument[0] | 
|  | 92 	else: | 
|  | 93 		variable = "" | 
|  | 94 	return variable | 
|  | 95 | 
|  | 96 ###########################################################' | 
|  | 97 | 
|  | 98 ###########################################################' | 
|  | 99 # server dictionary | 
|  | 100 serverDict = { | 
|  | 101 | 
|  | 102     #http://protists.rsat.eu/ | 
|  | 103     "fr_ens":"http://rsat01.biologie.ens.fr/rsa-tools/web_services/RSATWS.wsdl", | 
|  | 104     "fr_mrs":"http://rsat-tagc.univ-mrs.fr/rsat/web_services/RSATWS.wsdl", | 
|  | 105     "fr_ro":"http://rsat.sb-roscoff.fr/web_services/RSATWS.wsdl", | 
|  | 106     "fr_mrs_2":"http://pedagogix-tagc.univ-mrs.fr/rsat/web_services/RSATWS.wsdl", | 
|  | 107     "es":"http://floresta.eead.csic.es/rsat/web_services/RSATWS.wsdl", | 
|  | 108     "mx":"http://embnet.ccg.unam.mx/rsa-tools/web_services/RSATWS.wsdl" | 
|  | 109 | 
|  | 110     } | 
|  | 111 | 
|  | 112 ###########################################################' | 
|  | 113 | 
|  | 114 if __name__ == '__main__': | 
|  | 115 | 
|  | 116     parser = argparse.ArgumentParser(description='Client to download convert-matrix results from RSAT server.', epilog='Version '+convertMatrixVersion) | 
|  | 117 | 
|  | 118 | 
|  | 119     ########### convert matrix arguments #################### | 
|  | 120 | 
|  | 121     parser.add_argument('-matrix', '--matrix', metavar='<MATRIX>', type=argparse.FileType('r'), nargs=1, help='Matrix (or assembly or features) you want to convert.', required=True) | 
|  | 122     parser.add_argument('-input_form', '--input_form', metavar='<INPUT_FORM>', type=str, nargs=1, help='Input matrix format. Supported: alignace, assembly, cb, clustal, consensus, feature, gibbs, infogibbs, meme, motifsampler, tab, transfac.', required=True) | 
|  | 123     parser.add_argument('-output_form', '--output_form', metavar='<OUTPUT_FORM>', type=str, nargs=1, help='Output matrix format. Supported: consensus, patser, tab, transfac.', required=True) | 
|  | 124     parser.add_argument('-return_type', '--return_type', metavar='<RETURN_TYPE>', type=str, nargs=1, help='Result type (matrix content). Supported: consensus, counts, frequencies, info, logo, margins, parameters, profile, sites, wdistrib, weights.', required=False) | 
|  | 125     parser.add_argument('-perm', '--perm', metavar='<PERM>', type=int, nargs=1, help='Number of permuted matrices to return.', required=False) | 
|  | 126     parser.add_argument('-rc', '--rc', metavar='<RC>', type=int, nargs=1, help='Convert the matrix to its reverse complement if value = 1.', required=False) | 
|  | 127     parser.add_argument('-decimals', '--decimals', metavar='<DECIMALS>', type=int, nargs=1, help='Number of decimals to print for real matrices (frequencies, weights, information) or to compute score distributions.', required=False) | 
|  | 128 | 
|  | 129 | 
|  | 130 | 
|  | 131     ########### galaxy arguments ############################## | 
|  | 132     parser.add_argument('-server', '--server', metavar='<SERVEUR>', type=str, nargs=1, required=True) | 
|  | 133     parser.add_argument('-outGalaxy', '--outGalaxy', metavar='<OUT_GALAXY>', type=str, nargs=1, required=True) | 
|  | 134     ########################################################### | 
|  | 135 | 
|  | 136     args = parser.parse_args() | 
|  | 137 | 
|  | 138 | 
|  | 139     matrix_file = args.matrix[0].read() | 
|  | 140     serverValue = testNone(args.server) | 
|  | 141     outGalaxyValue = testNone(args.outGalaxy) | 
|  | 142     inputFormValue = testNone(args.input_form) | 
|  | 143     outputFormValue = testNone(args.output_form) | 
|  | 144     returnTypeValue = testNone(args.return_type) | 
|  | 145     permValue = testNone(args.perm) | 
|  | 146     rcValue = testNone(args.rc) | 
|  | 147     decimalsValue = testNone(args.decimals) | 
|  | 148 | 
|  | 149     ###########################################################' | 
|  | 150     ## Create the SOAP client to request the RSAT service | 
|  | 151 | 
|  | 152 | 
|  | 153     # Load Client class from suds | 
|  | 154     # Define URL for RSAT services | 
|  | 155     url =  serverDict[serverValue] | 
|  | 156     # Create the client | 
|  | 157     client = Client(url) | 
|  | 158 | 
|  | 159     # Need service interface to perform requests | 
|  | 160     rsat_service = client.service | 
|  | 161 | 
|  | 162 | 
|  | 163     #print client | 
|  | 164 | 
|  | 165 | 
|  | 166     # Define client header | 
|  | 167     userAgent = 'RSAT-Client/v%s (%s; Python %s; %s)' % ( | 
|  | 168         convertMatrixVersion, | 
|  | 169         os.path.basename( __file__ ), | 
|  | 170         platform.python_version(), | 
|  | 171         platform.system() | 
|  | 172     ) | 
|  | 173 | 
|  | 174     httpHeaders = {'User-agent': userAgent} | 
|  | 175     client.set_options(headers=httpHeaders) | 
|  | 176     client.set_options(timeout=300) | 
|  | 177 | 
|  | 178 | 
|  | 179     convertMatrixRequest = { | 
|  | 180 | 
|  | 181         'matrix' : matrix_file, | 
|  | 182         'from' : inputFormValue, | 
|  | 183         'to' : outputFormValue, | 
|  | 184         'output' : 'both', | 
|  | 185         'return' : returnTypeValue, | 
|  | 186         'perm' : permValue, | 
|  | 187         'rc' : rcValue, | 
|  | 188         'decimals' : decimalsValue | 
|  | 189 | 
|  | 190 | 
|  | 191     } | 
|  | 192 | 
|  | 193 | 
|  | 194     result = call_run_service(rsat_service, convertMatrixRequest) | 
|  | 195 | 
|  | 196     print url | 
|  | 197 | 
|  | 198     print "###############################################" | 
|  | 199     print "Command performed on server" | 
|  | 200     print result.command | 
|  | 201     print "###############################################" | 
|  | 202     print "Result" | 
|  | 203     print result.server | 
|  | 204 | 
|  | 205 | 
|  | 206     nameFile = "convert-matrix_results.txt" | 
|  | 207 | 
|  | 208     urlResult=result.server.replace("$RSAT/public_html/",url.replace("web_services/RSATWS.wsdl","")) | 
|  | 209 | 
|  | 210     urllib.urlretrieve(urlResult, nameFile) | 
|  | 211 | 
|  | 212     os.popen("cp "+nameFile+" "+outGalaxyValue) | 
|  | 213 | 
|  | 214 |