@@ -327,7 +327,108 @@ NULL
327
327
}
328
328
329
329
330
+ # ###############################################################################
331
+ # #
332
+ # # FEATURES THINGS
333
+ # #
334
+ # ###############################################################################
335
+
336
+ # ' Extracts feature values for one sample summing intensities for features
337
+ # ' with multiple peaks assigned.
338
+ # '
339
+ # ' @param hdf5_file `character(1)` with the HDF5 file name.
340
+ # '
341
+ # ' @param sample_id `character(1)` with the sample ID.
342
+ # '
343
+ # ' @param ms_level `integer(1)` with the MS level.
344
+ # '
345
+ # ' @param n_features `integer(1)` with the total number of features for that
346
+ # ' MS level.
347
+ # '
348
+ # ' @param method `character(1)` defining the method to be used to tackle
349
+ # ' features with multiple peaks.
350
+ # '
351
+ # ' @param col_idx `integer` with the index of the peak columns that should be
352
+ # ' loaded and processed by the functions. The first index **must** be the
353
+ # ' index of the `value` column (i.e. the column that should be reported).
354
+ # ' For `method = "maxint"`, the second column should be the column defined
355
+ # ' with parameter `intensity`, i.e. the column with the intensity values
356
+ # ' to select the *larger* peak. For `method = "rtmed"` it should be the
357
+ # ' index of the column `"rt"`.
358
+ # '
359
+ # ' @param filled `logical(1)` whether gap-filled values should be reported or
360
+ # ' removed.
361
+ # '
362
+ # ' @param rtmed `numeric` with the `"rtmed"` column of the feature definitions.
363
+ # ' Only used (but required) for `method = "medret"`.
364
+ # '
365
+ # ' @noRd
366
+ .h5_feature_values_sample <- function (sample_id , hdf5_file , ms_level ,
367
+ n_features , method ,
368
+ col_idx = integer(),
369
+ filled = TRUE , rtmed , ... ) {
370
+ res <- rep(NA_real_ , n_features )
371
+ sid <- paste0(" /" , sample_id , " /ms_" , ms_level )
372
+ vals <- .h5_read_data(hdf5_file , sample_id , name = " chrom_peaks" ,
373
+ ms_level = ms_level , column = col_idx )[[1L ]]
374
+ fidx <- .h5_read_data(hdf5_file , sample_id , name = " feature_to_chrom_peaks" ,
375
+ ms_level = ms_level )[[1L ]]
376
+ # # remove gap-filled values
377
+ if (! filled ) {
378
+ is_filled <- rhdf5 :: h5read(hdf5_file ,
379
+ paste0(sid , " /chrom_peak_data/is_filled" ),
380
+ drop = TRUE )
381
+ vals [is_filled , 1L ] <- NA_real_
382
+ }
383
+ # # set/assign single and multiple values.
384
+ res [fidx [, 1L ]] <- vals [fidx [, 2L ], 1L ]
385
+ if (method == " medret" ) {
386
+ # # calculate difference between feature and peak rt
387
+ vals [fidx [, 2L ], 2L ] <- vals [fidx [, 2L ], 2L ] - rtmed [fidx [, 1L ]]
388
+ }
389
+ # # handle duplicates
390
+ f <- factor (fidx [, 1L ], levels = seq_len(n_features ))
391
+ pk_idx <- split(fidx [, 2L ], f )
392
+ idx_multi <- which(lengths(pk_idx ) > 1L )
393
+ FUN <- switch (
394
+ method ,
395
+ sum = function (z ) sum(vals [z , 1L ]),
396
+ maxint = function (z ) vals [z , 1L ][which.max(vals [z , 2L ])],
397
+ medret = function (z ) vals [z , 1L ][which.min(abs(vals [z , 2L ]))])
398
+ res [idx_multi ] <- vapply(pk_idx [idx_multi ], FUN , 1.1 )
399
+ res
400
+ }
330
401
402
+ # ' Get feature values for a specific MS level.
403
+ # '
404
+ # ' @noRd
405
+ .h5_feature_values_ms_level <- function (ms_level , x , method , value , intensity ,
406
+ filled = TRUE ) {
407
+ cn <- h5read(x @ hdf5_file , paste0(" /" , x @ sample_id [1L ], " /ms_" , ms_level ,
408
+ " /chrom_peaks_colnames" ), drop = TRUE )
409
+ col <- switch (method ,
410
+ sum = value ,
411
+ medret = c(value , " rt" ),
412
+ maxint = c(value , intensity ))
413
+ if (! all(col %in% cn ))
414
+ stop(" Not all requested columns available. Please make sure 'value' " ,
415
+ " and 'intensity' (if defined) are available columns in the " ,
416
+ " chrom peak matrix." , call. = FALSE )
417
+ col_idx <- match(col , cn )
418
+ rtmed <- rhdf5 :: h5read(x @ hdf5_file ,
419
+ paste0(" /features/ms_" , ms_level ,
420
+ " /feature_definitions/rtmed" ), drop = TRUE )
421
+ rn <- rhdf5 :: h5read(x @ hdf5_file ,
422
+ paste0(" /features/ms_" , ms_level ,
423
+ " /feature_definitions_rownames" ), drop = TRUE )
424
+ res <- do.call(
425
+ cbind , lapply(x @ sample_id , .h5_feature_values_sample ,
426
+ hdf5_file = x @ hdf5_file , ms_level = ms_level ,
427
+ n_features = length(rtmed ), method = method ,
428
+ col_idx = col_idx , filled = filled , rtmed = rtmed ))
429
+ rownames(res ) <- rn
430
+ res
431
+ }
331
432
332
433
# ###############################################################################
333
434
# #
709
810
h5 <- rhdf5 :: H5Fopen(h5_file )
710
811
on.exit(invisible (rhdf5 :: H5Fclose(h5 )))
711
812
FUN <- .h5_write_matrix
712
- if (name %in% c(" chrom_peak_data" , " feature_definition " ))
813
+ if (name %in% c(" chrom_peak_data" , " feature_definitions " ))
713
814
FUN <- .h5_write_data_frame
714
815
comp_level <- .h5_compression_level()
715
816
for (i in seq_along(data_list )) {
725
826
replace = replace )
726
827
}
727
828
.h5_increment_mod_count(h5 )
728
- }
829
+ }
0 commit comments