| 29 | 1 #! /usr/bin/python | 
|  | 2 # -*- coding: utf8 -*- | 
|  | 3 """#Compare Matrices - 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 compare matrices results from RSAT server. | 
|  | 22 # | 
|  | 23 ###########################################################'""" | 
|  | 24 | 
|  | 25 __author__ =  'Jocelyn Brayet' | 
|  | 26 compareMatricesVersion = '0.1 - 14/04/2015' | 
|  | 27 | 
|  | 28 | 
|  | 29 ###########################################################' | 
|  | 30 ## Import | 
|  | 31 | 
|  | 32 import argparse | 
|  | 33 import os | 
|  | 34 import urllib | 
|  | 35 from suds.client import Client | 
|  | 36 import platform | 
|  | 37 | 
|  | 38 ###########################################################' | 
|  | 39 | 
|  | 40 ################################ functions ############################################################ | 
|  | 41 ## Define a function to make a service perform the desired request using provided arguments | 
|  | 42 def call_run_service(service, args): | 
|  | 43 	""" | 
|  | 44 	Run job in RSAT server. | 
|  | 45 		service -> RSAT web service | 
|  | 46 		args -> web service request | 
|  | 47 	""" | 
|  | 48 | 
|  | 49 	result = rsat_service.compare_matrices(args) | 
|  | 50 	return result | 
|  | 51 | 
|  | 52 def testNone(argument): | 
|  | 53 	""" | 
|  | 54 	Test if argument is None or not. | 
|  | 55 		argument -> argument give by user | 
|  | 56 	""" | 
|  | 57 | 
|  | 58 	if not argument is None: | 
|  | 59 		variable = argument[0] | 
|  | 60 	else: | 
|  | 61 		variable = "" | 
|  | 62 	return variable | 
|  | 63 | 
|  | 64 ###########################################################' | 
|  | 65 # server dictionary | 
|  | 66 serverDict = { | 
|  | 67 | 
|  | 68     "fr_ens":"http://rsat01.biologie.ens.fr/rsa-tools/web_services/RSATWS.wsdl", | 
|  | 69     "fr_mrs":"http://rsat-tagc.univ-mrs.fr/rsat/web_services/RSATWS.wsdl", | 
|  | 70     "fr_ro":"http://rsat.sb-roscoff.fr/web_services/RSATWS.wsdl", | 
|  | 71     "fr_mrs_2":"http://pedagogix-tagc.univ-mrs.fr/rsat/web_services/RSATWS.wsdl", | 
|  | 72     "es":"http://floresta.eead.csic.es/rsat/web_services/RSATWS.wsdl", | 
|  | 73     "mx":"http://embnet.ccg.unam.mx/rsa-tools/web_services/RSATWS.wsdl" | 
|  | 74 | 
|  | 75     } | 
|  | 76 | 
|  | 77 ###########################################################' | 
|  | 78 | 
|  | 79 | 
|  | 80 if __name__ == '__main__': | 
|  | 81 | 
|  | 82     parser = argparse.ArgumentParser(description='Client to download compare matrices results from RSAT server.', epilog='Version '+compareMatricesVersion) | 
|  | 83 | 
|  | 84 | 
|  | 85     ########### compare matrices arguments #################### | 
|  | 86 | 
|  | 87     parser.add_argument('-matrix_1', '--matrix_1', metavar='<MATRIX_1>', type=argparse.FileType('r'), nargs=1, help='The first input containing one or several matrices.', required=True) | 
|  | 88     parser.add_argument('-matrix_2', '--matrix_2', metavar='<MATRIX_2>', type=argparse.FileType('r'), nargs=1, help='The second input containing one or several matrices.', required=True) | 
|  | 89     parser.add_argument('-format1', '--format1', metavar='<FORMAT_1>', type=str, nargs=1, help='Martix format for the first input. Supported fields: tab, cb, consensus, gibbs, meme, assembly.', required=True) | 
|  | 90     parser.add_argument('-format2', '--format2', metavar='<FORMAT_2>', type=str, nargs=1, help='Martix format for the second input. Supported fields: tab, cb, consensus, gibbs, meme, assembly.', required=True) | 
|  | 91     parser.add_argument('-returnResults', '--returnResults', metavar='<RETURN_RESULTS>', type=str, nargs=1, help='List of fields to return (only valid for the formats "profiles" and "matches").', required=False) | 
|  | 92     #parser.add_argument('-rc', '--rc', metavar='<RC>', type=int, nargs=1, help='Convert the matrix to its reverse complement if value = 1.', required=False) | 
|  | 93     #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) | 
|  | 94 | 
|  | 95     ########### galaxy arguments ############################## | 
|  | 96     parser.add_argument('-server', '--server', metavar='<SERVEUR>', type=str, nargs=1, required=True) | 
|  | 97     parser.add_argument('-outGalaxy', '--outGalaxy', metavar='<OUT_GALAXY>', type=str, nargs=1, required=True) | 
|  | 98     ########################################################### | 
|  | 99 | 
|  | 100     args = parser.parse_args() | 
|  | 101 | 
|  | 102     matrix_1 = args.matrix_1[0].read() | 
|  | 103     matrix_2 = args.matrix_2[0].read() | 
|  | 104     serverValue = testNone(args.server) | 
|  | 105     format1 = testNone(args.format1) | 
|  | 106     format2 = testNone(args.format2) | 
|  | 107     returnValue = testNone(args.returnResults) | 
|  | 108     outGalaxyValue = testNone(args.outGalaxy) | 
|  | 109     #returnTypeValue = testNone(args.return_type) | 
|  | 110     #permValue = testNone(args.perm) | 
|  | 111     #rcValue = testNone(args.rc) | 
|  | 112     #decimalsValue = testNone(args.decimals) | 
|  | 113 | 
|  | 114     ###########################################################' | 
|  | 115     ## Create the SOAP client to request the RSAT service | 
|  | 116 | 
|  | 117 | 
|  | 118     # Load Client class from suds | 
|  | 119     # Define URL for RSAT services | 
|  | 120     url =  serverDict[serverValue] | 
|  | 121     # Create the client | 
|  | 122     client = Client(url) | 
|  | 123 | 
|  | 124     # Need service interface to perform requests | 
|  | 125     rsat_service = client.service | 
|  | 126 | 
|  | 127     #print client | 
|  | 128 | 
|  | 129     # Define client header | 
|  | 130     userAgent = 'RSAT-Client/v%s (%s; Python %s; %s)' % ( | 
|  | 131         compareMatricesVersion, | 
|  | 132         os.path.basename( __file__ ), | 
|  | 133         platform.python_version(), | 
|  | 134         platform.system() | 
|  | 135     ) | 
|  | 136 | 
|  | 137     httpHeaders = {'User-agent': userAgent} | 
|  | 138     client.set_options(headers=httpHeaders) | 
|  | 139     client.set_options(timeout=300) | 
|  | 140 | 
|  | 141 | 
|  | 142     compareMatricesRequest = { | 
|  | 143 | 
|  | 144        	'output' : 'both', | 
|  | 145 	'matrix_1' : matrix_1, | 
|  | 146 	'matrix_2' : matrix_2, | 
|  | 147 	'format1' : format1, | 
|  | 148 	'format2' : format2, | 
|  | 149 	'return' : returnValue | 
|  | 150 | 
|  | 151     } | 
|  | 152 | 
|  | 153 | 
|  | 154     result = call_run_service(rsat_service, compareMatricesRequest) | 
|  | 155 | 
|  | 156 | 
|  | 157     print url | 
|  | 158 | 
|  | 159     print "###############################################" | 
|  | 160     print "Command performed on server" | 
|  | 161     print result.command | 
|  | 162     print "###############################################" | 
|  | 163     print "Result" | 
|  | 164     print result.server | 
|  | 165 | 
|  | 166     nameFile = "test_compare_matrices" | 
|  | 167 | 
|  | 168     urlResult=result.server.replace("$RSAT/public_html/",url.replace("web_services/RSATWS.wsdl",""))+"/.tab" | 
|  | 169 | 
|  | 170     urllib.urlretrieve(urlResult, nameFile) | 
|  | 171 | 
|  | 172     os.popen("cp "+nameFile+" "+outGalaxyValue) | 
|  | 173 | 
|  | 174 | 
|  | 175 | 
|  | 176 | 
|  | 177 |