# HG changeset patch # User jbrayet # Date 1439975283 14400 # Node ID 15b36be518518153661492de060fb616f88005c8 # Parent f6c6a0156c8a4b3ce77e21ee877cc335f51ab481 Uploaded diff -r f6c6a0156c8a -r 15b36be51851 sequenza_wrapper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sequenza_wrapper.py Wed Aug 19 05:08:03 2015 -0400 @@ -0,0 +1,274 @@ +#! /usr/bin/python +# -*- coding: utf8 -*- +"""#Sequenza Galaxy - 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 . +# +###########################################################' +# +#usage: sequenza_wrapper.py [-h] -normal -tumor +# -name -gcContent -format +# -estimation +# [-ref_file ] [-cellularity ] +# [-ploidy ] [-selector_index ] +# [-samtools_options ] -outGalaxy +# +# +#Run Sequenza with Galaxy. +# +#optional arguments: +# -h, --help show this help message and exit +# -normal , --normalFile +# Normal input file (BAM, pileup, pileup.gz). +# -tumor , --tumorFile +# Tumor input file (BAM, pileup, pileup.gz). +# -name , --sampleName +# Sample Name. +# -gcContent , --GCfile +# GC content file (txt). +# -format , --fileFormat +# Files format. +# -estimation , --usePersonalEstimation +# To use sequenza estimation or not. +# -ref_file , --refFile +# Index file to samtools mpileup. +# -cellularity , --cellularityToUsed +# If estimation = no, cellularity used. +# -ploidy , --ploidyToUsed +# If estimation = no, plody used. +# -selector_index , --selector +# Source for the reference list. +# -samtools_options , --samtoolsOptions +# Samtools options (-A, -B, -d, -q and -Q). +# -outGalaxy , --outGalaxy +# +#Version 1.2 - 05/08/2015 - Adapted from Jocelyn Brayet, France Genomique team +# +###########################################################""" +__author__ = 'Jocelyn Brayet' + +###########################################################' +## Import + +import argparse +import glob +import os +import signal +import subprocess + +## Tested with python 2.6.6 +sequenzaVersion = '1.2 - 05/08/2015' + +################################ functions ############################################################ +## Define a function to test arguments + +def testNone(argument): + """ + Test if argument is None or not. + argument -> argument gived by user (XML file) + """ + + if not argument is None: + variable = argument[0] + else: + variable = "" + return variable + +def subprocess_setup(): + """ + Python installs a SIGPIPE handler by default. This is usually not what non-Python subprocesses expect. + """ + + signal.signal(signal.SIGPIPE, signal.SIG_DFL) + +def createHTML(resultFile,resultPath,sample): + """ + Building a result HTML displayed in Galaxy. + resultFile -> HTML file + resultPath -> Galaxy result path + sample -> sample name + """ + + resultHTML=open(resultFile,"w") + resultHTML.write("\n\n\n\n") + resultHTML.write("

Sequenza - results

\n") + resultHTML.write("

\n\n\n\n\n") + resultHTML.write("\n\n\n\n") + resultHTML.write("\n
Additional files
Out Sequenza
Out small Sequenza
Cellularity and ploidy matrix (RData)
Sequenza extract (RData)
Log file
\n

\n") + resultHTML.write("

Genome-wide view of the allele and copy number state

\n") + resultHTML.write("

\n") + resultHTML.write("

Genome-wide allele-specific copy number profile obtained from exome sequencing (top), genome-wide absolute copy number profile obtained from exome sequencing (middle) and depth ratio (bottom).

\n") + + fileList=glob.glob(resultPath+"/segments_*.txt") + alternativeSolutionsList="" + + for segmentsFile in fileList: + if not "segments_1.txt" in segmentsFile: + alternativeSolutionsList = alternativeSolutionsList+" "+segmentsFile + + os.system("mkdir "+resultPath+"/segments") + os.system("mv "+alternativeSolutionsList+" "+resultPath+"/segments") + cmdLine="cd "+resultPath+"; tar czf "+sample+"_segments.tar.gz segments" + os.system(cmdLine) + + resultHTML.write("

File (best solution or user solution): Segments

\n") + resultHTML.write("

File (alternative solutions): Segments

\n") + resultHTML.write("

File : Mutations

\n") + resultHTML.write("

Confidence intervals, confidence region and point estimate

\n") + resultHTML.write("

\n") + resultHTML.write("

Result from the inference over the defined range of cellularity and ploidy. Color intensity indicates the log posterior probability of corresponding cellularity/ploidy values.

\n") + resultHTML.write("

\n") + resultHTML.write("

\n") + resultHTML.write("

Observed depth ratio and BAF values for each genomic segment (black circles and dots) along with the representative joint LPP density (colors). The representative joint LPP density is calculated for the best cellularity and ploidy.

\n") + resultHTML.write("

File : Cellularity and ploidy

\n") + resultHTML.write("

\n") + resultHTML.write("

Observed depth ratio and BAF values for each genomic segment (black circles and dots) along with the representative joint LPP density (colors). The representative joint LPP density is calculated for the alternative cellularity and ploidy (scroll).

\n") + resultHTML.write("

File : Alternative solutions

\n") + resultHTML.write("

Normalization of depth ratio

\n") + resultHTML.write("

\n") + resultHTML.write("

Visualization of depth.ratio bias in relation of GC content (left), and resulting normalization effect (right)

\n") + resultHTML.write("

Plot chromosome view with mutations, BAF, depth ratio and segments (best solution or user solution)

\n") + resultHTML.write("

\n") + resultHTML.write("

Plots of mutant allele frequency (top), B allele frequency (middle) and depth ratio (bottom) vs. chromosome position (scroll).

\n") + resultHTML.write("\n") + resultHTML.close() + +if __name__ == '__main__': + + ########### sequenza arguments #################### + parser = argparse.ArgumentParser(description='Run Sequenza with Galaxy.', epilog='Version '+sequenzaVersion) + + parser.add_argument('-normal', '--normalFile', metavar='', type=str, nargs=1, help='Normal input file (BAM, pileup, pileup.gz).', required=True) + parser.add_argument('-tumor', '--tumorFile', metavar='', type=str, nargs=1, help='Tumor input file (BAM, pileup, pileup.gz).', required=True) + parser.add_argument('-name', '--sampleName', metavar='', type=str, nargs=1, help='Sample Name.', required=True) + parser.add_argument('-gcContent', '--GCfile', metavar='', type=str, nargs=1, help='GC content file (txt).', required=True) + parser.add_argument('-format', '--fileFormat', metavar='', type=str, nargs=1, help='Files format.', required=True) + parser.add_argument('-estimation', '--usePersonalEstimation', metavar='', type=str, nargs=1, help='To use sequenza estimation or not.', required=True) + parser.add_argument('-ref_file', '--refFile', metavar='', type=str, nargs=1, help='Index file to samtools mpileup.', required=False) + parser.add_argument('-cellularity', '--cellularityToUsed', metavar='', type=int, nargs=1, help='If estimation = no, cellularity used.', required=False) + parser.add_argument('-ploidy', '--ploidyToUsed', metavar='', type=int, nargs=1, help='If estimation = no, plody used.', required=False) + parser.add_argument('-selector_index', '--selector', metavar='', type=str, nargs=1, help='Source for the reference list.', required=False) + parser.add_argument('-samtools_options', '--samtoolsOptions', metavar='', type=str, nargs=1, help='Samtools options (-A, -B, -d, -q and -Q).', required=False) + + ################################ galaxy arguments ############################################################ + parser.add_argument('-outGalaxy', '--outGalaxy', metavar='', type=str, nargs=1, required=True) + ###########################################################' + + args = parser.parse_args() + + normalFile = testNone(args.normalFile) + tumorFile = testNone(args.tumorFile) + sampleName = testNone(args.sampleName) + gcContent = testNone(args.GCfile) + fileFormat = testNone(args.fileFormat) + usePersonalEstimation = testNone(args.usePersonalEstimation) + refFile = testNone(args.refFile) + cellularityToUsed = testNone(args.cellularityToUsed) + ploidyToUsed = testNone(args.ploidyToUsed) + selector = testNone(args.selector) + samtoolsOptions = testNone(args.samtoolsOptions) + outGalaxyValue = testNone(args.outGalaxy) + + ################Rscrip PATH################# + RscriptPath = "Rscript --vanilla " + ############################################ + + pathFile = os.path.dirname(os.path.realpath(__file__)) + samtoolsPath = pathFile.replace("copy_number","samtools/") + outGalaxyValueDir = outGalaxyValue.replace(".dat","_files") + os.popen("mkdir "+outGalaxyValueDir) + os.popen("chmod 777 -R "+outGalaxyValueDir) + + log = open(outGalaxyValueDir+"/logFile.txt", "w") + + log.write(samtoolsOptions) + + if fileFormat=="BAM" : + if selector=="cached": + + cmd=samtoolsPath+"samtools_wrapper.py -p 'samtools mpileup' -p \'-f \""+refFile+"\"\' -d \" \" \""+normalFile+"\" \"bam\" \"bam_normal\" -p\' "+samtoolsOptions+" > \""+normalFile+".tmp\" \'" + proc = subprocess.Popen( args=cmd, shell=True) + proc.wait() + os.system("gzip -f -c "+normalFile+".tmp > "+normalFile+".gz") + os.system("rm -f "+normalFile+".tmp") + + cmd=samtoolsPath+"samtools_wrapper.py -p 'samtools mpileup' -p \'-f \""+refFile+"\"\' -d \" \" \""+tumorFile+"\" \"bam\" \"bam_normal\" -p\' "+samtoolsOptions+" > \""+tumorFile+".tmp\" \'" + proc = subprocess.Popen( args=cmd, shell=True) + proc.wait() + os.system("gzip -f -c "+tumorFile+".tmp > "+tumorFile+".gz") + os.system("rm -f "+tumorFile+".tmp") + + else: + + cmd=samtoolsPath+"samtools_wrapper.py -p 'samtools mpileup' -d \'-f \""+refFile+"\"\' \"fa\" \"reference_input\" -d \" \" \""+normalFile+"\" \"bam\" \"bam_normal\" -p\' "+samtoolsOptions+" > \""+normalFile+".tmp\" \'" + proc = subprocess.Popen( args=cmd, shell=True) + proc.wait() + os.system("gzip -f -c "+normalFile+".tmp > "+normalFile+".gz") + os.system("rm -f "+normalFile+".tmp") + + cmd=samtoolsPath+"samtools_wrapper.py -p 'samtools mpileup' -d \'-f \""+refFile+"\"\' \"fa\" \"reference_input\" -d \" \" \""+tumorFile+"\" \"bam\" \"bam_normal\" -p\' "+samtoolsOptions+" > \""+tumorFile+".tmp\" \'" + proc = subprocess.Popen( args=cmd, shell=True) + proc.wait() + os.system("gzip -f -c "+tumorFile+".tmp > "+tumorFile+".gz") + os.system("rm -f "+tumorFile+".tmp") + + if fileFormat=="pileup" : + os.system("gzip -f -c "+normalFile+" > "+ normalFile+".gz") + os.system("gzip -f -c "+tumorFile+" > "+tumorFile+".gz") + + if fileFormat=="pileup_gz" : + os.system("cp -f "+normalFile+" "+normalFile+".gz") + os.system("cp -f "+tumorFile+" "+tumorFile+".gz") + + + + + ############################################################# + + if usePersonalEstimation=="yes": + + cellularityToUsed = float(cellularityToUsed)/100 + command = RscriptPath+pathFile+"/Sequenza_analysis.R -normal "+normalFile+".gz"+" -tumor "+tumorFile+".gz"+" -out "+outGalaxyValueDir+" -gcContent "+gcContent+" -name "+sampleName+ " -cellularity "+str(cellularityToUsed)+" -ploidy "+str(ploidyToUsed) + log.write(str(command)) + + Rscrpit = subprocess.Popen(command, preexec_fn=subprocess_setup, stdout=log, stderr=log, shell=True) + Rscrpit.wait() + + else : + + command = RscriptPath+pathFile+"/Sequenza_analysis.R -normal "+normalFile+".gz"+" -tumor "+tumorFile+".gz"+" -out "+outGalaxyValueDir+" -gcContent "+gcContent+" -name "+sampleName+ " -cellularity 0 -ploidy 0" + log.write(str(command)) + + Rscrpit = subprocess.Popen(command, preexec_fn=subprocess_setup, stdout=log, stderr=log, shell=True) + Rscrpit.wait() + + ############################################################# + + createHTML(outGalaxyValue,outGalaxyValueDir,sampleName) + + + log.close() + + + + + + + + + + + +