-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path3b_ConcatConvert.sh
executable file
·1527 lines (1112 loc) · 74.3 KB
/
3b_ConcatConvert.sh
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
#!/bin/bash
# Overview:
# =-==================
# This script will:
# 1. Concatenate GEN file
# 2. Analyze the variants on the concatenated GEN file (meanwhile...)
# 3. Concatenate the chromosomal gen files to a single dataset gen file
# 4. Convertt the chromosomal GEN file to a Pgen file for Plink Analysis
# Splash Screen
# --------------
source .TitleSplash.txt
printf "$Logo"
# Source from .config files (Program options via Settings.conf & Program execs via Programs.conf)
# ----------------------------
source Settings.conf
# Load Odysseys Dependencies -- pick from several methods
if [ "${OdysseySetup,,}" == "one" ]; then
printf "\n\nLoading Odyssey's Singularity Container Image \n\n"
source ./Configuration/Setup/Programs-Singularity.conf
elif [ "${OdysseySetup,,}" == "two" ]; then
printf "\n\nLoading Odyssey's Manually Configured Dependencies \n\n"
source ./Configuration/Setup/Programs-Manual.conf
else
echo
echo User Input Not Recognized -- Please specify One or Two
echo Exiting Dependency Loading
echo
exit
fi
# Set Working Directory
# -------------------------------------------------
echo
echo
echo Changing to Working Directory
echo ----------------------------------------------
echo ${WorkingDir}
cd ${WorkingDir}
# =-=-=-=-==========================================================
# ================================================================
# ====================== Error Check ===========================
# ================================================================
# ==================================================================
if [ "${UseImpute,,}" == "t" ]; then
# Perform Error Analysis on Phasing Step -- grep looks for .out files containing 'Killed', 'Aborted', 'segmentation', or 'error'
# -----------------------------
if [ "${ImputationErrorAnalysis,,}" == "t" ]; then
echo
echo --------------------------------------------------------
echo Performing Error Analysis on Imputation Jobs:
echo --------------------------------------------------------
echo
echo Note: Some errors are caused by no SNPs being in the imputed area -- this is not really an issue -- skip the segment
echo Note: Other errors are listed as segmentation faults -- memory access issues -- try re-running or use Impute2 and re-run
echo
echo
echo Imputation jobs that should be reviewed are listed:
echo It may take a while to scan all the .out files
echo ==============================================
echo
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V
echo
echo ==============================================
echo
# Examine the failed files?
echo
echo "The files listed above appeared to have failed."
echo "Would you like more details on why they failed (this will print the line that contains the error for each failed file)?"
echo "(y/n)?"
echo --------------------------------------------------
read UserInput1
echo
echo
if [ "${UserInput1,,}" == "y" ]; then
echo
echo "Outputting more details on failed file/s..."
echo ===========================================
echo
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -ri 'Killed\|Aborted\|segmentation\|error' | sort -V
echo
echo ===========================================
elif [ "${UserInput1,,}" == "n" ]; then
echo "Alright, will not output more details on failed file/s"
echo =========================================================
echo
else
echo "Input not recognized -- specify either 'y' or 'n' -- exiting Error Analysis"
echo ================================================================================
echo
fi
# Re-submit the failed scripts
echo
echo "Would you like to resubmit the failed scripts?"
echo "Script/s will be submitted to an HPS if specified in Conf.conf otherwise will submit via a simple 'sh' command"
echo "(y/n)?"
echo --------------------------------------------------
read UserInput2
echo
echo
if [ "${UserInput2,,}" == "y" ]; then
# Specify text document of failed scripts to re-run; manual script re-submission
echo
echo "Normally ALL the failed scripts will be re-submitted"
echo "However, you can provide a text doc that contains a list of the scripts you would like re-submitted"
echo "Would you prefer to manually provide this list?"
echo "Note: The file should contain the full path to the automatically created scripts you want re-submitted"
echo "Note: Each script should be listed on a new line of the text document"
echo "(y/n)?"
echo --------------------------------------------------
read UserInput3
echo
echo
if [ "${UserInput3,,}" == "y" ]; then
echo "You Said Yes to Manual Script Submission So Please Provide the Full Path to the Re-Submission Text Doc"
read UserInput4
echo "Using Text Doc: ${UserInput4} for manual script submission"
elif [ "${UserInput3}" == "n" ]; then
echo
else
echo "User Input not recognized -- please specify 'y' or 'n' -- ignoring input"
fi
if [ "${HPS_Submit,,}" == "t" ]; then
echo
echo Re-Submitting Failed Scripts to HPS...
echo ===========================================
echo
if [ "${UserInput3,,}" == "y" ]; then
echo "Manually reading in scripts to re-submit from $UserInput4"
# Manually read in scripts that need to be re-run
cat $UserInput4 | sort -V | xargs grep -r 'qsub' | sed 's/.*# //' > ReSubmitImputeJobs.txt
# Remove the errored .out file (otherwise the new .out will be appended to the old and the error will never be reported as fixed)
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V | xargs rm -f
# Read the file that contains the scripts that need to be re-submitted and submit then via Bash to the HPS queue
cat ReSubmitImputeJobs.txt | bash
# Remove ReSubmitJobs.txt
rm -f ReSubmitImputeJobs.txt
echo
echo ===========================================
echo
elif [ "${UserInput3,,}" == "n" ]; then
echo "Looking up all failed scripts from .out files for re-submission"
# The following line does a lot:
# 1) looks in the script directory that also contains output logs
# 2) find .out files that contain the words 'Killed', 'Aborted', 'segmentation', or 'error'
# 3,4) Sorts the .out files and subs .out for .sh to get the script
# 5) Within .sh should be a manual execution command that starts with '# qsub', grep finds the line and trims the off the '# ' to get the qsub command and saves it to ReSubmitPhaseJobs.txt
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V | sed 's/.out/.sh/g' | xargs grep -r 'qsub' | sed 's/.*# //' > ReSubmitImputeJobs.txt
# Remove the errored .out file (otherwise the new .out will be appended to the old and the error will never be reported as fixed)
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V | xargs rm -f
# Read the file that contains the scripts that need to be re-submitted and submit then via Bash to the HPS queue
cat ReSubmitImputeJobs.txt | bash
# Remove ReSubmitJobs.txt
rm -f ReSubmitImputeJobs.txt
echo
echo ===========================================
echo
else
echo
echo "User Input not recognized -- please specify 'y' or 'n'"
echo "Exiting Script Re-Submission"
echo
fi
else
echo
echo Re-Submitting Failed Scripts to Desktop...
echo ===========================================
echo
if [ "${UserInput3,,}" == "y" ]; then
echo "Manually reading in scripts to re-submit from $UserInput4"
# Manually read in scripts that need to be re-run
cat $UserInput4 | sort -V | xargs grep -r 'qsub' | sed 's/.*# //' > ReSubmitImputeJobs.txt
# Remove the errored .out file (otherwise the new .out will be appended to the old and the error will never be reported as fixed)
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V | xargs rm -f
# Read the file that contains the scripts that need to be re-submitted and submit then via Bash to the HPS queue
cat ReSubmitImputeJobs.txt | sh
# Remove ReSubmitJobs.txt
rm -f ReSubmitImputeJobs.txt
echo
echo ===========================================
echo
elif [ "${UserInput3,,}" == "n" ]; then
# The following line does a lot:
# 1) looks in the script directory that also contains output
# 2) find .out files that contain the words 'Killed', 'Aborted', 'segmentation', or 'error'
# 3,4) Sorts the .out files and subs .out for .sh to get the script
# 5) Within .sh should be a manual execution command that starts with 'time ', grep finds the line and saves it to ReSubmitPhaseJobs.txt
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V | sed 's/.out/.sh/g' | xargs grep -r 'time ' > ReSubmitImputeJobs.txt
# Remove the errored .out file (otherwise the new .out will be appended to the old and the error will never be reported as fixed)
find ./3_Impute/${BaseName}/Scripts2Impute -maxdepth 1 -type f -print | xargs grep -rli 'Killed\|Aborted\|segmentation\|error' | sort -V | xargs rm -f
# Read the file that contains the scripts that need to be re-submitted and submit then via sh to the Linux workstation
cat ReSubmitImputeJobs.txt | sh
# Remove ReSubmitJobs.txt
rm -f ReSubmitImputeJobs.txt
echo
echo ===========================================
echo
else
echo
echo "User Input not recognized -- please specify 'y' or 'n'"
echo "Exiting Script Re-Submission"
echo
fi
fi
elif [ "${UserInput2,,}" == "n" ]; then
echo "Alright, will not Re-Submit Failed Script/s"
echo ==============================================
echo
echo
else
echo "Input Not Recognized -- Specify Either 'yes' or 'no' -- Exiting Re-Submission"
echo ==============================================================================
echo
echo
fi
elif [ "${ImputationErrorAnalysis,,}" == "f" ]; then
echo
else
echo
echo User Input Not Recognized -- please specify T or F in Conf.conf
echo
fi
fi
# =-=-=-=-=-==============================================================================================
# ======================================================================================================
# =============================== IMPUTE 4 Post Imputation File Cleaning =============================
# ======================================================================================================
# ========================================================================================================
if [ "${UseImpute,,}" == "t" ]; then
echo
echo
echo ----------------------------------------------------------------
printf "Performing Post Imputation File Clean Up on IMPUTE4 Files\n"
echo ================================================================
# =-=-=-=-================================================================================================
# ======================================================================================================
# ========================= Perform Concatenation of chromosomal .gen file chunks ====================
# ======================================================================================================
# ========================================================================================================
if [ "${ConcatImpute,,}" == "t" ]; then
# Make Directory in which to place merged concatenated files
#----------------------------------------------------------------------------
echo
echo Creating Concat Cohort Folder within Impute Directory
echo ----------------------------------------------
echo
mkdir -p ./3_Impute/${BaseName}/ConcatImputation
# Use Lustre Stripping?
if [ "${LustreStrip,,}" == "t" ]; then
lfs setstripe -c 5 ./3_Impute/${BaseName}/ConcatImputation
fi
# Concatenation Command Using GNU-Parallel
if [ "${ConcatParallel,,}" == "t" ]; then
# This is a GNU Parallel command that first searches for *Chr[#]_* to see if the file exists
# GNU Parallel (https://www.gnu.org/software/parallel/) must be installed on the system to use this option
# If GNU-Parallel is installed it will find all the .gen chunks for the chromosome and concatenate them together
# If the chromosomal file doesn't exist then it will report that it doesn't exist
# All concatenations are performed in parallel based on the number of CPU cores detected (since concatenation is a light resource command)
# Load GNU-Parallel and execute the parallel command to concatenate in parallel
echo ---------------------------------------------------------
echo Running Parallel Concatenation with GNU-Parallel
echo Each CPU will concatenated 1 chromosome at a time
echo
echo You may have to configure GNU-Parallel manually to run on your system
echo See Config.conf for instructions on how to do this
echo ---------------------------------------------------------
echo
function Concat() {
if ls ./3_Impute/"$BaseName"/RawImputation/*Chr$1_*.gen 1> /dev/null 2>&1; then
#Say what chr was concatenated
printf "\n\nConcatenated Chromosome $1\n\n"
# Find all the chromosomal segments for a particular chromosome, sort them in order, replaces the first column with a chromsome number, then concatenate them to a single Chr .gen
find ./3_Impute/"$BaseName"/RawImputation/ -type f -name "*Chr$1_*.gen" |sort -V | xargs -r awk '{ $1='$1'; print }' | cat > ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.gen
# Zips the concatenated .gen file
${gzip_Exec} ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.gen
else echo "Files for Chromosome $1 does not exist -- Skipping"
fi
}
# -------- Configure GNU-Parallel (GNU Tag) --------
# On our system gnu-parallel is loaded via a module load command specified in the ./Odyssey/Configuration/Setup/*.conf file under the variable $LOAD_PARALLEL
# As an alternative you could simply configure GNU-Parallel manually so that calling "parallel" runs GNU-Parallel
# by adjusting the following lines so that GNU-Parallel runs on your system
# Load/Inititalize GNU-Parallel
$LOAD_PARALLEL
# -------- Configure GNU-Parallel --------
# Exports the BaseName variable so the child process can see it
export BaseName
export -f Concat
export gzip_Exec
# GNU-Parallel Command: Takes all the chromosomal chunks and concatenates them in parallel
if [ "${GNU_ETA,,}" == "t" ]; then
seq $ConcatStart $ConcatEnd | parallel --eta Concat {}
elif [ "${GNU_ETA,,}" == "f" ]; then
seq $ConcatStart $ConcatEnd | parallel Concat {}
else
echo 'Input not recognized for GNU_ETA -- specify either T or F'
fi
else
echo
echo ================================
echo Running Serial Concatenation
echo ================================
echo
# Run the Concatenation in Serial
for chr in `eval echo {${ConcatStart}..${ConcatEnd}}`; do
echo
echo ----- Concatenating Chromosome ${chr} -----
echo
#Searches for chromosome gen file/s; if exists then concatenates them; else skips the chromosome concatenation
if ls ./3_Impute/${BaseName}/RawImputation/*Chr${chr}_*gen 1> /dev/null 2>&1; then
find ./3_Impute/${BaseName}/RawImputation/ -type f -name "*Chr${chr}_*.gen" |sort -V | xargs -r awk '{ $1='${chr}'; print }' | cat > ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen
# Zips the concatenated .gen file
${gzip_Exec} ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen
else
echo "Files for Chromosome ${chr} does not exist -- Skipping"
fi
done
fi
else
printf "\n\n\n------ Skipping Concatenation of Impute4 Chromosomal .GEN Files ------\n"
fi
# =-=-=-=-=================================================================================================
# ======================================================================================================
# ===== Cleanup the Concatenated Impute4 Output Gen files (analyze INFO, Filter, Convert to VCF) =====
# ======================================================================================================
# =========================================================================================================
if [ "${Cleanup,,}" == "t" ]; then
printf "\n\n\nPreparing to Cleanup Concatenated Impute4 Chromosomal Gen Files\n"
echo ---------------------------------------------------------------
# Create Temporary Sample File that Creates Unique ID_1 By Combining ID_1 and ID-2 since SNPTEST only looks at ID_1
printf "Creating Temporary Sample File that Creates a Unique ID_1 from ID_1 and ID_2 \nSNPTEST only looks at ID_1, thus this must be unique\n"
echo
echo
awk -F " " 'NR==1; NR==2; NR > 2{print $1"_"$2,$2,$3,$4, $5}' OFS=' ' ./3_Impute/${BaseName}/${BaseName}.sample > ./3_Impute/${BaseName}/.TempSample4SNPTEST.sample
# =-=-=-=====================================================================================
# Cleanup Gen File in in parallel using GNU-Parallel
# =========================================================================================
# ===========================================================================================
if [ "${CleanupParallel,,}" == "t" ]; then
# Load GNU-Parallel and execute the parallel command to analyze variants in parallel
echo
echo =========================================================
printf " Running Parallel Chrom.gen.gz Cleanup with GNU-Parallel\n"
printf " Each CPU will analyze 1 chromosome at a time\n\n"
printf " You may have to configure GNU-Parallel manually\n"
printf " See Settings.conf for instructions on how to do this\n"
echo =========================================================
echo
# GNU-Parallel Function that Performs multiple steps to cleanup the Impute4 concatenated .gen.gz files
function CleanupFunc() {
# Create a SNP Report for the concatenated chromosomal GEN file (Includes INFO score)
# =-=-====================================================================================
# =========================================================================================
printf "\n\nUsing SNPTEST to Analyze Chr $1 INFO Scores\n"
echo ----------------------------------------------
# Conditional statement to see if there is a Concatenated Chromosomal GEN file from which to make a SNP Report
if ls ./3_Impute/"$BaseName"/ConcatImputation/*Chr$1.gen.gz 1> /dev/null 2>&1; then
# Conditional Statement to look to see if a snpstat file for the particular chromosome is already present
if ls ./3_Impute/"$BaseName"/ConcatImputation/*Chr$1.snpstat 1> /dev/null 2>&1; then
echo "A snpstat file for Chromosome $1 already exists -- What Should I do?"
# If a snpstat file for the particular chromosome is already present and overwrite set to false, then skip the Info analysis
if [ "${OverwriteIfExist,,}" == "f" ]; then
echo Do not overwrite ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.snpstat -- Skipping INFO Score Analysis for Chr $1
echo
# If a snpstat file for the particular chromosome is already present and overwrite set to true, then overwrite it
elif [ "${OverwriteIfExist,,}" == "t" ]; then
echo Will overwrite ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.snpstat
echo
#Perform SNPTEST SNP Analysis
printf "\n\nPerforming SNP Analysis on Chromosome $1 \n-----------------------------------------\n";
printf "Options in Effect: \n$SNPTEST_Exec \n-summary_stats_only -assume_chromosome $1 \n-data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.gen.gz ./3_Impute/${BaseName}/${BaseName}.sample \n-chunk 10000 \n-o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstatOut\n\n"
$SNPTEST_Exec -summary_stats_only -assume_chromosome $1 -data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.gen.gz ./3_Impute/${BaseName}/.TempSample4SNPTEST.sample -chunk 10000 -o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstatOut
else
echo ERROR -- Specify Either T or F for OverwriteIfExist Variable
echo
fi
else
# If a snpstat file for the particular chromosome is not present then do the Info analysis and make one
#Perform SNPTEST SNP Analysis
printf "\n\nPerforming SNP Analysis on Chromosome $1 \n-----------------------------------------\n";
printf "Options in Effect: \n$SNPTEST_Exec \n-summary_stats_only -assume_chromosome $1 \n-data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.gen.gz ./3_Impute/${BaseName}/${BaseName}.sample \n-chunk 10000 \n-o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstatOut\n\n"
$SNPTEST_Exec -summary_stats_only -assume_chromosome $1 -data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.gen.gz ./3_Impute/${BaseName}/.TempSample4SNPTEST.sample -chunk 10000 -o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstatOut
fi
else
# Otherwise if there are no files to create a SNP Report then say so
printf " \n\nNo Chromosomal .gen.gz File Present for Chromosome $1 with which to create a SNP Report -- Skipping \n"
fi
# Generate List to Filter Variants based on INFO scores
# =-=-=====================================================================================
# ==========================================================================================
printf " \n\n\nMake List to Filter Variants Based on INFO Scores for Chr $1\n"
echo ------------------------------------------------------------
# Conditional statement to see if there is a .snpstat file from which to filter variants
if ls ./3_Impute/"$BaseName"/ConcatImputation/*Chr$1.snpstat 1> /dev/null 2>&1; then
# Conditional Statement to look to see if an INFOFiltered_Chr$1.list is already present
if ls ./3_Impute/"$BaseName"/ConcatImputation/INFOFiltered_Chr$1.list 1> /dev/null 2>&1; then
printf "An INFOFiltered file for Chromosome $1 already exists -- What Should I do?\n"
# If a list file for the particular chromosome is already present and overwrite is set to false, then skip the list creation
if [ "${OverwriteIfExist,,}" == "f" ]; then
echo Do not overwrite ./3_Impute/"$BaseName"/ConcatImputation/INFOFiltered_Chr$1.list
echo Skipping INFO List Creation for Chr $1, but still report INFO SNP Stats
echo
# But Still Report SNP Filtering Stats to .snpstatOut
TOTAL_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat)"
FILTERED_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr$1.list)"
#Output to Screen
printf "\n\n\nINFO Score SNP Statistics for Chr$1:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome $1: $TOTAL_SNPS \nRemaining Total Variants in Chromosome $1 after INFO Filtering: $FILTERED_SNPS\n\n"
# Output to Snpstat.out
#printf "\n\nINFO Score SNP Statistics for Chr$1:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome $1: $TOTAL_SNPS \nRemaining Total Variants in Chromosome $1 after INFO Filtering: $FILTERED_SNPS\n\n" >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstatOut
# If a list file for the particular chromosome is already present and overwrite set to true, then overwrite it
elif [ "${OverwriteIfExist,,}" == "t" ]; then
echo Will overwrite ./3_Impute/"$BaseName"/ConcatImputation/INFOFiltered_Chr$1.list
# Output TEMP INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP names (used later to filter)
# Legacy Code
#awk '/^[^#]/ { print $2,$9}' ./Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat | awk '{ if($2 >= $'$INFOThresh') {print $1}}' > ./Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
#awk '{ if($9 >= $'$INFOThresh') { print $2}}' ./Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
#awk 'FNR > 11 { if($9 >= '$INFOThresh') { print $2,$9 }}' ./Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
printf "\n\n\nScanning .snpstat SNP-Report for SNPs that meet the specified INFO Score cutoff of greater than: $INFOThresh\n"
echo Outputting INFOFiltered list which contains SNP names and their corresponding INFO scores
echo ----------------------------------------------
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2 }}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list.temp
# Output INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP name and corresponding INFO score
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2, $9}}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
# Lookup SNP Filtering Stats
TOTAL_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat)"
FILTERED_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr$1.list)"
#Output to Screen
printf "\n\n\nINFO Score SNP Statistics for Chr$1:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome $1: $TOTAL_SNPS \nRemaining Total Variants in Chromosome $1 after INFO Filtering: $FILTERED_SNPS\n\n"
# Output to Snpstat.out
#printf "\n\nINFO Score SNP Statistics for Chr$1:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome $1: $TOTAL_SNPS \nRemaining Total Variants in Chromosome $1 after INFO Filtering: $FILTERED_SNPS\n\n" >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstatOut
else
echo ERROR -- Specify Either T or F for OverwriteIfExist Variable
fi
else
# Output TEMP INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP names (used later to filter)
# Legacy Code
#awk '/^[^#]/ { print $2,$9}' ./Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat | awk '{ if($2 >= $'$INFOThresh') {print $1}}' > ./Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
#awk '{ if($9 >= $'$INFOThresh') { print $2}}' ./Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
#awk 'FNR > 11 { if($9 >= '$INFOThresh') { print $2,$9 }}' ./Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
#awk '/^[^#]/ { first = \$1; \$1 = \"X\"; print \$0}' ./2_Phase/${BaseName}/Ody3_${BaseName}_Chr23_Phased.vcf.temp > ./2_Phase/${BaseName}/Ody3_${BaseName}_Chr23_Phased.vcf
printf "\n\n\nScanning .snpstat SNP-Report for SNPs that meet the specified INFO Score cutoff of greater than: ${INFOThresh}\n"
echo Outputting INFOFiltered list that contains INFO Filtered SNP names and their corresponding INFO scores
echo ----------------------------------------------
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2 }}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list.temp
# Output INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP name and corresponding INFO score
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2, $9}}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr$1.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr$1.list
# Append SNP Filtering Stats to .snpstatOut
printf "\n\nINFO Score SNP Statistics:\n"
echo --------------------------
TOTAL_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.snpstat)"
FILTERED_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr$1.list)"
echo Total Number of Imputed Variants in Chromosome $1: $TOTAL_SNPS
echo Remaining Total Variants in Chromosome $1 after INFO Filtering: $FILTERED_SNPS
echo
fi
else
# Otherwise if there are no files with which to make a SNP list then say so
printf " \n\nNo Chromosomal .snpstat File Present for Chromosome $1 with which to filter SNPs -- Skipping \n"
fi
# Use Plink to Filter/Convert .gen.gz with INFO score List to .VCF.gz
# =-=-======================================================================================
# ===========================================================================================
printf "\n\nUsing Plink to Filter/Convert the Chr$1.gen.gz to an INFO filtered .vcf\n"
echo -------------------------------------------------------------------
# Conditional statement to see if there is a .gen.gz to convert to .vcf
if ls ./3_Impute/"$BaseName"/ConcatImputation/*Chr$1.gen.gz 1> /dev/null 2>&1; then
# Conditional Statement to look to see if a Plink Created .vcf is already present
if ls ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.vcf.gz 1> /dev/null 2>&1; then
printf "\nA .VCF.gz file for Chromosome $1 already exists -- What Should I do?\n"
# If a list file for the particular chromosome is already present and overwrite is set to false, then skip the list creation
if [ "${OverwriteIfExist,,}" == "f" ]; then
echo Do not overwrite ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.vcf.gz -- Skipping Plink VCF Conversion for Chr $1
echo
# If a list file for the particular chromosome is already present and overwrite set to true, then overwrite it
elif [ "${OverwriteIfExist,,}" == "t" ]; then
echo Will overwrite ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr$1.vcf.gz
echo
# Get chromosome number from the name of the file
#FetchChr=$(echo $1 | egrep -o --ignore-case "chr[[:digit:]]{1,2}[^[:digit:]]{1}" | egrep -o --ignore-case "[[:digit:]]{1,2}")
# Runs Plink to convert the concatenated GEN to a VCF 4.3
${Plink2_Exec} --memory 2000 require \
--gen ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.gen.gz \
--sample ./3_Impute/${BaseName}/${BaseName}.sample \
--export vcf vcf-dosage=GP \
--extract ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr$1.list.temp\
--out ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1
# Zip the Resulting File
printf "\n\n\nZipping the Plink .VCF.gz file for Chromosome $1 to save space\n"
echo --------------------------------------------------------------
${gzip_Exec} ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.vcf
else
echo ERROR -- Specify Either T or F for OverwriteIfExist Variable
fi
else
# If does not exist then perform the conversion
# Get chromosome number from the name of the file
#FetchChr=$(echo $1 | egrep -o --ignore-case "chr[[:digit:]]{1,2}[^[:digit:]]{1}" | egrep -o --ignore-case "[[:digit:]]{1,2}")
# Runs Plink to convert the concatenated GEN to a VCF 4.3
${Plink2_Exec} --memory 2000 require \
--gen ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.gen.gz \
--sample ./3_Impute/${BaseName}/${BaseName}.sample \
--export vcf vcf-dosage=GP \
--extract ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr$1.list.temp\
--out ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1
# Zip the Resulting File
printf "\n\n\nZipping the Plink .VCF.gz file for Chromosome $1 to save space\n"
echo --------------------------------------------------------------
${gzip_Exec} ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr$1.vcf
fi
else
# Otherwise if there are no files with which to filter/convert to make a dosage VCF then say so
printf " \n\nNo Chromosomal .gen.gz File Present for Chromosome $1 with which to filter and convert to a VCF.gz -- Skipping \n"
fi
}
# ----- Configure GNU-Parallel (GNU-Parallel Tag) --------
# On our system gnu-parallel is loaded via a module load command specified in Config.conf under the variable $LOAD_PARALLEL
# As an alternative you could simply configure GNU-Parallel manually so that calling "parallel" runs GNU-Parallel
# by adjusting the following lines so that GNU-Parallel runs on your system
# Load/Inititalize GNU-Parallel
$LOAD_PARALLEL
# -------- Configure GNU-Parallel --------
# Exports the BaseName and other variables as well as the exec path for SNPTEST so the child process (GNU-Parallel) can see it
export BaseName
export SNPTEST_Exec
export -f CleanupFunc
export CleanupStart
export CleanupEnd
export OverwriteIfExist
export INFOThresh
export Plink2_Exec
export gzip_Exec
# GNU-Parallel Command: Takes all the chromosomal .gen files and analyzes them in parallel
# GNU-Parallel Request ETA: ETA output should only be run on interactive jobs
if [ "${GNU_ETA,,}" == "t" ]; then
seq $CleanupStart $CleanupEnd | parallel --eta CleanupFunc {} ">" ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr{}.snpstatOut
elif [ "${GNU_ETA,,}" == "f" ]; then
seq $CleanupStart $CleanupEnd | parallel CleanupFunc {} ">" ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr{}.snpstatOut
else
echo 'Input not recognized for GNU_ETA -- specify either T or F'
fi
# =-=-=-=====================================================================================
# Cleanup Gen File in Serial
# ==========================================================================================
# ===========================================================================================
elif [ "${CleanupParallel,,}" == "f" ]; then
echo
echo ==============================================
printf " Cleanup Chrom.gen.gz In Serial Mode\n"
echo ==============================================
echo
for chr in `eval echo {${CleanupStart}..${CleanupEnd}}`; do
# Create a SNP Report for the concatenated chromosomal GEN file (Includes INFO score)
# =-=-======================================================================================
# ===========================================================================================
printf "\n\nUsing SNPTEST to Analyze Chr $1 INFO Scores\n"
echo ----------------------------------------------
# Conditional statement to see if there is a Concatenated Chromosomal GEN file from which to make a SNP Report
if ls ./3_Impute/${BaseName}/ConcatImputation/*Chr${chr}.gen.gz 1> /dev/null 2>&1; then
# Conditional Statement to look to see if a snpstat file for the particular chromosome is already present
if ls ./3_Impute/${BaseName}/ConcatImputation/*Chr${chr}.snpstat 1> /dev/null 2>&1; then
printf "A snpstat file for Chromosome ${chr}.snpstat already exists -- What Should I do?\n"
# If a snpstat file for the particular chromosome is already present and overwrite is set to false, then skip the Info analysis
if [ "${OverwriteIfExist,,}" == "f" ]; then
printf "Do not overwrite ./3_Impute/$BaseName/ConcatImputation/$BaseName_Chr${chr}.snpstat \nSkipping INFO Score Analysis for Chr ${chr}\n\n"
# If a snpstat file for the particular chromosome is already present and overwrite is set to true, then overwrite it
elif [ "${OverwriteIfExist,,}" == "t" ]; then
printf "Will overwrite ./3_Impute/$BaseName/ConcatImputation/$BaseName_Chr${chr}.snpstat\n\n"
#Perform SNPTEST SNP Analysis
printf "\n\nPerforming SNP Analysis on Chromosome ${chr} \n-----------------------------------------\n";
printf "Options in Effect: \n$SNPTEST_Exec \n-summary_stats_only -assume_chromosome ${chr} \n-data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen.gz ./3_Impute/${BaseName}/${BaseName}.sample \n-chunk 10000 \n-o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstatOut\n\n"
$SNPTEST_Exec -summary_stats_only -assume_chromosome ${chr} -data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen.gz ./3_Impute/${BaseName}/.TempSample4SNPTEST.sample -chunk 10000 -o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstatOut
else
echo ERROR -- Specify Either T or F for OverwriteIfExist Variable
echo
fi
else
# If a snpstat file for the particular chromosome is not present then do the Info analysis and make one
#Perform SNPTEST SNP Analysis
printf "\n\nPerforming SNP Analysis on Chromosome ${chr} \n-----------------------------------------\n";
printf "Options in Effect: \n$SNPTEST_Exec \n-summary_stats_only -assume_chromosome ${chr} \n-data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen.gz ./3_Impute/${BaseName}/${BaseName}.sample \n-chunk 10000 \n-o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstatOut\n\n"
$SNPTEST_Exec -summary_stats_only -assume_chromosome ${chr} -data ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen.gz ./3_Impute/${BaseName}/.TempSample4SNPTEST.sample -chunk 10000 -o ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstatOut
fi
else
# Otherwise if there are no files to create a SNP Report then say so
printf " \n\nNo Chromosomal .gen.gz File Present for Chromosome $1 with which to create a SNP Report -- Skipping \n"
fi
# Generate List to Filter Variants based on INFO scores
# =-=-=======================================================================================
# ========================================================================================
printf " \n\nMake List to Filter Variants Based on INFO Scores for Chr ${chr}\n"
echo ------------------------------------------------------------
# Conditional statement to see if there is a .snpstat file from which to filter variants
if ls ./3_Impute/$BaseName/ConcatImputation/*Chr${chr}.snpstat 1> /dev/null 2>&1; then
# Conditional Statement to look to see if an INFOFiltered_Chr$1.list is already present
if ls ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list 1> /dev/null 2>&1; then
printf "An INFOFiltered file for Chromosome ${chr} already exists -- What Should I do?\n"
# If a list file for the particular chromosome is already present and overwrite is set to false, then skip the list creation
if [ "${OverwriteIfExist,,}" == "f" ]; then
printf "Do not overwrite ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list \nSkipping INFO List Creation for Chr ${chr}, but still report INFO SNP Stats\n\n"
# But Still Report SNP Filtering Stats to .snpstatOut
printf "\n\nINFO Score SNP Statistics:\n"
echo --------------------------
TOTAL_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat)"
FILTERED_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr${chr}.list)"
echo Total Number of Imputed Variants in Chromosome ${chr}: $TOTAL_SNPS
echo Remaining Total Variants in Chromosome ${chr} after INFO Filtering: $FILTERED_SNPS
echo
# If a list file for the particular chromosome is already present and overwrite set to true, then overwrite it
elif [ "${OverwriteIfExist,,}" == "t" ]; then
echo Will overwrite ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list
printf "\n\n\nScanning .snpstat SNP-Report for SNPs that meet the specified INFO Score cutoff of greater than: $INFOThresh\n"
echo Outputting INFOFiltered list which contains SNP names and their corresponding INFO scores
echo ----------------------------------------------
# Output TEMP INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP names (used later to filter)
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2 }}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr${chr}.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list.temp
# Output INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP name and corresponding INFO score
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2, $9}}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr${chr}.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list
# Lookup SNP Filtering Stats
TOTAL_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat)"
FILTERED_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr${chr}.list)"
#Output to Screen
printf "\n\n\nINFO Score SNP Statistics for Chr${chr}:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome ${chr}: $TOTAL_SNPS \nRemaining Total Variants in Chromosome ${chr} after INFO Filtering: $FILTERED_SNPS\n\n"
# Output to Snpstat.out
printf "\n\nINFO Score SNP Statistics for Chr${chr}:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome ${chr}: $TOTAL_SNPS \nRemaining Total Variants in Chromosome ${chr} after INFO Filtering: $FILTERED_SNPS\n\n" >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstatOut
else
echo ERROR -- Specify Either T or F for OverwriteIfExist Variable
fi
else
# If an INFOFiltered_Chr$1.list is not already present then make one
# Output TEMP INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP names (used later to filter)
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2 }}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr${chr}.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list.temp
# Output INFO filtered list that ignores '#' headers, scans .snpstat for INFO scores (9th column of .snpstat), and filters/outputs SNP name and corresponding INFO score
awk '{ if (!/#|info/ && $9 >= '$INFOThresh') { print $2, $9}}' ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr${chr}.snpstat > ./3_Impute/$BaseName/ConcatImputation/INFOFiltered_Chr${chr}.list
# Lookup SNP Filtering Stats
TOTAL_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstat)"
FILTERED_SNPS="$(wc -l < ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr${chr}.list)"
#Output to Screen
printf "\n\n\nINFO Score SNP Statistics for Chr${chr}:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome ${chr}: $TOTAL_SNPS \nRemaining Total Variants in Chromosome ${chr} after INFO Filtering: $FILTERED_SNPS\n\n"
# Output to Snpstat.out
printf "\n\nINFO Score SNP Statistics for Chr${chr}:\n-------------------------- \nTotal Number of Imputed Variants in Chromosome ${chr}: $TOTAL_SNPS \nRemaining Total Variants in Chromosome ${chr} after INFO Filtering: $FILTERED_SNPS\n\n" >> ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.snpstatOut
fi
else
# Otherwise if there are no files with which to make a SNP list then say so
printf " \n\nNo Chromosomal .snpstat File Present for Chromosome ${chr} with which to filter SNPs -- Skipping \n"
fi
# Use Plink to Filter/Convert .gen.gz with INFO score List to .VCF.gz
# =-=-=====================================================================================
# =========================================================================================
printf "\n\nUsing Plink to Filter/Convert the Chr${chr}.gen.gz to an INFO filtered .vcf\n"
echo --------------------------------------------------------------------------
# Conditional statement to see if there is a .gen.gz to convert to .vcf
if ls ./3_Impute/$BaseName/ConcatImputation/*Chr${chr}.gen.gz 1> /dev/null 2>&1; then
# Conditional Statement to look to see if a Plink Created .vcf is already present
if ls ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr${chr}.vcf.gz 1> /dev/null 2>&1; then
printf "A .VCF.gz file for Chromosome ${chr} already exists -- What Should I do?\n"
# If a vcf.gz file for the particular chromosome is already present and overwrite is set to false, then skip the list creation
if [ "${OverwriteIfExist,,}" == "f" ]; then
echo Do not overwrite ./3_Impute/$BaseName/ConcatImputation/${BaseName}_Chr${chr}.vcf.gz -- Skipping Plink VCF Conversion for Chr ${chr}
echo
echo
# If a vcf.gz file for the particular chromosome is already present and overwrite set to true, then overwrite it
elif [ "${OverwriteIfExist,,}" == "t" ]; then
echo Will overwrite ./3_Impute/"$BaseName"/ConcatImputation/"$BaseName"_Chr${chr}.vcf.gz
echo
echo
# Get chromosome number from the name of the file
#FetchChr=$(echo $1 | egrep -o --ignore-case "chr[[:digit:]]{1,2}[^[:digit:]]{1}" | egrep -o --ignore-case "[[:digit:]]{1,2}")
# Runs Plink to convert the concatenated GEN to a VCF 4.3
${Plink2_Exec} --memory 2000 require \
--gen ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen.gz \
--sample ./3_Impute/${BaseName}/${BaseName}.sample \
--export vcf vcf-dosage=GP \
--extract ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr${chr}.list.temp\
--out ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}
# Zip the Resulting File
printf "\n\n\nZipping the Plink .VCF.gz file for Chromosome ${chr} \n"
echo --------------------------------------------------------------
${gzip_Exec} ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.vcf
else
echo ERROR -- Specify Either T or F for OverwriteIfExist Variable
fi
else
# If does not yet exist then perform the conversion to make one
# Runs Plink to convert the concatenated GEN to a VCF 4.3
${Plink2_Exec} --memory 2000 require\
--gen ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.gen.gz \
--sample ./3_Impute/${BaseName}/${BaseName}.sample \
--export vcf vcf-dosage=GP \
--extract ./3_Impute/${BaseName}/ConcatImputation/INFOFiltered_Chr${chr}.list.temp\
--out ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}
# Zip the Resulting File
printf "\n\n\nZipping the Plink .VCF.gz file for Chromosome ${chr} \n"
echo --------------------------------------------------------------
${gzip_Exec} ./3_Impute/${BaseName}/ConcatImputation/${BaseName}_Chr${chr}.vcf
fi
else
# Otherwise if there are no files with which to filter/convert to make a dosage VCF then say so
printf " \n\nNo Chromosomal .gen.gz File Present for Chromosome ${chr} with which to filter and convert to a VCF.gz -- Skipping \n"
fi
done
else
echo ERROR -- Specify Either T or F for Cleanup Variable
fi
# TarGZ .snpstat/Out and INFOFiltered files
# -------------------------------------------
printf "\n\nTar and Zipping IMPUTE INFO score information\n"
echo -----------------------------------------------------
# TGZ the .snpstat files
# =======================
printf "\n\nTar-Zipping .snpstat files to ./3_Impute/$BaseName/ConcatImputation/${BaseName}_INFOMetrics.tgz \n"
# Are .snpstat files present to tgz?
if ls ./3_Impute/$BaseName/ConcatImputation/*.snpstat 1> /dev/null 2>&1; then
# If .snpstat files are present does a corresponding tgz file already exist?
if ls ./3_Impute/$BaseName/ConcatImputation/${BaseName}_INFOMetricsSummary.tgz 1> /dev/null 2>&1; then
printf "\nFile Already Exists! What should I do? Checking Overwrite Settings...\n"
# If they exist overwrite if on
if [ "${OverwriteIfExist,,}" == "t" ]; then
printf "......Overwrite is ON so will overwrite\n"
${tar_Exec} -czvf ./3_Impute/$BaseName/ConcatImputation/${BaseName}_INFOMetricsSummary.tgz ./3_Impute/$BaseName/ConcatImputation/*.snpstat
# Do not overwrite if off
elif [ "${OverwriteIfExist,,}" == "f" ]; then
printf "......Overwrite is OFF so will skip\n"
else
printf "ERROR -- Specify Either T or F for OverwriteIfExist Variable"
fi
# If doesn't exist then make it
else
${tar_Exec} -czvf ./3_Impute/$BaseName/ConcatImputation/${BaseName}_INFOMetricsSummary.tgz ./3_Impute/$BaseName/ConcatImputation/*.snpstat