view convertMatrix_wrapper.py @ 52:a1d369ead6d7 draft default tip

Uploaded
author jbrayet
date Tue, 29 Sep 2015 08:25:39 -0400
parents 4f7cb568384f
children
line wrap: on
line source

#! /usr/bin/python
# -*- coding: utf8 -*-
"""#Convert Matrix - developed by Jocelyn Brayet <jocelyn.brayet@curie.fr>
#Copyright (C) 2015  Institut Curie
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################'
#
#Client to download convert matrix results from RSAT server.
#
#
#usage: convertMatrix_wrapper.py [-h] -matrix <MATRIX> -input_form <INPUT_FORM>
#                                -output_form <OUTPUT_FORM>
#                                [-return_type <RETURN_TYPE>] [-perm <PERM>]
#                                [-rc <RC>] [-decimals <DECIMALS>] -server
#                                <SERVEUR> -outGalaxy <OUT_GALAXY>
#
#optional arguments:
#  -h, --help            show this help message and exit
#  -matrix <MATRIX>, --matrix <MATRIX>
#                        Matrix (or assembly or features) you want to convert.
#  -input_form <INPUT_FORM>, --input_form <INPUT_FORM>
#                        Input matrix format. Supported: alignace, assembly,
#                        cb, clustal, consensus, feature, gibbs, infogibbs,
#                        meme, motifsampler, tab, transfac.
#  -output_form <OUTPUT_FORM>, --output_form <OUTPUT_FORM>
#                        Output matrix format. Supported: consensus, patser,
#                        tab, transfac.
#  -return_type <RETURN_TYPE>, --return_type <RETURN_TYPE>
#                        Result type (matrix content). Supported: consensus,
#                        counts, frequencies, info, logo, margins, parameters,
#                        profile, sites, wdistrib, weights.
#  -perm <PERM>, --perm <PERM>
#                        Number of permuted matrices to return.
#  -rc <RC>, --rc <RC>   Convert the matrix to its reverse complement if value
#                        = 1.
#  -decimals <DECIMALS>, --decimals <DECIMALS>
#                        Number of decimals to print for real matrices
#                        (frequencies, weights, information) or to compute
#                        score distributions.
#  -server <SERVEUR>, --server <SERVEUR>
#  -outGalaxy <OUT_GALAXY>, --outGalaxy <OUT_GALAXY>
#
###########################################################"""
__author__ =  'Jocelyn Brayet'
convertMatrixVersion = '0.1 - 06/03/2015'


###########################################################'
## Import

import argparse
import os
import urllib
from suds.client import Client
import platform

###########################################################'

################################ functions ############################################################
## Define a function to make a service perform the desired request using provided arguments
def call_run_service(service, args):
	"""
	Run job in RSAT server.
		service -> RSAT web service
		args -> web service request 	 
	"""
	
	result = rsat_service.convert_matrix(args)
	return result

def testNone(argument):
	"""
	Test if argument is None or not.
		argument -> argument give by user
	"""

	if not argument is None:
		variable = argument[0]
	else:
		variable = ""
	return variable

###########################################################'

###########################################################'
# server dictionary
serverDict = {
    
    #http://protists.rsat.eu/
    "fr_ens":"http://rsat01.biologie.ens.fr/rsa-tools/web_services/RSATWS.wsdl",
    "fr_mrs":"http://rsat-tagc.univ-mrs.fr/rsat/web_services/RSATWS.wsdl",
    "fr_ro":"http://rsat.sb-roscoff.fr/web_services/RSATWS.wsdl",
    "fr_mrs_2":"http://pedagogix-tagc.univ-mrs.fr/rsat/web_services/RSATWS.wsdl",
    "es":"http://floresta.eead.csic.es/rsat/web_services/RSATWS.wsdl",
    "mx":"http://embnet.ccg.unam.mx/rsa-tools/web_services/RSATWS.wsdl"
    
    }

###########################################################'

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Client to download convert-matrix results from RSAT server.', epilog='Version '+convertMatrixVersion)


    ########### convert matrix arguments ####################

    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)
    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)
    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)
    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)
    parser.add_argument('-perm', '--perm', metavar='<PERM>', type=int, nargs=1, help='Number of permuted matrices to return.', required=False)
    parser.add_argument('-rc', '--rc', metavar='<RC>', type=int, nargs=1, help='Convert the matrix to its reverse complement if value = 1.', required=False)
    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)
    
    

    ########### galaxy arguments ##############################
    parser.add_argument('-server', '--server', metavar='<SERVEUR>', type=str, nargs=1, required=True)
    parser.add_argument('-outGalaxy', '--outGalaxy', metavar='<OUT_GALAXY>', type=str, nargs=1, required=True)
    ###########################################################

    args = parser.parse_args()


    matrix_file = args.matrix[0].read()
    serverValue = testNone(args.server)
    outGalaxyValue = testNone(args.outGalaxy)
    inputFormValue = testNone(args.input_form)
    outputFormValue = testNone(args.output_form)
    returnTypeValue = testNone(args.return_type)
    permValue = testNone(args.perm)
    rcValue = testNone(args.rc)
    decimalsValue = testNone(args.decimals)
    
    ###########################################################'
    ## Create the SOAP client to request the RSAT service
    
    
    # Load Client class from suds
    # Define URL for RSAT services 
    url =  serverDict[serverValue]
    # Create the client
    client = Client(url)
    
    # Need service interface to perform requests
    rsat_service = client.service
    
    
    #print client
    
    
    # Define client header
    userAgent = 'RSAT-Client/v%s (%s; Python %s; %s)' % (
        convertMatrixVersion, 
        os.path.basename( __file__ ),
        platform.python_version(), 
        platform.system()
    )
    
    httpHeaders = {'User-agent': userAgent}
    client.set_options(headers=httpHeaders)
    client.set_options(timeout=300)
    
    
    convertMatrixRequest = {
        
        'matrix' : matrix_file,
        'from' : inputFormValue,
        'to' : outputFormValue,
        'output' : 'both',
        'return' : returnTypeValue,
        'perm' : permValue,
        'rc' : rcValue,
        'decimals' : decimalsValue
    
        
    }
    
      
    result = call_run_service(rsat_service, convertMatrixRequest)
    
    print url
    
    print "###############################################"
    print "Command performed on server"
    print result.command
    print "###############################################"
    print "Result"
    print result.server
    
    
    nameFile = "convert-matrix_results.txt"
    
    urlResult=result.server.replace("$RSAT/public_html/",url.replace("web_services/RSATWS.wsdl",""))
    
    urllib.urlretrieve(urlResult, nameFile)
    
    os.popen("cp "+nameFile+" "+outGalaxyValue)