view oligoAnalysis_wrapper.py @ 52:a1d369ead6d7 draft default tip

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

#! /usr/bin/python
# -*- coding: utf8 -*-

"""#oligo analysis soap - 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/>.
#
###########################################################'
# 
#Analysis of oligomer occurrences in nucleotidic of peptidic sequences.
#
###########################################################'
# 
# Client to have list RSAT oligo analysis.
#
#
#usage: oligo-analysis_soap.py [-h] -sequence_file <SEQUENCE_FILE> -length
#                              <LENGTH> -organism <ORGANISM> -server <SERVEUR>
#
#optional arguments:
#  -h, --help            show this help message and exit
#  -sequence_file <SEQUENCE_FILE>, --sequence_file <SEQUENCE_FILE>
#                        Input sequence (FASTA file).
#  -length <LENGTH>, --length <LENGTH>
#                        Oligomer length.
#  -organism <ORGANISM>, --organism <ORGANISM>
#                        Organism.
#  -stats <STATS>, --stats <STATS>
#                        List of statistics to return. Supported:occ, mseq,
#                        freq, proba, ratio, zscore, like, pos, rank.
#  -server <SERVEUR>, --server <SERVEUR>
#
#Version 0.1 - 16/04/2015
#
# 
###########################################################'"""
__author__ =  'Jocelyn Brayet'
oligoAnalysisVersion = '0.1 - 16/04/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(rsat_service, args):
	"""
	Run job in RSAT server.
		service -> RSAT web service
		args -> web service request 	 
	"""
	
	result = rsat_service.oligo_analysis(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 = {
    
    "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='Analysis of oligomer occurrences in nucleotidic of peptidic sequences.', epilog='Version '+oligoAnalysisVersion)

    ########### compare matrices arguments ####################

    parser.add_argument('-sequence_file', '--sequence_file', metavar='<SEQUENCE_FILE>', type=argparse.FileType('r'), nargs=1, help='Input sequence (FASTA file).', required=True)
    parser.add_argument('-length', '--length', metavar='<LENGTH>', type=int, nargs=1, help='Oligomer length.', required=True)
    parser.add_argument('-organism', '--organism', metavar='<ORGANISM>', type=str, nargs=1, help='Organism.', required=True)
    parser.add_argument('-stats', '--stats', metavar='<STATS>', type=str, nargs=1, help='List of statistics to return. Supported:occ, mseq, freq, proba, ratio, zscore, like, pos, rank.', 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()

    sequence_file = args.sequence_file[0].read()
    serverValue = testNone(args.server)
    length = testNone(args.length)
    organism = testNone(args.organism)
    stats = testNone(args.stats)
    outGalaxyValue = testNone(args.outGalaxy)

    ###########################################################'
    ## 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)' % (
        oligoAnalysisVersion, 
        os.path.basename( __file__ ),
        platform.python_version(), 
        platform.system()
    )
    
    httpHeaders = {'User-agent': userAgent}
    client.set_options(headers=httpHeaders)
    client.set_options(timeout=300)
    

    oligoAnalysisRequest = {
        
       	'output' : 'both',
	'format' : 'fasta',
	'sequence' : sequence_file,
	'length' : length,
	'organism' : organism,
	'stats' : stats
  
    }

      
    result = call_run_service(rsat_service, oligoAnalysisRequest)

    
    print url

    print "###############################################"
    print "Command performed on server"
    print result.command
    print "###############################################"
    print "Result"
    print result.server    
    
    nameFile = "oligo-analysis_results.txt"
    
    urlResult=result.server.replace("$RSAT/public_html/",url.replace("web_services/RSATWS.wsdl",""))

    print urlResult
    
    urllib.urlretrieve(urlResult, nameFile)
    
    os.popen("cp "+nameFile+" "+outGalaxyValue)