forked from PaulKnoops/episodicSequenceData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRENAMEME_SYNC2COUNTS.R
More file actions
179 lines (118 loc) · 8.47 KB
/
RENAMEME_SYNC2COUNTS.R
File metadata and controls
179 lines (118 loc) · 8.47 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
# For loop sync to counts
## need next line to call arguments:
args <- commandArgs(trailingOnly = TRUE)
## Packages source code: only need these two for this script (need to be this order)
require('tidyr')
require('dplyr')
## List of positions of interest: keep positions of interest....
## Set working Directory:
setwd(args[1])
## Read in data:
episodic_counts <- fread(args[2])
## Keep rows with Position in list (position == column 2)
## Should all be same below:
name.Columns <- c("Chromosome", "Position", "ref", "ConR1_115", "ConR2_115", "SelR2_115", "SelR1_115", "ConR1_38", "ConR2_38", "SelR1_38", "SelR2_38", "ConR1_77", "ConR2_77", "SelR1_77", "SelR2_77", "SelR1_0")
colnames(episodic_counts) <- name.Columns
#Add "replicates" of ancestor -- all are equal
episodic_counts$SelR2_0 <- episodic_counts$SelR1_0
episodic_counts$ConR1_0 <- episodic_counts$SelR1_0
episodic_counts$ConR2_0 <- episodic_counts$SelR1_0
#Need the ancestor to stay (after making long) to call major/minor alleles later
episodic_counts$Ancestor <- episodic_counts$SelR1_0
# Make long by bring populations down
print("making long")
long_episodic <- gather(episodic_counts, Population, Allele_Freq , ConR1_115:ConR2_0, factor_key=TRUE)
rm(episodic_counts)
print("removed counts")
#Error???
# All geneneric below for sync files (only real issue through file is population naming convention)
###################################################
#Seperate the allele counts into independent columns for each base
print("splitting allele freq")
Episodic_split_2 <- long_episodic %>%
separate(Allele_Freq, c("A","T","C","G","N","del"), ":")
rm(long_episodic)
print("removed long")
#Seperate the ancestor to base later things on
Episodic_split_2 <- Episodic_split_2 %>%
separate(Ancestor, c("A_0","T_0","C_0","G_0","N_0","del_0"), ":")
# as.numeric to multiple columns:
cols.num <- c("A_0", "T_0", "C_0", "G_0", "N_0", "del_0", "A", "T", "C", "G", "N", "del")
#Seems to take a long time for this step?
Episodic_split_2[cols.num] <- sapply(Episodic_split_2[cols.num],as.numeric)
#Get the sum of all the rows (all the different bases) for each population position:
print("getting row sums")
Episodic_split_2$sum <- (rowSums(Episodic_split_2[,11:16]))
#Ancestor Major_Allele and minor allele:
# Major allele of ancestor == the maximum positional count
Episodic_split_2$anc_max <- apply(Episodic_split_2[,4:9], 1, max)
# Minor is the ancestor second highest count
Episodic_split_2$anc_min <- apply(Episodic_split_2[,4:9], 1,
function(x)max(x[x!=max(x)]))
#Major / Minor Base name: match the number of anc_max with the column to call the correct base:
Episodic_split_2 <- within(Episodic_split_2, {
MajorAllele = ifelse(anc_max== Episodic_split_2[,4], "A", ifelse(anc_max== Episodic_split_2[,5], "T", ifelse(anc_max== Episodic_split_2[,6], "C",ifelse(anc_max== Episodic_split_2[,7], "G", ifelse(anc_max== Episodic_split_2[,8], "N", ifelse(anc_max== Episodic_split_2[,9], "del", "N/A" ))))))})
#Major Allele Count of evolved populations; match the Major allele with the count of certain columns for each population
Episodic_split_2 <- within(Episodic_split_2, {
Maj_count = ifelse (MajorAllele == "A", Episodic_split_2[,11], ifelse (MajorAllele == "T", Episodic_split_2[,12], ifelse (MajorAllele == "C", Episodic_split_2[,13], ifelse (MajorAllele == "G", Episodic_split_2[,14], ifelse (MajorAllele == "N", Episodic_split_2[,15], ifelse (MajorAllele == "del", Episodic_split_2[,16], "N/A"))))))})
# Same thing for minor allele: first ensure that if the sum of all counts == the Major coutn and the ancestor had no minor allele, their is no minor allele (N/A), then follow the same match of anc_min to a certain base
Episodic_split_2 <- within(Episodic_split_2, {
MinorAllele = ifelse(Maj_count==Episodic_split_2[,17] & anc_min==0, "N/A", ifelse(anc_min== Episodic_split_2[,4], "A", ifelse(anc_min== Episodic_split_2[,5], "T", ifelse(anc_min== Episodic_split_2[,6], "C",ifelse(anc_min== Episodic_split_2[,7], "G", ifelse(anc_min== Episodic_split_2[,8], "N", ifelse(anc_min== Episodic_split_2[,9], "del", "Z") ))))))})
#Minor Allele Count of the ancestreal minor allele count
Episodic_split_2 <- within(Episodic_split_2, {
Min_count = ifelse (MinorAllele == "A", Episodic_split_2[,11], ifelse (MinorAllele == "T", Episodic_split_2[,12], ifelse (MinorAllele == "C", Episodic_split_2[,13], ifelse (MinorAllele == "G", Episodic_split_2[,14], ifelse (MinorAllele == "N", Episodic_split_2[,15],ifelse (MinorAllele == "del", Episodic_split_2[,16],"N/A"))))))})
print("called major and minor alleles and counts")
# To determine the minor allele base if not specified by the ancestor (new allele brough up etc.)
#max for the population (could be the minor allele)
Episodic_split_2$maj_all <- apply(Episodic_split_2[,11:16], 1, max)
#alt== second highest count for populations
Episodic_split_2$alt_allele <- apply(Episodic_split_2[,11:16], 1,
function(x)max(x[x!=max(x)]))
print("define unknown alleles")
Episodic_split_2 <- within(Episodic_split_2, {
Min_count_2 = ifelse (Maj_count == sum, 0, ifelse(Maj_count==maj_all, alt_allele, maj_all))})
Episodic_split_2 <- within(Episodic_split_2, {
MinorAllele_base = ifelse(Min_count_2==0, "N/A", ifelse(Min_count_2== Episodic_split_2[,11], "A", ifelse(Min_count_2== Episodic_split_2[,12], "T", ifelse(Min_count_2== Episodic_split_2[,13], "C",ifelse(Min_count_2== Episodic_split_2[,14], "G", ifelse(Min_count_2== Episodic_split_2[,15], "N", ifelse(Min_count_2== Episodic_split_2[,16], "del", "Z") ))))))})
# Remove unneeded columns (6,7,8,9,10,11,13,14,15)
Episodic_split_2 <- subset(Episodic_split_2, select = -c(A_0,T_0,C_0,G_0,N_0,del_0,A,T,C,G,N,del,anc_max,anc_min, MinorAllele, Min_count, maj_all, alt_allele))
print("removed unneeded columns")
nam.col <- c("chr", "pos", "ref", "Population", "sum", "MajorAllele", "Major_count", "Minor_count", "MinorAllele")
colnames(Episodic_split_2) <- nam.col
#Remove unneccessary Columns (as needed)
#Keep them all for now (except sum) as may be needed later
#Episodic_split_2 <- subset( Episodic_split_2, select = -ref )
#Episodic_split_2 <- subset( Episodic_split_2, select = -chr)
#Episodic_split_2 <- subset( Episodic_split_2, select = -MajorAllele )
#Episodic_split_2 <- subset( Episodic_split_2, select = -MinorAllele)
Episodic_split_2<- subset( Episodic_split_2, select = -sum)
## Depends on the filter method:
print("begin filtering")
#Filter method: take the sum of each position, and must have at least 5 counts called (i.e over the 16 populations, the total of alleles called for the minor allele must be over 5)
grp <- Episodic_split_2 %>%
group_by(pos) %>%
summarise(sum=sum(Minor_count))
grp2 <- grp[which(grp$sum<=5),]
Episodic_split_2 <- Episodic_split_2[!(Episodic_split_2$pos %in% grp2$pos),]
#check that the number of obs for episodic_long2 == obs for those without 0's sum (*16 for number of "populations") (needed later as well == grp3)
#grp3 <- grp[-which(grp$sum<=5),]
rm(grp)
rm(grp2)
print("remove filter inermediates")
#################################################
#Should be all genetic above (from start specificed)
## Below depends on the population name layout etc. made above
#Split Population into Treatment, Rep, and Generation - need to do twice, different seperators (change above??)
print("seperate population to Treatment, Generation and Cage")
episodic_long <- Episodic_split_2 %>%
separate(Population, c("Treatment", "Generation"), "_")
rm(Episodic_split_2)
episodic_long <- episodic_long %>%
separate(Treatment, c("Treatment", "Cage"), "R")
cols.num <- c("Cage", "Generation", "Major_count", "Minor_count")
episodic_long[cols.num] <- sapply(episodic_long[cols.num],as.numeric)
print("Have final episodic long; now write a csv")
#will need to rename .csv files
write.csv(episodic_long, file=paste(sync, ".csv", sep=""))
print("wrote csv and now done this .sync file")
}
}