view matrixScan_wrapper.py @ 30:24dd26f2918f draft

Uploaded
author jbrayet
date Tue, 22 Sep 2015 08:02:44 -0400
parents
children
line wrap: on
line source

#! /usr/bin/python
# -*- coding: utf8 -*-
"""#Matrix Scan - 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 matrix scan results from RSAT server.
#
#
#usage: matrixScan_wrapper.py [-h] -sequence <SEQUENCE> -matrix <MATRIX>
#                             -n_treatment <N_TREATMENT> -markov <MARKOV>
#                             -matrix_format <MATRIX_FORMAT>
#                             [-organism <ORGANISM>] [-background <BACKGROUND>]
#                             [-origin <ORIGIN>] [-return_param <RETURN_PARAM>]
#                             [-uth <UTH>] -server <SERVEUR> -outGalaxy
#                             <OUT_GALAXY>
#
#optional arguments:
#  -h, --help            show this help message and exit
#  -sequence <SEQUENCE>, --sequence <SEQUENCE>
#                        Sequence(s) to scan - all the formats supported in
#                        RSAT can be used as input (default: fasta).
#  -matrix <MATRIX>, --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>, --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>, --markov <MARKOV>
#                        Order of the markov chain for the background model.
#  -matrix_format <MATRIX_FORMAT>, --matrix_format <MATRIX_FORMAT>
#                        Supported fields: tab, cb, transfac, jaspar,
#                        consensus, gibbs, meme, assembly.
#  -organism <ORGANISM>, --organism <ORGANISM>
#                        To use a precalculated background model from RSAT,
#                        choose the organism corresponding to the background
#                        model.
#  -background <BACKGROUND>, --background <BACKGROUND>
#                        Type of sequences used as background model for
#                        estimating expected oligonucleotide frequencies.
#                        Supported: upstream, upstream-noorf
#  -origin <ORIGIN>, --origin <ORIGIN>
#                        Define the origin for the calculation of positions.
#  -return_param <RETURN_PARAM>, --return_param <RETURN_PARAM>
#                        List of fields to return.
#  -uth <UTH>, --uth <UTH>
#                        Lower threshold on some parameter.
#  -server <SERVEUR>, --server <SERVEUR>
#  -outGalaxy <OUT_GALAXY>, --outGalaxy <OUT_GALAXY>
#
#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='<SEQUENCE>', 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='<MATRIX>', 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='<N_TREATMENT>', 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='<MARKOV>', type=int, nargs=1, help='Order of the markov chain for the background model.', required=True)
    parser.add_argument('-matrix_format', '--matrix_format', metavar='<MATRIX_FORMAT>', type=str, nargs=1, help='Supported fields: tab, cb, transfac, jaspar, consensus, gibbs, meme, assembly.', required=True)

    parser.add_argument('-organism', '--organism', metavar='<ORGANISM>', 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='<BACKGROUND>', 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='<BACKGROUND_MODEL>', type=str, nargs=1, help='Background model is a tab-delimited specification of oligonucleotide frequencies.', required=True)
    parser.add_argument('-origin', '--origin', metavar='<ORIGIN>', type=str, nargs=1, help='Define the origin for the calculation of positions.', required=False)
    parser.add_argument('-return_param', '--return_param', metavar='<RETURN_PARAM>', type=str, nargs=1, help='List of fields to return.', required=False)
    parser.add_argument('-uth', '--uth', metavar='<UTH>', type=str, nargs=1, help='Lower threshold on some parameter.', required=False)

    #parser.add_argument('-quick', '--quick', metavar='<QUICK>', type=str, help='Delegates scanning to the C program matrix-scan-quick.', required=True)


    ########### 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()




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

    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[] = <empty>
    #       uth[] = <empty>
    #       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)