-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathgenetic_format_conversion.sh
More file actions
351 lines (285 loc) · 11.5 KB
/
genetic_format_conversion.sh
File metadata and controls
351 lines (285 loc) · 11.5 KB
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
#!/bin/bash
# Print an error message to stderr and exit with code 1
die() {
echo "ERROR: $*" >&2
exit 1
}
input_file="$1"
input_name="$2"
input_format="$3"
output_format="$4"
if [[ "$input_format" == "vcf" ]] && [[ "$output_format" == "genepop" ]]; then
ploidy="$5"
pl_gl="$6"
exclude_loci="$7"
non_polymorphic="$8"
indels="$9"
regions="${10}"
PHRED_SCALED="${11}"
MISSING_GQ="${12}"
MISSING_DEPTH="${13}"
individuals="${14}"
pop_file="${15}"
indpop="${16}"
elif [[ "$input_format" == "tabular" ]] && [[ "$output_format" == "genepop" ]]; then
sep_char="$5"
digit_num="$6"
elif [[ "$input_format" == "genepop" ]] && [[ "$output_format" == "bayescan" ]]; then
indpop="$5"
[[ -f "$indpop" ]] || die "Input indpop was not found: $indpop"
marker_type="$6"
alleles_coded="$7"
fi
##### Validate inputs #####
# Check that input files exist on disk
[[ -f "$input_file" ]] || die "Input VCF was not found: $input_file"
##### Check output directory #####
readonly output_dir="convert_directory"
readonly pop_dir="pop_affiliation"
if [[ ! -d "${output_dir}" ]]; then
die "ERROR: Failed to create output directory: ${output_dir}"
fi
if [[ ! -d "${pop_dir}" ]]; then
die "ERROR: Failed to create output directory: ${pop_dir}"
fi
##### Build output filename #####
name_without_ext="$(basename -- "$input_name")"
name_without_ext="${name_without_ext%.vcf}"
name_without_ext="${name_without_ext%.txt}"
name_without_ext="${name_without_ext%.tabular}"
# In Galaxy, dataset names may contain a trailing label in parentheses,
# e.g. "Tool name (dataset 42)". Extract the content inside the last
# parentheses if present; otherwise use the full name.
regex='\(([^)]+)\)[[:space:]]*$'
if [[ "$name_without_ext" =~ $regex ]]; then
base_name="${BASH_REMATCH[1]}"
else
base_name="$name_without_ext"
fi
[[ -n "$base_name" ]] || die "Could not derive a valid output filename from: $input_name"
###############################################################################
#Function: spid_vcf2genepop
#Description: Creation of the spid file required to convert vcf on genepop
###############################################################################
spid_vcf2genepop(){
local ploidy="$1"
local pl_gl="$2"
local exclude_loci="$3"
local non_polymorphic="$4"
local indels="$5"
local regions="$6"
local PHRED_SCALED="$7"
local MISSING_GQ="$8"
local MISSING_DEPTH="$9"
local individuals="${10}"
local indpop="${11}"
local pop_file="${12}"
cat > "spid_file.spid" << EOF
# spid-file generated: $(date '+%a %b %d %H:%M:%S %Z %Y')
# VCF Parser questions
PARSER_FORMAT=VCF
# Only output SNPs with a phred-scaled quality of at least:
VCF_PARSER_QUAL_QUESTION=${PHRED_SCALED}
# Include a file with population definitions:
VCF_PARSER_POP_FILE_QUESTION=${indpop}
# What is the ploidy of the data? (DIPLOID/HAPLOID)
VCF_PARSER_PLOIDY_QUESTION=${ploidy}
# Do you want to include a file with population definitions?
VCF_PARSER_POP_QUESTION=${pop_file}
# Output genotypes as missing if the phred-scale genotype quality is below:
VCF_PARSER_GTQUAL_QUESTION=${MISSING_GQ}
# Do you want to include INDELS as STANDARD genetic markers? (TRUE/FALSE)
VCF_PARSER_INDEL_QUESTION=${indels}
# Do you want to include non-polymorphic SNPs? (TRUE/FALSE)
VCF_PARSER_MONOMORPHIC_QUESTION=${non_polymorphic}
# Only output following individuals (ind1, ind2, ind4, ...):
VCF_PARSER_IND_QUESTION=${individuals}
# Only input following regions (refSeqName:start:end, multiple regions: whitespace separated):
VCF_PARSER_REGION_QUESTION=${regions}
# Output genotypes as missing if the read depth of a position for the sample is below:
VCF_PARSER_READ_QUESTION=${MISSING_DEPTH}
# Take most likely genotype if "PL" or "GL" is given in the genotype field? (TRUE/FALSE)
VCF_PARSER_PL_QUESTION=${pl_gl}
# Do you want to exclude loci with only missing data? (TRUE/FALSE)
VCF_PARSER_EXC_MISSING_LOCI_QUESTION=${exclude_loci}
# GENEPOP Writer questions
WRITER_FORMAT=GENEPOP
# Specify which data type should be included in the GENEPOP file (GENEPOP can only analyze one data type per file): (MICROSAT/STANDARD/SNP/DNA)
GENEPOP_WRITER_DATA_TYPE_QUESTION=SNP
# If SNP data are encoded as nucleotides, enter the integers that code for nucleotide A, T, C, G (comma separated, e.g 1,2,3,4):
GENEPOP_WRITER_SNP_CODE_QUESTION=
EOF
}
###############################################################################
#Function: spid_snp2bayes
#Description: Creation of the spid file required to convert ssr on genepop
###############################################################################
spid_genepop2bayes(){
local marker_type="$1"
local alleles_coded="$2"
cat > "spid_file.spid" << EOF
# spid-file generated: $(date '+%a %b %d %H:%M:%S %Z %Y')
# GENEPOP Parser questions
PARSER_FORMAT=GENEPOP
# Select the type of the data: (MICROSAT/SNP/STANDARD)
GENEPOP_PARSER_DATA_TYPE_QUESTION=$marker_type
# How are Microsat alleles coded? (REPEATS/LENGTH)
GENEPOP_PARSER_MICROSAT_CODING_QUESTION=$alleles_coded
# GESTE / BayeScan Writer questions
WRITER_FORMAT=GESTE_BAYE_SCAN
# Specify which data type should be included in the GESTE / BayeScan file (GESTE / BayeScan can only analyze one data type per file): (MICROSAT/SNP/STANDARD/DNA)
GESTE_BAYE_SCAN_WRITER_DATA_TYPE_QUESTION=$marker_type
EOF
}
##########################################################
#Fonction : ssr2genepop
# Description :
##########################################################
ssr2genepop() {
local input_file="$1"
local input_name="$2"
local output_file="$3"
local sep_char="$4"
local digit_num="$5"
local width=$((digit_num*2))
case "$sep_char" in
TAB) sep_char=$'\t' ;;
COMMA) sep_char=',' ;;
SEMICOLON) sep_char=';' ;;
WHITESPACE) sep_char=' ' ;;
*)
echo "ERROR: Unknown separator: '$sep_char'" >&2
exit 1
;;
esac
echo "=== SEP_CHAR ===" >&2
echo "sep_char: '${sep_char}'" >&2
printf "sep_char hex: " >&2
echo -n "${sep_char}" | xxd | head -1 >&2
echo "=== PREMIÈRE LIGNE DU FICHIER ===" >&2
head -1 "$input" | cat -A >&2
printf "hex: " >&2
head -1 "$input" | xxd | head -3 >&2
# Fonction pour parser un génotype
parse_genotype() {
local genotype="$1"
# Management of missing data
if [[ -z "$genotype" ]] || [[ "$genotype" =~ ^(0|NA|N/A|-|\.)$ ]]; then
printf "%0*d" "$width" 0
return
fi
# Allele separation
local allele1 allele2
#rajouter vérification de la présence de / sinon break code
IFS="/" read -r allele1 allele2 <<< "$genotype" #allele are separated by '/'
# Cleaning
allele1=$(echo "$allele1" | tr -d '[:space:]')
allele2=$(echo "$allele2" | tr -d '[:space:]')
# Check that they are coded with numbers
if ! [[ "$allele1" =~ ^[0-9]+$ ]] || ! [[ "$allele2" =~ ^[0-9]+$ ]]; then
printf "%0*d" "$width" 0 #Convert to missing data if they aren't numbers
return
fi
printf "%0${digit_num}d%0${digit_num}d" "$allele1" "$allele2"
}
local line_num=0
local -a loci_names
declare -A populations
declare -A pop_individuals
declare -A pop_genotypes
while IFS= read -r line; do
line_num=$((line_num + 1))
# Ignore empty lines
[[ -z "$line" ]] && continue
# First line = header
if [[ $line_num -eq 1 ]]; then
IFS="$sep_char" read -ra fields <<< "$line"
# Check header (ind_col=0 pop_col=1)
if [[ "${fields[0]}" != "Ind" ]] || [[ "${fields[1]}" != "Pop" ]]; then
echo "Ind and Pop column not found"
return 1
fi
# Loci after pop_col (2)
for ((i=2; i<${#fields[@]}; i++)); do
loci_names+=("${fields[$i]}")
done
continue
fi
# Data rows
IFS="$sep_char" read -ra fields <<< "$line"
#Check presence of almost 3 columns (Ind Pop and 1 genotype)
if [[ ${#fields[@]} -lt 3 ]]; then
continue
fi
# [0]=Ind, [1]=Pop, [2+]=Genotypes
local ind_name="${fields[0]}"
local pop_name="${fields[1]}"
# Keep population
populations[$pop_name]=1
pop_individuals[$pop_name]+="$ind_name "
# Convert genotype (col 2+)
local genepop_line=""
for ((i=2; i<${#fields[@]}; i++)); do
local genotype="${fields[$i]}"
local converted=$(parse_genotype "$genotype")
genepop_line+="$converted "
done
pop_genotypes["${pop_name}__${ind_name}"]="$genepop_line"
done < "$input_file"
# Write genepop file
{
echo "SSR data: ${input_name}"
for locus in "${loci_names[@]}"; do
echo "$locus"
done
for pop_name in $(printf '%s\n' "${!populations[@]}" | sort); do
echo "Pop"
for ind_name in ${pop_individuals[$pop_name]}; do
local genotypes="${pop_genotypes[${pop_name}__${ind_name}]}"
echo "$ind_name , $genotypes"
done
done
} > "$output_file"
return 0
}
#########################################
# Main execution
#########################################
if [ "$input_format" = "vcf" ] && [ "$output_format" = "genepop" ]; then
output_genfile="${output_dir}/${base_name}_genepop.txt"
spid_vcf2genepop "$ploidy" "$pl_gl" "$exclude_loci" "$non_polymorphic" "$indels" "$regions" "$PHRED_SCALED" "$MISSING_GQ" "$MISSING_DEPTH" "$individuals" "$indpop" "$pop_file"
PGDSpider2-cli -inputfile "$input_file" -inputformat VCF -outputfile "$output_genfile" -outputformat GENEPOP -spid spid_file.spid
elif [ "$input_format" = "genepop" ] && [ "$output_format" = "bayescan" ]; then
##### Retrieve pop order #####
pop_order_file="${pop_dir}/pop_affiliation.txt"
in_pop=false
first_ind=false
while IFS= read -r line; do
line="${line%$'\r'}" # remove windows end lines
if [[ "${line,,}" == "pop" ]]; then
in_pop=true
first_ind=true
continue
fi
if [[ "$in_pop" == true && "$first_ind" == true && -n "$line" ]]; then
pop_count=$((pop_count + 1))
ind_id=$(echo "$line" | cut -d',' -f1 | xargs)
actual_pop_name=$(awk -v id="$ind_id" '{ gsub(/\r/, ""); if ($1 == id) print $2 }' "$indpop")
if [[ -z "$actual_pop_name" ]]; then
actual_pop_name="Unknown_Pop_${pop_count}"
fi
echo -e "${actual_pop_name}\t${pop_count}" >> "$pop_order_file"
first_ind=false
fi
done < "$input_file"
##### Bayes conversion #####
output_genfile="${output_dir}/${base_name}_bayes.txt"
spid_genepop2bayes "$marker_type" "$alleles_coded"
PGDSpider2-cli -inputfile "$input_file" -inputformat GENEPOP -outputfile "$output_genfile" -outputformat GESTE_BAYE_SCAN -spid spid_file.spid
elif [ "$input_format" = "tabular" ] && [ "$output_format" = "genepop" ]; then
output_genfile="${output_dir}/${base_name}_genepop.txt"
ssr2genepop "$input_file" "$base_name" "$output_genfile" "$sep_char" "$digit_num"
else
echo "Unrecognised combination of parameters"
exit 1
fi