-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep_TEDDY_for_VOE.Rmd
6942 lines (5211 loc) · 355 KB
/
prep_TEDDY_for_VOE.Rmd
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
---
title: "process_TEDDY_for_voe"
author: "Sam Zimmerman"
date: "3/16/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{bash}
#Analysis steps post alignment with bowtie2
#Step 1. Normalize genes in each sample by total number of aligned reads in a sample
# location_file is a file where each line is the name of a gz file containing the number of reads that align to each gene. one line/file per sample.
cd /n/scratch3/users/a/adk9/_RESTORE/adk9/TEDDY/alignment_output
normalize_teddy.py <location_file>
# e.g.
#normalize_teddy.py SRR7559403_alignment_data.tsv.gz
```
#Step 2. extract gene names using quick python
```{python}
cd /n/scratch3/users/a/adk9/_RESTORE/adk9/TEDDY/alignment_output
data = pd.read_csv("SRR7556756_alignment_data.tsv",sep='\t',index_col=0,header=None)
geneNames=data.index
geneNames_df = pd.DataFrame(geneNames.values,columns=['genename'])
geneNames_df.to_csv('gene-names_alignment_data_normalized.tsv',index=False)
```
#Step 3. divide each normalized abundnce file into several files of 50000 genes per file. will create new folders with file in them in current working directory
```{bash}
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/normalized_alignments
#./parse_normalized_abundance_data.sh
./parse_normalized_abundance_data_other_path.sh
```
#Step 4. also split gene names
```{bash}
mkdir gene-names_batched
split -l 50000 /n/scratch3/users/a/adk9/TEDDY/alignment_output/normalized_data/gene-names_alignment_data_normalized.tsv gene-names_batched/gene_names_
```
#Step 5. merge normalized data. so we have Run by gene matrices.
```{bash}
mkdir parsed_data
mkdir merge_noramized_data_input_files
ls *_locs | grep -v "all_batch_locs" | grep -v "gene_name_locs" > merge_noramized_data_input_files/all_locs
cd merge_noramized_data_input_files
split -l 5 all_locs all_locs_
cd ..
for x in merge_noramized_data_input_files/all_locs_*
do
sbatch -c 1 -t 0-11:59 -p short --mem=20G merge_normalized_data_batch.bash ${x}
done
# rerun one that timed out
sbatch -c 1 -t 0-11:59 -p short --mem=20G merge_normalized_data_batch.bash merge_noramized_data_input_files/all_locs_iv_only
sbatch -c 1 -t 0-11:59 -p short --mem=20G merge_normalized_data_batch.bash merge_noramized_data_input_files/all_locs_mc_only
```
# step 6 and 7 run code in process_teddy_metadatav2.R so I get the case and controls as well as mapping files of subjects to samples. Located in metadata folder of github.
#step 8. average samples that belong to the same subject
#first prepare input files. uses output of process_teddy_metadatav2.R
```{r}
# first get input files
all_files = list.files(pattern=".csv")
abundance_files = all_files[-grep("healthy",all_files)]
mapping_files = all_files[grepl("healthy",all_files) & grepl(".mapping.",all_files)]
metadata_files = gsub(".mapping.csv",".csv",mapping_files)
mapping_metadata = data.frame(metadata_files,mapping_files)
df_lists = apply(mapping_metadata,1, function(myrow) {
metadata_file = myrow[1]
mapping_file = myrow[2]
metadata_files_rep = rep(metadata_file,length(abundance_files))
mapping_files_rep = rep(mapping_file,length(abundance_files))
my_df = data.frame(abundance_files,metadata_files_rep,mapping_files_rep)
# split into 2 pieces
split_point = floor(nrow(my_df)/2)
my_df1 = my_df[1:split_point,]
my_df2 = my_df[(split_point +1):nrow(my_df),]
# this extra column is just so I don't mess with the metadata since I am doing things in parallel
my_df1$metadata_suffix = 1
my_df2$metadata_suffix = 2
my_label1 = gsub(".csv","_input_file_1.tsv",metadata_file)
my_label2 = gsub(".csv","_input_file_2.tsv",metadata_file)
write.table(my_df1,file=paste("prep_voe_input_files/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
write.table(my_df2,file=paste("prep_voe_input_files/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
return(my_df)
})
```
#Prepare input files for other associations besides pre-t1d
```{r}
# # first get input files
# all_files = list.files(pattern=".csv")
# abundance_files = all_files[-grep("healthy",all_files)]
# mapping_files = all_files[grepl("healthy",all_files) & grepl(".mapping.",all_files)]
# mapping_files = mapping_files[-grep("healthy_pre-t1d",mapping_files)]
# metadata_files = gsub(".mapping.csv",".csv",mapping_files)
# mapping_metadata = data.frame(metadata_files,mapping_files)
#
# df_lists = apply(mapping_metadata,1, function(myrow) {
# metadata_file = myrow[1]
# mapping_file = myrow[2]
# metadata_files_rep = rep(metadata_file,length(abundance_files))
# mapping_files_rep = rep(mapping_file,length(abundance_files))
# my_df = data.frame(abundance_files,metadata_files_rep,mapping_files_rep)
# # split into 2 pieces
# split_point = floor(nrow(my_df)/2)
# my_df1 = my_df[1:split_point,]
# my_df2 = my_df[(split_point +1):nrow(my_df),]
# # this extra column is just so I don't mess with the metadata since I am doing things in parallel
# my_df1$metadata_suffix = 1
# my_df2$metadata_suffix = 2
# my_label1 = gsub(".csv","_input_file_1.tsv",metadata_file)
# my_label2 = gsub(".csv","_input_file_2.tsv",metadata_file)
# write.table(my_df1,file=paste("prep_voe_input_files_antibody_associations/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# write.table(my_df2,file=paste("prep_voe_input_files_antibody_associations/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# return(my_df)
# })
```
#Also we are doing another version where I only do associations on a training set. So lets get the input for those
```{r}
# setwd("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_2")
# # first get input files
#
# abundance_files = list.files("/n/scratch3/users/s/sez10/_RESTORE/TEDDY/parsed_data",pattern=".csv",full.names = TRUE)
# abundance_files = abundance_files[-grep("healthy",abundance_files)]
# mapping_files = list.files(pattern = ".mapping.csv")
# metadata_files = gsub(".mapping.csv",".csv",mapping_files)
# training_data_files = gsub(".mapping.csv",".train_subjects.txt",mapping_files)
# testing_data_files = gsub(".mapping.csv",".test_subjects.txt",mapping_files)
# mapping_metadata = data.frame(metadata_files,mapping_files,training_data_files,testing_data_files)
#
# df_lists = apply(mapping_metadata,1, function(myrow) {
# metadata_file = myrow[1]
# mapping_file = myrow[2]
# training_file = myrow[3]
# testing_file = myrow[4]
# metadata_files_rep = rep(metadata_file,length(abundance_files))
# mapping_files_rep = rep(mapping_file,length(abundance_files))
# training_files_rep = rep(training_file,length(abundance_files))
# testing_files_rep = rep(testing_file,length(abundance_files))
# my_df = data.frame(abundance_files,metadata_files_rep,mapping_files_rep,training_files_rep,testing_files_rep)
# # split into 2 pieces
# split_point = floor(nrow(my_df)/2)
# my_df1 = my_df[1:split_point,]
# my_df2 = my_df[(split_point +1):nrow(my_df),]
# # this extra column is just so I don't mess with the metadata since I am doing things in parallel
# my_df1$metadata_suffix = 1
# my_df2$metadata_suffix = 2
# my_label1 = gsub(".csv","_input_file_1.tsv",metadata_file)
# my_label2 = gsub(".csv","_input_file_2.tsv",metadata_file)
# write.table(my_df1,file=paste("prep_voe_input_files/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# write.table(my_df2,file=paste("prep_voe_input_files/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# return(my_df)
# })
```
#Do a third version where I do 3 fold CV
```{r}
# setwd("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3")
#
# abundance_files = list.files("/n/scratch3/users/s/sez10/_RESTORE/TEDDY/parsed_data",pattern=".csv",full.names = TRUE)
# abundance_files = abundance_files[-grep("healthy",abundance_files)]
# abundance_files = abundance_files[-grep("total_genes_left_each_timepoint.csv",abundance_files)]
# mapping_files = list.files(pattern = ".mapping.csv")
# metadata_files = gsub(".mapping.csv",".csv",mapping_files)
# training_data_files_two_thirds_train = gsub(".mapping.csv","_proportion_train_0.66.train_subjects.txt",mapping_files)
# training_data_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5.train_subjects.txt",mapping_files)
# testing_data_files_two_third_train = gsub(".mapping.csv","_proportion_train_0.66.test_subjects.txt",mapping_files)
# testing_data_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5.test_subjects.txt",mapping_files)
#
# mapping_metadata_two_third_train = data.frame(metadata_files,mapping_files,training_data_files_two_thirds_train,testing_data_files_two_third_train)
#
# mapping_metadata_fifty_fity_train = data.frame(metadata_files,mapping_files,training_data_files_fifty_train,testing_data_files_fifty_train)
#
# write_output_files = function(mapping_metadata) {
# df_lists = apply(mapping_metadata, 1, function(myrow) {
# metadata_file = myrow[1]
# mapping_file = myrow[2]
# training_file = myrow[3]
# testing_file = myrow[4]
# metadata_files_rep = rep(metadata_file,length(abundance_files))
# mapping_files_rep = rep(mapping_file,length(abundance_files))
# training_files_rep = rep(training_file,length(abundance_files))
# testing_files_rep = rep(testing_file,length(abundance_files))
# my_df = data.frame(abundance_files,metadata_files_rep,mapping_files_rep,training_files_rep,testing_files_rep)
# # split into 2 pieces
# split_point = floor(nrow(my_df)/2)
# my_df1 = my_df[1:split_point,]
# my_df2 = my_df[(split_point +1):nrow(my_df),]
# # this extra column is just so I don't mess with the metadata since I am doing things in parallel
# my_df1$metadata_suffix = 1
# my_df2$metadata_suffix = 2
# my_label1 = gsub(".train_subjects.txt","_input_file_1.tsv",training_file)
# my_label2 = gsub(".train_subjects.txt","_input_file_2.tsv",training_file)
# write.table(my_df1,file=paste("prep_voe_input_files/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# write.table(my_df2,file=paste("prep_voe_input_files/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# return(my_df)
# })
# return(df_lists)
# }
# output_files_two_thirds_train = write_output_files(mapping_metadata=mapping_metadata_two_third_train)
# output_files_fifty_train = write_output_files(mapping_metadata=mapping_metadata_fifty_fity_train)
#df_lists = apply(mapping_metadata,1, function(myrow) {
# metadata_file = myrow[1]
# mapping_file = myrow[2]
# training_file = myrow[3]
# testing_file = myrow[4]
# metadata_files_rep = rep(metadata_file,length(abundance_files))
# mapping_files_rep = rep(mapping_file,length(abundance_files))
# training_files_rep = rep(training_file,length(abundance_files))
# testing_files_rep = rep(testing_file,length(abundance_files))
# my_df = data.frame(abundance_files,metadata_files_rep,mapping_files_rep,training_files_rep,testing_files_rep)
# # split into 2 pieces
# split_point = floor(nrow(my_df)/2)
# my_df1 = my_df[1:split_point,]
# my_df2 = my_df[(split_point +1):nrow(my_df),]
# # this extra column is just so I don't mess with the metadata since I am doing things in parallel
# my_df1$metadata_suffix = 1
# my_df2$metadata_suffix = 2
# my_label1 = gsub(".csv","_input_file_1.tsv",metadata_file)
# my_label2 = gsub(".csv","_input_file_2.tsv",metadata_file)
# write.table(my_df1,file=paste("prep_voe_input_files/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# write.table(my_df2,file=paste("prep_voe_input_files/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
# return(my_df)
#})
```
#Do a 4th version.
```{r}
setwd("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4")
abundance_files = list.files("/n/scratch3/users/s/sez10/_RESTORE/TEDDY/parsed_data",pattern=".csv",full.names = TRUE)
abundance_files = abundance_files[-grep("healthy",abundance_files)]
abundance_files = abundance_files[-grep("total_genes_left_each_timepoint.csv",abundance_files)]
mapping_files = list.files(pattern = ".mapping.csv")
#train_metadata_files = gsub(".mapping.csv","_train_metadata.csv",mapping_files)
#test_metadata_files = gsub(".mapping.csv","_test_metadata.csv",mapping_files)
training_metadata_files_two_thirds_train = gsub(".mapping.csv","_proportion_train_0.66_train_metadata.csv",mapping_files)
training_metadata_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5_train_metadata.csv",mapping_files)
testing_metadata_files_two_third_train = gsub(".mapping.csv","_proportion_train_0.66_test_metadata.csv",mapping_files)
testing_metadata_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5_test_metadata.csv",mapping_files)
training_data_files_two_thirds_train = gsub(".mapping.csv","_proportion_train_0.66.train_subjects.txt",mapping_files)
training_data_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5.train_subjects.txt",mapping_files)
testing_data_files_two_third_train = gsub(".mapping.csv","_proportion_train_0.66.test_subjects.txt",mapping_files)
testing_data_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5.test_subjects.txt",mapping_files)
mapping_metadata_two_third_train = data.frame(training_metadata_files_two_thirds_train,testing_metadata_files_two_third_train,mapping_files,training_data_files_two_thirds_train,testing_data_files_two_third_train)
mapping_metadata_fifty_fity_train = data.frame(training_metadata_files_fifty_train,testing_metadata_files_fifty_train,mapping_files,training_data_files_fifty_train,testing_data_files_fifty_train)
write_output_files = function(mapping_metadata) {
df_lists = apply(mapping_metadata, 1, function(myrow) {
train_metadata_file = myrow[1]
test_metadata_file = myrow[2]
mapping_file = myrow[3]
training_file = myrow[4]
testing_file = myrow[5]
train_metadata_file_rep = rep(train_metadata_file,length(abundance_files))
test_metadata_file_rep = rep(test_metadata_file,length(abundance_files))
mapping_files_rep = rep(mapping_file,length(abundance_files))
training_files_rep = rep(training_file,length(abundance_files))
testing_files_rep = rep(testing_file,length(abundance_files))
my_df = data.frame(abundance_files,train_metadata_file_rep,test_metadata_file_rep,mapping_files_rep,training_files_rep,testing_files_rep)
# split into 2 pieces
split_point = floor(nrow(my_df)/2)
my_df1 = my_df[1:split_point,]
my_df2 = my_df[(split_point +1):nrow(my_df),]
# this extra column is just so I don't mess with the metadata since I am doing things in parallel
my_df1$metadata_suffix = 1
my_df2$metadata_suffix = 2
my_label1 = gsub(".train_subjects.txt","_input_file_1.tsv",training_file)
my_label2 = gsub(".train_subjects.txt","_input_file_2.tsv",training_file)
write.table(my_df1,file=paste("prep_voe_input_files/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
write.table(my_df2,file=paste("prep_voe_input_files/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
return(my_df)
})
return(df_lists)
}
output_files_two_thirds_train = write_output_files(mapping_metadata=mapping_metadata_two_third_train)
output_files_fifty_train = write_output_files(mapping_metadata=mapping_metadata_fifty_fity_train)
```
#Create input files for when I compare ctrl to case right before onset of T1D
```{r}
setwd("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4")
abundance_files = list.files("/n/scratch3/users/s/sez10/_RESTORE/TEDDY/parsed_data",pattern=".csv",full.names = TRUE)
abundance_files = abundance_files[-grep("healthy",abundance_files)]
abundance_files = abundance_files[-grep("total_genes_left_each_timepoint.csv",abundance_files)]
mapping_files = list.files(pattern = ".mapping.csv")
mapping_files = mapping_files[grep("before_condition_case_vs_control",mapping_files)]
#train_metadata_files = gsub(".mapping.csv","_train_metadata.csv",mapping_files)
#test_metadata_files = gsub(".mapping.csv","_test_metadata.csv",mapping_files)
training_metadata_files_two_thirds_train = gsub(".mapping.csv","_proportion_train_0.66_train_metadata.csv",mapping_files)
training_metadata_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5_train_metadata.csv",mapping_files)
testing_metadata_files_two_third_train = gsub(".mapping.csv","_proportion_train_0.66_test_metadata.csv",mapping_files)
testing_metadata_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5_test_metadata.csv",mapping_files)
training_data_files_two_thirds_train = gsub(".mapping.csv","_proportion_train_0.66.train_subjects.txt",mapping_files)
training_data_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5.train_subjects.txt",mapping_files)
testing_data_files_two_third_train = gsub(".mapping.csv","_proportion_train_0.66.test_subjects.txt",mapping_files)
testing_data_files_fifty_train = gsub(".mapping.csv","_proportion_train_0.5.test_subjects.txt",mapping_files)
mapping_metadata_two_third_train = data.frame(training_metadata_files_two_thirds_train,testing_metadata_files_two_third_train,mapping_files,training_data_files_two_thirds_train,testing_data_files_two_third_train)
mapping_metadata_fifty_fity_train = data.frame(training_metadata_files_fifty_train,testing_metadata_files_fifty_train,mapping_files,training_data_files_fifty_train,testing_data_files_fifty_train)
write_output_files = function(mapping_metadata) {
df_lists = apply(mapping_metadata, 1, function(myrow) {
train_metadata_file = myrow[1]
test_metadata_file = myrow[2]
mapping_file = myrow[3]
training_file = myrow[4]
testing_file = myrow[5]
train_metadata_file_rep = rep(train_metadata_file,length(abundance_files))
test_metadata_file_rep = rep(test_metadata_file,length(abundance_files))
mapping_files_rep = rep(mapping_file,length(abundance_files))
training_files_rep = rep(training_file,length(abundance_files))
testing_files_rep = rep(testing_file,length(abundance_files))
my_df = data.frame(abundance_files,train_metadata_file_rep,test_metadata_file_rep,mapping_files_rep,training_files_rep,testing_files_rep)
# split into 2 pieces
split_point = floor(nrow(my_df)/2)
my_df1 = my_df[1:split_point,]
my_df2 = my_df[(split_point +1):nrow(my_df),]
# this extra column is just so I don't mess with the metadata since I am doing things in parallel
my_df1$metadata_suffix = 1
my_df2$metadata_suffix = 2
my_label1 = gsub(".train_subjects.txt","_input_file_1.tsv",training_file)
my_label2 = gsub(".train_subjects.txt","_input_file_2.tsv",training_file)
write.table(my_df1,file=paste("prep_voe_input_files_before_case_vs_ctrl/",my_label1,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
write.table(my_df2,file=paste("prep_voe_input_files_before_case_vs_ctrl/",my_label2,sep=""),sep="\t",col.names=FALSE,row.names=FALSE,quote=FALSE)
return(my_df)
})
return(df_lists)
}
output_files_two_thirds_train = write_output_files(mapping_metadata=mapping_metadata_two_third_train)
output_files_fifty_train = write_output_files(mapping_metadata=mapping_metadata_fifty_fity_train)
```
#Now run to average samples together that are apart of the same subjects
```{bash}
#cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/normalized_alignments/parsed_data
#for x in prep_voe_input_files/*tsv
#do
# sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk.bash ${x}
#done
#sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk.bash prep_voe_input_files/healthy_pre-t1d-all_HLA_input_file_1.tsv
```
#Run for antibody associations
```{bash}
#cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/normalized_alignments/parsed_data
#for x in prep_voe_input_files_antibody_associations/*tsv
#do
# sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk.bash ${x}
#done
```
#average samples together that are apart of the same subjects for training data
```{bash}
#cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_2
#for x in prep_voe_input_files/*tsv
#do
# sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk_training_data.bash ${x}
#done
#cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3
#for x in prep_voe_input_files/*tsv
#do
# sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk_training_data.bash ${x}
#done # 3403522 still running. check this guy later
# redo timed out ones
#sacct -S 2022-02-18 | grep "prep_abun" | grep -v "COMPLETED" | grep "TIMEOUT" | awk '{print $1}' | while read line; do head -n 4 slurm-$line.out | tail -n 1 ; done | while read line; do grep -w -l $line prep_voe_input_files/*.tsv; done > voe_input_files_to_redo.txt
#while read line
#do
# sbatch -c 1 -t 1-00:00 -p medium --mem=50G prep_abundance_for_voe_bulk_training_data_2.bash ${line}
#done < voe_input_files_to_redo.txt
# we are going to do another version
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4
for x in prep_voe_input_files/*tsv
do
sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk_training_data_v2.bash ${x}
done
# do version where we compare case vs control but cases are only x months prior to onset
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4
for x in prep_voe_input_files_before_case_vs_ctrl/*tsv
do
sbatch -c 1 -t 0-11:59 -p short --mem=10G prep_abundance_for_voe_bulk_training_data_v2.bash ${x}
done # this should have been like 5 GBs. I'm gonna get in trouble!!
```
#see which still have to run
```{r}
input_files = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/prep_voe_input_files",pattern="tsv",full.names = TRUE)
# get all output folders that should exist
output_folders = gsub("_input_file_2.tsv$","",basename(input_files))
output_folders = gsub("_input_file_1.tsv$","",output_folders)
# output files
output_files_mc = paste("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4","/",output_folders,"/",basename(output_folders),"_test_mc.rds",sep="")
output_files_ga = paste("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4","/",output_folders,"/",basename(output_folders),"_test_ga.rds",sep="")
mydf = data.frame(input_files,output_folders,output_files_mc,output_files_ga)
mydf_1= mydf[grep("_input_file_1.tsv",mydf[,1]),]
mydf_2= mydf[grep("_input_file_2.tsv",mydf[,1]),]
# get files we have
output_files_mc = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",pattern="_test_mc.rds",recursive = TRUE,full.names = TRUE)
output_files_ga = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",pattern="_test_ga.rds",recursive = TRUE,full.names = TRUE)
hello = mydf_1[!mydf_1$output_files_mc%in%output_files_mc,]
goodbye = mydf_2[!mydf_2$output_files_ga%in%output_files_ga,]
to_do = c(hello$input_files,goodbye$input_files)
write.table(to_do,file = "prep_voe_input_files/files_to_do.txt",quote=FALSE,col.names=FALSE,row.names=FALSE)
```
```{bash}
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4
while read line
do
sbatch -c 1 -t 0-06:00 -p short --mem=30G prep_abundance_for_voe_bulk_training_data_v2.bash ${line}
done < prep_voe_input_files/files_to_do.txt
```
```{r}
input_files = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/prep_voe_input_files",pattern="tsv",full.names = TRUE)
# get all output folders that should exist
output_folders = gsub("_input_file_2.tsv$","",basename(input_files))
output_folders = gsub("_input_file_1.tsv$","",output_folders)
# output files
output_files_mc = paste("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4","/",output_folders,"/",basename(output_folders),"_test_mc.rds",sep="")
output_files_ga = paste("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4","/",output_folders,"/",basename(output_folders),"_test_ga.rds",sep="")
mydf = data.frame(input_files,output_folders,output_files_mc,output_files_ga)
mydf_1= mydf[grep("_input_file_1.tsv",mydf[,1]),]
mydf_2= mydf[grep("_input_file_2.tsv",mydf[,1]),]
# get files we have
output_files_mc = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",pattern="_test_mc.rds",recursive = TRUE,full.names = TRUE)
output_files_ga = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",pattern="_test_ga.rds",recursive = TRUE,full.names = TRUE)
hello = mydf_1[!mydf_1$output_files_mc%in%output_files_mc,]
goodbye = mydf_2[!mydf_2$output_files_ga%in%output_files_ga,]
to_do = c(hello$input_files,goodbye$input_files)
write.table(to_do,file = "prep_voe_input_files/files_to_do_v2.txt",quote=FALSE,col.names=FALSE,row.names=FALSE)
```
```{bash}
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4
while read line
do
sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk_training_data_v2.bash ${line}
done < prep_voe_input_files/files_to_do_v2.txt
```
#We have to redo the 0.66 ones
```{bash}
ls /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/prep_voe_input_files/*_proportion_train_0.66* > prop_0.66_prep_voe_input_files.txt
split -l 557 prop_0.66_prep_voe_input_files.txt prop_0.66_prep_voe_input_files_
# done on sez10
while read line
do
sbatch -c 1 -t 0-06:00 -p short --mem=30G prep_abundance_for_voe_bulk_training_data_v2.bash ${line}
done < prop_0.66_prep_voe_input_files_aa
# done on ldp9
while read line
do
sbatch -c 1 -t 0-06:00 -p short --mem=30G prep_abundance_for_voe_bulk_training_data_v2.bash ${line}
done < prop_0.66_prep_voe_input_files_ab
```
#Redo timed out ones
```{r}
input_files = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/prep_voe_input_files",pattern="tsv",full.names = TRUE)
# get all output folders that should exist
output_folders = gsub("_input_file_2.tsv$","",basename(input_files))
output_folders = gsub("_input_file_1.tsv$","",output_folders)
# output files
output_files_mc = paste("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4","/",output_folders,"/",basename(output_folders),"_test_mc.rds",sep="")
output_files_ga = paste("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4","/",output_folders,"/",basename(output_folders),"_test_ga.rds",sep="")
mydf = data.frame(input_files,output_folders,output_files_mc,output_files_ga)
mydf_1= mydf[grep("_input_file_1.tsv",mydf[,1]),]
mydf_2= mydf[grep("_input_file_2.tsv",mydf[,1]),]
# get files we have
output_files_mc = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",pattern="_test_mc.rds",recursive = TRUE,full.names = TRUE)
output_files_ga = list.files("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",pattern="_test_ga.rds",recursive = TRUE,full.names = TRUE)
hello = mydf_1[!mydf_1$output_files_mc%in%output_files_mc,]
goodbye = mydf_2[!mydf_2$output_files_ga%in%output_files_ga,]
to_do = c(hello$input_files,goodbye$input_files)
write.table(to_do,file = "prep_voe_input_files/files_to_do_v3.txt",quote=FALSE,col.names=FALSE,row.names=FALSE)
```
```{bash}
split -l 97 prep_voe_input_files/files_to_do_v3.txt prep_voe_input_files/files_to_do_v3_
while read line
do
sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk_training_data_v2.bash ${line}
done < prep_voe_input_files/files_to_do_v3_aa
while read line
do
sbatch -c 1 -t 0-11:59 -p short --mem=50G prep_abundance_for_voe_bulk_training_data_v2.bash ${line}
done < prep_voe_input_files/files_to_do_v3_ab
# I also want to remove folders that do not have a _proportion_train_0.5_train_metadata.csv or _proportion_train_0.66_train_metadata.csv. I will make one huge bash script single line to do this!
ls -d /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/*/ | grep -v "prep_voe_input_files" | rev | cut -c2- | rev | while read line; do echo ${line}_train_metadata.csv; done | while read line; do if [ ! -f "$line" ]; then echo $line; fi; done | while read line; do echo ${line%_train_metadata.csv}; done | while read line; do rm -r $line; done```
#make a file that tells you where each gene is located
```{bash}
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/normalized_alignments/parsed_data
ls *.csv | grep -v "healthy" | while read line; do awk -F ',' '{print $2,FILENAME}' ${line} | grep "genename"; done > gene_locs.txt
gzip gene_locs.txt
```
#Next filter to only include genes prevalent in 90% of subjects and do inverse normal transformation
```{bash}
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis
#ls -d /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3/*/ | grep -v "prep_voe_input_files" | rev | cut -c2- | rev > input_folder_list_parsed_3.txt
#while read line
#do
# sbatch -n 1 -c 5 --mem=50G -p short -t 0-04:00 scripts/filter_normalize_abundance_metadata_data_v2.bash ${line} 0.9 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/teddy_metadata_20190821_with_GRS2.csv 5 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3
#done < input_folder_list_parsed_3.txt
# did a bug that only affects non landmark analysis so redo those
#grep -v "month" input_folder_list_parsed_3.txt > input_folder_list_parsed_3_notlandmark.txt
#while read line
#do
# sbatch -n 1 -c 5 --mem=50G -p short -t 0-04:00 scripts/filter_normalize_abundance_metadata_data_v2.bash ${line} 0.9 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/teddy_metadata_20190821_with_GRS2.csv 5 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3
#done < input_folder_list_parsed_3_notlandmark.txt
##get jobs that ran out of memory
#sacct -S 2022-02-23 | grep "OUT_OF_ME" | grep "filter_no" | awk '{print $1}' | while read line; do grep "filtered_transformed_abundance_train.csv" slurm-${line}.out; done | while read line; do dirname $line; done > input_folder_list_parsed_3_outOfMem.txt
#while read line
#do
# sbatch -n 1 -c 5 --mem=100G -p short -t 0-04:00 scripts/filter_normalize_abundance_metadata_data_v2.bash ${line} 0.9 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/teddy_metadata_20190821_with_GRS2.csv 5 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3
#done < input_folder_list_parsed_3_outOfMem.txt
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis
ls -d /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/*/ | grep -v "prep_voe_input_files" | rev | cut -c2- | rev > input_folder_list_parsed_4.txt
split -l 533 input_folder_list_parsed_4.txt input_folder_list_parsed_4_
while read line
do
sbatch -n 1 -c 1 --mem=60G -p short -t 0-02:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_aa
while read line
do
sbatch -n 1 -c 1 --mem=60G -p short -t 0-02:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_ab
sacct -S 2023-04-12 | grep "filter_no" | grep "OUT_OF_ME" | grep -v "6686663" | awk '{print $1}' | while read line; do grep -m 1 "_proportion_train_" slurm-${line}.out; done | while read line; do dirname $line; done | tr -d '"' > input_folder_list_parsed_4_outOfMem_a.txt
sacct -S 2023-04-12 | grep "filter_no" | grep "OUT_OF_ME" | grep -v "6684440" | awk '{print $1}' | while read line; do grep -m 1 "_proportion_train_" slurm-${line}.out; done | while read line; do dirname $line; done | tr -d '"' > input_folder_list_parsed_4_outOfMem_b.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-02:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_outOfMem_a.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-02:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_outOfMem_b.txt
sacct -S 2023-04-12 | grep "filter_no" | grep -A 1000 "6703998" | grep "OUT_OF_ME" | awk '{print $1}' | while read line; do grep -m 1 "_proportion_train_" slurm-${line}.out; done | while read line; do dirname $line; done | tr -d '"' > input_folder_list_parsed_4_outOfMem2_a.txt
sacct -S 2023-04-12 | grep "filter_no" | grep -A 1000 "6704025" | grep "OUT_OF_ME" | awk '{print $1}' | while read line; do grep -m 1 "_proportion_train_" slurm-${line}.out; done | while read line; do dirname $line; done | tr -d '"' > input_folder_list_parsed_4_outOfMem2_b.txt
while read line
do
sbatch -n 1 -c 1 --mem=200G -p short -t 0-04:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_outOfMem2_a.txt
while read line
do
sbatch -n 1 -c 1 --mem=200G -p short -t 0-04:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_outOfMem2_b.txt
```
```{r}
library(data.table)
#seems to be an issue with some abundance files. lets find out which ones
folder_list = read.table("input_folder_list_parsed_4.txt")
folder_list = folder_list[,1]
abundance_test_list = paste(folder_list,"/",basename(folder_list),"filtered_transformed_abundance_test.csv",sep="")
abundance_train_list = paste(folder_list,"/",basename(folder_list),"filtered_transformed_abundance_test.csv",sep="")
abundance_list = data.frame(abundance_test_list,abundance_train_list)
bool_list = rep("FALSE",nrow(abundance_list))
for(counter in 1:nrow(abundance_list)) {
x = unlist(abundance_list[counter,])
test_data = x[1]
train_data = x[2]
test_df = fread(cmd = paste("head -n 1",test_data, "| cut -d ',' -f1"),nrows=2)
train_df = fread(cmd = paste("head -n 1",train_data, "| cut -d ',' -f1"),nrows=2)
if(colnames(test_df) == "test_data_filtered_transformed") {
bool_list[counter] = TRUE
}
if(colnames(train_df) == "train_data_filtered_transformed") {
bool_list[counter] = TRUE
}
}
to_redo_processing = folder_list[which(bool_list=="TRUE")]
write.table(to_redo_processing,file="to_redo_processing.txt",quote=FALSE,col.names=FALSE,row.names=FALSE)
```
```{bash}
while read line
do
sbatch -n 1 -c 1 --mem=60G -p short -t 0-02:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < to_redo_processing.txt
```
# Filter and normalize for case right before onset vs control
```{bash}
cd /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis
ls -d /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4/*before_condition_case_vs_control*/ | rev | cut -c2- | rev > input_folder_list_parsed_4_before_condition_case_vs_ctrl.txt
while read line
do
sbatch -n 1 -c 1 --mem=60G -p short -t 0-02:00 scripts/filter_normalize_abundance_metadata_data_v3.bash ${line} 0.9 1
done < input_folder_list_parsed_4_before_condition_case_vs_ctrl.txt
```
#create input files for running models
```{r}
all_dirs = list.dirs("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_3",recursive=FALSE)
all_dirs = all_dirs[!grepl("prep_voe_input_files",all_dirs)]
transformed_abundance_file_list = lapply(all_dirs, function(x) list.files(x,pattern="transform",full.names = TRUE))
names(transformed_abundance_file_list) = all_dirs
transformed_abundance_file_list = transformed_abundance_file_list[sapply(transformed_abundance_file_list, length)==2]
test_abundance_data = sapply(transformed_abundance_file_list, function(x) x[grep("filtered_transformed_abundance_test.csv",x)])
train_abundance_data = sapply(transformed_abundance_file_list, function(x) x[grep("filtered_transformed_abundance_train.csv",x)])
input_info = data.frame(test_abundance_data,train_abundance_data)
# now get metadata
metadata_test = sapply(rownames(input_info), function(x) list.files(x,pattern="_test_1_metadata_filtered_baseline_",full.names = TRUE))
metadata_train = sapply(rownames(input_info), function(x) list.files(x,pattern="_train_1_metadata_filtered_baseline_",full.names = TRUE))
train_fold1 = paste(rownames(input_info),".train_subjects_1.txt",sep="")
train_fold2 = paste(rownames(input_info),".train_subjects_2.txt",sep="")
train_fold3 = paste(rownames(input_info),".train_subjects_3.txt",sep="")
test_subjects = paste(rownames(input_info),".test_subjects.txt",sep="")
input_info$metadata_train = metadata_train
input_info$metadata_test = metadata_test
input_info$train_fold1 = train_fold1
input_info$train_fold2 = train_fold2
input_info$train_fold3 = train_fold3
input_info$test_subjects = test_subjects
write.csv(input_info,file="/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v3.csv",row.names = FALSE,quote=FALSE)
#write.table(input_info,file="/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v3.tsv",row.names = FALSE,col.names=FALSE,sep="\t",quote=FALSE)
```
#create input files for running models parse_data_4
```{r}
all_dirs = list.dirs("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",recursive=FALSE)
all_dirs = all_dirs[!grepl("prep_voe_input_files",all_dirs)]
transformed_abundance_file_list = lapply(all_dirs, function(x) list.files(x,pattern="transform",full.names = TRUE))
names(transformed_abundance_file_list) = all_dirs
transformed_abundance_file_list = transformed_abundance_file_list[sapply(transformed_abundance_file_list, length)==2]
test_abundance_data = sapply(transformed_abundance_file_list, function(x) x[grep("filtered_transformed_abundance_test.csv",x)])
train_abundance_data = sapply(transformed_abundance_file_list, function(x) x[grep("filtered_transformed_abundance_train.csv",x)])
input_info = data.frame(test_abundance_data,train_abundance_data)
# now get metadata
metadata_train = paste(rownames(input_info),"_train_metadata.csv",sep="")
metadata_test = paste(rownames(input_info),"_test_metadata.csv",sep="")
train_fold1 = paste(rownames(input_info),".train_subjects_1.txt",sep="")
train_fold2 = paste(rownames(input_info),".train_subjects_2.txt",sep="")
train_fold3 = paste(rownames(input_info),".train_subjects_3.txt",sep="")
test_subjects = paste(rownames(input_info),".test_subjects.txt",sep="")
input_info$metadata_train = metadata_train
input_info$metadata_test = metadata_test
input_info$train_fold1 = train_fold1
input_info$train_fold2 = train_fold2
input_info$train_fold3 = train_fold3
input_info$test_subjects = test_subjects
write.csv(input_info,file="/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4.csv",row.names = FALSE,quote=FALSE)
#write.table(input_info,file="/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v3.tsv",row.names = FALSE,col.names=FALSE,sep="\t",quote=FALSE)
```
#Create inputs for running the models where cases are right before onset and controls are aged matched
```{r}
all_dirs = list.dirs("/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_alignments/parsed_data_4",recursive=FALSE)
all_dirs = all_dirs[!grepl("prep_voe_input_files",all_dirs)]
all_dirs = all_dirs[grep("before_condition_case_vs_control",all_dirs)]
transformed_abundance_file_list = lapply(all_dirs, function(x) list.files(x,pattern="transform",full.names = TRUE))
names(transformed_abundance_file_list) = all_dirs
transformed_abundance_file_list = transformed_abundance_file_list[sapply(transformed_abundance_file_list, length)==2]
test_abundance_data = sapply(transformed_abundance_file_list, function(x) x[grep("filtered_transformed_abundance_test.csv",x)])
train_abundance_data = sapply(transformed_abundance_file_list, function(x) x[grep("filtered_transformed_abundance_train.csv",x)])
input_info = data.frame(test_abundance_data,train_abundance_data)
# now get metadata
metadata_train = paste(rownames(input_info),"_train_metadata.csv",sep="")
metadata_test = paste(rownames(input_info),"_test_metadata.csv",sep="")
train_fold1 = paste(rownames(input_info),".train_subjects_1.txt",sep="")
train_fold2 = paste(rownames(input_info),".train_subjects_2.txt",sep="")
train_fold3 = paste(rownames(input_info),".train_subjects_3.txt",sep="")
test_subjects = paste(rownames(input_info),".test_subjects.txt",sep="")
input_info$metadata_train = metadata_train
input_info$metadata_test = metadata_test
input_info$train_fold1 = train_fold1
input_info$train_fold2 = train_fold2
input_info$train_fold3 = train_fold3
input_info$test_subjects = test_subjects
write.table(input_info,file="/n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_before_condition_case_vs_control.csv",row.names = FALSE,quote=FALSE,col.names=FALSE,sep=",")
```
#Run random forest on models
```{bash}
# remove header
tail -n +2 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4.csv > /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header.csv
# remove before_condition cause that is only for binary classification
grep -v "before_condition" /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header.csv > /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv
#while read line
#do
# sbatch /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_RF.bash ${line} microbiome,number_autoantibodies,fdr,grs2 all
#done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v3_noheader.csv
# run lasso
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-11:59 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/test.csv
split -l 358 models_input_files_parsed_v4_no_header_no_before_condition.csv models_input_files_parsed_v4_no_header_no_before_condition_
while read line
do
sbatch -n 1 -c 1 --mem=70G -p short -t 0-03:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition_aa
while read line
do
sbatch -n 1 -c 1 --mem=70G -p short -t 0-03:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition_ab
# now redo out of memory or timeout
sacct -S 2023-04-13 | grep "run_lasso" | grep -A 1000 "6742575" | grep -v "COMPLETED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4b.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-04:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4b.txt
sacct -S 2023-04-13 | grep "run_lasso" | grep -v "COMPLETED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4a.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-04:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4a.txt
# more jobs still failed. see what I need to redo
sacct -S 2023-04-14 | grep "run_lasso" | grep -A 1000 "6792650" | grep -v "COMPLETED" |grep -v "FAILED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4aa.txt
sacct -S 2023-04-14 | grep "run_lasso" | grep -A 1000 "6792379" | grep -v "COMPLETED" |grep -v "FAILED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4ab.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-08:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4aa.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p short -t 0-08:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4ab.txt
sacct | grep "run_lasso" | grep -v "COMPLETED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4aaa.txt
sacct | grep "run_lasso" | grep -v "COMPLETED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4aab.txt
while read line
do
sbatch -n 1 -c 1 --mem=200G -p medium -t 1-00:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4aaa.txt
while read line
do
sbatch -n 1 -c 1 --mem=100G -p medium -t 1-00:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4aab.txt
#locs account
sacct -S 2023-04-15 | grep -A 1000 6824859 | grep "run_lasso" | grep -v "COMPLETED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4aaab.txt
while read line
do
sbatch -n 1 -c 1 --mem=150G -p medium -t 1-12:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/out_of_mem_jobs_v4aaab.txt
# my account
sacct -S 2023-04-15 | grep -A 1000 6824848 | grep "run_lasso" | grep -v "COMPLETED" | awk '{print $1}' | while read line; do grep -m 1 "filtered_transformed_abundance_test.csv" slurm-${line}.out ; done | while read line; do grep ${line} /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv; done > out_of_mem_jobs_v4aaab.txt
# make sure I got everything
Rscript scripts/find_failed_job_folders.R /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header_no_before_condition.csv output_lasso_time_to_event_C_loss_microbiome_selection_method_all_feature_list_microbiome.rds failed_lasso_survival.txt
while read line
do
sbatch -n 1 -c 1 --mem=60G -p short -t 0-03:00 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/scripts/run_lasso.bash ${line} microbiome NA all 1 C
done < /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/failed_lasso_survival.txt
# run ttest
head -n 1 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header.csv > test.txt
split -l 533 /n/data1/joslin/icrb/kostic/szimmerman/TEDDY_analysis/models_input_files_parsed_v4_no_header.csv models_input_files_parsed_v4_no_header_
while read line
do