# HG changeset patch # User devteam # Date 1380646537 14400 # Node ID b50aacc8ae493d945d3e66833edc35c8ef363853 Uploaded tool tarball. diff -r 000000000000 -r b50aacc8ae49 cufflinks_wrapper.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cufflinks_wrapper.py Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,227 @@ +#!/usr/bin/env python + +# Supports Cufflinks versions 1.3 and newer. + +import optparse, os, shutil, subprocess, sys, tempfile +from galaxy import eggs +from galaxy.datatypes.util.gff_util import parse_gff_attributes, gff_attributes_to_str + +def stop_err( msg ): + sys.stderr.write( "%s\n" % msg ) + sys.exit() + +# Copied from sam_to_bam.py: +def check_seq_file( dbkey, cached_seqs_pointer_file ): + seq_path = '' + for line in open( cached_seqs_pointer_file ): + line = line.rstrip( '\r\n' ) + if line and not line.startswith( '#' ) and line.startswith( 'index' ): + fields = line.split( '\t' ) + if len( fields ) < 3: + continue + if fields[1] == dbkey: + seq_path = fields[2].strip() + break + return seq_path + +def __main__(): + #Parse Command Line + parser = optparse.OptionParser() + parser.add_option( '-1', '--input', dest='input', help=' file of RNA-Seq read alignments in the SAM format. SAM is a standard short read alignment, that allows aligners to attach custom tags to individual alignments, and Cufflinks requires that the alignments you supply have some of these tags. Please see Input formats for more details.' ) + parser.add_option( '-s', '--inner-dist-std-dev', dest='inner_dist_std_dev', help='The standard deviation for the distribution on inner distances between mate pairs. The default is 20bp.' ) + parser.add_option( '-I', '--max-intron-length', dest='max_intron_len', help='The minimum intron length. Cufflinks will not report transcripts with introns longer than this, and will ignore SAM alignments with REF_SKIP CIGAR operations longer than this. The default is 300,000.' ) + parser.add_option( '-F', '--min-isoform-fraction', dest='min_isoform_fraction', help='After calculating isoform abundance for a gene, Cufflinks filters out transcripts that it believes are very low abundance, because isoforms expressed at extremely low levels often cannot reliably be assembled, and may even be artifacts of incompletely spliced precursors of processed transcripts. This parameter is also used to filter out introns that have far fewer spliced alignments supporting them. The default is 0.05, or 5% of the most abundant isoform (the major isoform) of the gene.' ) + parser.add_option( '-j', '--pre-mrna-fraction', dest='pre_mrna_fraction', help='Some RNA-Seq protocols produce a significant amount of reads that originate from incompletely spliced transcripts, and these reads can confound the assembly of fully spliced mRNAs. Cufflinks uses this parameter to filter out alignments that lie within the intronic intervals implied by the spliced alignments. The minimum depth of coverage in the intronic region covered by the alignment is divided by the number of spliced reads, and if the result is lower than this parameter value, the intronic alignments are ignored. The default is 5%.' ) + parser.add_option( '-p', '--num-threads', dest='num_threads', help='Use this many threads to align reads. The default is 1.' ) + parser.add_option( '-m', '--inner-mean-dist', dest='inner_mean_dist', help='This is the expected (mean) inner distance between mate pairs. \ + For, example, for paired end runs with fragments selected at 300bp, \ + where each end is 50bp, you should set -r to be 200. The default is 45bp.') + parser.add_option( '-G', '--GTF', dest='GTF', help='Tells Cufflinks to use the supplied reference annotation to estimate isoform expression. It will not assemble novel transcripts, and the program will ignore alignments not structurally compatible with any reference transcript.' ) + parser.add_option( '-g', '--GTF-guide', dest='GTFguide', help='use reference transcript annotation to guide assembly' ) + parser.add_option( '-u', '--multi-read-correct', dest='multi_read_correct', action="store_true", help='Tells Cufflinks to do an initial estimation procedure to more accurately weight reads mapping to multiple locations in the genome') + + # Normalization options. + parser.add_option( "-N", "--quartile-normalization", dest="do_normalization", action="store_true" ) + parser.add_option( "--no-effective-length-correction", dest="no_effective_length_correction", action="store_true" ) + + # Wrapper / Galaxy options. + parser.add_option( '-A', '--assembled-isoforms-output', dest='assembled_isoforms_output_file', help='Assembled isoforms output file; formate is GTF.' ) + + # Advanced Options: + parser.add_option( '--num-importance-samples', dest='num_importance_samples', help='Sets the number of importance samples generated for each locus during abundance estimation. Default: 1000' ) + parser.add_option( '--max-mle-iterations', dest='max_mle_iterations', help='Sets the number of iterations allowed during maximum likelihood estimation of abundances. Default: 5000' ) + + # Bias correction options. + parser.add_option( '-b', dest='do_bias_correction', action="store_true", help='Providing Cufflinks with a multifasta file via this option instructs it to run our new bias detection and correction algorithm which can significantly improve accuracy of transcript abundance estimates.') + parser.add_option( '', '--dbkey', dest='dbkey', help='The build of the reference dataset' ) + parser.add_option( '', '--index_dir', dest='index_dir', help='GALAXY_DATA_INDEX_DIR' ) + parser.add_option( '', '--ref_file', dest='ref_file', help='The reference dataset from the history' ) + + # Global model. + parser.add_option( '', '--global_model', dest='global_model_file', help='Global model used for computing on local data' ) + + (options, args) = parser.parse_args() + + # output version # of tool + try: + tmp = tempfile.NamedTemporaryFile().name + tmp_stdout = open( tmp, 'wb' ) + proc = subprocess.Popen( args='cufflinks --no-update-check 2>&1', shell=True, stdout=tmp_stdout ) + tmp_stdout.close() + returncode = proc.wait() + stdout = None + for line in open( tmp_stdout.name, 'rb' ): + if line.lower().find( 'cufflinks v' ) >= 0: + stdout = line.strip() + break + if stdout: + sys.stdout.write( '%s\n' % stdout ) + else: + raise Exception + except: + sys.stdout.write( 'Could not determine Cufflinks version\n' ) + + # If doing bias correction, set/link to sequence file. + if options.do_bias_correction: + if options.ref_file != 'None': + # Sequence data from history. + # Create symbolic link to ref_file so that index will be created in working directory. + seq_path = "ref.fa" + os.symlink( options.ref_file, seq_path ) + else: + # Sequence data from loc file. + cached_seqs_pointer_file = os.path.join( options.index_dir, 'sam_fa_indices.loc' ) + if not os.path.exists( cached_seqs_pointer_file ): + stop_err( 'The required file (%s) does not exist.' % cached_seqs_pointer_file ) + # If found for the dbkey, seq_path will look something like /galaxy/data/equCab2/sam_index/equCab2.fa, + # and the equCab2.fa file will contain fasta sequences. + seq_path = check_seq_file( options.dbkey, cached_seqs_pointer_file ) + if seq_path == '': + stop_err( 'No sequence data found for dbkey %s, so bias correction cannot be used.' % options.dbkey ) + + # Build command. + + # Base; always use quiet mode to avoid problems with storing log output. + cmd = "cufflinks -q --no-update-check" + + # Add options. + if options.inner_dist_std_dev: + cmd += ( " -s %i" % int ( options.inner_dist_std_dev ) ) + if options.max_intron_len: + cmd += ( " -I %i" % int ( options.max_intron_len ) ) + if options.min_isoform_fraction: + cmd += ( " -F %f" % float ( options.min_isoform_fraction ) ) + if options.pre_mrna_fraction: + cmd += ( " -j %f" % float ( options.pre_mrna_fraction ) ) + if options.num_threads: + cmd += ( " -p %i" % int ( options.num_threads ) ) + if options.inner_mean_dist: + cmd += ( " -m %i" % int ( options.inner_mean_dist ) ) + if options.GTF: + cmd += ( " -G %s" % options.GTF ) + if options.GTFguide: + cmd += ( " -g %s" % options.GTFguide ) + if options.multi_read_correct: + cmd += ( " -u" ) + if options.num_importance_samples: + cmd += ( " --num-importance-samples %i" % int ( options.num_importance_samples ) ) + if options.max_mle_iterations: + cmd += ( " --max-mle-iterations %i" % int ( options.max_mle_iterations ) ) + if options.do_normalization: + cmd += ( " -N" ) + if options.do_bias_correction: + cmd += ( " -b %s" % seq_path ) + if options.no_effective_length_correction: + cmd += ( " --no-effective-length-correction" ) + + # Debugging. + print cmd + + # Add input files. + cmd += " " + options.input + + # + # Run command and handle output. + # + try: + # + # Run command. + # + tmp_name = tempfile.NamedTemporaryFile( dir="." ).name + tmp_stderr = open( tmp_name, 'wb' ) + proc = subprocess.Popen( args=cmd, shell=True, stderr=tmp_stderr.fileno() ) + returncode = proc.wait() + tmp_stderr.close() + + # Error checking. + if returncode != 0: + raise Exception, "return code = %i" % returncode + + # + # Handle output. + # + + # Read standard error to get total map/upper quartile mass. + total_map_mass = -1 + tmp_stderr = open( tmp_name, 'r' ) + for line in tmp_stderr: + if line.lower().find( "map mass" ) >= 0 or line.lower().find( "upper quartile" ) >= 0: + total_map_mass = float( line.split(":")[1].strip() ) + break + tmp_stderr.close() + + # + # If there's a global model provided, use model's total map mass + # to adjust FPKM + confidence intervals. + # + if options.global_model_file: + # Global model is simply total map mass from original run. + global_model_file = open( options.global_model_file, 'r' ) + global_model_total_map_mass = float( global_model_file.readline() ) + global_model_file.close() + + # Ratio of global model's total map mass to original run's map mass is + # factor used to adjust FPKM. + fpkm_map_mass_ratio = total_map_mass / global_model_total_map_mass + + # Update FPKM values in transcripts.gtf file. + transcripts_file = open( "transcripts.gtf", 'r' ) + tmp_transcripts = tempfile.NamedTemporaryFile( dir="." ).name + new_transcripts_file = open( tmp_transcripts, 'w' ) + for line in transcripts_file: + fields = line.split( '\t' ) + attrs = parse_gff_attributes( fields[8] ) + attrs[ "FPKM" ] = str( float( attrs[ "FPKM" ] ) * fpkm_map_mass_ratio ) + attrs[ "conf_lo" ] = str( float( attrs[ "conf_lo" ] ) * fpkm_map_mass_ratio ) + attrs[ "conf_hi" ] = str( float( attrs[ "conf_hi" ] ) * fpkm_map_mass_ratio ) + fields[8] = gff_attributes_to_str( attrs, "GTF" ) + new_transcripts_file.write( "%s\n" % '\t'.join( fields ) ) + transcripts_file.close() + new_transcripts_file.close() + shutil.copyfile( tmp_transcripts, "transcripts.gtf" ) + + # TODO: update expression files as well. + + # Set outputs. Transcript and gene expression handled by wrapper directives. + shutil.copyfile( "transcripts.gtf" , options.assembled_isoforms_output_file ) + if total_map_mass > -1: + f = open( "global_model.txt", 'w' ) + f.write( "%f\n" % total_map_mass ) + f.close() + except Exception, e: + # Read stderr so that it can be reported: + tmp_stderr = open( tmp_name, 'rb' ) + stderr = '' + buffsize = 1048576 + try: + while True: + stderr += tmp_stderr.read( buffsize ) + if not stderr or len( stderr ) % buffsize != 0: + break + except OverflowError: + pass + tmp_stderr.close() + + stop_err( 'Error running cufflinks.\n%s\n%s' % ( str( e ), stderr ) ) + +if __name__=="__main__": __main__() diff -r 000000000000 -r b50aacc8ae49 cufflinks_wrapper.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cufflinks_wrapper.xml Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,235 @@ + + + transcript assembly and FPKM (RPKM) estimates for RNA-Seq data + + cufflinks + + cufflinks 2>&1 | head -n 1 + + cufflinks_wrapper.py + --input=$input + --assembled-isoforms-output=$assembled_isoforms + --num-threads="4" + -I $max_intron_len + -F $min_isoform_fraction + -j $pre_mrna_fraction + $effective_length_correction + + ## Include reference annotation? + #if $reference_annotation.use_ref == "Use reference annotation": + -G $reference_annotation.reference_annotation_file + #end if + #if $reference_annotation.use_ref == "Use reference annotation guide": + -g $reference_annotation.reference_annotation_guide_file + #end if + + ## Normalization? + #if str($do_normalization) == "Yes": + -N + #end if + + ## Bias correction? + #if $bias_correction.do_bias_correction == "Yes": + -b + #if $bias_correction.seq_source.index_source == "history": + --ref_file=$bias_correction.seq_source.ref_file + #else: + --ref_file="None" + #end if + --dbkey=${input.metadata.dbkey} + --index_dir=${GALAXY_DATA_INDEX_DIR} + #end if + + ## Multi-read correct? + #if str($multiread_correct) == "Yes": + -u + #end if + + ## Include global model if available. + #if $global_model: + --global_model=$global_model + #end if + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +**Cufflinks Overview** + +Cufflinks_ assembles transcripts, estimates their abundances, and tests for differential expression and regulation in RNA-Seq samples. It accepts aligned RNA-Seq reads and assembles the alignments into a parsimonious set of transcripts. Cufflinks then estimates the relative abundances of these transcripts based on how many reads support each one. Please cite: Trapnell C, Williams BA, Pertea G, Mortazavi AM, Kwan G, van Baren MJ, Salzberg SL, Wold B, Pachter L. Transcript assembly and abundance estimation from RNA-Seq reveals thousands of new transcripts and switching among isoforms. Nature Biotechnology doi:10.1038/nbt.1621 + +.. _Cufflinks: http://cufflinks.cbcb.umd.edu/ + +------ + +**Know what you are doing** + +.. class:: warningmark + +There is no such thing (yet) as an automated gearshift in expression analysis. It is all like stick-shift driving in San Francisco. In other words, running this tool with default parameters will probably not give you meaningful results. A way to deal with this is to **understand** the parameters by carefully reading the `documentation`__ and experimenting. Fortunately, Galaxy makes experimenting easy. + +.. __: http://cufflinks.cbcb.umd.edu/manual.html + +------ + +**Input formats** + +Cufflinks takes a text file of SAM alignments as input. The RNA-Seq read mapper TopHat produces output in this format, and is recommended for use with Cufflinks. However Cufflinks will accept SAM alignments generated by any read mapper. Here's an example of an alignment Cufflinks will accept:: + + s6.25mer.txt-913508 16 chr1 4482736 255 14M431N11M * 0 0 \ + CAAGATGCTAGGCAAGTCTTGGAAG IIIIIIIIIIIIIIIIIIIIIIIII NM:i:0 XS:A:- + +Note the use of the custom tag XS. This attribute, which must have a value of "+" or "-", indicates which strand the RNA that produced this read came from. While this tag can be applied to any alignment, including unspliced ones, it must be present for all spliced alignment records (those with a 'N' operation in the CIGAR string). +The SAM file supplied to Cufflinks must be sorted by reference position. If you aligned your reads with TopHat, your alignments will be properly sorted already. If you used another tool, you may want to make sure they are properly sorted as follows:: + + sort -k 3,3 -k 4,4n hits.sam > hits.sam.sorted + +NOTE: Cufflinks currently only supports SAM alignments with the CIGAR match ('M') and reference skip ('N') operations. Support for the other operations, such as insertions, deletions, and clipping, will be added in the future. + +------ + +**Outputs** + +Cufflinks produces three output files: + +Transcripts and Genes: + +This GTF file contains Cufflinks' assembled isoforms. The first 7 columns are standard GTF, and the last column contains attributes, some of which are also standardized (e.g. gene_id, transcript_id). There one GTF record per row, and each record represents either a transcript or an exon within a transcript. The columns are defined as follows:: + + Column number Column name Example Description + ----------------------------------------------------- + 1 seqname chrX Chromosome or contig name + 2 source Cufflinks The name of the program that generated this file (always 'Cufflinks') + 3 feature exon The type of record (always either "transcript" or "exon"). + 4 start 77696957 The leftmost coordinate of this record (where 0 is the leftmost possible coordinate) + 5 end 77712009 The rightmost coordinate of this record, inclusive. + 6 score 77712009 The most abundant isoform for each gene is assigned a score of 1000. Minor isoforms are scored by the ratio (minor FPKM/major FPKM) + 7 strand + Cufflinks' guess for which strand the isoform came from. Always one of '+', '-' '.' + 7 frame . Cufflinks does not predict where the start and stop codons (if any) are located within each transcript, so this field is not used. + 8 attributes See below + +Each GTF record is decorated with the following attributes:: + + Attribute Example Description + ----------------------------------------- + gene_id CUFF.1 Cufflinks gene id + transcript_id CUFF.1.1 Cufflinks transcript id + FPKM 101.267 Isoform-level relative abundance in Reads Per Kilobase of exon model per Million mapped reads + frac 0.7647 Reserved. Please ignore, as this attribute may be deprecated in the future + conf_lo 0.07 Lower bound of the 95% confidence interval of the abundance of this isoform, as a fraction of the isoform abundance. That is, lower bound = FPKM * (1.0 - conf_lo) + conf_hi 0.1102 Upper bound of the 95% confidence interval of the abundance of this isoform, as a fraction of the isoform abundance. That is, upper bound = FPKM * (1.0 + conf_lo) + cov 100.765 Estimate for the absolute depth of read coverage across the whole transcript + + +Transcripts only: + This file is simply a tab delimited file containing one row per transcript and with columns containing the attributes above. There are a few additional attributes not in the table above, but these are reserved for debugging, and may change or disappear in the future. + +Genes only: +This file contains gene-level coordinates and expression values. + +------- + +**Cufflinks settings** + +All of the options have a default value. You can change any of them. Most of the options in Cufflinks have been implemented here. + +------ + +**Cufflinks parameter list** + +This is a list of implemented Cufflinks options:: + + -m INT This is the expected (mean) inner distance between mate pairs. For, example, for paired end runs with fragments selected at 300bp, where each end is 50bp, you should set -r to be 200. The default is 45bp. + -s INT The standard deviation for the distribution on inner distances between mate pairs. The default is 20bp. + -I INT The minimum intron length. Cufflinks will not report transcripts with introns longer than this, and will ignore SAM alignments with REF_SKIP CIGAR operations longer than this. The default is 300,000. + -F After calculating isoform abundance for a gene, Cufflinks filters out transcripts that it believes are very low abundance, because isoforms expressed at extremely low levels often cannot reliably be assembled, and may even be artifacts of incompletely spliced precursors of processed transcripts. This parameter is also used to filter out introns that have far fewer spliced alignments supporting them. The default is 0.05, or 5% of the most abundant isoform (the major isoform) of the gene. + -j Some RNA-Seq protocols produce a significant amount of reads that originate from incompletely spliced transcripts, and these reads can confound the assembly of fully spliced mRNAs. Cufflinks uses this parameter to filter out alignments that lie within the intronic intervals implied by the spliced alignments. The minimum depth of coverage in the intronic region covered by the alignment is divided by the number of spliced reads, and if the result is lower than this parameter value, the intronic alignments are ignored. The default is 5%. + -G Tells Cufflinks to use the supplied reference annotation to estimate isoform expression. It will not assemble novel transcripts, and the program will ignore alignments not structurally compatible with any reference transcript. + -N With this option, Cufflinks excludes the contribution of the top 25 percent most highly expressed genes from the number of mapped fragments used in the FPKM denominator. This can improve robustness of differential expression calls for less abundant genes and transcripts. + + diff -r 000000000000 -r b50aacc8ae49 test-data/cufflinks_in.bam Binary file test-data/cufflinks_in.bam has changed diff -r 000000000000 -r b50aacc8ae49 test-data/cufflinks_out1.gtf --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/cufflinks_out1.gtf Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,4 @@ +test_chromosome Cufflinks transcript 53 550 1000 + . gene_id "CUFF.1"; transcript_id "CUFF.1.1"; FPKM "10679134.4063403048"; frac "1.000000"; conf_lo "8543307.525072"; conf_hi "12814961.287608"; cov "145.770185"; +test_chromosome Cufflinks exon 53 250 1000 + . gene_id "CUFF.1"; transcript_id "CUFF.1.1"; exon_number "1"; FPKM "10679134.4063403048"; frac "1.000000"; conf_lo "8543307.525072"; conf_hi "12814961.287608"; cov "145.770185"; +test_chromosome Cufflinks exon 351 400 1000 + . gene_id "CUFF.1"; transcript_id "CUFF.1.1"; exon_number "2"; FPKM "10679134.4063403048"; frac "1.000000"; conf_lo "8543307.525072"; conf_hi "12814961.287608"; cov "145.770185"; +test_chromosome Cufflinks exon 501 550 1000 + . gene_id "CUFF.1"; transcript_id "CUFF.1.1"; exon_number "3"; FPKM "10679134.4063403048"; frac "1.000000"; conf_lo "8543307.525072"; conf_hi "12814961.287608"; cov "145.770185"; diff -r 000000000000 -r b50aacc8ae49 test-data/cufflinks_out2.fpkm_tracking --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/cufflinks_out2.fpkm_tracking Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,2 @@ +tracking_id class_code nearest_ref_id gene_id gene_short_name tss_id locus length coverage FPKM FPKM_conf_lo FPKM_conf_hi FPKM_status +CUFF.1.1 - - CUFF.1 - - test_chromosome:52-550 298 142.855 1.04656e+07 8.35119e+06 1.25799e+07 OK diff -r 000000000000 -r b50aacc8ae49 test-data/cufflinks_out3.fpkm_tracking --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/cufflinks_out3.fpkm_tracking Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,2 @@ +tracking_id class_code nearest_ref_id gene_id gene_short_name tss_id locus length coverage FPKM FPKM_conf_lo FPKM_conf_hi FPKM_status +CUFF.1 - - CUFF.1 - - test_chromosome:52-550 - - 1.04656e+07 8.35119e+06 1.25799e+07 OK diff -r 000000000000 -r b50aacc8ae49 test-data/cufflinks_out4.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/cufflinks_out4.txt Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,1 @@ +100.000000 diff -r 000000000000 -r b50aacc8ae49 tool_dependencies.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_dependencies.xml Tue Oct 01 12:55:37 2013 -0400 @@ -0,0 +1,6 @@ + + + + + +