Mercurial > repos > prog > lcmsmatching
comparison NcbiGeneCompound.R @ 0:e66bb061af06 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 3529b25417f8e1a5836474c9adec4b696d35099d-dirty
| author | prog |
|---|---|
| date | Tue, 12 Jul 2016 12:02:37 -0400 |
| parents | |
| children | 253d531a0193 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e66bb061af06 |
|---|---|
| 1 if ( ! exists('NcbigeneCompound')) { # Do not load again if already loaded | |
| 2 | |
| 3 source('BiodbEntry.R') | |
| 4 source(file.path('strhlp.R'), chdir = TRUE) | |
| 5 | |
| 6 ##################### | |
| 7 # CLASS DECLARATION # | |
| 8 ##################### | |
| 9 | |
| 10 NcbigeneCompound <- setRefClass("NcbigeneCompound", contains = "BiodbEntry") | |
| 11 | |
| 12 ########### | |
| 13 # FACTORY # | |
| 14 ########### | |
| 15 | |
| 16 createNcbigeneCompoundFromXml <- function(contents, drop = TRUE) { | |
| 17 | |
| 18 library(XML) | |
| 19 | |
| 20 compounds <- list() | |
| 21 | |
| 22 # Define xpath expressions | |
| 23 xpath.expr <- character() | |
| 24 xpath.expr[[RBIODB.ACCESSION]] <- "//Gene-track_geneid" | |
| 25 xpath.expr[[RBIODB.KEGG.ID]] <- "/Dbtag_db[text()='KEGG']/..//Object-id_str" | |
| 26 xpath.expr[[RBIODB.UNIPROT.ID]] <- "//Gene-commentary_heading[text()='UniProtKB']/..//Dbtag_db[text()='UniProtKB/Swiss-Prot']/..//Object-id_str" | |
| 27 xpath.expr[[RBIODB.LOCATION]] <- "//Gene-ref_maploc" | |
| 28 xpath.expr[[RBIODB.PROTEIN.DESCRIPTION]] <- "//Gene-ref_desc" | |
| 29 xpath.expr[[RBIODB.SYMBOL]] <- "//Gene-ref_locus" | |
| 30 xpath.expr[[RBIODB.SYNONYMS]] <- "//Gene-ref_syn_E" | |
| 31 | |
| 32 for (content in contents) { | |
| 33 | |
| 34 # Create instance | |
| 35 compound <- NcbigeneCompound$new() | |
| 36 | |
| 37 # Parse HTML | |
| 38 xml <- xmlInternalTreeParse(content, asText = TRUE) | |
| 39 | |
| 40 # An error occured | |
| 41 if (length(getNodeSet(xml, "//Error")) == 0 && length(getNodeSet(xml, "//ERROR")) == 0) { | |
| 42 | |
| 43 # Test generic xpath expressions | |
| 44 for (field in names(xpath.expr)) { | |
| 45 v <- xpathSApply(xml, xpath.expr[[field]], xmlValue) | |
| 46 if (length(v) > 0) { | |
| 47 | |
| 48 # Eliminate duplicates | |
| 49 v <- v[ ! duplicated(v)] | |
| 50 | |
| 51 # Set field | |
| 52 compound$setField(field, v) | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 # CCDS ID | |
| 57 ccdsid <- .find.ccds.id(xml) | |
| 58 if ( ! is.na(ccdsid)) | |
| 59 compound$setField(RBIODB.NCBI.CCDS.ID, ccdsid) | |
| 60 } | |
| 61 | |
| 62 compounds <- c(compounds, compound) | |
| 63 } | |
| 64 | |
| 65 # Replace elements with no accession id by NULL | |
| 66 compounds <- lapply(compounds, function(x) if (is.na(x$getField(RBIODB.ACCESSION))) NULL else x) | |
| 67 | |
| 68 # If the input was a single element, then output a single object | |
| 69 if (drop && length(contents) == 1) | |
| 70 compounds <- compounds[[1]] | |
| 71 | |
| 72 return(compounds) | |
| 73 | |
| 74 # Get data | |
| 75 | |
| 76 } | |
| 77 | |
| 78 ################ | |
| 79 # FIND CCDS ID # | |
| 80 ################ | |
| 81 | |
| 82 .find.ccds.id <- function(xml) { | |
| 83 | |
| 84 # 1) Get all CCDS tags. | |
| 85 ccds_elements <- getNodeSet(xml, "//Dbtag_db[text()='CCDS']/..//Object-id_str") | |
| 86 | |
| 87 # 2) If all CCDS are the same, go to point 4. | |
| 88 ccds <- NA_character_ | |
| 89 for (e in ccds_elements) { | |
| 90 current_ccds <- xmlValue(e) | |
| 91 if (is.na(ccds)) | |
| 92 ccds <- current_ccds | |
| 93 else { | |
| 94 if (current_ccds != ccds) { | |
| 95 ccds <- NA_character_ | |
| 96 break | |
| 97 } | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 # 3) There are several CCDS values, we need to find the best one (i.e.: the most current one). | |
| 102 if (is.na(ccds)) { | |
| 103 # For each CCDS, look for the parent Gene-commentary tag. Then look for the text content of the Gene-commentary_label which is situed under. Ignore CCDS that have no Gene-commentary_label associated. Choose the CCDS that has the smallest Gene-commentary_label in alphabetical order. | |
| 104 version <- NA_character_ | |
| 105 for (e in ccds_elements) { | |
| 106 versions <- xpathSApply(e, "ancestor::Gene-commentary/Gene-commentary_label", xmlValue) | |
| 107 if (length(versions) < 1) next | |
| 108 current_version <- versions[[length(versions)]] | |
| 109 if (is.na(version) || current_version < version) { | |
| 110 version <- current_version | |
| 111 ccds <- xmlValue(e) | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 return(ccds) | |
| 117 } | |
| 118 } |
