-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathima2arlequin.R
149 lines (133 loc) · 4.27 KB
/
ima2arlequin.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#### Initialize ####
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(tidyverse)
library(janitor)
library(xml2)
####USER DEFINED VARS####
# pgd_file <- "Pfalcifer.3.7.Fltr19.popmap.3.7.ppr_genclust.haps2.pgd"
ima_metadata_file <- "Pfalcifer.3.7.Fltr19.popmap.3.7.ppr_genclust.haps2.ima.metadata"
ima_seqs_file <- "Pfalcifer.3.7.Fltr19.popmap.3.7.ppr_genclust.haps2.ima.seqs"
out_file_name <-
str_replace(ima_seqs_file,
"\\.seqs$",
"\\.prearp")
#### READ IN DATA & WRANGLE ####
ima_metadata <-
read_delim(ima_metadata_file,
col_names = FALSE) %>%
######################### make smaller for testing ###################
# filter(X1 == "dDocent_Contig_1018" | X1 == "dDocent_Contig_3003" | X1 == "dDocent_Contig_6699") %>%
rename(locus = X1,
At_LightBlue = X2,
At_DarkBlue = X3,
Kr = X4,
Pk = X5,
St = X6,
bp = X7) %>%
select(-X8:-X9) %>%
# mutate(alleles_per_locus = At_LightBlue + At_DarkBlue + Kr + Pk + St) %>%
mutate(bp = bp -30) %>%
pivot_longer(cols = At_LightBlue:St) %>%
uncount(weights = value)
ima_seqs <-
read_delim(ima_seqs_file,
col_names = FALSE,
################################# make smaller for testing ###########
n_max = nrow(ima_metadata),
skip =5) %>%
select(-X2:-X4) %>%
rename(individual_id = X1,
sequence = X5)
#### CALCULATE HOW MANY BP IN EACH CONTIG TO MAKE MISSING HAPLOTYPES #####
# depricated, the bp from the ima file is generally wrong, so this doesn't work
# locus_na <-
# ima_metadata %>%
# mutate(dummy_seq = strrep("?",
# bp)) %>%
# select(-name) %>%
# distinct() %>%
# pull(dummy_seq) %>%
# as.character() %>%
# as.list()
locus_na <-
bind_cols(ima_metadata,
ima_seqs) %>%
distinct(locus,
sequence) %>%
mutate(bp = nchar(sequence)) %>%
select(-sequence) %>%
distinct() %>%
mutate(dummy_seq = strrep("?",
bp)) %>%
pull(dummy_seq) %>%
as.character() %>%
as.list()
names(locus_na) <-
ima_metadata %>%
pull(locus) %>%
unique()
#### COMBINE DATA INTO PSEUDO ARLEQUIN FORMAT ####
data <-
bind_cols(ima_metadata,
ima_seqs) %>%
select(-bp) %>%
pivot_wider(names_from = locus,
values_from = sequence) %>%
replace_na(locus_na) %>%
mutate(orig_id = str_c(str_sub(name,
1,
2),
"_P",
individual_id,
sep=""),
place_holder = str_sub(individual_id,
-1,
-1)) %>%
select(name,
orig_id,
place_holder,
dDocent_Contig_1018:last_col()) %>%
arrange(name,
orig_id,
place_holder)
#### CHECK BP CALCS, SHOULD BE NO VARIATION WITHIN LOCI ####
data %>%
pivot_longer(cols = starts_with("dDocent_Contig"),
names_to = "contig",
values_to = "sequence") %>%
mutate(bp = nchar(sequence)) %>%
group_by(contig,
name) %>%
summarize(bp_mean = mean(bp,
na.rm=T),
bp_sd = sd(bp,
na.rm=T)) %>%
filter(bp_sd != 0) %>%
ggplot(aes(x=contig,
y=bp_mean,
# color = contig
)) +
geom_point() +
geom_errorbar(aes(ymin=bp_mean-bp_sd,
ymax=bp_mean+bp_sd)) +
labs(title="There should be no contigs in the plot")
# data %>%
# select(name:place_holder,
# dDocent_Contig_2860,
# dDocent_Contig_3455,
# dDocent_Contig_529,
# dDocent_Contig_8858,
# dDocent_Contig_9040,) %>%
# pivot_longer(cols = starts_with("dDocent_Contig"),
# names_to = "contig",
# values_to = "sequence") %>%
# mutate(bp = nchar(sequence)) %>%
# ggplot(aes(x=orig_id,
# y=bp,
# color = contig
# )) +
# geom_point()
#### OUTPUT PSEUDO ARLEQUIN FILE ####
write_delim(data,
out_file_name,
quote="none")