comparison gtf2featureAnnotation.R @ 1:247b439a78f7 draft

planemo upload for repository https://github.com/ebi-gene-expression-group/container-galaxy-sc-tertiary/tree/develop/tools/util/.shed.yml commit 194d2e0af16624c9a3d1af92f7b3686d2e0ee552
author ebi-gxa
date Fri, 18 Oct 2019 10:10:54 -0400
parents
children 00ee933b08fd
comparison
equal deleted inserted replaced
0:040d4b3a19d5 1:247b439a78f7
1 #!/usr/bin/env Rscript
2
3 # This script parses the GTF file to create a feature-wise annotation file with
4 # mitochondrial features flagged, to assist in annotation and QC of single-cell
5 # expression data analysis.
6
7 suppressPackageStartupMessages(require(rtracklayer))
8 suppressPackageStartupMessages(require(optparse))
9
10 ucfirst <- function (str) {
11 paste(toupper(substring(str, 1, 1)), tolower(substring(str, 2)), sep = "")
12 }
13
14 die <- function(message){
15 write(message, stderr())
16 q(status = 1)
17 }
18
19 cleanlist <- function(str){
20 tolower(unlist(strsplit(str, ',')))
21 }
22
23 cl <- commandArgs(trailingOnly = TRUE)
24
25 option_list = list(
26 make_option(
27 c("-g", "--gtf-file"),
28 action = "store",
29 default = NA,
30 type = 'character',
31 help = "Path to a valid GTF file"
32 ),
33 make_option(
34 c("-t", "--feature-type"),
35 action = "store",
36 default = 'gene',
37 type = 'character',
38 help = 'Feature type to use (default: gene)'
39 ),
40 make_option(
41 c("-f", "--first-field"),
42 action = "store",
43 default = 'gene_id',
44 type = 'character',
45 help = 'Field to place first in output table (default: gene_id)'
46 ),
47 make_option(
48 c("-r", "--no-header"),
49 action = "store_false",
50 default = TRUE,
51 type = 'logical',
52 help = 'Suppress header on output'
53 ),
54 make_option(
55 c("-l", "--fields"),
56 action = "store",
57 default = NULL,
58 type = 'character',
59 help = 'Comma-separated list of output fields to retain (default: all)'
60 ),
61 make_option(
62 c("-m", "--mito"),
63 action = "store_true",
64 default = FALSE,
65 type = 'character',
66 help = 'Mark mitochondrial elements with reference to chromsomes and biotypes'
67 ),
68 make_option(
69 c("-n", "--mito-chr"),
70 action = "store",
71 default = 'mt,mitochondrion_genome,mito,m,chrM,chrMt',
72 type = 'character',
73 help = 'If specified, marks in a column called "mito" features on the specified chromosomes (case insensitive)'
74 ),
75 make_option(
76 c("-p", "--mito-biotypes"),
77 action = "store",
78 default = 'mt_trna,mt_rrna,mt_trna_pseudogene',
79 type = 'character',
80 help = 'If specified, marks in a column called "mito" features with the specified biotypes (case insensitve)'
81 ),
82 make_option(
83 c("-c", "--filter-cdnas"),
84 action = "store",
85 default = NULL,
86 type = 'character',
87 help = 'If specified, sequences in the provided FASTA-format cDNAs file will be filtered to remove entries not present in the annotation'
88 ),
89 make_option(
90 c("-d", "--filter-cdnas-field"),
91 action = "store",
92 default = 'transcript_id',
93 type = 'character',
94 help = 'Where --filter-cdnas is specified, what field should be used to compare to identfiers from the FASTA?'
95 ),
96 make_option(
97 c("-e", "--filter-cdnas-output"),
98 action = "store",
99 default = 'filtered.fa.gz',
100 type = 'character',
101 help = 'Where --filter-cdnas is specified, what file should the filtered sequences be output to?'
102 ),
103 make_option(
104 c("-u", "--version-transcripts"),
105 action = "store_true",
106 default = FALSE,
107 type = 'logical',
108 help = 'Where the GTF contains transcript versions, should these be appended to transcript identifiers? Useful when generating transcript/gene mappings for use with transcriptomes.'
109 ),
110 make_option(
111 c("-o", "--output-file"),
112 action = "store",
113 default = NA,
114 type = 'character',
115 help = 'Output file path'
116 )
117 )
118
119 opt <- parse_args(OptionParser(option_list = option_list), convert_hyphens_to_underscores = TRUE)
120
121 if (is.na(opt$gtf_file)){
122 die('ERROR: No input GTF file specified')
123 }
124
125 if (is.na(opt$output_file)){
126 die('ERROR: No output file specified')
127 }
128
129 # Import the GTF
130
131 print(paste('Reading', opt$gtf_file, 'elements of type', opt$feature_type))
132 gtf <- import(opt$gtf_file, feature.type = opt$feature_type )
133
134 # Combine basic info (chromosomes, coordinates) with annotation found in GTF attributes
135
136 anno <- cbind(chromosome = seqnames(gtf), as.data.frame(ranges(gtf)), elementMetadata(gtf))
137 print(paste('Found', nrow(anno), 'features'))
138
139 # Mark mitochondrial features
140
141 if (opt$mito){
142 anno$mito <- ucfirst(as.character(tolower(anno$gene_biotype) %in% cleanlist(opt$mito_biotypes) | tolower(anno$chromosome) %in% cleanlist(opt$mito_chr)))
143 }
144
145 # If specified, put the desired field first
146
147 if (! is.na(opt$first_field)){
148 if (! opt$first_field %in% colnames(anno)){
149 die(paste(first_field, 'is not a valid field'))
150 }
151 anno <- anno[,c(opt$first_field, colnames(anno)[colnames(anno) != opt$first_field])]
152 }
153
154 # Version transcripts
155
156 if ( opt$feature_type == 'transcript' && opt$version_transcripts && all(c('transcript_id', 'transcript_version') %in% colnames(anno) )){
157 anno$transcript_id <- paste(anno$transcript_id, anno$transcript_version, sep='.')
158 }
159
160 # If specified, filter down a provided cDNA FASTA file
161
162 if (! is.null(opt$filter_cdnas)){
163
164 print(paste("Filtering", opt$filter_cdnas, "to match the GTF"))
165
166 suppressPackageStartupMessages(require(Biostrings))
167
168 cdna <- readDNAStringSet(opt$filter_cdnas)
169 cdna_transcript_names <- unlist(lapply(names(cdna), function(x) unlist(strsplit(x, ' '))[1] ))
170
171 # Filter out cDNAs without matching transcript entries in the GTF
172
173 if (! any(cdna_transcript_names %in% anno[[opt$filter_cdnas_field]])){
174 die(paste("ERROR: None of the input sequences have matching", opt$filter_cdnas_field, 'values in the GTF file'))
175 }
176
177 cdna <- cdna[which(cdna_transcript_names %in% anno[[opt$filter_cdnas_field]])]
178
179 print(paste('Storing filtered seqeunces to', opt$filter_cdnas_output))
180 writeXStringSet(x = cdna, filepath = opt$filter_cdnas_output, compress = 'gzip')
181 }
182
183 # If specified, subset to desired fields
184
185 if (! is.null(opt$fields) && opt$fields != ''){
186 fields <- unlist(strsplit(opt$fields, ','))
187 if (any(! fields %in% colnames(anno))){
188 die(paste('ERROR:', fields, 'contains invalid field(s)'))
189 }
190 anno <- anno[,fields, drop = FALSE]
191 anno <- anno[apply(anno, 1, function(x) all(! is.na(x))), ]
192 }
193
194 print(paste('Storing output to', opt$output_file))
195 write.table(anno, file = opt$output_file, sep = "\t", quote=FALSE, row.names = FALSE, col.names = opt$no_header)