Mercurial > repos > galaxyp > custom_pro_db_annotation_data_manager
comparison data_manager/customProDB_annotation.R @ 0:45755942ae7b draft
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tools/bumbershoot/custom_pro_db commit 4bb5b663989d5f04e8fb74b111456f16d6edaa66
| author | galaxyp |
|---|---|
| date | Tue, 14 Mar 2017 14:11:55 -0400 |
| parents | |
| children | 9b4ee836e35b |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:45755942ae7b |
|---|---|
| 1 #!/usr/bin/env Rscript | |
| 2 | |
| 3 initial.options <- commandArgs(trailingOnly = FALSE) | |
| 4 script_parent_dir <- dirname(sub("--file=", "", initial.options[grep("--file=", initial.options)])) | |
| 5 | |
| 6 ## begin warning handler | |
| 7 withCallingHandlers({ | |
| 8 | |
| 9 library(methods) # Because Rscript does not always do this | |
| 10 | |
| 11 options('useFancyQuotes' = FALSE) | |
| 12 | |
| 13 suppressPackageStartupMessages(library("optparse")) | |
| 14 suppressPackageStartupMessages(library("RGalaxy")) | |
| 15 | |
| 16 | |
| 17 option_list <- list() | |
| 18 option_list$dbkey <- make_option('--dbkey', type='character') | |
| 19 option_list$dbsnp <- make_option('--dbsnp', type='character') | |
| 20 option_list$cosmic <- make_option('--cosmic', type='logical') | |
| 21 option_list$outputFile <- make_option('--outputFile', type='character') | |
| 22 option_list$dbkey_description <- make_option('--dbkey_description', type='character') | |
| 23 | |
| 24 opt <- parse_args(OptionParser(option_list=option_list)) | |
| 25 | |
| 26 | |
| 27 customProDB_annotation <- function( | |
| 28 dbkey = GalaxyCharacterParam(required=TRUE), | |
| 29 dbsnp_str = GalaxyCharacterParam(required=FALSE), | |
| 30 cosmic = GalaxyLogicalParam(required=FALSE), | |
| 31 dbkey_description = GalaxyCharacterParam(required=FALSE), | |
| 32 outputFile = GalaxyOutput("output","json")) | |
| 33 { | |
| 34 if (!file.exists(outputFile)) | |
| 35 { | |
| 36 gstop("json params file does not exist") | |
| 37 } | |
| 38 | |
| 39 if (length(dbkey_description) < 1) | |
| 40 { | |
| 41 dbkey_description = dbkey | |
| 42 } | |
| 43 | |
| 44 if (length(dbsnp_str) > 0) | |
| 45 { | |
| 46 dbsnp = dbsnp_str | |
| 47 } | |
| 48 else | |
| 49 { | |
| 50 dbsnp = NULL | |
| 51 } | |
| 52 | |
| 53 use_cosmic = FALSE | |
| 54 if (length(cosmic) > 0) | |
| 55 { | |
| 56 if (grepl("^hg", dbkey)) | |
| 57 { | |
| 58 use_cosmic = TRUE | |
| 59 } | |
| 60 else | |
| 61 { | |
| 62 gstop("COSMIC annotation requested but dbkey does not indicate a human genome (e.g. hg19)") | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 suppressPackageStartupMessages(library(rjson)) | |
| 67 params = fromJSON(file=outputFile) | |
| 68 target_directory = params$output_data[[1]]$extra_files_path | |
| 69 dir.create(target_directory) | |
| 70 | |
| 71 tryCatch( | |
| 72 { | |
| 73 file.remove(outputFile) | |
| 74 }, error=function(err) | |
| 75 { | |
| 76 gstop("failed to remove json params file after reading") | |
| 77 }) | |
| 78 | |
| 79 ucscTableCodingFastaURL = paste("http://genome.ucsc.edu/cgi-bin/hgTables?db=", dbkey, "&hgSeq.cdsExon=on&hgSeq.granularity=gene&hgSeq.casing=exon&hgSeq.repMasking=lower&hgta_doGenomicDna=get+sequence&hgta_group=genes&hgta_track=refGene&hgta_table=refGene&hgta_regionType=genome", sep="") | |
| 80 ucscTableProteinFastaURL = paste("http://genome.ucsc.edu/cgi-bin/hgTables?db=", dbkey, "&hgta_geneSeqType=protein&hgta_doGenePredSequence=submit&hgta_track=refGene&hgta_table=refGene", sep="") | |
| 81 codingFastaFilepath = paste(target_directory, "/", dbkey, ".cds.fa", sep="") | |
| 82 proteinFastaFilepath = paste(target_directory, "/", dbkey, ".protein.fa", sep="") | |
| 83 | |
| 84 suppressPackageStartupMessages(library(customProDB)) | |
| 85 options(timeout=3600) | |
| 86 | |
| 87 cat(paste("Downloading coding FASTA from:", ucscTableCodingFastaURL, "\n")) | |
| 88 download.file(ucscTableCodingFastaURL, codingFastaFilepath, quiet=T, mode='wb') | |
| 89 | |
| 90 cat(paste("Downloading protein FASTA from:", ucscTableProteinFastaURL, "\n")) | |
| 91 download.file(ucscTableProteinFastaURL, proteinFastaFilepath, quiet=T, mode='wb') | |
| 92 | |
| 93 cat(paste("Preparing Refseq annotation files\n")) | |
| 94 customProDB::PrepareAnnotationRefseq(genome=dbkey, CDSfasta=codingFastaFilepath, pepfasta=proteinFastaFilepath, annotation_path=target_directory, dbsnp=dbsnp, COSMIC=use_cosmic) | |
| 95 | |
| 96 outputPath = paste(dbkey, "/customProDB", sep="") | |
| 97 output = list(data_tables = list()) | |
| 98 output[["data_tables"]][["customProDB"]]=c(path=outputPath, name=dbkey_description, dbkey=dbkey, value=dbkey) | |
| 99 write(toJSON(output), file=outputFile) | |
| 100 } | |
| 101 | |
| 102 | |
| 103 params <- list() | |
| 104 for(param in names(opt)) | |
| 105 { | |
| 106 if (!param == "help") | |
| 107 params[param] <- opt[param] | |
| 108 } | |
| 109 | |
| 110 setClass("GalaxyRemoteError", contains="character") | |
| 111 wrappedFunction <- function(f) | |
| 112 { | |
| 113 tryCatch(do.call(f, params), | |
| 114 error=function(e) new("GalaxyRemoteError", conditionMessage(e))) | |
| 115 } | |
| 116 | |
| 117 | |
| 118 suppressPackageStartupMessages(library(RGalaxy)) | |
| 119 do.call(customProDB_annotation, params) | |
| 120 | |
| 121 ## end warning handler | |
| 122 }, warning = function(w) { | |
| 123 cat(paste("Warning:", conditionMessage(w), "\n")) | |
| 124 invokeRestart("muffleWarning") | |
| 125 }) |
