-
Notifications
You must be signed in to change notification settings - Fork 1
/
genind.R
83 lines (60 loc) · 2.69 KB
/
genind.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
## ------------------------------------------------------------------------- ##
# --------------- format individual names in genind object ----------------- #
library(adegenet)
# arguments: gen (genind object), file (genepop file path, as "../file.gen")
# new adegenet package formats individual names
# genind.ind.names <- function(gen, file)
# {
# # Count the number of loci in the file
# num.loci <- length(locNames(gen))
#
# # read in genepop file as table, (skip header, list of loci, first 'POP')
# ind.tab <- read.table(file, skip = num.loci+2, fill=TRUE)
#
# # Remove remaining 'POP' lines (rows) in table
# ind.tab <- subset(ind.tab, V1 !='POP')
#
# # Return the column of individual names as list of characters
# inds <- ind.tab$V1
#
# # Remove the trailing comma
# inds <- gsub(',', '', inds)
#
# # assign new list of individual (genotype) names to genind object
# indNames(gen) <- inds
# }
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# -------------------- filter loci from genind object ---------------------- #
# arguments: gen (genind object), removeloc (vector with loci to be removed)
genind.rem.loci <- function(gen, removeloc)
{
all_loci <- locNames(gen)
keeploc <- base::setdiff(all_loci, removeloc)
gen <- gen[loc = keeploc]
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# --------------- filter individuals from genind object -------------------- #
# arguments: gen (genind object), removeInd (vector with individuals to be removed)
gen.ind.rem.Ind <- function(gen, removeInd)
{
gen <- gen[!row.names(gen@tab) %in% removeInd]
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ----------------- load Sample meta data as strata ------------------------- #
# arguments: gen (genind object), SampleInfo (data frame with meta data)
load.strata <- function(gen, SampleInfo){
Inds <- as.data.frame(indNames(gen)) %>%
rename(LIB_ID = `indNames(gen)`)
Inds$LIB_ID <- as.character(Inds$LIB_ID)
GTF_Strata <- left_join(Inds, SampleInfo) %>%
distinct()
strata(gen) <- GTF_Strata
gen
}
## ------------------------------------------------------------------------- ##
## ------------------------------------------------------------------------- ##
# ------------------------------ function ---------------------------------- #
## ------------------------------------------------------------------------- ##