-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun_metaboprep_pipeline.R
1083 lines (913 loc) · 42.8 KB
/
run_metaboprep_pipeline.R
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
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
699
700
701
702
703
704
705
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########################################
## metaboprep pipeline
##
## by: Laura Corbin & David Hughes
## date: May 14th 2019
###########################################
######################################
## RUN DIRECTIONS:
## - run the script by running the line
## > Rscript run_metaboprep_pipeline.R paramater_file.txt
######################################
#######################################
##
## I) Load the metaboprep R package
##
#######################################
library(metaboprep)
## define nightingale object
# ng_anno = metaboprep:::ng_anno
#########################################
##
## II) Read in arguments from command line
##
#######################################
## You should pass a single argument
## that is the path to the paramater_file.txt
args = commandArgs(trailingOnly=TRUE)
## Check that you passed an argument to the script
if(length(args) != 1){
stop(
paste0("You must provide a single argument, the paramater file, when you call the script.\n\tEXAMPLE:$ Rscript --vanilla run_metaboprep_pipeline.R parameter_file.txt.\n"),
call.=FALSE)
}
## record todays date
today = Sys.Date()
today = gsub("-","_",today)
#######################################
##
## III) Parse the info in the paramater file
##
#######################################
## Read in the paramater file
pfile = read.table(args[1], header = FALSE, sep = "=", as.is = TRUE)
##################################
## (A) What is the project name?
##################################
project <- as.character( pfile[1,2] )
##################################
## (B) What is the full path to the directory containing the DATA?
##################################
data_dir <- as.character( pfile[2,2] )
## make sure data directory ends with a "/"
if(substring(data_dir, nchar(data_dir)) != "/"){
data_dir = paste0(data_dir,"/")
}
#######################################
##
## IV) Make a new sub directory
##
#######################################
## check for spaces in fle paths
dd = data_dir
dd = gsub(" ","\\\\ ", dd)
cmd = paste0("mkdir -p ", dd, "metaboprep_release_", today)
system(cmd)
#######################################
##
## V) Start writing a log file
##
#######################################
logfilename = paste0(data_dir, "metaboprep_release_", today, "/", project, "_", today, "_logfile.txt")
sink(file = logfilename , split = TRUE )
cat(paste0("I. Setting up your pipeline\n"))
cat(paste0("\t-Your data directory is: ", data_dir, "\n"))
#######################################
##
## III - CONT'd) Parse the info in the paramater file
##
#######################################
##################################
## (C) Name of metabolite data file
##################################
METABO_file2process = as.character( pfile[3,2] )
## identify the file type
xl_ftype = c(".xls",".xlsx", ".XLS", ".XLSX")
isexcel = sum(unlist( sapply(xl_ftype, function(x){ grep(x, METABO_file2process) } ) ) )
flat_ftype = c(".csv",".txt", ".tsv", ".TXT", ".CSV", ".TSV")
isflat = sum(unlist( sapply(flat_ftype, function(x){ grep(x, METABO_file2process) } ) ) )
cat(paste0("\t- Your metabolite data file is: ", METABO_file2process, "\n"))
if(isexcel > 0){
cat(paste0("\t\tYour metabolite data file was identified as an excel sheet\n\t\tand will be processed as a commercial source file.\n"))
} else {
if(isflat > 0){
cat(paste0("\t\tYour metabolite data file was identified as a previously processed flat text file.\n"))
} else {
stop( paste0("\t\tUnable to identify the type of file you provided.\n\t\tPlease be sure it is an xls, xlsx, txt, or csv.\n"), call.=FALSE )
}
}
##################################
## (D) Name of feature annotation file
## If you are reading in a commercial source EXCEL file
## can be NA. If reading in a pre-processed flat text file
## and data is from Metabolon, it would be best to provide a
## column called "SUPER_PATHWAY" to identify "Xenobiotics"
##################################
FeatureAnno_file2process = as.character( pfile[4,2] )
if( !is.na(FeatureAnno_file2process)){
cat(paste0("\t- Your provided feature annotation file to process is: ", FeatureAnno_file2process, "\n"))
} else{
cat(paste0("\t- You have NOT provided a feature annotation file to process.\n"))
}
##################################
## (E) Name of sample (batch) annotation file
## If you are reading in a commercial source EXCEL file
## can be NA.
##################################
SampleAnno_file2process = as.character( pfile[5,2] )
if( !is.na(SampleAnno_file2process)){
cat(paste0("\t- Your provided sample | batch annotation file to process is: ", SampleAnno_file2process, "\n"))
} else{
cat(paste0("\t- You have NOT provided a sample | batch annotation file to process.\n"))
}
##################################
## (F) What platform does your data come from?
##################################
platform = as.character( pfile[6,2] )
cat(paste0("\t- Your declared platform is: ", platform, "\n"))
##################################
## (G) QC values
##################################
feature_missingness = as.numeric( pfile[7,2] )
cat(paste0("\t- Feature filtering: Your declared feature missingness level is: ", feature_missingness, "\n"))
sample_missingness = as.numeric( pfile[8,2] )
cat(paste0("\t- Sample filtering: Your declared sample missingness level is: ", sample_missingness, "\n"))
total_peak_area_SD = as.numeric( pfile[9,2] )
cat(paste0("\t- Sample filtering: Your declared total peak area filter level, in standard deviations from the mean is: ", total_peak_area_SD, "\n"))
outlier_udist = as.numeric( pfile[10,2] )
cat(paste0("\t- Sample outliers at features: Your declared that the interquartile range unit distance from the median of each feature to call a sample an outlier to be: ", outlier_udist, "\n"))
outlier_treatment = as.character( pfile[11,2] )
cat(paste0("\t- Sample outliers at features: Your declared that outliers, for the purposes of the PCA & PCA only, should be: ", outlier_treatment, "\n"))
tree_cut_height = as.numeric( pfile[12,2] )
cat(paste0("\t- Metabolite independence: Your declared tree cut height is: ", tree_cut_height, "\n\n"))
PC_outlier_SD = as.numeric( pfile[13,2] )
cat(paste0("\t- Sample filtering: Your declared principal component (PC1 and PC2) exclusion, in standard deviations from the mean is: ", PC_outlier_SD, "\n"))
## Nightingale derived variable exclusions
## when evaluting SAMPLE quality for QC
derived_var_exclusion = pfile[14,2]
if(platform == "Nightingale"){
if(derived_var_exclusion == TRUE){
cat(paste0("\t- You have declared that Nightingale derived variables should be excluded from data filtering steps.\n\n"))
}
}
## Mass Spec Run Mode for each metabolite.
## This variable defines the column name, in the feature_annotation_file, indexing the run mode string(s).
## There should in turn be column name(s) in the sample_annotation_file that match the run mode string(s)
## and hold the batch IDs for each sample, in that run mode.
feat_anno_run_mode_col = as.character( pfile[15,2] )
if( !is.na(feat_anno_run_mode_col)){
cat(paste0("\t- You have declared that the column name in the feature annotation file holding the run mode variables is: ", feat_anno_run_mode_col, "\n"))
}
## Should a scatter plot, histogram, and table of summary statsitics be
## written to PDF for visual inspection? TRUE or FALSE?
plot_feature_distributions = pfile[16,2]
if(plot_feature_distributions==TRUE){
cat(paste0("\t- You have declared that a scatter plot, histogram, and table of summary statsitic for each feature in the data set should be written to pdf.\n"))
}
#######################################
##
## VI) Reading in the data
##
#######################################
##################################
## (A) process Metabolite data file
##################################
n = paste0(data_dir, METABO_file2process)
##
if(isflat > 0){
if( length(grep(".csv", n )) > 0 ){
cat(paste0("\t- Reading in your csv metabolite file\n"))
metabolitedata = read.csv(n, header = TRUE, as.is = TRUE, na.strings = c("NA","NDEF", "TAG", -1, -9), row.names = 1 )
}
####
if( length( c( grep(".txt", n ), grep(".tsv", n ) ) ) > 0 ){
cat(paste0("\t- Reading in your txt|tsv metabolite file\n"))
metabolitedata = read.table(n, header = TRUE, as.is = TRUE, sep = "\t", na.strings = c("NA","NDEF", "TAG", -1, -9), row.names = 1 )
}
## remove any possible commas
metabolitedata = apply(metabolitedata, 2, function(x){
o = sapply(x, function(y){
gsub(",","",y)
})
return(o)
})
metabolitedata = as.data.frame(metabolitedata)
## format metabolite data:
## look to see of row names are just numerics. if yes redefine rownames and column 1 values
editrownames = sum( rownames(metabolitedata) == 1:nrow(metabolitedata) ) /nrow(metabolitedata)
if(editrownames == 1){
cat(paste0("\t\t- Assuming sample IDs are in column 1 and redefining rownames\n"))
rownames(metabolitedata) = as.character(metabolitedata[,1])
metabolitedata = metabolitedata[,-1]
}
## look to see of column names are just numerics. R will add X to numberic column names
editcolnames = sum( substring(colnames(metabolitedata) ,1,1) == "X", na.rm = TRUE ) / ncol(metabolitedata)
if(editcolnames == 1){
cat(paste0("\t\t- Column names are numeric. Adding 'featID_' prefix to each.\n") )
numeric_ids = substring(colnames(metabolitedata) , 2, nchar(colnames(metabolitedata)) )
new_col_id = paste0("featID_", as.character(numeric_ids))
colnames(metabolitedata) = new_col_id
}
## if platform is Nightingale edit metabolite names
if( platform == "Nightingale"){
cat(paste0("\t\t- Your defined platform is Nightingale,\n\t\t so editing metabolite names in an attempt to match the metaboprep annotation file.\n"))
## edit column metabolite names
colnames(metabolitedata) = gsub("_.", "pct", colnames(metabolitedata))
colnames(metabolitedata) = gsub("%", "pct", colnames(metabolitedata))
colnames(metabolitedata) = gsub("by", "", colnames(metabolitedata))
colnames(metabolitedata) = gsub("/", "_", colnames(metabolitedata))
colnames(metabolitedata) = gsub("\\.", "", colnames(metabolitedata))
colnames(metabolitedata) = gsub("-", "", colnames(metabolitedata))
colnames(metabolitedata) = gsub("_", "", colnames(metabolitedata))
colnames(metabolitedata) = tolower(colnames(metabolitedata))
}
### insure everything is numeric
ids = rownames(metabolitedata)
metabolitedata = apply(metabolitedata, 2, function(x){ as.numeric(as.character(x)) })
rownames(metabolitedata) = ids
### END OF "isflat" if statement
}
##################################
## (B) Checking for and process a
## flat text Feature Annotation File
##################################
if( !is.na(FeatureAnno_file2process) ){
## full path to feature annotation file
n = paste0(data_dir, FeatureAnno_file2process)
##
if( length(grep(".csv", n )) > 0 ){
cat(paste0("\t- Reading in you csv feature annotation file\n"))
featuredata = read.csv(n, header = TRUE, as.is = TRUE, quote = "", fill = TRUE)
# featuredata = readr::read_delim(n, show_col_types = FALSE)
}
####
if( length( c( grep(".txt", n ), grep(".tsv", n ) ) ) > 0 ){
cat(paste0("\t- Reading in you txt feature annotation file\n"))
#featuredata = read.table(n, header = TRUE, as.is = TRUE, sep = "\t", quote = "", fill = TRUE)
featuredata = readr::read_delim(n, show_col_types = FALSE)
featuredata = as.data.frame(featuredata)
}
## format featuredata data
editrownames = sum( rownames(featuredata) == 1:nrow(featuredata) ) /nrow(featuredata)
if(editrownames == 1){
## redefine only if the number of unique strings is the same as the number of rows
if( length(unique(featuredata[,1])) == nrow(featuredata) ){
cat(paste0("\t\t- Assuming feature IDs are in column 1 and redefining rownames\n"))
## looking to see if column names of metabolite data file were also numeric. If yes then "X" was added by R.
## this was removed and "featID_" was added as a prefix. So we have to do the same here for name matching
## during filtering steps later.
if(editcolnames == 1){
cat(paste0("\t\t- Column were numeric so will also add 'featID_' as a prefix to each row names here.\n") )
rownames(featuredata) = paste0( "featID_", as.character( featuredata[,1] ) )
} else {
rownames(featuredata) = as.character(featuredata[,1])
#featuredata = featuredata[,-1]
}
}
}
## Make sure there is a "feature_names" and that it has same values as rownames
featuredata$feature_names = rownames(featuredata)
##
if( platform == "Nightingale"){
cat(paste0("\t\t- Your defined platform is Nightingale,\n\t\t so editing metabolite names in an attempt to match the metaboprep annotation file.\n"))
## edit column metabolite names
rownames(featuredata) = gsub("_.", "pct", rownames(metabolitedata))
rownames(featuredata) = gsub("%", "pct", rownames(featuredata))
rownames(featuredata) = gsub("/", "_", rownames(featuredata))
rownames(featuredata) = gsub("\\.", "", rownames(featuredata))
rownames(featuredata) = gsub("-", "", rownames(featuredata))
rownames(featuredata) = gsub("_", "", rownames(featuredata))
rownames(featuredata) = tolower(rownames(featuredata))
}
## Verify that the first column of feature names matches the columnnames in metabolite data
idsmatch = sum(rownames(featuredata) %in% colnames(metabolitedata)) == nrow(featuredata)
if(idsmatch == FALSE){
stop(
paste0("The feature or metabolite IDs in the first column of your feature annotation file
\tdo not match the feature (column) ids in the metabolite data file.
\tPlease insure that these ids match and come back and try again."),
call.=FALSE)
}
## If the IDs do match lets make sure they are ordered properly
if(idsmatch == TRUE){
m = match( colnames(metabolitedata), rownames(featuredata))
featuredata = featuredata[m, ]
}
}
##################################
## (C) Checking for and process a
## flat text Sample Annotation File
##################################
if( !is.na(SampleAnno_file2process) ){
## full path to feature annotation file
n = paste0(data_dir, SampleAnno_file2process)
##
if( length(grep(".csv", n )) > 0 ){
cat(paste0("\t- Reading in you csv sample annotation file\n"))
# sampledata = read.csv(n, header = TRUE, quote = "", as.is = TRUE)
sampledata = read.csv(n, header = TRUE, quote = "", as.is = TRUE)
}
####
if(length( c( grep(".txt", n ), grep(".tsv", n ) ) ) > 0 ){
cat(paste0("\t- Reading in you txt sample annotation file\n"))
# sampledata = read.table(n, header = TRUE, quote = "", as.is = TRUE, sep = "\t")
sampledata = read.table(n, header = TRUE, as.is = TRUE, sep = "\t")
}
## format sampledata data
editrownames = sum( rownames(sampledata) == 1:nrow(sampledata) ) /nrow(sampledata)
if(editrownames == 1){
cat(paste0("\t\t- Assuming sample IDs are in column 1 and redefining rownames\n"))
rownames(sampledata) = as.character(sampledata[,1])
#sampledata = sampledata[,-1]
}
## Verify that the first column of feature names matches the columnnames in metabolite data
idsmatch = sum( rownames(sampledata) %in% rownames(metabolitedata) ) == nrow(sampledata)
if(idsmatch == FALSE){
stop(
paste0("The sample IDs in the first column of your sample annotation file
\tdo not match the sample (row) ids in the metabolite data file.
\tPlease insure that these ids match and come back and try again."),
call.=FALSE)
}
## If the IDs do match lets make sure they are ordered properly
if(idsmatch == TRUE){
m = match( rownames(metabolitedata), rownames(sampledata))
sampledata = sampledata[m, ]
}
}
##################################
## (D) Generate a WORKING data set
## for flat text source files
##################################
if(isflat > 0){
if( !exists( x = "sampledata" ) ){
sampledata = data.frame(SampleID = rownames(metabolitedata))
}
####
if( !exists( x = "featuredata" ) ){
featuredata = data.frame(feature_names = colnames(metabolitedata))
rownames(featuredata) = as.character( featuredata[,1] )
}
## add Nightingale feature annotation data
if(platform == "Nightingale"){
m = match( rownames(featuredata), ng_anno$metabolite)
featuredata = cbind(featuredata, ng_anno[m, -1])
## Look any id that I could not match
w = which(is.na(m))
if(length(w)>0){
ids_i_could_not_match = rownames(featuredata)[w]
## make the NAs "unknown"
featuredata[w, c("feature_names", "raw.label", "class", "subclass", "label", "label.no.units","derived_features")] = "unknown"
## STOP as ERROR
cat( paste0("\n\n
\t- !!!! WARNING !!!!
\t\tPlease be aware that there is|are ", length(w) ," metabolite IDs in your Nightingale data set that metaboprep could not annotate.
\t\tAt present Nightingale Health data annotation is performed with the metaboprep data frame ng_anno.
\t\tYou can see it by opening an R session and typing
\n\t\t\t> metaboprep::ng_anno
\n\t\t\tThe annotation is necessary for proper treatment of derived features in the Nightingale data sets
\t\t(i.e. ratios or features derived from two or more features already present in the data).
\t\tThis error may be the product of a new spelling Nightingale Health has chosen or the addition of a new feature.
\t\tThis warning is ONLY an issue IF the new or unmapped metabolite is a derived feature and you are excluding derived features
\t\tfrom the indpendent feature identification step, that also feeds into the PCA.
\t\tIf it is spelling mismatch you can edit your files and try again.
\t\tIf it is a new feature, we are aware of this issue, and are working on a solution.\n") )
cat( paste0("\n\t\t- The features metaboprep could not match are:\n") )
cat( paste0(ids_i_could_not_match, "\n\n\n------\n") )
}
}
###
mydata = list(metabolitedata = metabolitedata, sampledata = sampledata, featuredata = featuredata)
}
##################################
## (E) IF Data file is Excel
##################################
if(isexcel > 0){
#############################
### Process if Nightingale
#############################
if(platform == "Nightingale"){
cat( paste0("II. Processing your Nightingale data.\n") )
if( !is.na(pfile[3,2]) ){
## Read in the raw data, excel files, write to flat text in the data directory and return all data as a list
mydata = read.in.nightingale( file2process = METABO_file2process, data_dir = data_dir, projectname = project )
cat( paste0("\t- Your raw Nightingale data has been read in and converted to working tab delimited text files.\n\n") )
}
}
#############################
### Process if Metabolon
#############################
if(platform == "Metabolon"){
cat( paste0("II. Processing your Metabolon data.\n") )
if( !is.na(pfile[3,2]) ){
## Read in the raw data, excel files, write to flat text in the data directory and return all data as a list
mydata = read.in.metabolon( file2process = METABO_file2process, data_dir = data_dir, projectname = project )
cat( paste0("\t- Your raw Metabolon data has been read in and converted to working tab delimited text files.\n\n") )
}
}
}
## READING IN DATA DONE
cat( paste0("III. Your data has been read in.\n\n") )
cat( paste0("\t-Your data has ", nrow(mydata$metabolitedata), " individuals and ", ncol(mydata$metabolitedata), " metabolites.\n\n") )
cat( paste0("\t-There are also ", ncol(mydata$sampledata), " sample annotation|batch variables.\n\n") )
cat( paste0("\t-There are also ", ncol(mydata$featuredata), " feature annotation|batch variables.\n\n") )
if(length(mydata)>3){
for(i in 4:length(mydata)){
cat( paste0("\t-Your data also has an additional metabolite data tab named ", names(mydata)[i] ," with ", nrow(mydata[[i]]), " individuals and ", ncol(mydata[[i]]), " metabolites.\n\n") )
}
}
#########################
##
## (VII) Normalize Metabolon or Other (MS) Data
##
#########################
if(platform == "Metabolon"){
cat( paste0("Normalization. Performing normalization on Metabolon Data.\n\n") )
if(!is.na(feat_anno_run_mode_col)){
cat( paste0("\t- Performing normalization with parameter file provided feature annotation column '",feat_anno_run_mode_col,"'.\n") )
norm_metabolite_data = batch_normalization( wdata = mydata$metabolitedata,
feature_data_sheet = mydata$featuredata,
sample_data_sheet = mydata$sampledata,
feature_runmode_col = feat_anno_run_mode_col,
batch_ids = NULL )
## save the raw data as another object in the list
mydata$raw_metabolitedata = mydata$metabolitedata
## redefine the working metabolitedata object
mydata$metabolitedata = norm_metabolite_data
## remove the unnecessary data frame
rm(norm_metabolite_data)
cat( paste0("\t- Normalization completed.\n\n") )
} else {
##################################################
## look for run mode information in feature data
##################################################
cat( paste0("\t- Looking for run mode information automatically given Metabolon standard data release formatting.\n") )
fanno_col_number = which(colnames(mydata$featuredata) %in% c("PLATFORM","platform"))
if(length(fanno_col_number) == 1){
runmode = unlist( mydata$featuredata[,fanno_col_number] )
## remove "LC/MS " if present
runmode = gsub("LC\\/MS\\ ","",runmode)
## remove spaces
runmode = gsub(" ","",runmode)
## make lower case
runmode = tolower(runmode)
## redefine runmode string in feature data file
mydata$featuredata[,fanno_col_number] = runmode
batchrunmodes = unique(runmode)
} else {
cat(paste0("\t- NOTE: Unable to identify a column header called
'PLATFORM' or 'platform' in the feature data sheet.
This is necessary to perform batch normalization\n\n") )
}
##################################################
## look for batch info in the sample sheet
##################################################
n = tolower( colnames(mydata$sampledata) )
## remove spaces
n = gsub(" ","",n)
## remove underscore
n = gsub("_","",n)
## remove dots
n = gsub("\\.","",n)
## redefine column names
k = which(n %in% runmode)
colnames(mydata$sampledata)[k] = n[k]
if(length(k) == length(batchrunmodes) ){
## perfom normalization
cat( paste0("\t- Performing normalization with automatically identified Metabolon standard data release formatting column '",colnames(mydata$featuredata)[fanno_col_number],"'.\n") )
norm_metabolite_data = batch_normalization( wdata = mydata$metabolitedata,
feature_data_sheet = mydata$featuredata,
sample_data_sheet = mydata$sampledata,
feature_runmode_col = fanno_col_number,
batch_ids = NULL )
## save the raw data as another object in the list
mydata$raw_metabolitedata = mydata$metabolitedata
## redefine the working metabolitedata object
mydata$metabolitedata = norm_metabolite_data
## remove the unnecessary data frame
rm(norm_metabolite_data)
cat( paste0("\t- Normalization completed.\n\n") )
} else {
cat(paste0("\t- NOTE: Unable to identify a column headers in sample sheet
that match the platform run modes found in the feature data sheet.
This should be something like neg, polar, pos early, pos late.\n") )
cat( paste0("\t- NOTE: We will take the ScaledImpData data and remove
the imputed data to extract the normalized data.\n\n") )
scaled_imputed_data_tab = grep("ScaledImp", names(mydata))
if(length(scaled_imputed_data_tab) == 1){
ndata = mydata[[scaled_imputed_data_tab]]
if(sum(is.na(mydata$metabolitedata))>0){
ndata[is.na(mydata$metabolitedata)] = NA
}
## save the raw data as another object in the list
mydata[["raw_metabolitedata"]] = mydata$metabolitedata
## redefine the working metabolitedata object
mydata$metabolitedata = ndata
## remove the unnecessary data frame
rm(ndata)
cat( paste0("\t- Normalization completed.\n\n") )
} else {
cat( paste0("\t- NOTE: Unable to identify a 'ScaledImp' data tab in the excel file.
No normalization carried out.\n\n") )
}
}
}
}
#########################
##
## (VII) Normalize Other MS Data
##
#########################
if( !is.na(feat_anno_run_mode_col) & platform == "Other" ){
cat( paste0("Normalization. Performing normalization parameter file provided feature annotation column '",feat_anno_run_mode_col,"'.\n") )
norm_metabolite_data = batch_normalization( wdata = mydata$metabolitedata,
feature_data_sheet = mydata$featuredata,
sample_data_sheet = mydata$sampledata,
feature_runmode_col = feat_anno_run_mode_col,
batch_ids = NULL )
## save the raw data as another object in the list
mydata$raw_metabolitedata = mydata$metabolitedata
## redefine the working metabolitedata object
mydata$metabolitedata = norm_metabolite_data
## remove the unnecessary data frame
rm(norm_metabolite_data)
cat( paste0(" - Normalization completed.\n\n") )
}
#########################
##
## (VII) Estimate Summary Statistics
##
#########################
cat( paste0("IV. Estimating Summary Statistics On Raw Data Set.\n") )
##################################
## A. Summary Statistics for samples
##################################
cat( paste0("\ta. Estimating summary statistics for samples\n") )
## Is this Metabolon data??
## -- is the column SUPER_PATHWAY present in the feature data
## -- if yes, exclude Xenobiotics from one of the missingness estimate
if( length(mydata$featuredata$SUPER_PATHWAY) > 0){
w = which( mydata$featuredata$SUPER_PATHWAY %in% c("xenobiotics", "Xenobiotics") )
xeno_names = mydata$featuredata$feature_names[w]
samplesumstats = sample.sum.stats( wdata = mydata$metabolitedata, feature_names_2_exclude = xeno_names, outlier_udist = outlier_udist )
} else {
## Is this Nightingale data??
## -- is the column derived_features present in the feature data
## -- if yes, exclude derived variables from one of the missingness estimate
if( length(mydata$featuredata$derived_features) > 0 & derived_var_exclusion == "TRUE" ){
w = which( mydata$featuredata$derived_features == "yes")
derivedfeature_names = as.character( mydata$featuredata$feature_names[w] )
samplesumstats = sample.sum.stats( wdata = mydata$metabolitedata, feature_names_2_exclude = derivedfeature_names, outlier_udist = outlier_udist )
} else {
samplesumstats = sample.sum.stats( wdata = mydata$metabolitedata, outlier_udist = outlier_udist)
}
}
### write sample sum stats to file
cat( paste0("\t\t- Writing sample summary statistics to file.\n") )
## make a sum stats directory in data_dir
## evaluate and account for spaces in file paths
dd = data_dir
dd = gsub(" ","\\\\ ", dd)
###
cmd = paste0("mkdir -p ", dd, "metaboprep_release_", today, "/", "sumstats")
system(cmd)
## make a raw_dataset directory inside the sumstats folder
cmd = paste0("mkdir -p ", dd, "metaboprep_release_", today, "/", "sumstats/raw_dataset")
system(cmd)
### SAMPLES
if( "sampledata" %in% names(mydata) ){
# mydata$sampledata = cbind(mydata$sampledata, samplesumstats)
samplesumstats = cbind(mydata$sampledata, samplesumstats[,-1])
}
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/raw_dataset/", project, "_", today, "_sample_anno_sumstats.txt")
# write.table(mydata$sampledata, file = n,
write.table( samplesumstats, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = FALSE)
##################################
## B. Summary Statistics for features
##################################
cat( paste0("\tb. Estimating summary statistics for features.\n") )
### sample missingness
if( length(samplesumstats$sample_missingness_w_exclusions) > 0 ){
sammis = samplesumstats$sample_missingness_w_exclusions
} else {
sammis = samplesumstats$sample_missingness
}
### feature names to exclude
if( length(mydata$featuredata$derived_features) > 0 & derived_var_exclusion == "TRUE" ){
w = which( mydata$featuredata$derived_features == "yes")
fn2e = as.character( mydata$featuredata$feature_names[w] )
} else {
fn2e = NA
}
### RUN feature summary stats funtion
featuresumstats = feature.sum.stats( wdata = mydata$metabolitedata,
sammis = sammis,
tree_cut_height = tree_cut_height,
outlier_udist = outlier_udist,
feature_names_2_exclude = fn2e )
### write feature sum stats to file
cat( paste0("\t\t- Writing feature summary statistics to file.\n") )
if( "featuredata" %in% names(mydata) ){
featuresumstats$table = cbind( mydata$featuredata, featuresumstats$table[,-1])
}
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/raw_dataset/", project, "_", today, "_feature_anno_sumstats.txt")
write.table(featuresumstats$table, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = TRUE)
##################################
## C. PC outliers
##################################
cat( paste0("\tc. Performing principle component analysis and identifying outliers.\n") )
## identify independent feature names as reported in featuresumstats
w = which(featuresumstats$table$independent_features_binary == 1)
indf = as.character( featuresumstats$table[w,1] )
if( sum( indf %in% colnames(mydata$metabolitedata) ) == 0 ){
indf = as.character( featuresumstats$table$feature_names[w] )
}
PCs_outliers = pc.and.outliers(metabolitedata = mydata$metabolitedata,
indfeature_names = indf)
### write sample sum stats to file
cat( paste0("\t\t- Re-Writing sample summary statistics to file to include PCs.\n") )
### SAMPLES
samplesumstats = cbind( samplesumstats, PCs_outliers[[1]])
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/raw_dataset/", project, "_", today, "_sample_anno_sumstats.txt")
write.table( samplesumstats, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = FALSE)
### write the variance explained by pcs out to file
cat( paste0("\t\t- Writing PC statistics to file.\n\n") )
varexp = data.frame(VarExp = PCs_outliers[[2]])
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/raw_dataset/", project, "_", today, "_pc_varexp.txt")
write.table(varexp, file = n,
row.names = TRUE, col.names = TRUE,
sep = "\t", quote = FALSE)
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/raw_dataset/", project, "_", today, "_featuretree.Rdata")
feature_tree = featuresumstats[[2]]
save(feature_tree, file = n)
#############################
##
## Make a raw data set object
##
#############################
raw_data = list(metabolite_data = mydata$metabolitedata,
sample_data = samplesumstats,
feature_data = featuresumstats$table,
feature_tree = feature_tree,
varexp = varexp
)
#########################
##
## (VIII) Perform QC
##
#########################
##################################
## A. Perform QC
##################################
cat( paste0("V. Performing data filtering.\n") )
dd = data_dir
dd = gsub(" ","\\\\ ", dd)
##
cmd = paste0("mkdir -p ", dd, "metaboprep_release_", today, "/", "filtered_data")
system(cmd)
### xenobiotics to exclude
w = which( mydata$featuredata$SUPER_PATHWAY %in% c("xenobiotics", "Xenobiotics") )
xeno_names = mydata$featuredata$feature_names[w]
if( length(xeno_names) == 0){xeno_names = NA}
### derived variables to exclude
w = which( mydata$featuredata$derived_features == "yes" )
derived_names = as.character( mydata$featuredata$feature_names[w] )
if(length(derived_names) == 0){derived_names = NA}
if(derived_var_exclusion != "TRUE"){derived_names = NA}
### execute super function
cat( paste0("\ta. Performing data filtering.\n") )
dataQC = perform.metabolite.qc(wdata = mydata$metabolitedata,
fmis = feature_missingness,
smis = sample_missingness,
tpa_out_SD = total_peak_area_SD,
outlier_treatment = outlier_treatment, ## options are "leave_be", "turn_NA", "winsorize"
winsorize_quantile = 1, ## winsorize to what quantile of remaining (not outlier) values
outlier_udist = outlier_udist,
PC_out_SD = PC_outlier_SD,
tree_cut_height = tree_cut_height,
feature_colnames_2_exclude = xeno_names,
derived_colnames_2_exclude = derived_names
)
#################################
## B. write QC data to file
#################################
cat( paste0("\tb. Writing QC data to file.\n\n") )
#############################
##
## B.1. Make a QCing data set object
##
#############################
qcing_data = list(metabolite_data = dataQC$wdata,
exclusion_data = dataQC$exclusion_data,
feature_sumstats = dataQC$featuresumstats$table,
feature_tree = dataQC$featuresumstats$tree,
pcs = dataQC$pca$pcs,
varexp = dataQC$pca$varexp,
accelerationfactor = dataQC$pca$accelerationfactor,
nparallel = dataQC$pca$nsig_parrallel
)
##################################
## B.2. Add sample and feature data to qcdata
##################################
temp = dataQC$wdata
m = match( rownames(temp) , mydata$sampledata[,1] )
n = match( colnames(temp) , mydata$featuredata[,1] )
if( length(n) == sum(is.na(n)) ){
n = match( colnames(temp) , mydata$featuredata$feature_names )
}
qcdata = list(metabolitedata = temp,
sampledata = as.data.frame( mydata$sampledata[m,] ),
featuredata = as.data.frame( mydata$featuredata[n,] ) )
rm(temp)
if( colnames(qcdata$sampledata)[1] == "mydata$sampledata[m, ]" ){ colnames(qcdata$sampledata)[1] = "SampleID" }
if( colnames(qcdata$featuredata)[1] == "mydata$featuredata[n, ]" ){ colnames(qcdata$featuredata)[1] = "feature_names" }
##################################
## B.3. Write to file
##################################
## qc metabolite data
n = paste0(data_dir, "metaboprep_release_", today, "/filtered_data/", project, "_", today, "_Filtered_metabolite_data.txt")
write.table(qcdata$metabolitedata, file = n,
row.names = TRUE, col.names = TRUE,
sep = "\t", quote = FALSE)
## qc sample data
n = paste0(data_dir, "metaboprep_release_", today, "/filtered_data/", project, "_", today, "_Filtered_sample_data.txt")
write.table(qcdata$sampledata, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = FALSE)
## qc metabolite data
n = paste0(data_dir, "metaboprep_release_", today, "/filtered_data/", project, "_", today, "_Filtered_feature_data.txt")
write.table(qcdata$featuredata, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = FALSE)
#####################################
##
## (IX) Estimate Summary Statistics
## on the QC'd Data Set
##
#####################################
cat( paste0("VI. Estimating Summary Statistics on Filtered Data Set.\n") )
##################################
## A. Summary Statistics for samples
##################################
cat( paste0("\ta. Estimating summary statistics for Filtered samples\n") )
## A.1. Estiamte sum stats
## Is this metabolon data??
## -- is the column SUPER_PATHWAY present in the feature data
## -- if yes, exclude Xenobiotics from one of the missingness estimate
if( length(qcdata$featuredata$SUPER_PATHWAY) > 0){
w = which( qcdata$featuredata$SUPER_PATHWAY %in% c("xenobiotics", "Xenobiotics") )
xeno_names = rownames(qcdata$featuredata)[w]
samplesumstats = sample.sum.stats( wdata = qcdata$metabolitedata, feature_names_2_exclude = xeno_names, outlier_udist = outlier_udist )
} else {
## Is this Nightingale data??
## -- is the column derived_features present in the feature data
## -- if yes, exclude derived variables from one of the missingness estimate
if( length(qcdata$featuredata$derived_features) > 0 & derived_var_exclusion == "TRUE" ){
w = which( qcdata$featuredata$derived_features == "yes")
derivedfeature_names = as.character( qcdata$featuredata$feature_names[w] )
samplesumstats = sample.sum.stats( wdata = qcdata$metabolitedata, feature_names_2_exclude = derivedfeature_names, outlier_udist = outlier_udist )
} else {
samplesumstats = sample.sum.stats( wdata = qcdata$metabolitedata, outlier_udist = outlier_udist)
}
}
### A.2. Write sample sum stats to file
cat( paste0("\tb. Writing filtered sample summary statistics to file.\n") )
## make a filtered_dataset directory inside the sumstats directory in data_dir
## evaluate and account for spaces in file paths
dd = data_dir
dd = gsub(" ","\\\\ ", dd)
## system command
cmd = paste0("mkdir -p ", dd, "metaboprep_release_", today, "/", "sumstats/filtered_dataset")
system(cmd)
### WRITE
if( "sampledata" %in% names(qcdata) ){
## add sample stats to the sample annotation file
samplesumstats = cbind(qcdata$sampledata, samplesumstats[,-1])
}
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/filtered_dataset/", project, "_", today, "_sample_anno_sumstats.txt")
write.table(samplesumstats, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = FALSE)
##################################
## B. Summary Statistics for features
##################################
cat( paste0("\tc. Estimating summary statistics for filtered features.\n") )
### sample missingness
if( length(samplesumstats$sample_missingness_w_exclusions) > 0 ){
sammis = samplesumstats$sample_missingness_w_exclusions
} else {
sammis = samplesumstats$sample_missingness
}
### feature names to exclude
if( length(qcdata$featuredata$derived_features) > 0 & derived_var_exclusion == "TRUE" ){
w = which( qcdata$featuredata$derived_features == "yes")
fn2e = as.character( qcdata$featuredata$feature_names[w] )
} else {
fn2e = NA
}
### RUN feature summary stats funtion
featuresumstats = feature.sum.stats( wdata = qcdata$metabolitedata,
sammis = sammis,
tree_cut_height = tree_cut_height,
outlier_udist = outlier_udist,
feature_names_2_exclude = fn2e )
## count of independent features
icount = sum(featuresumstats$table$independent_features_binary)
cat(paste0("\t\t\t- A total of ", icount ," independent features were identified in the total filtered data set.\n"))
### write feature sum stats to file
cat( paste0("\td. Writing feature summary statistics to file.\n") )
if( "featuredata" %in% names(qcdata) ){
## add feature stats to the feature annotation file
# featuresumstats$table = cbind(featuresumstats$table[, 1], qcdata$featuredata, featuresumstats$table[, -1])
featuresumstats$table = cbind( qcdata$featuredata, featuresumstats$table[, -1] )
}
n = paste0(data_dir, "metaboprep_release_", today, "/sumstats/filtered_dataset/", project, "_", today, "_feature_anno_sumstats.txt")
write.table(featuresumstats$table, file = n,
row.names = FALSE, col.names = TRUE,
sep = "\t", quote = TRUE)
##################################
## C. Generation of PCs
##################################
cat( paste0("\te. Performing principle component analysis on final filtered data set.\n") )
## identify independent feature names as reported in featuresumstats
w = which(featuresumstats$table$independent_features_binary == 1)
indf = featuresumstats$table[w,1]
if( sum( indf %in% colnames(mydata$metabolitedata) ) == 0 ){
indf = as.character( featuresumstats$table$feature_names[w] )
}
PCs_outliers = pc.and.outliers(metabolitedata = qcdata$metabolitedata,
indfeature_names = indf)
cat( paste0("\t\t The number of informative principle components:\n") )