input_check.R 45.2 KB
Newer Older
Robin Engler's avatar
Robin Engler committed
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
####################################################################################################
#' Scans the input directory and searches for the following files:
#'  - sub-directories containing "_cell_seg_data.txt" and "_tissue_seg_data_summary.txt" files.
#'  - PARAMETERS_FILE
#'  - SAMPLE_RENAME_FILE
#'
#' @param input_dir [string] Path of input directory to scan.
#' @param output_dir [string] Path of output directory.
#'
inputdir_check <- function(input_dir, output_dir){

    # Verify the specified input and output directories exist.
    if(!dir.exists(input_dir)) raise_error(paste('Input directory could not be found:', input_dir))
    if(!dir.exists(output_dir)) raise_error(paste('Output directory could not be found:', output_dir))

    # Verify that at least one sub-directory is present and that it contains at least one pair of
    # cell/tissue segmentation files. We do not check file content at this point.
    subdirs = list.dirs(input_dir, recursive=FALSE)
    if(length(subdirs) == 0 |
       all(sapply(subdirs,
                  FUN=function(x) length(scan_dir_for_seg_data_files(input_directory=x)) == 0))
       ) raise_error(
            msg = paste0('At least one subdirectory with *_cell_seg_data.txt and ',
                         '*_tissue_seg_data_summary.txt files must be present in the input data.'),
            file = input_dir)

    # Backward compatibility: if no 'parameters.txt' file is present, we attempt to generate one
    # from legacy "marker combination" and "threshold" files.
    file_list = list.files(path=input_dir, all.files=FALSE, full.names=FALSE, recursive=FALSE)
    file_list = file_list[!file.info(file.path(input_dir, file_list))$isdir]
    if(!PARAMETERS_FILE %in% file_list){
        if(any(grepl('^marker_combinations.*\\.txt$',
                     x=file_list))) generate_parameter_file_from_legacy_input(input_dir)
    }

    # Verify that input parameters and sample rename files are present, and if needed rename them.
    rename_file_by_pattern(file_name=PARAMETERS_FILE, pattern='param', dir_name=input_dir,
                           out_dir=output_dir, raise_error_if_absent=TRUE)
Robin Engler's avatar
Robin Engler committed
39
40
    rename_file_by_pattern(file_name=THRESHOLDS_FILE, pattern='threshold', dir_name=input_dir,
                           out_dir=output_dir, raise_error_if_absent=FALSE)
Robin Engler's avatar
Robin Engler committed
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
    rename_file_by_pattern(file_name=SAMPLE_RENAME_FILE, pattern='rename', dir_name=input_dir,
                           out_dir=output_dir, raise_error_if_absent=FALSE)
    return(invisible(NULL))
}
####################################################################################################



####################################################################################################
#' Search for a single file whose name matches @pattern in directory @dir_name. If a file is found,
#' but its name is not exactly @out_dir/@file_name, rename/copy it to @out_dir/@file_name.
#'
#' @param file_name [string]
#' @param raise_error_if_absent [logical] If TRUE, and error is raised if not exactly 1 file
#'     matching @pattern is found in @dir_name.
#'
rename_file_by_pattern <- function(file_name, pattern, dir_name,
                                   out_dir = dir_name,
                                   raise_error_if_absent = FALSE){

    # Verify input and output directories exist.
    if(!dir.exists(dir_name)) raise_error("Unable to find input directory.", file=dir_name)
    if(!dir.exists(out_dir)) raise_error("Unable to find output directory.", file=out_dir)

    # List all files in input directory.
    file_list = list.files(path=dir_name, all.files=FALSE, full.names=FALSE, recursive=F)
    file_list = file_list[!file.info(file.path(dir_name, file_list))$isdir]

    # If the target file already exists, copy it to the output directory, if needed.
    if(file_name %in% file_list){
        if(out_dir != dir_name) file.copy(from=file.path(dir_name, file_name), to=out_dir)
        return(invisible(NULL))
    }

    # Rename single file matching the search pattern.
    file_to_rename = grep(pattern, file_list, ignore.case=TRUE, value=TRUE)
    if(length(file_to_rename) == 1){
        if(out_dir == dir_name){
            file.rename(from=file.path(dir_name, file_to_rename), to=file.path(out_dir, file_name))
        } else{
            file.copy(from=file.path(dir_name, file_to_rename), to=file.path(out_dir, file_name))
        }
        return(invisible(NULL))
    }

    # If no or multiple matches are found, raise error.
    if(length(file_to_rename) > 1) raise_error(
        msg = paste0('Multiple potential [', file_name, '] files found in input directory:'),
        file = dir_name,
        items_to_list = file_to_rename)
    if(raise_error_if_absent & length(file_to_rename) == 0) raise_error(
        msg = paste0('No [', file_name, '] file found in input directory.'),
        file = dir_name)
    return(invisible(NULL))
}
####################################################################################################



####################################################################################################
standardize_and_split_cell_data <- function(input_file,
                                            samples,
                                            markers_phenotyped,
                                            markers_scored,
                                            cell_compartment,
                                            phenotype_confidence_threshold,
                                            delete_input_file = FALSE){
    # ********************************************************************************************
    # Differences between inForm versions:
    # version 2.2
    #  - *_tissue_seg_data_summary.txt files contain a column named "Region Area (pixels)".
    #
    # version 2.4
    #  - a new column named "Annotation ID" is added to *_cell_seg_data.txt.
    #  - *_tissue_seg_data_summary.txt files contain column named "Region Area (square microns)"
    #  - in addition the "Sample ID" column of the *_cell_seg_data.txt files no longer contains
Robin Engler's avatar
Robin Engler committed
117
    #    the image ID values. These are now present in the "Annotation ID" column.
Robin Engler's avatar
Robin Engler committed
118
119
120
121
122
123
124
125
126
127
128
    #
    #
    # Input arguments:
    #   input_file:
    #   markers_phenotyped: vector of strings. List of input markers that are phenotyped.
    #   markers_scored
    #   cell_compartment
    #   phenotype_confidence_threshold
    #   delete_input_file: if TRUE, the input_file is deleted after it was split by samples.
    # ********************************************************************************************

Robin Engler's avatar
Robin Engler committed
129
    # Load input table. Verify it is not empty and standardize the column names.
Robin Engler's avatar
Robin Engler committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    input_table = read.table(input_file, sep='\t', as.is=T, header=T,
                             colClasses='character', check.names=T, strip.white=T)
    if(nrow(input_table) == 0) raise_error('Input file has zero rows.', file = input_file)
    colnames(input_table) = standardize_column_names(colnames(input_table), input_file)


    # Verify all required columns are present in the table. If the 'phenotype' and/or 'confidence'
    # columns are missing, dummy values are created for them.
    if(!'phenotype' %in% colnames(input_table)) input_table[,'phenotype'] = 'MISSING'
    if(!'confidence' %in% colnames(input_table)) input_table[,'confidence'] = 100
    verify_columns_are_present(required_columns = c('sample_name', 'tissue_category', 'phenotype',
                                                    'confidence', 'cell_id', 'cell_x_position',
                                                    'cell_y_position'),
                               actual_columns   = colnames(input_table),
                               file_name        = input_file)


    # Extract sample names and filter to keep only rows matching samples names present in the
    # input parameters.
    sample_names = extract_sample_name(input_table[,'sample_name'], input_file=input_file)
    input_table = input_table[sample_names %in% samples,]
    sample_names = sample_names[sample_names %in% samples]
Robin Engler's avatar
Robin Engler committed
152
153
154
155
    if(nrow(input_table) == 0) raise_error(
        msg = 'No matching values found in [sample_name] column for any of the input samples',
        file = input_file,
        items_to_list = samples)
Robin Engler's avatar
Robin Engler committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179


    # Extract image ID values. If the "annotation_id" column is present (inForm 2.4), extract
    # image ID values from it. Otherwise extract from the "sample_name" column (inForm 2.2).
    colnb = tail(which(colnames(input_table) %in% c('sample_name', 'annotation_id')), n=1)
    image_ids = extract_imageid(input_table[,colnb], input_file=input_file)


    # Capitalize tissue types in the "tissue_category" column, if needed.
    tissue_type_values = check_and_fix_tissue_type(
        tissue_type_values = input_table[,'tissue_category'],
        file_values_are_from = input_file)


    # Verify and fix 'Phenotype' and 'Confidence' values.
    confidence_values = check_and_fix_confidence_values(
        confidence_values    = input_table[,'confidence'],
        file_values_are_from = input_file)
    phenotype_values = check_and_fix_phenotype_values(
        phenotype_values     = input_table[,'phenotype'],
        markers_phenotyped   = markers_phenotyped,
        file_values_are_from = input_file)


Robin Engler's avatar
Robin Engler committed
180
    # Re-class 'phenotype_values' for rows where confidence < phenotype_confidence_threshold
Robin Engler's avatar
Robin Engler committed
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
    # to the value of 'MISSING'.
    phenotype_values[which(confidence_values < phenotype_confidence_threshold)] = 'MISSING'


    # Verify marker intensity values. Verify all scored markers have matching '_mean' column.
    marker_column_list = which(endsWith(colnames(input_table), '_mean'))
    for(i in marker_column_list) input_table[,i] = check_and_fix_marker_intensity_values(
                                                                intensity_values = input_table[,i],
                                                                file_values_are_from = input_file)
    if(length(markers_scored) > 0){
        missing_markers = which(! paste0(markers_scored, '_mean') %in% colnames(input_table))
        if(length(missing_markers) > 0) raise_error(
            msg  = 'Missing scored marker columns for the following markers:',
            file = input_file,
            items_to_list = markers_scored[missing_markers])
    }


    # Split data by sample and save it to disk
    # ****************************************
    # Test whether file contains individual markers.
    prefix = paste0('individualmarker_', paste(markers_in_file_name(input_file), collapse=''), '_')
    if(prefix == 'individualmarker__') prefix = ''

    # Split data by samples.
    for(sample in sort(unique(sample_names))){
        rows_to_keep = which(sample_names==sample)
        out_dir = file.path(dirname(dirname(input_file)), sample)
        if(!dir.exists(out_dir)) dir.create(out_dir)
        out_file = file.path(out_dir, paste0(prefix, sample, CELL_FILES_EXTENSION))
        out_file_exists = file.exists(out_file)
        write.table(data.frame('sample_name'     = sample_names[rows_to_keep],
                               'image_id'        = image_ids[rows_to_keep],
                               'cell_x_position' = input_table[rows_to_keep, 'cell_x_position'],
                               'cell_y_position' = input_table[rows_to_keep, 'cell_y_position'],
216
                               'cell_id'         = input_table[rows_to_keep, 'cell_id'],
Robin Engler's avatar
Robin Engler committed
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
                               'tissue_category' = tissue_type_values[rows_to_keep],
                               'phenotype'       = phenotype_values[rows_to_keep],
                               input_table[rows_to_keep, marker_column_list],
                               stringsAsFactors  = FALSE),
                    file      = out_file,
                    append    = out_file_exists,
                    col.names = !out_file_exists,
                    sep       = '\t',
                    quote     = FALSE,
                    row.names = FALSE)
    }
    # Delete the original input file from disk.
    if(delete_input_file) unlink(input_file)

}
####################################################################################################



####################################################################################################
standardize_and_split_tissue_data <- function(input_file,
                                              samples,
                                              delete_input_file = FALSE){
    # ********************************************************************************************
    #
    # Input arguments:
    #  -> input_file:  string. Path + name of tissue data file to process.
244
    #  -> delete_input_file: logical. If TRUE, the original input_file is deleted at the end of
Robin Engler's avatar
Robin Engler committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
    #                        the function, after it was split by samples.
    #
    # Return value:
    # The function does not return anything. It writes the split files directly to disk.
    # ********************************************************************************************

    # Standardize and verify column names of input file.
    # *************************************************
    input_table = read.table(input_file, sep='\t', as.is=T, header=T,
                             colClasses='character', check.names=T, strip.white=T)
    if(nrow(input_table) == 0) raise_error('Input file has zero rows.', input_file = input_file)
    colnames(input_table) = standardize_column_names(colnames(input_table))
    verify_columns_are_present(required_columns = c('sample_name', 'tissue_category',
                                                    'region_area_percent'),
                               actual_columns   = colnames(input_table),
                               file_name        = input_file)


    # Extract sample name and "image ID" values.
    # *****************************************
    # Extract sample names and filter to keep only rows matching samples names present in the
    # input parameters.
    sample_names = extract_sample_name(input_table[,'sample_name'], input_file=input_file)
    input_table = input_table[sample_names %in% samples,]
    sample_names = sample_names[sample_names %in% samples]
270
271
    if(nrow(input_table) == 0) raise_error('Input file has zero rows for samples:',
                                           paste(samples, collapse=', '), file=input_file)
Robin Engler's avatar
Robin Engler committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358


    # Extract image ID values. If the "annotation_id" column is present (inForm 2.4), extract
    # image ID values from it. Otherwise extract from the "sample_name" column (inForm 2.2).
    colnb = tail(which(colnames(input_table) %in% c('sample_name', 'annotation_id')), n=1)
    image_ids = extract_imageid(input_table[,colnb], input_file=input_file)


    # Capitalize tissue types in the "tissue category" column, if needed.
    # ******************************************************************
    tissue_type_values = check_and_fix_tissue_type(
        tissue_type_values = input_table[,'tissue_category'],
        file_values_are_from = input_file)


    # Extract tissue region surface values.
    # ************************************
    # Tissue surface values are recorded differently between inForm versions:
    #  -> inForm 2.2: surface is given in pixels and we thus have to convert it to square microns.
    #  -> inForm 2.4: surface is given directly in square microns.
    #
    # Case of inForm 2.4 files.
    if('region_area_square_microns' %in% colnames(input_table)){
        region_area_surface = as.numeric(input_table[, 'region_area_square_microns'])

        # Case of inForm 2.2 files.
        # Convert surfaces to square micrometers: 1 pixel is 0.246 square micrometers.
    } else if('region_area_pixels' %in% colnames(input_table)){
        region_area_surface = round(as.numeric(input_table[, 'region_area_pixels']) * 0.246, 1)

        # Non-recognized format. Raise error.
    } else{
        raise_error(msg = 'Missing [region_area_square_microns] or [region_area_pixels] column.',
                    file = input_file)
    }
    region_area_surface[which(is.na(region_area_surface))] = 0


    # Verify tissue region percentage values.
    # **************************************
    # Remove trailing '%' character, replace ',' by '.' (in case ',' was used as number fractional
    # part separator) and replace empty values with 0. Then convert values to number.
    region_area_percent = sub('%', '', input_table[, 'region_area_percent'])
    region_area_percent = sub(',', '.', region_area_percent)
    region_area_percent[which(region_area_percent=='')] = 0
    region_area_percent = as.numeric(region_area_percent)
    if(any(is.na(region_area_percent))){
        raise_error(msg = 'non-numeric or missing value in [Region Area (percent)] column.',
                    file = input_file)
    }


    # Split data by sample and save it to disk.
    # ****************************************
    # Test whether file contains individual markers.
    prefix = paste0('individualmarker_', paste(markers_in_file_name(input_file), collapse=''), '_')
    if(prefix == 'individualmarker__') prefix = ''

    # Split data by sample.
    for(sample in sort(unique(sample_names))){
        rows_to_keep = which(sample_names==sample)
        out_dir = file.path(dirname(dirname(input_file)), sample)
        if(!dir.exists(out_dir)) dir.create(out_dir)
        out_file = file.path(out_dir, paste0(prefix, sample, TISSUE_FILES_EXTENSION))
        out_file_exists = file.exists(out_file)
        write.table(sort_by_imageid_samplename_and_tissuecategory(data.frame(
                        'sample_name'         = sample_names[rows_to_keep],
                        'image_id'            = image_ids[rows_to_keep],
                        'tissue_category'     = tissue_type_values[rows_to_keep],
                        'region_area_surface' = region_area_surface[rows_to_keep],
                        'region_area_percent' = region_area_percent[rows_to_keep],
                        stringsAsFactors      = FALSE)),
                    file=out_file,
                    append=out_file_exists,
                    col.names=!out_file_exists,
                    sep='\t',
                    quote=FALSE,
                    row.names=FALSE)
    }
    # Delete the original input file from disk.
    if(delete_input_file) unlink(input_file)
}
####################################################################################################



####################################################################################################
Robin Engler's avatar
Robin Engler committed
359
360
361
362
363
364
#' Standardize column names of input files.
#'
#' @param column_names [string vector] Names of columns to standardize.
#' @param input_file [string] Path and name of file from which the columns were taken. Only used to
#'     display an error message.
#' @return Standardized column names.
Robin Engler's avatar
Robin Engler committed
365
366
367
368
standardize_column_names = function(column_names, input_file){

    # Replace any '.' in column names by an '_'. The '.' are generally introduced in column names
    # by R as a replacement of a non-authorized character such as a blank space or a bracket.
Robin Engler's avatar
Robin Engler committed
369
    # For readability, multiple '.' are replaced by a single '_'.
Robin Engler's avatar
Robin Engler committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
    column_names = gsub(pattern='\\.+', replacement='_', x=column_names)
    column_names = gsub(pattern='_+$', replacement='', x=column_names)

    # Convert standard column and cell compartment names to lower case.
    standard_names = c('sample_name', 'tissue_category', 'phenotype', 'cell_id',
                       'cell_x_position', 'cell_y_position', 'annotation_id', 'confidence',
                       'region_area_square_microns', 'region_area_percent', 'region_area_pixels')
    for(x in c(standard_names, AUTHORIZED_COMPARTMENTS)){
        column_names = sub(pattern=paste0('^', x), replacement=x, x=column_names, ignore.case=T)
    }

    # Convert '_Mean_' to '_mean_' (in "Mean_Normalized_Counts").
    column_names = sub(pattern='_mean_', replacement='_mean_', x=column_names, ignore.case=T)

    # Rename columns that contain mean marker intensity values.
    col_start_regexp = paste0('^(', paste(AUTHORIZED_COMPARTMENTS, collapse = '|'), ')_')
    for(i in grep(paste0(col_start_regexp, '.*_mean_.*'), x=column_names)){
        marker_name = sub(col_start_regexp, '', column_names[i])
        marker_name = sub('_.*$', '', marker_name)
Robin Engler's avatar
Robin Engler committed
389
390
391
392
393
394
395
396

        # If the marker is present in the AUTHORIZED_MARKERS list, correct its capitalization if
        # needed.
        x = which(toupper(marker_name) == toupper(AUTHORIZED_MARKERS))
        stopifnot(length(x) <= 1)
        marker_name = ifelse(length(x) == 0, marker_name, AUTHORIZED_MARKERS[x])

        # Rename column.
Robin Engler's avatar
Robin Engler committed
397
398
399
        column_names[i] = paste0(marker_name, '_mean')
    }

Robin Engler's avatar
Robin Engler committed
400
    # Verify there are no duplicate columns.
Robin Engler's avatar
Robin Engler committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
    duplicated_columns = which(duplicated(column_names))
    if(length(duplicated_columns) > 0) raise_error(
        msg  = 'Duplicated column names found in input file:',
        file = input_file,
        items_to_list = column_names[duplicated_columns])

    return(column_names)
}
####################################################################################################



####################################################################################################
check_and_fix_tissue_type = function(tissue_type_values, file_values_are_from){
    # ********************************************************************************************
    # Convert tissue type values to lower case and verify they are part of the authorized tissue
    # list.
    # tissue_type_values:   string vector. List of values to check.
    # file_values_are_from: string. Path + name of files from where the tissue type values
    #                       were taken. This is only used to display an error message.
    # ********************************************************************************************

    # Convert values to lower case. Replace missing tissue values with the default NOTISSUE value.
    tissue_type_values = tolower(trimws(tissue_type_values))
    tissue_type_values[tissue_type_values %in% NOTISSUES_SYNONYMS] = NOTISSUE

    # Verify that all values in tissue_type_values are part of the authorized list.
    offending_values = unique(tissue_type_values[!tissue_type_values %in% AUTHORIZED_TISSUES])
    if(length(offending_values) > 0) raise_error(
        msg = c(paste0('Invalid tissue type(s) found in input file: ',
                       paste0(offending_values, collapse = ', '), '.'),
                'The list of authorized tissue values is the following:'),
        items_to_list = AUTHORIZED_TISSUES,
        file = file_values_are_from)
    return(tissue_type_values)
}
####################################################################################################



####################################################################################################
#' Verifies that phenotype values are correct, and if needed, corrects them.
#'
#' @param phenotype_values
#' @param markers_phenotyped  : List of phenotyped markers for the current session.
#' @param file_values_are_from [string] Name of file from where phenotype values were taken. Only
#'     used to display error message.
check_and_fix_phenotype_values <- function(phenotype_values,
                                           markers_phenotyped,
                                           file_values_are_from = ''){

    # Replace all empty phenotype values by 'MISSING'. This is the placeholder for missing
    # phenotype values.
    phenotype_values = trimws(as.character(phenotype_values))
    row_ids = which(phenotype_values == '')
    if(length(row_ids) > 0){
        phenotype_values[row_ids] = 'MISSING'

        # Display warning indicating how many values where modified.
        missing_fraction = round(length(row_ids) / length(phenotype_values) * 100, 2)
        if(missing_fraction > 1.0) raise_error(
            msg=c('Empty rows found in [Phenotype] column of input file: ',
                paste0('Number of offending values: ', length(row_ids), ' [',missing_fraction,'%]'),
                'The values of these rows has been set to [MISSING].'),
            file = file_values_are_from,
            type = 'warning')
    }

Robin Engler's avatar
Robin Engler committed
469
470
    # Replace

Robin Engler's avatar
Robin Engler committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
    # Substitute '-' with '_' in Phenotype values. This is for the case where a '-' was used as
    # separator value instead of a '_'.
    phenotype_values = gsub(pattern='-', replacement='_', phenotype_values)

    # Substitute any trailing '+' with 'p' in the phenotype values.  E.g. 'CD8+' becomes 'CD8p',
    # or 'CD8+_CD11+' becomes 'CD8p_CD11p'.
    phenotype_values = gsub(pattern='\\+', replacement='p', phenotype_values)

    # Substitute any duplicated '_' with a single '_'. Remove leading or trailing '_'.
    phenotype_values = gsub(pattern='_[_]+', replacement='_', phenotype_values)
    phenotype_values = gsub(pattern='^_|_$', replacement='', phenotype_values)

    # To save time, we exit the function here if all phenotype values are correct. If this
    # is not the case, then the function carries-on and will try to correct those values.
    if(length(get_bad_markers(phenotype_values,
                              authorized_markers = markers_phenotyped,
                              ignored = IGNORED_PHENOTYPES)) == 0) return(phenotype_values)


Robin Engler's avatar
Robin Engler committed
490
491
492
493
    # Replace accepted NO_PHENOTYPE synonym values with NO_PHENOTYPE.
    phenotype_values[which(toupper(phenotype_values) %in%
                           toupper(c(NO_PHENOTYPE, NO_PHENOTYPE_SYNONYMS)))] = NO_PHENOTYPE

Robin Engler's avatar
Robin Engler committed
494
495
496
497
    # Replace accepted stroma and tumor synonyms with 'DAPIp' and 'CKp' respectively.
    for(x in 1:2) phenotype_values = gsub(
        pattern=paste0(rep(switch(x, AUTHORIZED_STROMA_VALUES, AUTHORIZED_TUMOR_VALUES), each=2),
                       c('', 'p'), collapse='|'),
Robin Engler's avatar
Robin Engler committed
498
        replacement=switch(x, 'DAPIp', 'CKp'), x=phenotype_values, ignore.case=TRUE)
Robin Engler's avatar
Robin Engler committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698


    # Correct capitalization and add a 'p' suffix (for 'positive') to any marker missing it.
    bad_values = get_bad_markers(phenotype_values,
                                 authorized_markers=AUTHORIZED_MARKERS, ignored=IGNORED_PHENOTYPES)
    authorized_values = paste0(AUTHORIZED_MARKERS,'p')
    for(bad_value in bad_values){
        correct_value = authorized_values[which(tolower(authorized_values) == tolower(bad_value))]
        if(length(correct_value) == 0) correct_value = authorized_values[which(
                                    tolower(authorized_values) == paste0(tolower(bad_value),'p'))]
        if(length(correct_value) != 1) next
        for(x in 1:4){
            regexp_pattern = switch(x, paste0('^',bad_value,'$'),
                                       paste0('^',bad_value,'_'),
                                       paste0('_',bad_value,'_'),
                                       paste0('_',bad_value,'$'))
            replacement = switch(x, correct_value,
                                    paste0(correct_value,'_'),
                                    paste0('_',correct_value,'_'),
                                    paste0('_',correct_value))
            bad_ids = grep(regexp_pattern, phenotype_values, ignore.case = TRUE)
            if(length(bad_ids) > 0) phenotype_values[bad_ids] = gsub(pattern = regexp_pattern,
                                                                     replacement = replacement,
                                                                     x = phenotype_values[bad_ids],
                                                                     ignore.case=TRUE)
        }
    }
    stopifnot(sum(grepl('__', phenotype_values)) == 0)


    # At this point all values should be in the AUTHORIZED_MARKERS list.
    bad_values = get_bad_markers(phenotype_values,
                                 authorized_markers=AUTHORIZED_MARKERS, ignored=IGNORED_PHENOTYPES)
    if(length(bad_values) > 0) raise_error(
        msg = c(paste('One or more unauthorized markers were found in the [Phenotype] column:',
                      paste(bad_values, collapse=', ')),
                'The list of authorized markers is the following:'),
        file = file_values_are_from,
        items_to_list = AUTHORIZED_MARKERS)

    # If there are values not part of the phenotyped marker list, display a warning.
    bad_values = get_bad_markers(phenotype_values,
                                 authorized_markers=markers_phenotyped, ignored=IGNORED_PHENOTYPES)
    if(length(bad_values) > 0) raise_error(
        msg = 'Some marker values in the [Phenotype] column are not part of the input parameters:',
        file = file_values_are_from,
        items_to_list = bad_values, type = 'warning')

    return(phenotype_values)
}
####################################################################################################



####################################################################################################
get_bad_markers <- function(phenotype_values, authorized_markers, ignored){
    # ********************************************************************************************
    #
    # ********************************************************************************************
    marker_values = unique(unlist(strsplit(unique(phenotype_values), split='_')))
    authorized_list = c(paste0(authorized_markers,'p'), ignored)
    return(marker_values[which(! marker_values %in% authorized_list)])
}
####################################################################################################



####################################################################################################
check_and_fix_confidence_values <- function(confidence_values, file_values_are_from=''){
    # ********************************************************************************************
    # Takes a vector of confidence values, checks that they are correct, and if needed - and
    # possible - corrects them.
    #
    # Input arguments:
    #  -> confidence_values   : confidence values to check/fix.
    #  -> file_values_are_from: file name from which the confidence values are taken from.
    #                           This is only used to display in the error message.
    # ********************************************************************************************

    # Remove any trailing '%' character or whitespaces, and replace empty values '' with 0.
    confidence_values = gsub('%', '', trimws(confidence_values))
    confidence_values[which(confidence_values == '' | confidence_values == 'NaN')] = 0
    confidence_values = as.numeric(confidence_values)

    # Make sure that all values are now numeric.
    if(any(is.na(confidence_values))){
        offending_values = unique(confidence_values[which(is.na(confidence_values))])
        raise_error(msg = 'Non-numeric value detected in [Confidence] column:',
                    file = file_values_are_from,
                    items_to_list = offending_values)
    }
    return(confidence_values)
}
####################################################################################################


####################################################################################################
check_and_fix_marker_intensity_values <- function(intensity_values,
                                                  file_values_are_from = ''){
    # *******************************************************************************************
    # Takes a vector of marker intensity values, checks that they are correct, and if needed
    # corrects them.
    #
    # -> intensity_values    : marker intensity values to check/fix.
    # -> file_values_are_from: file name from which the marker intensity values are taken from.
    #                          This is only used to display in the error message.
    # *******************************************************************************************

    # Replace empty values with 0.
    intensity_values[which(grepl('^[[:space:]]*$', intensity_values))] = 0

    # Check whether values are in US format, i.e. they use a ',' instead of '.' as separator
    # between the integer and fractional part of the number.
    if(any(grepl(',', intensity_values))) intensity_values = sub(',','.',intensity_values)

    # Check for non-numeric values.
    tmp = unique(intensity_values)
    bad_ids = which(is.na(as.numeric(tmp)))
    if(length(bad_ids) > 0) raise_error(
        msg = 'Non-numeric values detected in one of the marker intensity columns:',
        file = file_values_are_from,
        items_to_list = tmp[bad_ids])

    # Convert all values to numeric.
    return(as.numeric(intensity_values))
}
####################################################################################################



####################################################################################################
check_and_fix_marker_combination_values <- function(marker_combinations,
                                                    markers_phenotyped,
                                                    markers_scored,
                                                    file_values_are_from = ''){
    # ********************************************************************************************
    # Verify marker combination (cell type) values.
    #   marker_list: list of accepted markers for the current session, i.e. list of phenotyped
    #                and scored markers for the current session.

    # ********************************************************************************************

    # Substitute '+' by 'p' if needed. Both '+' and 'p' can be used to indicate positive markers.
    # Remove duplicates.
    marker_combinations = gsub(pattern='\\+', replacement='p', x=marker_combinations)
    marker_combinations = unique(marker_combinations)

    # Check that all individual markers that compose a cell type (e.g. 'CD4p_CD8p' is decomposed in
    # 'CD4p' and 'CD8p') end in 'p' or 'm' (for positive/negative).
    individual_markers = unique(unlist(strsplit(marker_combinations, split='_')))
    if(any(! grepl(pattern="p$|m$", x=individual_markers) )) raise_error(
        'One or more cell types are missing the [p] or [m] suffix.', file=file_values_are_from)

    # Cell types that are composed of a single marker (e.g. "CD8p", "CD11p") must always end in
    # "p" (positive). Negative ("m") single markers make no sense.
    single_markers = marker_combinations[!grepl(pattern='_', x=marker_combinations)]
    if(any(! grepl(pattern="p$", x=single_markers))) raise_error(
        'one or more single cell types do not end in [p].', file=file_values_are_from)

    # Check that all individual markers belong to the authorized marker list.
    individual_markers = sub('p$|m$', '', individual_markers)
    check_marker_is_authorized(individual_markers, marker_type='combination')

    # Check that all individual markers are either part of the phenotyped or scored marker list.
    bad_values = individual_markers[!individual_markers %in% c(markers_phenotyped, markers_scored)]
    if(length(bad_values) > 0) raise_error(
        msg='The following markers present in combinations are not lised as phenotyped or scored.',
        items_to_list = bad_values,
        file = file_values_are_from)

    return(marker_combinations)
}
####################################################################################################



####################################################################################################
check_marker_is_authorized <- function(marker_list, marker_type){
    # ********************************************************************************************
    # Verify that all makers passed in the marker_list are part of the AUTHORIZED_MARKERS list,
    # and raise an error if not.
    #
    # Input arguments:
    #   marker_list: string vector. List of markers to check.
    #   marker_type: string. Type of marker, e.g. 'phenotyped', 'scored', 'combination'. This is
    #                used in the error message that is displayed.
    # ********************************************************************************************
    bad_markers = marker_list[!marker_list %in% AUTHORIZED_MARKERS]
    if(length(bad_markers) > 0) raise_error(
        msg = c(paste0('Non-authorized marker detected in [', marker_type, '] markers input: ',
                     paste(bad_markers, collapse= ', ')),
                'The list of authorized markers is the following:'),
        file = PARAMETERS_FILE,
        items_to_list = AUTHORIZED_MARKERS)
}
####################################################################################################



####################################################################################################
Robin Engler's avatar
Robin Engler committed
699
700
701
702
#' Test whether a file contains "individual marker" values. This is simply done by looking at the
#' file name. The convention is that individual marker files will contain at least one marker name
#' in their file name.
#' The function returns the list of markers in the file name, or character(0) if none is found.
Robin Engler's avatar
Robin Engler committed
703
704
markers_in_file_name <- function(file_name){
    return(sort(as.character(names(unlist(
Robin Engler's avatar
Robin Engler committed
705
        sapply(AUTHORIZED_MARKERS, FUN=function(x) grep(x, basename(file_name), ignore.case=T)))))))
Robin Engler's avatar
Robin Engler committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
}
####################################################################################################



####################################################################################################
#'
#' @param samples_from_parameters Sample names passed as input parameter by the user.
#' @param session_root_dir [string] Root directory for the current session.
check_sample_names <- function(samples_from_parameters, session_root_dir){

    # Retrieve samples present in data from subdirectory names present in session_root_dir.
    samples_from_data = sort(basename(list.dirs(session_root_dir, full.names=TRUE, recursive=F)))

    # If samples present in the raw data are missing from the input parameters, raise warning.
    missing_samples = samples_from_data[!samples_from_data %in% samples_from_parameters]
    if(length(missing_samples) > 0) raise_error(
        msg = 'One or more samples present in the data are not listed in input parameters:',
        items_to_list = missing_samples,
        type = 'warning')

    # All sample names passed as input by the user must be present in the data.
    missing_samples = samples_from_parameters[!samples_from_parameters %in% samples_from_data]
    if(length(missing_samples) > 0) raise_error(
        msg = 'One or more samples listed in the input paramters are missing from the data:',
        items_to_list = missing_samples)
}
####################################################################################################



####################################################################################################
check_sample_directories <- function(sample_list, session_root_dir){
    # ********************************************************************************************
    #
    #  sample_list     : samples to process in the current analysis.
    #  session_root_dir: root directory for the current session.
    # ********************************************************************************************
    error_msg = NULL
    for(sample_name in sample_list){

        # Verify there is a subdirectory for the sample.
        sample_dir = file.path(session_root_dir, sample_name)
        if(!dir.exists(sample_dir)){
            error_msg = c(error_msg, paste0(sample_name,': no subdirectory found for sample.'))
            next
        }

        # Verify that each sample subdirectory contains exactly one cell and one tissue
        # segmentation file.
        cell_files = list.files(path=sample_dir, pattern=paste0('.*', CELL_FILES_EXTENSION, '$'),
                                all.files=F, full.names=F, recursive=F, ignore.case=F)
        tissue_files = list.files(path=sample_dir, pattern=paste0('.*',TISSUE_FILES_EXTENSION,'$'),
                                  all.files=F, full.names=F, recursive=F, ignore.case=F)
        if(length(cell_files) == 0) error_msg = c(error_msg,
                                          paste0(sample_name,': no cell segmentation data found.'))
        if(length(tissue_files) == 0) error_msg = c(error_msg,
                                        paste0(sample_name,': no tissue segmentation data found.'))
        if(length(cell_files) > 1) error_msg = c(error_msg,
                     paste0(sample_name,': more than one cell segmentation file in subdirectory.'))
        if(length(tissue_files) > 1) error_msg = c(error_msg,
                   paste0(sample_name,': more than one tissue segmentation file in subdirectory.'))
    }
    if(length(error_msg) > 0){
        log_message(paste('ERROR:', error_msg), padding='')
        raise_error('One or more errors in input data detected. See above.')
    }
}
####################################################################################################



####################################################################################################
extract_sample_name <- function(input_vector, input_file='file not specified'){
    # ********************************************************************************************
    # Extract "sample name" values from input_vector strings. Depending on the version of inForm
    # that was used to generate the data, the format of the sample name is different:
    #  -> inForm 2.2: <sample name>[image id].im3, e.g.: OE37_V0[66030,14035].im3
    #  -> inForm 2.4: <sanple name>_Scan[number].qptiff, e.g. Bern_TMA1_Scan1.qptiff
    #
    # Input parameters:
    #  -> input_vector: vector of strings from which to extract sample names.
    #  -> input_file:   name of file from which the input strings were loaded from. This is only
    #                   needed to display an error message.
    # ********************************************************************************************

    # Define regexp for each supported version of inForm.
    regexp_inform22 = '\\_[[0-9]+,[0-9]+\\](_M[0-9])?\\.im3$'
    regexp_inform24 = '_Scan[0-9]+\\.qptiff$'


    # Case 1. Data format is inForm 2.2.
    inform22_pattern = grepl(pattern=regexp_inform22, x=input_vector, ignore.case=F)
    if(all(inform22_pattern)){
        return(sub(pattern=regexp_inform22, replacement='', x=input_vector))
    }
    # Case 2. Data format is inForm 2.4.
    inform24_pattern = grepl(pattern=regexp_inform24, x=input_vector, ignore.case=F)
    if(all(inform24_pattern)){
        return(sub(pattern=regexp_inform24, replacement='', x=input_vector))
    }

    # Case 3. Unsupported data format.
    if(all(inform22_pattern | inform24_pattern)){
        raise_error(msg='Mix of inForm 2.2. and 2.4 formats detected in [Sample Name] column.',
                    file=input_file)
    }
    if(sum(inform22_pattern) > 0){
        error_examples = input_vector[which(!inform22_pattern)]
    } else if(sum(inform24_pattern) > 0){
        error_examples = input_vector[which(!inform24_pattern)]
    } else{
        error_examples = input_vector
    }
    if(length(error_examples) > 10) error_examples = error_examples[1:10]
    raise_error(msg = 'Unsupported format detected in [Sample Name] column of input file:',
                file = input_file,
                items_to_list= error_examples)
}
####################################################################################################



####################################################################################################
sort_by_imageid_samplename_and_tissuecategory <- function(input_table){
    # ********************************************************************************************
    # Sort the input_table by its 'image_id' column values. Image ID values are composed of two
    # numbers separated by an '_', and represent coordinates of the image_id within the total
    # image file.
    # One problem is that sometimes the coordinates have 4 digits, and sometimes they have 5, but
    # using a simple sort on strings, '47430_12886' and '47430_8512' will be sorted in this order
    # (alphanumeric), when in fact it should be in the opposite order: "47430_8512", "47430_12886".
    # To solve that problem, the image ID strings are decomposed in individual coordinates, and
    # a sort based on the first, with a subsort based on the second is performed. Since these sorts
    # are now numeric and not alphabetical, things get put in the correct order.
    # ********************************************************************************************

    # Verify input data.
    stopifnot(is.data.frame(input_table))
    stopifnot('image_id' %in% colnames(input_table))
    stopifnot('sample_name' %in% colnames(input_table))
    stopifnot('tissue_category' %in% colnames(input_table))
    stopifnot(all(input_table[,'tissue_category'] %in% AUTHORIZED_TISSUES))

    # Split image ID values into their 2 components. Each image ID string must split in exactly
    # 2 values.
    tmp_list = strsplit(input_table$image_id, '_')
    stopifnot(all(sapply(tmp_list, length) == 2))

    # Retrieve individual coordinates. Make sure they are numeric values.
    coord1 = as.numeric(sapply(tmp_list, FUN=function(x) x[[1]]))
    coord2 = as.numeric(sapply(tmp_list, FUN=function(x) x[[2]]))
    stopifnot(all(!is.na(coord1)))
    stopifnot(all(!is.na(coord2)))

    # Attribute order value to tissue categories based on their oder in AUTHORIZED_TISSUES.
    tissue_category_order = as.numeric(sapply(input_table[,'tissue_category'],
                                              FUN=function(x) which(x == AUTHORIZED_TISSUES)))

    # Return the input table sorted by image ID values.
    input_table = input_table[order(input_table$sample_name,
                                    coord1, coord2, tissue_category_order, decreasing=F), ]
    row.names(input_table) = 1:nrow(input_table)
    return(input_table)
}
####################################################################################################