view readmap.xml @ 7:c9e267cb84c0 draft

planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/msp_sr_readmap_and_size_histograms commit 263dcfb32e54c4a033cf4a584b961938da969848
author mvdbeek
date Sun, 18 Sep 2016 12:55:27 -0400
parents 68f58363f1c6
children be0c6b6466cc
line wrap: on
line source

<tool id="Readmap" name="Generate readmap and histograms from alignment files" version="1.1.5">
  <description>from sRbowtie aligment</description>
  <requirements>
        <requirement type="package" version="0.12.7">bowtie</requirement>
        <requirement type="package" version="0.7.7">pysam</requirement>
        <requirement type="package" version="3.1.2">R</requirement>
        <requirement type="package" version="2.14">biocbasics</requirement>
        <requirement type="package" version="1.9">numpy</requirement>
  </requirements>
<command interpreter="python">
        readmap.py 
	          #if $refGenomeSource.genomeSource == "history":
         	    --reference_fasta  ## sys.argv[2]
                    $refGenomeSource.ownFile ## index source
          	  #else:
                    #silent reference= filter( lambda x: str( x[0] ) == str( $refGenomeSource.series[0].input.dbkey ), $__app__.tool_data_tables[ 'bowtie_indexes' ].get_fields() )[0][-1]
		    --reference_bowtie_index
                    $reference
          	  #end if
		  --rcode
		  $plotCode
		  --output_readmap
		  $readmap_dataframe
		  --output_size_distribution
		  $size_distribution_dataframe
		  --minquery
		  $minquery
		  --maxquery
		  $maxquery
		  --input
		  #for $i in $refGenomeSource.series
    		    $i.input 
		  #end for
		  --ext
		  #for $i in $refGenomeSource.series
    		    $i.input.ext 
		  #end for
		  --label
		  #for $i in $refGenomeSource.series
    		    "$i.input.name" 
		  #end for
		  --normalization_factor
		  #for $i in $refGenomeSource.series
    		    $i.norm
		  #end for
		  #if $gff:
		    --gff
                    $gff
                  #end if

</command>
  <inputs>
       <conditional name="refGenomeSource">
           <param name="genomeSource" type="select" label="Will you select a reference genome from your history or use a built-in index?" help="Built-ins were indexed using default options">
               <option value="indexed">Use a built-in index</option>
               <option value="history">Use one from the history</option>
           </param>
           <when value="indexed">
	     <repeat name="series" title="Add alignment files">
	       <param name="input" type="data" label="Select multiple alignments to parse" format="tabular,sam,bam">
                  <validator type="dataset_metadata_in_data_table" table_name="bowtie_indexes" metadata_name="dbkey" metadata_column="0" message="database not set for this bowtie output. Select the database(=genome used for matching) manually, or select a reference fasta from your history."/>
               </param>
	       <param name="norm" type="float" value="1" label="Indicate a normalization factor to compare multiple aligments"/>
	     </repeat>
           </when>
           <when value="history">
             <param name="ownFile" type="data" format="fasta" label="Select a fasta file, that served as the reference index for the alignments" />
	     <repeat name="series" title="Add alignment files">
	       <param name="input" type="data" label="Select multiple alignments to parse" format="tabular,sam,bam"/>
	       <param name="norm" type="float" value="1" label="Indicate a normalization factor to compare multiple aligments"/>
	     </repeat>
	   </when>
       </conditional>
                <param name="gff" type="data" format="gff3" optional="true" label="Optional: select a GFF to investigate regions of interest" help="GFF must match genome build"/>
                 <!-- <validator type="dataset_metadata_in_data_table" table_name="bowtie_indexes" metadata_name="dbkey" metadata_column="0" message="GFF database and alignment file databse do not match!"/> -->
                <param name="minquery" type="integer" size="3" value="18" label="Min size of query small RNAs" help="'18' = 18 nucleotides"/>
                <param name="maxquery" type="integer" size="3" value="28" label="Max size of query small RNAs" help="'28' = 28 nucleotides"/>
                <param name="title" type="text" size="15" value= "Readmaps and size distributions" label="Main Titles"/>
                <param name="xlabel" type="text" size="15" value="Coordinates/read size" label="x axis label"/>
                <param name="ylabel" type="text" size="15" value="Number of reads" label="y axis label"/>
                <param name="yrange" type="integer" size="3" value="0" label="y axis range for readmaps. 0 means auto-scaling."/>
                <param name="rows_per_page" type="text" size="9" value="8" label="How many items to display per page?">
		  <validator type="in_range" min="6" max="20" message="Select between 6 and 20 rows, as the readability will suffer otherwise."/>
                </param>
  </inputs>
   <configfiles>
     <configfile name="plotCode">
      ## Setup R error handling to go to stderr
      options( show.error.messages=F,
               error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) } )
      library(RColorBrewer)
      library(lattice)
      library(latticeExtra)
      library(grid)
      library(gridExtra)
      
      ## data frames implementation

      rm=read.delim("${readmap_dataframe}", header=T, row.names=NULL)
      n_samples=length(unique(rm\$sample))
      genes=unique(levels(rm\$gene))
      per_gene_readmap=lapply(genes, function(x) subset(rm, gene==x)) ####### ?
      n_genes=length(per_gene_readmap)
      
      size=read.delim("${size_distribution_dataframe}", header=T, row.names=NULL)
      per_gene_size=lapply(genes, function(x) subset(size, gene==x)) ###### ?

      ## end of data frames implementation
      
      ## functions
      
      plot_readmap=function(df, ...) {
      combineLimits(xyplot(count~coord|factor(sample, levels=unique(sample))+reorder(gene, count, function(x) -sum(abs(x))), 
      data=df, 
      type='h', 
      scales= list(relation="free", x=list(rot=0, cex=0.7, axs="i", tck=0.5), y=list(tick.number=4, rot=90, cex=0.7)),
      xlab=NULL, main=NULL, ylab=NULL, 
      as.table=T, 
      origin = 0, 
      horizontal=FALSE, 
      group=polarity,
      col=c("red","blue"),
      par.strip.text = list(cex=0.7),
      ...))
      }

      plot_size_distribution= function(df, ...) {
          smR.prepanel=function(x,y,...){; yscale=c(-max(abs(y)), max(abs(y)));list(ylim=yscale);}
          bc= barchart(count~as.factor(size)|factor(sample, levels=unique(sample))+gene, data = df, origin = 0,
            horizontal=FALSE,
	    group=polarity,
	    stack=TRUE,
            col=c('red', 'blue'),
            cex=0.75,
            scales=list(y=list(tick.number=4, rot=90, relation="free", cex=0.7), x=list(cex=0.7) ),
            prepanel=smR.prepanel,
            xlab = NULL,
            ylab = NULL,
            main = NULL,
            as.table=TRUE,
            newpage = T,
            par.strip.text = list(cex=0.7),
            ...)
          combineLimits(bc)
          }
          
      ## end of functions
      
      ## function parameters'
      
      par.settings.readmap=list(layout.heights=list(top.padding=0, bottom.padding=-2.5), strip.background = list(col=c("lightblue","lightgreen")) )
      par.settings.size=list(layout.heights=list(top.padding=-1, bottom.padding=-2.5), strip.background = list(col=c("lightblue","lightgreen")) )
      par.settings.combination.readmap=list(layout.heights=list(top.padding=0, bottom.padding=-3), strip.background=list(col=c("lightblue","lightgreen")) )
      par.settings.combination.size=list(layout.heights=list(top.padding=-2, bottom.padding=-0.5), strip.background=list(col=c("lightblue", "lightgreen")) )

      ## end of function parameters'
      
      ## GRAPHS

      if (n_genes > 7) {page_height_simple = 11.69; page_height_combi=11.69; rows_per_page=${rows_per_page}; extrarow=0 } else {
                         rows_per_page= n_genes; page_height_simple = 2.5*n_genes; page_height_combi=page_height_simple*2; extrarow=0 }
                         ## rows_per_page= 8; page_height_simple = 11.69/7*n_genes; page_height_combi=11.69/9*(n_genes*2); extrarow=0 }
                         ## rows_per_page= n_genes; page_height_simple = 11.69/n_genes/4; page_height_combi=11.69/(n_genes*2); extrarow=1 }
     if (n_samples > 4) {page_width = 8.2677*n_samples/4} else {page_width = 8.2677*n_samples/3} # to test

      pdf(file="${readmap_PDF}", paper="special", height=page_height_simple, width=page_width)
      for (i in seq(1,n_genes,rows_per_page)) {
        start=i
        end=i+rows_per_page-1
        if (end>n_genes) {end=n_genes}
        if (${yrange} == 0) { readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, par.settings=par.settings.readmap)) } else {
        readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, ylim=c(-${yrange}, ${yrange}) , par.settings=par.settings.readmap)) }
        args.list=c(readmap_plot.list, list(nrow=rows_per_page, ncol=1,
                                            main=textGrob("Read Maps (nucleotide coordinates)", gp=gpar(cex=1), just="top"),
                                            left=textGrob("${ylabel}", gp=gpar(cex=1), vjust=1, rot=90)
                                            #sub=textGrob("readmap coordinates", gp=gpar(cex=.75), just="bottom")
                                            )
                   )
        do.call(grid.arrange, args.list)
      }
      devname=dev.off()


      pdf(file="${size_PDF}", paper="special", height=page_height_simple, width=page_width)
      for (i in seq(1,n_genes,rows_per_page)) {
        start=i
        end=i+rows_per_page-1
        if (end>n_genes) {end=n_genes}
        plot.list=lapply(per_gene_size[start:end], function(x) plot_size_distribution(x, par.settings=par.settings.size) )
        args.list=c(plot.list, list(nrow=rows_per_page, ncol=1,
                                    main=textGrob("Size distributions (in nucleotides)", gp=gpar(cex=1), just="top"),
                                    left=textGrob("${ylabel}", gp=gpar(cex=1), vjust=1, rot=90)
                                    #sub="readsize in nucleotides"
                                    )
                    )
        do.call(grid.arrange, args.list)
      }
      devname=dev.off()

      pdf(file="${combi_PDF}", paper="special", height=page_height_combi, width=page_width)
      if (rows_per_page %% 2 != 0) { rows_per_page = rows_per_page + 1}
      for (i in seq(1,n_genes,rows_per_page/2)) {
        start=i
        end=i+rows_per_page/2-1
        if (end>n_genes) {end=n_genes}
        if (${yrange} == 0) {readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, par.settings=par.settings.readmap)) } else {
        readmap_plot.list=lapply(per_gene_readmap[start:end], function(x) plot_readmap(x, ylim=c(-${yrange}, ${yrange}), par.settings=par.settings.readmap)) }
        size_plot.list=lapply(per_gene_size[start:end], function(x) plot_size_distribution(x, strip=FALSE, par.settings=par.settings.combination.size))
	plot.list=rbind(readmap_plot.list, size_plot.list )
        args.list=c(plot.list, list(nrow=rows_per_page + extrarow, ncol=1,
                                    main=textGrob("${title}", gp=gpar(cex=1), just="top"),
                                    left=textGrob("${ylabel}", gp=gpar(cex=1), vjust=1, rot=90),
                                    sub=textGrob("${xlabel}", gp=gpar(cex=1), just="bottom")
                                    )
                    )
        do.call(grid.arrange, args.list)
      }
      devname=dev.off()


     </configfile>
   </configfiles>

   <outputs>
   <data format="tabular" name="readmap_dataframe" label="Readmap dataframe"/>
   <data format="tabular" name="size_distribution_dataframe" label="Size distribution dataframe"/>
   <data format="pdf" name="readmap_PDF" label="Readmaps"/>
   <data format="pdf" name="size_PDF" label="Size distribution"/>
   <data format="pdf" name="combi_PDF" label="Size distribution and Readmaps"/>
   </outputs>
<help>

**What it does**

Takes one or more alignment files (BAM, SAM or tabular bowtie output) as input and produces a "Readmap", 
where by default for each "chromosome" the position of the read is recorded on the x-axis, and the y-axis indicates 
the number of reads per position. Reads that map in sense are on the top, reads that map antisense are on the bottom.


.. class:: warningmark

'''TIP''' The input data can be produced using the sRbowtie tool.

----

'''Example'''

Query sequence::
For a SAM file as the following:

  5	16	2L_79	24393	255	17M	*	0	0	CCTTCATCTTTTTTTTT	IIIIIIIIIIIIIIIII	XA:i:0	MD:Z:17	NM:i:0

  11	0	2R_1	12675	255	21M	*	0	0	AAAAAAAACGCGTCCTTGTGC	IIIIIIIIIIIIIIIIIIIII	XA:i:0	MD:Z:21	NM:i:0

  2	16	2L_5	669	255	23M	*	0	0	TGTTGCTGCATTTCTTTTTTTTT	IIIIIIIIIIIIIIIIIIIIIII	XA:i:0	MD:Z:23	NM:i:0

produce a plot like this:

----

.. image:: static/images/readmap.png 
    :height: 800 
    :width: 500

</help>
  <tests>
  <test>
      <param name="genomeSource" value="history" />
      <param name="ownFile" value ="transposons.fasta" ftype="fasta" />
      <param name="series_0|input" value="sample1.srbowtie_out" ftype="tabular"/>
      <param name="series_0|norm" value="1" />
      <param name="series_1|input" value="sample2.srbowtie_out" ftype="tabular"/>
      <param name="series_1|norm" value="1" />
      <param name="series_2|input" value="sample3.srbowtie_out" ftype="tabular"/>
      <param name="series_2|norm" value="1" />
      <param name="minquery" value="20" />
      <param name="maxquery" value="30" />
      <param name="title" value="Readmaps and size distributions" />
      <param name="xlabel" value="Coordinates/read size" />
      <param name="ylabel" value="Number of reads" />
      <param name="rows_per_page" value="8" />
      <output name="readmap_dataframe" ftype="tabular" file="Readmap_dataframe.tab" />
      <output name="size_distribution_dataframe" ftype="tabular" file="Size_distribution_dataframe.tab" />
      <output name="readmap_PDF" ftype="pdf" file="Readmaps.pdf" />
      <output name="size_PDF" ftype="pdf" file="Size_distribution.pdf" />
      <output name="combi_PDF" ftype="pdf" file="Size_distribution_and_Readmaps.pdf" />
  </test>
  </tests>
</tool>