<tool id="venn" name="Venn Diagram">
  <description>from summary counts</description>
  <command interpreter="python">$script_file $labels $counts $output $script</command>
  <inputs>   
      <param name="labels" type="text" value="A,B" size="50" label="Set Labels"/>
    <param name="counts" type="text" value="30,10,20" size="80" label="Counts in each region" help="region order: two sets: A, B, AB; three sets: A,B,C,AB,BC,AC,ABC"/>
  </inputs>
  <configfiles>
    <configfile name="script_file">
import os
labels = '${labels}'.replace(' ','_').split(',')
counts = '${counts}'.replace(' ','').split(',')
counts = map(int,counts)
rscript = open('${script}','w')
rscript.write("options(warn=-1)\n")
rscript.write("pdf('"+"${output}"+"')\n")
rscript.write("library(grid)\n")
rscript.write("library(VennDiagram)\n")
if len(labels)==2:
    for i in range(2):
        counts[i+1] = counts[i+1]+counts[i]
    rscript.write("venn =venn.diagram(\n\tx=list(\n\t\t"+labels[0]+"=c(1:"+str(counts[0])+","+str(counts[1]+1)+":"+str(counts[2])+"),\n\t\t"+labels[1]+"="+str(counts[0]+1)+":"+str(counts[2])+"),\n\tfilename=NULL,\n\tfill=c('red','blue'),\n\tcol='transparent',\n\talpha=0.5,\n\tlabel.col='black',\n\tcex=2,\n\tlwd=0,\n\tfontfamily='serif',\n\tfontface='bold',\n\tcat.col = c('red', 'blue'),\n\tcat.cex=2,\n\tcat.fontfamily='serif',\n\tcat.fontface='bold')\n")
else:
    for i in range(6):
        counts[i+1] = counts[i+1]+counts[i]
    rscript.write("venn =venn.diagram(\n\tx=list(\n\t\t"+labels[0]+"=c(1:"+str(counts[0])+","+str(counts[2]+1)+":"+str(counts[3])+","+str(counts[4]+1)+":"+str(counts[6])+"),\n\t\t"+labels[1]+"=c("+str(counts[0]+1)+":"+str(counts[1])+","+str(counts[2]+1)+":"+str(counts[4])+","+str(counts[5]+1)+":"+str(counts[6])+"),\n\t\t"+labels[2]+"=c("+str(counts[1]+1)+":"+str(counts[2])+","+str(counts[3]+1)+":"+str(counts[6])+")),\n\tfilename=NULL,\n\tfill=c('red','blue','green'),\n\tcol='transparent',\n\talpha=0.5,\n\tlabel.col='black',\n\tcex=2,\n\tlwd=0,\n\tfontfamily='serif',\n\tfontface='bold',\n\tcat.col = c('red', 'blue','green'),\n\tcat.cex=2,\n\tcat.fontfamily='serif',\n\tcat.fontface='bold')\n")
rscript.write("grid.draw(venn)\n")
rscript.write("dev.off()\n")
rscript.close()
os.system("cat "+"${script}"+" | R --vanilla --slave")    
    </configfile>
  </configfiles>

  <outputs>
   <data format="txt" name="script" label="${tool.name} on ${on_string}: (script)" />
    <data format="pdf" name="output" label="${tool.name} on ${on_string}: (plot)" />
   
  </outputs>

<help>
.. class:: infomark

This is a wrapper for R package VennDiagram. It allows you to plot two-set or three-set venn diagrams based on counts. The R script used to generate the plot is also in the output. 

Input: labels for sets and counts for each region in the diagram.

A: A-only

B: B-only

C: C-only

AB: in A and B but not C

BC: in B and C but not A

AC: in A and C but not B

ABC: in A, B, and C 

-----

**Example**

Labels: X,Y

Counts: 30,10,20


.. image:: ./static/images/venn2.png


Labels: A,B,C

Counts: 10,20,30,40,50,60,70


.. image:: ./static/images/venn3.png


</help>
</tool>
