view dyadAnalysis_wrapper.py @ 26:2f3462ac56aa draft

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

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

"""#dyad 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 spaced dyads in a set of DNA sequences.
#
###########################################################'
# 
#usage: dyad-analysis_soap.py [-h] -sequence_file <SEQUENCE_FILE> -length
#                             <LENGTH> -organism <ORGANISM> -spacing <SPACING>
#                             [-stats <STATS>] [-type <TYPE>] -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>
#                        Dyad length.
#  -organism <ORGANISM>, --organism <ORGANISM>
#                        Organism.
#  -spacing <SPACING>, --spacing <SPACING>
#                        Spacing between elements of the dyads.
#  -stats <STATS>, --stats <STATS>
#                        List of statistics to return. Supported:occ, mseq,
#                        freq, proba, ratio, zscore, like, pos, rank.
#  -type <TYPE>, --type <TYPE>
#                        Four types are accepted: dr (direct repeats: the
#                        second element is the same as the first one); ir
#                        (inverted repeats: the second element is the revers
#                        complement of the first one); rep (repeats: direct and
#                        inverted repeats are evaluated); any
#  -server <SERVEUR>, --server <SERVEUR>
#
#Version 0.1 - 16/04/2015
# 
###########################################################'"""
__author__ =  'Jocelyn Brayet'
dyadAnalysisVersion = '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.dyad_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 spaced dyads in a set of DNA sequences.', epilog='Version '+dyadAnalysisVersion)

    ########### 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='Dyad length.', required=True)
    parser.add_argument('-organism', '--organism', metavar='<ORGANISM>', type=str, nargs=1, help='Organism.', required=True)
    parser.add_argument('-spacing', '--spacing', metavar='<SPACING>', type=int, nargs=1, help='Spacing between elements of the dyads.', 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)
    parser.add_argument('-type', '--type', metavar='<TYPE>', type=str, nargs=1, help='Four types are accepted: dr (direct repeats: the second element is the same as the first one); ir (inverted repeats: the second element is the revers complement of the first one); rep (repeats: direct and inverted repeats are evaluated); any', 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)
    spacing = testNone(args.spacing)
    stats = testNone(args.stats)
    typeValue = testNone(args.type)
    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)' % (
        dyadAnalysisVersion, 
        os.path.basename( __file__ ),
        platform.python_version(), 
        platform.system()
    )
    
    httpHeaders = {'User-agent': userAgent}
    client.set_options(headers=httpHeaders)
    client.set_options(timeout=300)
    

    dyadAnalysisRequest = {
        
       	'output' : 'both',
	'format' : 'fasta',
	'sequence' : sequence_file,
	'length' : length,
	'organism' : organism,
	'spacing' : spacing,
	'stats' : stats,
	'type' : typeValue

    }

      
    result = call_run_service(rsat_service, dyadAnalysisRequest)

    
    print url

    print "###############################################"
    print "Command performed on server"
    print result.command
    print "###############################################"
    print "Result"
    print result.server    
    
    nameFile = "dyad_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)