OTU table Wri+ng an R func+on Execute the new func+on

<=525$-.$>*,$/(51/9$./-0/-$12.3$
4??@A$
!"#$%&$'()*+$,"-"$./-0/-$12.3$
4))356$-2"*(1.23"7.*(6$8/"9)-:$
;.*-2.9$$
•!BC'$-"#95&$
wf_da/uclust_picked_otus/rep_set/pynast_aligned_seqs/
rdp_assigned_taxonomy/otu_table/seqs_otu_table.txt!
•!@"00)*+$>95&$
Fasting_Map.txt!
•!C255$>95&$
wf_da/uclust_picked_otus/rep_set/pynast_aligned_seqs/
fasttree_phylogeny/seqs_rep_set.tre!
BC'$-"#95$
O
T
U
ID!
C
M
0
1_
1
6
W!
C
M
0
1_
2
6
W!
"! P
M
1
2_
I!
D$
D$
D$
E$ D$ "Root;Bacteria;Firmicutes;""Clostridia"";Clostridiales;""Lachnospiraceae"""$
%$
D$
D$
E$ D$ Root;Bacteria;Bacteroidetes;Bacteroidetes;Bacteroidales$
F$
D$
D$
E$ D$ Root;Bacteria;Firmicutes;""Clostridia"";Clostridiales;""Lachnospiraceae"””!
G#(.9/-5$"#/*,"*;5$-.$259"7H5$
"#/*,"*;5$
Consensus Lineage!
I5;"/(5$-=5$*/3#52$.1$(58/5*;5($1.2$5";=$("3095$)($,)J525*-6$K5$*55,$-.$
(-"*,"2,)L5$5";=$("3095$-.$3"M5$-=53$;.30"2"#95N$C=5$3.(-$*"-/2"9$*.23"9)L"7.*$
)($-.$+.$12.3$"#(.9/-5$"#/*,"*;5$-.$259"7H5$"#/*,"*;5(N$O.$5";=$*/3#52$)*$-=5$BC'$
-"#95$K)99$25025(5*-$-=5$02.0.27.*$.1$(58/5*;5($12.3$-="-$("3095($#59.*+)*+$-.$-="-$
BC'N$
d = scale(d, center=F, scale=colSums(d))!
d = t(d)!
d = read.table(’otu_table.txt', header=T, row.names=1, sep='\t')!
taxa.names = d$Consensus.Lineage!
d = as.matrix(d[,-dim(d)[2]])!
<2)7*+$"*$P$1/*;7.*$
•!
•!
•!
•!
G$1/*;7.*$)($"*$P$.#Q5;-$-="-$;"*$#5$;"995,$R5S5;/-5,T$
U/*;7.*$="($)*0/-(&$0"2"35-52($
U/*;7.*$25-/2*($"*$./-0/-&$H"9/5$
G$H"9/5$)($5)-=52$-=2./+=$5S09);)-$return ;.33"*,$
.2$#:$,51"/9-$-=5$H"9/5$.1$-=5$9"(-$1/*;7.*$;"995,$12.3$
K)-=)*$-="-$1/*;7.*$
extract.name.level = function(x, level){!
!a=c(unlist(strsplit(x,';')),'Other')!
!paste(a[1:min(level,length(a))],collapse=';')!
}!
AS5;/-5$-=5$*5K$1/*;7.*$
V"99$-=5$1/*;7.*$-="-$K5$="H5$Q/(-$;25"-5,$"*,$(55$K="-$-=5$
25(/9-($"25N$
a = as.character(d$Consensus.Lineage[1])!
a!
extract.name.level(a, 2)!
extract.name.level(a, 3)!
extract.name.level(a, 5)!
U/*;7.*$-.$(/33"2)L5$,"-"$"-$,)J525*-$-"S.*.3);$95H59($
otu2taxonomy = function(x, level, taxa=NULL){!
!if(is.null(taxa)){!
!!taxa = colnames(x)!
!}!
!if(length(taxa)!=dim(x)[2]){!
!!print("ERROR: taxonomy should have the same length
as the number of columns in OTU table")!
!!return;!
!}!
!level.names = sapply(as.character(taxa), !
!!function(x)!
!! !extract.name.level(x,level=level))!
!t(apply(x, 1, !
!!function(y) !
!! !tapply(y,level.names,sum)))!
}!
O/33"2)L5$-=5$BC'$-"#95$
AS09.25$-=5$BC'$-"#95(6$(/33"2)L5,$"-$,)J525*-$-"S.*.3);$95H59($#:$
2/**)*+$-=5$1.99.K)*+$;.33"*,(N$
W.K$3"*:$X=:9"6$V9"((5(6$B2,52(6$U"3)9)5(6$Y5*52"6$BC'($"25$
25025(5*-5,$)*$-=5$,"-"(5-Z$W)*-&$/(5$dim 1/*;7.*N$$
d.genus = otu2taxonomy(d,level=7,taxa=taxa.names)!
d.family = otu2taxonomy(d,level=6,taxa=taxa.names)!
d.order = otu2taxonomy(d,level=5,taxa=taxa.names)!
d.class = otu2taxonomy(d,level=4,taxa=taxa.names)!
d.phylum = otu2taxonomy(d,level=3,taxa=taxa.names)!
@"-;=)*+$/0$-=5$2.K($.1$-=5$3"00)*+$
>95$K)-=$-=5$BC'$-"#95$
@"00)*+$>95$
#$%&'()*! "!
+,($-%(.-!
/(.0(,!
1((2!
345$64.! 7489(!
"!
V@D%[V$
E$
V$
@$
\D$
V5;"9$
V%$
E$
V@DF[V$
E$
V$
@$
\D$
V5;"9$
VF$
E$
V@D\[V$
E$
V$
@$
\D$
V5;"9$
V\$
E$
X@%%[?$
E$
X$
@$
\D$
?95"9$
X%%$
E$
X@%F[?$
E$
X$
@$
\D$
?95"9$
X%F$
E$
ff = read.table('mapping.txt', header=T)!
P/*$-=5$1.99.K)*+$;.33"*,(&$
ff$SampleID!
rownames(d.phylum)!
].7;5$-="-$-=5$2.K($)*$-=5$BC'$-"#95$"25$)*$"$,)J525*-$.2,52$
R(.2-5,$"90="*/352);"99:T$-=5*$)*$-=5$3"00)*+$-"#95N$C=5$
1.99.K)*+$;.33"*,$.2,52($-=5$2.K($.1$-=5$3"00)*+$-"#95&$
ff=ff[order(ff$SampleID),]!
V=5;M$-="-$-=5$2.K($*.K$3"-;=$/0&$
ff$SampleID == rownames(d.phylum)!
O/#(5^*+$-=5$,"-"$
# select columns by name!
vars = c("Root;Bacteria;Bacteroidetes", !
! !"Root;Bacteria;Firmicutes", !
! !"Root;Bacteria;Proteobacteria")!
d.phylum[,vars]!
# select columns by number!
cnum = c(1,3,6)!
d.phylum[,cnum]!
# dropping columns!
d.phylum[,!colnames(d.phylum) %in% vars]!
# selecting/dropping rows!
d.phylum[1:3,]!
d.phylum[-(1:90),]!
# subset function!
week16fecal = subset(d.phylum, ff$Location ==
‘Fecal’ & ff$Week == 16, select = vars)!
I"();$09.^*+&$5S09.25$.*$:./2$.K*$
plot(week16fecal)!
week16map = subset(ff, ff$Location == 'Fecal' &
ff$Week == 16)!
plot(week16fecal ~ week16map$Treatment)!
plot(week16fecal[,1] ~ week16map$Treatment,
main=colnames(week16fecal)[1],
xlab='Treatment', ylab='Relative abundance
(fraction of the total)')!
plot(week16fecal[,2] ~ week16map$Treatment, main
= colnames(week16fecal)[2], xlab='Treatment',
ylab='Relative abundance (fraction of the
total)')!
X9.^*+$R;.*-_,T$
X9.^*+$R;.*-_,T$
# two plots on one figure!
par(mfrow=c(1,2))!
#saving the plot!
pdf(file='bf.pdf')!
par(mfrow=c(1,2))!
plot(week16fecal[,1] ~ week16map
$Treatment, main = colnames(week16fecal)
[1], xlab='Treatment', ylab='Relative
abundance (fraction of the total)')!
plot(week16fecal[,1] ~ week16map$Treatment, main
= colnames(week16fecal)[1], xlab='Treatment',
ylab='Relative abundance (fraction of the
total)')!
plot(week16fecal[,2] ~ week16map
$Treatment, main = colnames(week16fecal)
[2], xlab='Treatment', ylab='Relative
abundance (fraction of the total)')!
plot(week16fecal[,2] ~ week16map$Treatment, main
= colnames(week16fecal)[2], xlab='Treatment',
ylab='Relative abundance (fraction of the
total)')!
dev.off()!
W5"-3"0($
W5"-3"0($R1";-.2($"($(),5$;.9.2(T$
# heatmaps!
ma.phylum = subset(d.phylum, ff
$Location=='Fecal', colMeans(d.phylum) >
0.01)!
ma.ff = subset(ff, ff$Location=='Fecal')!
heatmap.2(ma.phylum, dendrogram="both",
scale="col", col=redgreen(75), distfun =
function(x) dist(x, method='euclidean'),
key=T, tracecol=NULL,
RowSideColors=rainbow(2)
[as.integer(ma.ff$Treatment)])!
library(gplots)!
heatmap.2(ma.phylum, dendrogram="both",
scale="col", col=redgreen(75), distfun =
function(x) dist(x, method='euclidean'),
key=T, tracecol=NULL,
RowSideColors=rainbow(3)
[as.integer(factor(ma.ff$Week))])!
heatmap.2(ma.phylum, dendrogram="both",
scale="col", col=redgreen(75), distfun =
function(x) dist(x, method='euclidean'),
key=T, tracecol=NULL)!
B*$:./2$.K*$
•! '()*+$09.-RT$"*,$=5"-3"0NFRT$H)(/"99:$5S09.25$
-=5$,"-"$"-$"$,)J525*-$-"S.*.3);$95H59$RV9"((6$
B2,526$U"3)9:$.2$X=:9/3T$
•! C)0&$U.;/($.*9:$.*$=)+=9:$"#/*,"*-$-"S"$R`%a6$
`baT$
'(51/9$;.33"*,(&$>*,$=590$0"+5($.*$
-=5(5$"*,$/(5$1.2$5S09.2)*+$-=5$,"-"$
•!
•!
•!
•!
•!
•!
•!
;.9*"35(RT6$2.K*"35(RT$
,)3RT$
3"SRT6$3)*RT$
35"*RT6$(,RT$
(;"95RT$
#.S09.-RT$
=5"-3"0NFRT$