File size: 8,197 Bytes
2071122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251



#' Assemble a Rosetta models dataset
#'
#' @param data_path character directory .pdb.gz files are located
#' @param output_path character output .parquet path
#'
#' Write output_path .parquet file with columns
#'   <id> <pdb> [<scores>]
#' where [<scores>] are key-value entries following
#' the TER line in each .pdb.gz file
assemble_rosetta_models <- function(
    data_path,
    output_path) {

    cat(
        "data path: ", data_path, "\n",
        "output path: ", output_path, "\n",
        sep = "")

    file_index <- 1
    models <- list.files(
        path = data_path,
        full.names = TRUE,
        pattern = "*.pdb.gz",
        recursive = TRUE) |>
        purrr::map_dfr(.f = function(path) {
            file_handle <- path |>
                file(open = "rb") |>
                gzcon()

            if( file_index %% 1000 == 0) {
                cat("Reading  '", path, "' ", file_index, "\n", sep = "")
            }
            file_index <<- file_index + 1

            lines <- file_handle |> readLines()
            file_handle |> close()

            ter_line_index <- which(
                lines |> stringr::str_detect("^TER"),
                arr.ind = TRUE)

        lines[(ter_line_index + 1) : (length(lines) - 1)] |>
            paste0(collapse = "\n") |>
            readr::read_delim(
                delim = " ",
                col_names = c("key", "value"),
                show_col_types = FALSE) |>
            dplyr::mutate(
                id = path |>
                    basename() |>
                    stringr::str_replace_all(".pdb.gz", ""),
                .before = 1) |>
            dplyr::mutate(
                pdb = lines[1:ter_line_index] |> paste0(collapse = "\n"))

        })
    models <- arrow::arrow_table(models)
    models$pdb <- models$pdb$cast(arrow::string())
    models |> arrow::write_parquet(output_path)
}


# call assemble_rosetta_models for the high_quality and low_quality datasets
dataset_tag <- "rosetta_high_quality_models"
assemble_rosetta_models(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))

dataset_tag <- "rosetta_low_quality_models"
assemble_rosetta_models(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))

#' Assemble a DMP-Fold models dataset
#'
#' @param data_path character directory .pdb.gz files are located
#' @param output_path character output .parquet path
#'
#' Write output_path .parquet file with columns
#'   <id> <pdb>
#'
#' Note that dmpfold doesn't write out score lines like Rosetta
assemble_dmpfold_models <- function(
    data_path,
    output_path) {

    cat(
        "data path: ", data_path, "\n",
        "output path: ", output_path, "\n",
        sep = "")

    file_index <- 1
    models <- list.files(
        path = data_path,
        full.names = TRUE,
        pattern = "*.pdb.gz",
        recursive = TRUE) |>
        purrr::map_dfr(.f = function(path) {
            file_handle <- path |>
                file(open = "rb") |>
                gzcon()

            if (file_index %% 1000 == 0) {
                cat("Reading  '", path, "' ", file_index, "\n", sep = "")
            }
            file_index <<- file_index + 1

            lines <- file_handle |> readLines()
            file_handle |> close()

            ter_line_index <- which(
                lines |> stringr::str_detect("^TER"),
                arr.ind = TRUE)

            data.frame(
                id = path |> basename() |> stringr::str_replace_all(".pdb.gz", ""),
                pdb = lines[1:ter_line_index] |> paste0(collapse = "\n"))
        })
    models |>
        arrow::write_parquet(output_path)
}

# call assemble_rosetta_models for the high_quality and low_quality datasets
dataset_tag <- "dmpfold_high_quality_models"
assemble_dmpfold_models(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))

dataset_tag <- "dmpfold_low_quality_models"
assemble_dmpfold_models(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))


####################################
##                                ##
##  Assemble Function Predictions ##
##                                ##
####################################


#' Assemble DeepFRI Function Prediction dataset
#'
#' @param data_path character directory where *_pred_scores.json.gz files are located
#' @param output_path character output .parquet path
#'
#' Write output_path .parquet file with columns
#'   <id> <term_id> <term_name> <Y_hat>
#'
#' <id>: Structure identifier like `MIP_00004873`
#' <term_ontology>: term onlogy, one of [BP, CC, EC, or MF]
#' <term_id>: GO or EC term identifiers like `GO:0009225`
#' <term_name> is the description of the term
assemble_DeepFRI_function_predictions <- function(
    data_path,
    output_path) {

    cat(
        "data path: ", data_path, "\n",
        "output path: ", output_path, "\n",
        sep = "")

    file_index <- 1

    scores <- c("BP", "CC", "EC", "MF") |>
        purrr::map_dfr(.f = function(ontology) {
            cat("Reading predictions cores for ontology ", ontology, "\n", sep = "")
            list.files(
                path = data_path,
                full.names = TRUE,
                pattern = paste0("*_", ontology, "_pred_scores.json.gz"),
                recursive = TRUE) |>
                purrr::map_dfr(.f = function(path) {
                    cat("Reading  '", path, "' ", file_index, "\n", sep = "")
                    file_index <<- file_index + 1

                    data <- jsonlite::fromJSON(txt = path)

                    scores <- as.data.frame(data$Y_hat)
                    names(scores) <- data$goterms
                    scores <- scores |>
                        dplyr::mutate(
                            id = data$pdb_chains,
                            .before = 1) |>
                        tidyr::pivot_longer(
                            cols = -"id",
                            names_to = "term_id",
                            values_to = "Y_hat") |>
                        dplyr::left_join(
                            data.frame(
                                term_ontology = ontology,
                                term_id = data$goterms,
                                term_name = data$gonames),
                            by = "term_id") |>
                        dplyr::select(
                            id,
                            term_ontology,
                            term_id,
                            term_name,
                            Y_hat)
                })
        })

    scores |>
        arrow::write_parquet(output_path)

}

# call assemble_rosetta_models for all the datasets
dataset_tag <- "rosetta_high_quality_function_predictions"
assemble_DeepFRI_function_predictions(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))

dataset_tag <- "rosetta_low_quality_function_predictions"
assemble_DeepFRI_function_predictions(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))


dataset_tag <- "dmpfold_high_quality_function_predictions"
assemble_DeepFRI_function_predictions(
    data_path = paste0(
        "data/mxoicrobiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))

dataset_tag <- "dmpfold_low_quality_function_predictions"
assemble_DeepFRI_function_predictions(
    data_path = paste0(
        "data/microbiome_immunity_project_dataset/dataset/",
        dataset_tag),
    output_path = paste0("intermediate/", dataset_tag, ".parquet"))