-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_remove_contaminants.R
84 lines (61 loc) · 2.72 KB
/
02_remove_contaminants.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
# ---------------------------------------------------------------------------------
#
# In this script: I REMOVE the CONTAMINANTS
# which are 5 features (ASVs) that we have identified as
# likely to be contaminants based on presence/absence and
# BLSTn matches (info about source or environment)
#
# ---------------------------------------------------------------------------------
source("01_find_contaminants.R")
# Import the table that I have BLASTn-ed and annotated ------------------
contaminants <- read_csv("./out/Gfas_16S/find_contaminants/potential_contaminants_GP_MZ_GP_simplified_for_R.csv")
# Clean it up to have a list of the sequences to remove
contaminants <- contaminants %>%
select(Feature, Sequence, Frequency, Final_decision) %>%
drop_na() %>%
filter(Final_decision == "remove") %>%
select(-Final_decision) %>%
rename(ASV_id = Feature,
count_overall = Frequency)
# Write .csv file for import in Qiime2
write_csv(contaminants,
"./out/Gfas_16S/find_contaminants/contaminants_features_sequences.csv")
# Remove CONTAMINANTS ----------------------------------------
decont <- contaminants %>%
select(ASV_id) %>%
anti_join(all_data, ., by = "ASV_id")
# Check how many features have been removed (ASV_id) - should be 5 .....................
all_data$ASV_id %>% unique() %>% length() # 526 -
contaminants$ASV_id %>% unique() %>% length() # 5 =
decont$ASV_id %>% unique() %>% length() # 521 :D
# Check how many reads have been removed ...............................................
all_data$count_sample %>% sum() # 133892 -
contaminants$count_overall %>% sum() # 18990 =
decont$count_sample %>% sum() # 114902
sum(all_data$count_sample) - sum(contaminants$count_overall) == sum(decont$count_sample)
# # TRUE all good :D
# So now we have removed the contaminants and we can proceed
# Let's clean up a bit of memory ... keep only 'decont'
rm(list = setdiff(ls(), c("decont", "metadata"))) #
# Add RELATIVE ABUNDANCE -------------------------------------------
decont <- decont %>%
group_by(sample_id) %>%
mutate(rel_abund = count_sample / sum(count_sample)) %>%
relocate(rel_abund, .after = count_sample) %>%
ungroup()
# Check that rel_abund sums up to a tot of 1
decont %>%
group_by(sample_id) %>%
summarise(tot_abund = sum(rel_abund)) #%>% view()
# all good :D
# Make LONG -------------------------------------------------
decont_long <- decont %>%
pivot_longer(
cols = c("Kingdom", "Phylum", "Class", "Order", "Family", "uncX_FAM",
"Genus", "Species", "ASV_id"),
names_to = "taxon_level",
values_to = "taxon_name"
)
rm(decont)
# Ok, so now the data is ready to be plotted!
# just source this code in the next scripts ;)