# HG changeset patch # User jbrayet # Date 1442923364 14400 # Node ID 24dd26f2918f659bcccce0a3730e30db20605100 # Parent b34046b0255623f93e6f95235fa8694d46c2f4ff Uploaded diff -r b34046b02556 -r 24dd26f2918f matrixScan_wrapper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/matrixScan_wrapper.py Tue Sep 22 08:02:44 2015 -0400 @@ -0,0 +1,314 @@ +#! /usr/bin/python +# -*- coding: utf8 -*- +"""#Matrix Scan - developed by Jocelyn Brayet +#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 . +# +###########################################################' +# +#Client to download matrix scan results from RSAT server. +# +# +#usage: matrixScan_wrapper.py [-h] -sequence -matrix +# -n_treatment -markov +# -matrix_format +# [-organism ] [-background ] +# [-origin ] [-return_param ] +# [-uth ] -server -outGalaxy +# +# +#optional arguments: +# -h, --help show this help message and exit +# -sequence , --sequence +# Sequence(s) to scan - all the formats supported in +# RSAT can be used as input (default: fasta). +# -matrix , --matrix +# Matrix/ces to scan with. The matrix format is +# specified with the option "matrix_format" (see below) +# Default format: tab. +# -n_treatment , --n_treatment +# Treatment of N characters. These characters are often +# used in DNA sequences to represent undefined or masked +# nucleotides (skip or score). +# -markov , --markov +# Order of the markov chain for the background model. +# -matrix_format , --matrix_format +# Supported fields: tab, cb, transfac, jaspar, +# consensus, gibbs, meme, assembly. +# -organism , --organism +# To use a precalculated background model from RSAT, +# choose the organism corresponding to the background +# model. +# -background , --background +# Type of sequences used as background model for +# estimating expected oligonucleotide frequencies. +# Supported: upstream, upstream-noorf +# -origin , --origin +# Define the origin for the calculation of positions. +# -return_param , --return_param +# List of fields to return. +# -uth , --uth +# Lower threshold on some parameter. +# -server , --server +# -outGalaxy , --outGalaxy +# +#Version 0.1 - 10/03/2015 +# +###########################################################""" +__author__ = 'Jocelyn Brayet' +matrixScanVersion = '0.1 - 10/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.matrix_scan(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 matrix-scan results from RSAT server.', epilog='Version '+matrixScanVersion) + + + ########### convert matrix arguments #################### + + parser.add_argument('-sequence', '--sequence', metavar='', type=argparse.FileType('r'), nargs=1, help='Sequence(s) to scan - all the formats supported in RSAT can be used as input (default: fasta).', required=True) + parser.add_argument('-matrix', '--matrix', metavar='', type=argparse.FileType('r'), nargs=1, help='Matrix/ces to scan with. The matrix format is specified with the option "matrix_format" (see below) Default format: tab.', required=True) + parser.add_argument('-n_treatment', '--n_treatment', metavar='', type=str, nargs=1, help='Treatment of N characters. These characters are often used in DNA sequences to represent undefined or masked nucleotides (skip or score).', required=True) + parser.add_argument('-markov', '--markov', metavar='', type=int, nargs=1, help='Order of the markov chain for the background model.', required=True) + parser.add_argument('-matrix_format', '--matrix_format', metavar='', type=str, nargs=1, help='Supported fields: tab, cb, transfac, jaspar, consensus, gibbs, meme, assembly.', required=True) + + parser.add_argument('-organism', '--organism', metavar='', type=str, nargs=1, help='To use a precalculated background model from RSAT, choose the organism corresponding to the background model.', required=False) + parser.add_argument('-background', '--background', metavar='', type=str, nargs=1, help='Type of sequences used as background model for estimating expected oligonucleotide frequencies. Supported: upstream, upstream-noorf', required=False) + #parser.add_argument('-background_model', '--background_model', metavar='', type=str, nargs=1, help='Background model is a tab-delimited specification of oligonucleotide frequencies.', required=True) + parser.add_argument('-origin', '--origin', metavar='', type=str, nargs=1, help='Define the origin for the calculation of positions.', required=False) + parser.add_argument('-return_param', '--return_param', metavar='', type=str, nargs=1, help='List of fields to return.', required=False) + parser.add_argument('-uth', '--uth', metavar='', type=str, nargs=1, help='Lower threshold on some parameter.', required=False) + + #parser.add_argument('-quick', '--quick', metavar='', type=str, help='Delegates scanning to the C program matrix-scan-quick.', required=True) + + + ########### galaxy arguments ############################## + parser.add_argument('-server', '--server', metavar='', type=str, nargs=1, required=True) + parser.add_argument('-outGalaxy', '--outGalaxy', metavar='', type=str, nargs=1, required=True) + ########################################################### + + + args = parser.parse_args() + + + + + ###########################################################' + + sequence_file = args.sequence[0].read() + matrix_file = args.matrix[0].read() + serverValue = testNone(args.server) + matrixFormatValue = testNone(args.matrix_format) + n_treatmentValue = testNone(args.n_treatment) + outGalaxyValue = testNone(args.outGalaxy) + markovValue = testNone(args.markov) + organismValue = testNone(args.organism) + backgroundValue = testNone(args.background) + originValue = testNone(args.origin) + returnValue = testNone(args.return_param) + uthValue = testNone(args.uth) + + #backgroundModelValue = testNone(args.background_model) + #quickValue = testNone(args.quick) + + ###########################################################' + ## 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 + #print(client.factory.create('matrix_scan')) + + #request = + # (MatrixScanRequest){ + # output = None + # sequence = None + # tmp_sequence_infile = None + # matrix = None + # tmp_matrix_infile = None + # sequence_format = None + # matrix_format = None + # quick = None + # n_treatment = None + # consensus_name = None + # pseudo = None + # equi_pseudo = None + # top_matrices = None + # background_model = None + # tmp_background_infile = None + # organism = None + # background = None + # background_input = None + # background_window = None + # markov = None + # background_pseudo = None + # return_fields = None + # sort_distrib = None + # lth[] = + # uth[] = + # str = None + # verbosity = None + # origin = None + # decimals = None + # crer_ids = None + # } + #} + + # Define client header + + userAgent = 'RSAT-Client/v%s (%s; Python %s; %s)' % ( + matrixScanVersion, + os.path.basename( __file__ ), + platform.python_version(), + platform.system() + ) + + httpHeaders = {'User-agent': userAgent} + client.set_options(headers=httpHeaders) + client.set_options(timeout=300) + + + if not uthValue == "": + matrixScanRequest = { + + #'output' : "ticket", + 'sequence' : sequence_file, + 'matrix' : matrix_file, + 'markov' : markovValue, + 'n_treatment' : n_treatmentValue, + 'matrix_format' : matrixFormatValue, + 'background' : backgroundValue, + 'organism' : organismValue, + + 'origin' : originValue, + 'return_fields' : returnValue, + 'uth' : 'pval ' + uthValue, + + #'background_window' : 12, + #'background_input' : " ", + + 'quick' : "-quick" + + } + + else: + matrixScanRequest = { + + #'output' : "ticket", + 'sequence' : sequence_file, + 'matrix' : matrix_file, + 'markov' : markovValue, + 'n_treatment' : n_treatmentValue, + 'matrix_format' : matrixFormatValue, + 'background' : backgroundValue, + 'organism' : organismValue, + + 'origin' : originValue, + 'return_fields' : returnValue, + #'background_window' : 12, + #'background_input' : " ", + + 'quick' : "-quick" + + } + + + + #print matrixScanRequest + + result = call_run_service(rsat_service, matrixScanRequest) + + print url + + print "###############################################" + print "Command performed on server" + print result.command + print "###############################################" + print "Result" + print result.server + + nameFile = "matrix-scan_results.txt" + + urlResult=result.server.replace("$RSAT/public_html/",url.replace("web_services/RSATWS.wsdl","")) + + + urllib.urlretrieve(urlResult, nameFile) + + + os.popen("cp "+nameFile+" "+outGalaxyValue) + +