-
Notifications
You must be signed in to change notification settings - Fork 0
/
completeWorkflow
222 lines (148 loc) · 8.74 KB
/
completeWorkflow
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
###############Data preparation VCF filtering######################
##Filter VCF by intergenic bed file
bcftools view -I -O z Schil_capture_variants_Chr1-12.reheader.filtered.vcf.gz \
-R ../bedFiles/intergenic_regions.bed -o Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.vcf.gz
##Filter VCF by high depth intergenic regions
awk '$6=="yes"' intergenic_regions.bed > intergenic_regions_goodDP.bed
bcftools view -I -O z Schil_capture_variants_Chr1-12.reheader.filtered.vcf.gz \
-R ../bedFiles/intergenic_regions_goodDP.bed -o Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.vcf.gz
#Filter SI VCF by captus BED regions
#######################Decompose complex variants################################
zcat Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.vcf.gz | \
vcfallelicprimitives > Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.decomposedVariants.vcf
bgzip Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.decomposedVariants.vcf
zcat VCFs/Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.vcf.gz | \
vcfallelicprimitives > finalWorkflow/VCFs/Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.vcf
bgzip finalWorkflow/VCFs/Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.vcf
#######################Create VCFs with no indels###############################
vcftools --gzvcf Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.vcf.gz \
--remove-indels --out Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.noIndels --recode-INFO-all --recode
bgzip Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.noIndels.recode.vcf
bcftools index Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.noIndels.recode.vcf.gz
######################Create files to measure VCF stats and apply filtering parameters based on results###########################
VCF=../VCFs/Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.decomposedVariants.noIndels.recode.vcf.gz;
OUT=intergenic.goodDP;
vcftools --gzvcf $VCF --freq2 --out $OUT --max-alleles 2;
vcftools --gzvcf $VCF --depth --out $OUT;
vcftools --gzvcf $VCF --site-mean-depth --out $OUT;
vcftools --gzvcf $VCF --site-quality --out $OUT;
vcftools --gzvcf $VCF --missing-indv --out $OUT;
vcftools --gzvcf $VCF --missing-site --out $OUT;
vcftools --gzvcf $VCF --het --out $OUT;
#######################Filter VCF based on R calculated stats###################################
##No MAF
vcftools --gzvcf Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.decomposedVariants.noIndels.recode.vcf.gz \
--remove-indels --minQ 30 --minDP 10 --max-missing 0.98 --out intergenicSNPs.noMAF --recode-INFO-all --recode
bcftools view -s ^Spenn_LA0716_ERR418107,\
S.chiCGN15532_1_ERR418097,\
S.chiCGN15530_1_ERR418098,\
Sper_LA1278_ERR418084,\
Sper_LA1954_ERR418094 intergenicSNPs.noMAF.recode.vcf > intergenicSNPs.noMAF.noOutgroup.vcf
##No singletons
vcftools --gzvcf Schil_capture_variants_Chr1-12.reheader.filtered.intergenic.goodDP.decomposedVariants.noIndels.recode.vcf.gz --remove-indels \
--minQ 30 --mac 2 --minDP 10 --max-missing 0.98 --out intergenicSNPs.noSingletons --recode-INFO-all --recode
bcftools view -s ^Spenn_LA0716_ERR418107,\
S.chiCGN15532_1_ERR418097,\
S.chiCGN15530_1_ERR418098,\
Sper_LA1278_ERR418084,\
Sper_LA1954_ERR418094 intergenicSNPs.noSingletons.recode.vcf > intergenicSNPs.noSingletons.noOutgroup.vcf
##Same filtering for SI genes
vcftools --gzvcf Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.noIndels.recode.vcf.gz \
--remove-indels --minQ 30 --minDP 10 --max-missing 0.98 --out SISNPs.noMAF --recode-INFO-all --recode
bcftools view -s ^Spenn_LA0716_ERR418107,\
S.chiCGN15532_1_ERR418097,\
S.chiCGN15530_1_ERR418098,\
Sper_LA1278_ERR418084,\
Sper_LA1954_ERR418094 SISNPs.noMAF.recode.vcf > SISNPs.noMAF.noOutgroup.vcf
##No singletons
vcftools --gzvcf Schil_capture_variants_Chr1-12.reheader.filtered.SIgenes.decomposedVariants.noIndels.recode.vcf.gz --remove-indels \
--minQ 30 --mac 2 --minDP 10 --max-missing 0.98 --out SISNPs.noSingletons --recode-INFO-all --recode
bcftools view -s ^Spenn_LA0716_ERR418107,\
S.chiCGN15532_1_ERR418097,\
S.chiCGN15530_1_ERR418098,\
Sper_LA1278_ERR418084,\
Sper_LA1954_ERR418094 SISNPs.noSingletons.recode.vcf > SISNPs.noSingletons.noOutgroup.vcf
##Intergenic plink
plink --vcf intergenicSNPs.noSingletons.noOutgroup.vcf --indep-pairwise 50 5 0.5 --const-fid --allow-extra-chr \
--recode vcf --out pruned.intergenicSNPs.noSingletons.noOutgroup --set-missing-var-ids @:#
## Edit VCF header to simplify sample names for visualization
bcftools query -l pruned.intergenicSNPs.noSingletons.noOutgroup.vcf > sampleIDlist.txt
#Edit txt in VIM
%s/\(0_\)\(.*\)/\1\2\t\2/
#Reheader in VCF
bcftools reheader -s sampleIDlist.txt -o pruned.intergenicSNPs.noSingletons.noOutgroup.rename.vcf pruned.intergenicSNPs.noSingletons.noOutgroup.vcf
##SI plink
plink --vcf SISNPs.noSingletons.noOutgroup.vcf --indep-pairwise 50 5 0.5 --const-fid --allow-extra-chr \
--recode vcf --out pruned.SISNPs.noSingletons.noOutgroup --set-missing-var-ids @:#
#Reheader in VCF
bcftools reheader -s sampleIDlist.txt -o pruned.SISNPs.noSingletons.noOutgroup.rename.vcf pruned.SISNPs.noSingletons.noOutgroup.vcf
####################Print region files from bed files in ANGSD format#############################
##Integenic regions
awk '{print $1":"$2"-"$3}' intergenic_regions_goodDP.bed > ../angsd/intergenic_regions_file.txt
##SI regions
awk '{print $1":"$2"-"$3}' captusSIgenes_150flanking.bed > ../angsd/SI_regions_file.txt
##CDS regions
awk '{print $1":"$2"-"$3}' SIgeneCDS.bed > ../finalWorkflow/angsd/CDS_regions_file.txt
####################Print population names list##################################
awk '{print $2}' populationDefinitions.txt | sort | uniq > populationNames.txt
####################Print bam file list for each population#######################
for i in `cat populationNames.txt`;
do pop=`echo "$i"`; awk -v var="$pop" '$2~var {print "/data/proj2/popgen/chilense/capture/06_map_calling/"$1"_prop-map.bam";}' \
populationDefinitions.txt > ${i}_accessions.txt ;
done
####################Create ancestral fasta#######################################
angsd -i /data/proj2/popgen/chilense/capture/06_map_calling/Spenn_LA0716_ERR418107_prop-map.bam -doFasta 1 -doCounts 1 -out ancestralState
###################SFS calculation for loop for all populations in central valleys#######################
for i in `cat ../populations/populationNamesCentralValleys.txt`; do
POPULATION=`echo "$i"`;
angsd -gl 1 -dosaf 1 -anc ancestralState.fa.gz -minMapQ 1 -minQ 20 -out $POPULATION \
-ref ../../../S_chilense_reference_rename.fasta \
-bam ../populations/${POPULATION}_accessions.txt -rf contigNames_ChrOnly_regions_file.txt -P 24;
done
##################Pairwise 2D SFS for all possible combinations using array loop#######################################
##Populate array with population names
mapfile -t populationArray < ../populations/populationNamesCentralValleys.txt
##Calculate the 2dsfs prior pairwise for all combinations using array elements in nested for loop
for ((i = 0; i < ${#populationArray[@]}; i++)); do
for ((j = i + 1; j < ${#populationArray[@]}; j++)); do
realSFS ${populationArray[i]}.saf.idx ${populationArray[j]}.saf.idx -P 24 > 2DSFS/${populationArray[i]}.${populationArray[j]}.ml
done;
done
##"Prepare the fst for easy window analysis etc"
cd 2Didx
for ((i = 0; i < ${#populationArray[@]}; i++)); do
for ((j = i + 1; j < ${#populationArray[@]}; j++)); do
realSFS fst index ../${populationArray[i]}.saf.idx ../${populationArray[j]}.saf.idx -whichFst 1 -sfs \
../2DSFS/${populationArray[i]}.${populationArray[j]}.ml \
-fstout ${populationArray[i]}.${populationArray[j]}
done;
done
##################Run FST per region for all pairwise combinations####################
cd 2Didx
##Intergenic regions
for j in `ls *.idx`; do
PAIR=`basename -s .fst.idx $j`;
for i in `cat ../intergenic_regions_file.txt`; do
REGION=`echo "$i"`;
realSFS fst stats ${j} -r $REGION | \
grep -o "[[:digit:]].[[:digit:]]*[[:space:]][[:digit:]].[[:digit:]]*" >> ../intergenicFST/${PAIR}.region.intergenic.FST;
done
done
##SI regions
for j in `ls *.idx`; do
PAIR=`basename -s .fst.idx $j`;
for i in `cat ../SI_gene_regions_file.txt`; do
REGION=`echo "$i"`;
realSFS fst stats ${j} -r $REGION | \
grep -o "[[:digit:]].[[:digit:]]*[[:space:]][[:digit:]].[[:digit:]]*" >> ../SIFST/${PAIR}.region.SI.FST;
done
done
##CDS regions
for j in `ls *.idx`; do
PAIR=`basename -s .fst.idx $j`;
for i in `cat ../CDS_regions_file.txt`; do
REGION=`echo "$i"`;
realSFS fst stats ${j} -r $REGION | \
grep -o "[[:digit:]].[[:digit:]]*[[:space:]][[:digit:]].[[:digit:]]*" >> ../CDSFST/${PAIR}.region.CDS.FST;
done
done