11# ' orgdb annotations
2- # '
2+ # '
33# ' Generate an annotation object for genes based on an "org.*.db" object, and pulling
44# ' information from it.
5- # '
5+ # '
66# ' @param orgdb the name of the org.*.db object
77# ' @param features which features to get annotations for
88# ' @param feature_type which type of IDs to map (see details)
99# ' @param annotation_type the type of annotation to grab (see details)
10- # '
10+ # '
1111# ' @md
12- # '
12+ # '
1313# ' @details This function generates a `categoryCompare2` annotation object
1414# ' from a Bioconductor "org.*.db" object. Even though different gene identifiers can
1515# ' be used, almost all of the mappings are via ENTREZID.
16- # '
16+ # '
1717# ' The set of feature or gene keys that can be used to create the annotations include:
1818# ' * ENTREZID: ENTREZ gene ids
1919# ' * ACCNUM: genbank accession numbers
2525# ' * REFSEQ: reference sequence IDs, NM, NP, NR, XP, etc
2626# ' * UNIGENE: gene ids from UNIPROT eg Hs.88556
2727# ' * UNIPROT: protein ids from UNIPROT eg P80404
28- # '
28+ # '
2929# ' The set of annotations that can be mapped to features include:
3030# ' * GO: annotations from gene ontology
3131# ' * PATH: KEGG Pathway identifiers (not updated since 2011!)
3535# ' * PROSITE
3636# ' * PFAM: protein family identifiers
3737# ' * IPI: protein-protein interactions
38- # '
38+ # '
3939# ' For GO annotations, it is also possible to pass `GO` to use all 3 sub-ontologies simultaneously,
4040# ' or any combination of `BP`, `MF`, and `CC`.
41- # '
41+ # '
4242# ' @export
4343# ' @return annotation object
44- # '
45- get_db_annotation <- function (orgdb = " org.Hs.eg.db" , features = NULL , feature_type = " ENTREZID" ,
46- annotation_type = " GO" ){
44+ # '
45+ get_db_annotation <- function (
46+ orgdb = " org.Hs.eg.db" ,
47+ features = NULL ,
48+ feature_type = " ENTREZID" ,
49+ annotation_type = " GO"
50+ ) {
4751 go_types <- c(" GO" , " BP" , " MF" , " CC" )
4852 go_sub <- c(" BP" , " MF" , " CC" )
4953 check_package_installed(orgdb )
5054 suppressPackageStartupMessages(library(orgdb , character.only = TRUE ))
51-
55+
5256 annotation_src <- eval(parse(text = orgdb ))
5357 annotation_columns <- AnnotationDbi :: columns(annotation_src )
5458 annotation_keytypes <- AnnotationDbi :: keytypes(annotation_src )
55-
59+
5660 if (! (annotation_type %in% c(go_types , annotation_columns ))) {
5761 stop(" Unknown annotation type!" )
5862 }
59-
63+
6064 if (! (feature_type %in% annotation_keytypes )) {
6165 stop(" Unknown feature_type!" )
6266 }
63-
67+
6468 if (is.null(features )) {
6569 features <- AnnotationDbi :: keys(annotation_src , feature_type )
6670 }
67-
71+
6872 if (annotation_type %in% go_types ) {
6973 check_package_installed(" GO.db" )
7074 requireNamespace(" GO.db" )
7175 godb = GO.db :: GO.db
72- feature_ann_map <- suppressMessages(AnnotationDbi :: select(annotation_src , keys = features ,
73- keytype = feature_type ,
74- columns = " GOALL" ))
75-
76+ feature_ann_map <- suppressMessages(AnnotationDbi :: select(
77+ annotation_src ,
78+ keys = features ,
79+ keytype = feature_type ,
80+ columns = " GOALL"
81+ ))
82+
7683 if (annotation_type %in% go_sub ) {
77- feature_ann_map <- feature_ann_map [feature_ann_map $ ONTOLOGYALL %in% annotation_type , ]
84+ feature_ann_map <- feature_ann_map [
85+ feature_ann_map $ ONTOLOGYALL %in% annotation_type ,
86+ ]
7887 }
79-
80-
81- ann_feature_list <- split(feature_ann_map [[feature_type ]], feature_ann_map [[" GOALL" ]])
88+
89+ ann_feature_list <- split(
90+ feature_ann_map [[feature_type ]],
91+ feature_ann_map [[" GOALL" ]]
92+ )
8293 ann_feature_list <- lapply(ann_feature_list , unique )
83- ann_description <- suppressMessages(AnnotationDbi :: select(godb , keys = names(ann_feature_list ), columns = " TERM" , keytype = " GOID" )$ TERM )
94+ ann_description <- suppressMessages(
95+ AnnotationDbi :: select(
96+ godb ,
97+ keys = names(ann_feature_list ),
98+ columns = " TERM" ,
99+ keytype = " GOID"
100+ )$ TERM
101+ )
84102 names(ann_description ) <- names(ann_feature_list )
85-
103+
86104 if (annotation_type %in% " GO" ) {
87105 go_ontology_map <- unique(feature_ann_map [, c(" GOALL" , " ONTOLOGYALL" )])
88106 go_ontology <- go_ontology_map $ ONTOLOGYALL
@@ -91,42 +109,51 @@ get_db_annotation <- function(orgdb = "org.Hs.eg.db", features = NULL, feature_t
91109 ann_description <- paste0(go_ontology , " :" , ann_description )
92110 names(ann_description ) <- names(go_ontology )
93111 }
94-
95-
96- annotation_obj <- categoryCompare2 :: annotation(annotation_features = ann_feature_list ,
97- description = ann_description ,
98- annotation_type = annotation_type ,
99- feature_type = feature_type )
112+
113+ annotation_obj <- categoryCompare2 :: annotation(
114+ annotation_features = ann_feature_list ,
115+ description = ann_description ,
116+ annotation_type = annotation_type ,
117+ feature_type = feature_type
118+ )
100119 } else {
101- feature_ann_map <- suppressMessages(AnnotationDbi :: select(annotation_src , keys = features ,
102- keytype = feature_type ,
103- columns = annotation_type ))
104- ann_feature_list <- split(feature_ann_map [[feature_type ]], feature_ann_map [[annotation_type ]])
120+ feature_ann_map <- suppressMessages(AnnotationDbi :: select(
121+ annotation_src ,
122+ keys = features ,
123+ keytype = feature_type ,
124+ columns = annotation_type
125+ ))
126+ ann_feature_list <- split(
127+ feature_ann_map [[feature_type ]],
128+ feature_ann_map [[annotation_type ]]
129+ )
105130 ann_feature_list <- lapply(ann_feature_list , unique )
106-
107- annotation_obj <- categoryCompare2 :: annotation(annotation_features = ann_feature_list ,
108- annotation_type = annotation_type ,
109- feature_type = feature_type )
131+
132+ annotation_obj <- categoryCompare2 :: annotation(
133+ annotation_features = ann_feature_list ,
134+ annotation_type = annotation_type ,
135+ feature_type = feature_type
136+ )
110137 }
111-
138+
112139 annotation_obj
113140}
114141
115142
116143# ' annotation to json
117- # '
144+ # '
118145# ' Given a `categoryCompare2` annotation object, generate a JSON representation
119146# ' that can be used with the command line executable
120- # '
147+ # '
121148# ' @param annotation_obj the annotation object
122149# ' @param json_file the file to save it to
123- # '
150+ # '
124151# ' @return the json string (invisibly)
125152# ' @export
126- annotation_2_json <- function (annotation_obj , json_file = NULL ){
127- obj_list <- purrr :: map(slotNames(annotation_obj ), function (x ){
153+ annotation_2_json <- function (annotation_obj , json_file = NULL ) {
154+ obj_list <- purrr :: map(slotNames(annotation_obj ), function (x ) {
128155 tmp_data <- slot(annotation_obj , x )
129-
156+
130157 if (length(tmp_data ) != 0 ) {
131158 if (x %in% c(" description" , " links" )) {
132159 out_data <- as.list(tmp_data )
@@ -139,17 +166,17 @@ annotation_2_json <- function(annotation_obj, json_file = NULL){
139166 out_data
140167 })
141168 names(obj_list ) <- slotNames(annotation_obj )
142-
169+
143170 obj_list <- obj_list [! purrr :: map_lgl(obj_list , is.null )]
144-
145- obj_json <- jsonlite :: toJSON(obj_list , pretty = TRUE , auto_unbox = TRUE )
171+
172+ obj_json <- jsonlite :: toJSON(obj_list , pretty = TRUE , auto_unbox = FALSE )
146173 if (! is.null(json_file )) {
147174 cat(obj_json , file = json_file , sep = " \n " )
148- }
175+ }
149176 invisible (obj_json )
150177}
151178
152- replace_null <- function (x ){
179+ replace_null <- function (x ) {
153180 if (is.null(x )) {
154181 NA
155182 } else {
@@ -158,26 +185,32 @@ replace_null <- function(x){
158185}
159186
160187# ' json to annotation
161- # '
188+ # '
162189# ' Given a JSON based annotation object, read it in and create the `annotation`
163- # ' for actually doing enrichment.
164- # '
190+ # ' for actually doing enrichment.
191+ # '
165192# ' @param json_file the json annotation file
166- # '
193+ # '
167194# ' @return annotation object
168195# ' @export
169- json_2_annotation <- function (json_file ){
196+ json_2_annotation <- function (json_file ) {
170197 stopifnot(file.exists(json_file ))
171198 annotation_list <- jsonlite :: fromJSON(json_file , simplifyVector = TRUE )
172199 # print(names(annotation_list))
173-
200+
174201 if (is.null(names(annotation_list ))) {
175202 annotation_list <- annotation_list [[1 ]]
176203 }
177-
204+
178205 if (! is.null(annotation_list $ description )) {
179- annotation_list $ description <- purrr :: map(annotation_list $ description , replace_null )
180- annotation_list $ description <- unlist(annotation_list $ description , use.names = TRUE )
206+ annotation_list $ description <- purrr :: map(
207+ annotation_list $ description ,
208+ replace_null
209+ )
210+ annotation_list $ description <- unlist(
211+ annotation_list $ description ,
212+ use.names = TRUE
213+ )
181214 } else {
182215 annotation_list $ description <- character (0 )
183216 }
@@ -189,65 +222,77 @@ json_2_annotation <- function(json_file){
189222
190223 # print(names(annotation_list))
191224 # print(annotation_list$description)
192- annotation(annotation_features = annotation_list $ annotation_features ,
193- annotation_type = annotation_list $ annotation_type ,
194- description = annotation_list $ description ,
195- links = annotation_list $ links ,
196- feature_type = annotation_list $ feature_type )
225+ annotation(
226+ annotation_features = annotation_list $ annotation_features ,
227+ annotation_type = annotation_list $ annotation_type ,
228+ description = annotation_list $ description ,
229+ links = annotation_list $ links ,
230+ feature_type = annotation_list $ feature_type
231+ )
197232}
198233
199234# ' annotation reversal
200- # '
235+ # '
201236# ' Given a JSON file of features to annotations, reverse to turn it into
202237# ' annotations to features, and optionally add some meta-information about them.
203- # '
238+ # '
204239# ' @param json_file the json file to use
205240# ' @param out_file the json file to write out to
206241# ' @param feature_type the type of features
207242# ' @param annotation_type the type of annotations
208- # '
243+ # '
209244# ' @importFrom jsonlite fromJSON toJSON
210245# ' @export
211246# ' @return the json object, invisibly
212- # '
213- json_annotation_reversal <- function (json_file , out_file = " annotations.json" ,
214- feature_type = NULL , annotation_type = NULL ){
247+ # '
248+ json_annotation_reversal <- function (
249+ json_file ,
250+ out_file = " annotations.json" ,
251+ feature_type = NULL ,
252+ annotation_type = NULL
253+ ) {
215254 stopifnot(file.exists(json_file ))
216-
217- in_annotation <- jsonlite :: fromJSON(json_file , simplifyVector = FALSE , flatten = TRUE )
255+
256+ in_annotation <- jsonlite :: fromJSON(
257+ json_file ,
258+ simplifyVector = FALSE ,
259+ flatten = TRUE
260+ )
218261 if (length(in_annotation ) == 1 ) {
219262 in_annotation <- in_annotation [[1 ]]
220263 }
221-
264+
222265 if (! is.null(in_annotation $ Annotations )) {
223266 gene_annotations <- in_annotation $ Annotations
224267 } else {
225268 gene_annotations <- in_annotation # we assume that if there is no Annotation
226- # specific entry, then it is probably just the
227- # gene annotations, and grab them all.
269+ # specific entry, then it is probably just the
270+ # gene annotations, and grab them all.
228271 }
229-
272+
230273 if (! is.null(in_annotation $ Description )) {
231274 annotation_description <- in_annotation $ Description
232275 if (is.list(annotation_description )) {
233276 annotation_description <- unlist(annotation_description , use.names = TRUE )
234277 } else {
235278 warning(" Description must be a named list! Removing Descriptions!" )
236279 annotation_description <- character (0 )
237- }
280+ }
238281 } else {
239282 annotation_description <- character (0 )
240283 }
241-
284+
242285 rev_annotation <- Biobase :: reverseSplit(gene_annotations )
243286 rev_annotation <- purrr :: map(rev_annotation , unique )
244-
245- out_annotation <- annotation(annotation_features = rev_annotation ,
246- description = annotation_description ,
247- links = character (0 ),
248- annotation_type = annotation_type ,
249- feature_type = feature_type )
250-
287+
288+ out_annotation <- annotation(
289+ annotation_features = rev_annotation ,
290+ description = annotation_description ,
291+ links = character (0 ),
292+ annotation_type = annotation_type ,
293+ feature_type = feature_type
294+ )
295+
251296 out_json <- annotation_2_json(out_annotation , out_file )
252297 out_json
253298}
0 commit comments