library(xtable)
goAnnotationAbbreviations <- function( protein.accessions, go.type.annos,
go.type.anno.space, unknown.anno.abbrev='0', go.type.abbrevs=list(
'biological_process'='B', 'cellular_component'='C',
'molecular_function'='M' ) ) {
# Generates abbreviations for Gene Ontology term annotations, so that i.e. in
# the plotted phylogenetic tree each protein accession can be suffixed with a
# six character text in which each two characters consist of the abbreviated
# GO type followed by the annotated indexed unique GO terms. I.e.: 'B2CøM2'.
#
# Args:
# protein.accessions : accessions of the proteins whose annotations shall
# be abbreviated.
# go.type.annos : The result of calling goTypeAnnotationMatrices(
# retrieveUniprotAnnotations( homolog.accessions ), … )
# go.type.anno.space : The result of calling goAnnotationSpaceList(
# go.type.annos, … )
# unknown.anno.abbrev : The string to be used to abbreviate the annotation
# 'unknown'. Default: 'ø'
# go.type.abbrevs : The abbreviations usd for each GO type, i.e. 'B' for
# 'biological_process'
#
# Returns: A named list which contains a list of abbreviated GO annotations
# for each protein accession in anno.mtrx.
#
setNames(
lapply( protein.accessions, function( acc ) {
setNames(
lapply( names( go.type.anno.space ), function( go.type ) {
paste( go.type.abbrevs[[ go.type ]],
if ( acc %in% colnames( go.type.annos[[ go.type ]] ) ) {
anno <- go.type.annos[[ go.type ]][[ 'GO', acc ]]
which( as.logical(
lapply( go.type.anno.space[[ go.type ]], identical, anno )
) )
} else {
unknown.anno.abbrev
},
sep=''
)
}),
names( go.type.anno.space )
)
}),
protein.accessions
)
}
goAnnotationSpaceAbbreviations <- function( go.anno.space,
abbrev.colnames=c( 'Abbreviation', 'Annotated GO terms' ),
go.type.abbrevs=list( 'biological_process'='B', 'cellular_component'='C',
'molecular_function'='M' ), go.con=connectToGeneOntology(),
close.db.connection=TRUE
) {
# Generates a table for each Gene Ontology term type mapping the unique
# annotations to an index. This is meant to be the caption for a PhyloFun
# plotted phylogenetic tree using abbreviated GO term annotations obtained by
# calling goAnnotationAbbreviations(…).
#
# Args:
# go.anno.space : A list of uniquely annotated GO terms for each GO term
# type. Generated by function goAnnotationSpaceList(…).
# abbrev.colnames : The column names for each of the abbreviation tables.
# go.type.abbrevs : The abbreviations used for each GO term type.
# go.con : Database connection to Gene Ontology
# close.db.connection : Set to TRUE the Database Connection to the Gene
# Ontology is automatically closed before this function
# returns.
#
# Returns: A list of abbreviation tables, one for each GO term type.
#
gas <- go.anno.space[ as.logical( lapply( names( go.anno.space ),
function(x) { ! is.null( go.anno.space[[x]] ) } ) ) ]
rslt <- setNames(
lapply( names( gas ), function( go.type ) {
gt.anno.space <- go.anno.space[[ go.type ]]
max.ind <- length( gt.anno.space )
abbrevs <- paste( go.type.abbrevs[[ go.type ]],
as.character( 1:max.ind ), sep=''
)
gt.annos <- as.character(
lapply( go.anno.space[[ go.type ]], function( go.terms ) {
gt.tbl <- goTermsForAccessionWithLevel( go.terms, con=go.con )
if ( ! is.null( gt.tbl ) ) {
toString( lapply( go.terms, function( go.term ) {
paste( go.term, '(',
gt.tbl[ gt.tbl[ , 'acc' ] == go.term, 'name' ][[ 1 ]], ')'
)
} ) )
} else
toString( go.terms )
} )
)
matrix( c( abbrevs, gt.annos ),
nrow=max.ind, dimnames=list( c(), abbrev.colnames )
)
}),
names( gas )
)
if ( close.db.connection )
dbDisconnect( go.con )
rslt
}
plotPhyloFunTree <- function( query.accession, phyl.tree, query.predictions,
homolog.go.type.annos, plot.path, device.funk=png, width=2000,
height.per.leaf=13, min.height=480 ) {
# Plots the phylogenetic tree to a device specified by 'device.funk'. Width
# and Height are adjusted to tree's size. GO term annotations are abbreviated
# and each tip is appended with its respective abbreviations, one for each
# Gene Ontology term type.
#
# Args:
# query.accession : The query proteins accession
# phyl.tree : An object of class phylo representing the
# phylogenetic tree
# query.predictions : The PhyloFun GO term predictions
# homolog.go.type.annos : The GO annotations passed to PhyloFun in order to
# annotate the Query
# plot.path : The valid file path to the plot to generate
# device.funk : The function used to redirect the plot to, default
# 'png'. Could be set to jpeg, pdf…
# width : The width in pixel
# height.per.leaf : The height of the plot is this ( argument *
# number.of.tree.tips ).
# min.height : If above product is smaller than this minimum
# height, this minimum will be used instead.
# Default: 480px
#
# Returns: A list with two entries. 'caption' holds GO term annotation
# abbreviations as returned by 'goAnnotationAbbreviations(…)' and
# 'tree.with.abbrevs' is the phylogenetic tree where the tip labels have
# their abbreviated GO annotations appended.
#
# Generate a merged GO annotation matrix for both the query and its homologs.
# One matrix for each GO type:
all.go.type.annos <- mergeQueryPredictionsAndHomologAnnotations(
query.accession, query.predictions, homolog.go.type.annos )
go.type.anno.space <- goAnnotationSpaceList( all.go.type.annos,
unknown.annot=NULL )
# Abbreviate each proteins GO annotation one two character string for each GO
# type:
anno.abbrevs <- as.character( lapply(
goAnnotationAbbreviations( phyl.tree$tip.label, all.go.type.annos,
go.type.anno.space ), paste, collapse=',' ) )
# Append above abbreviations to the proteins' accessions in the phylogenetic
# tree:
phyl.tree$tip.label <- paste( phyl.tree$tip.label, anno.abbrevs )
hgt <- height.per.leaf * length( phyl.tree$tip.label )
device.funk( file=plot.path, width=width,
height= ( if ( hgt < min.height ) min.height else hgt ) )
plot( phyl.tree, show.node.label=T )
axisPhylo()
dev.off()
# Generate the plot's caption, that is map the abbreviations to the complete
# annotations:
list(
'caption'=goAnnotationSpaceAbbreviations( go.type.anno.space ),
'tree.with.abbrevs'=phyl.tree
)
}
htmlReport <- function( rel.path.2.phyl.tree.plot, query.accession,
anno.abbrevs, path.2.html.report ) {
brew( text=REPORT.TEMPLATE, output=path.2.html.report )
}
REPORT.TEMPLATE <-
'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PhyloFun report for <%= query.accession %></title>
<style type="text/css">
.table {
min-width: 820px;
width: 820px;
border: 1px solid #e6e6e6;
border-collapse: collapse;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
color: #171717;
font-size: 9pt;
}
.table td, .table th, .tablescroll_head th {
padding: 0.5em;
font-size: 9pt;
border-bottom: 1px solid #e6e6e6;
}
.table th, .tablescroll_head th {
background-color: #e6e6e6;
}
.table td {
border-right: 1px solid #e6e6e6;
}
</style>
</head>
<body>
<div id="phylogenetic_tree_div">
<img id="phylogenetic_tree_img" src="<%= rel.path.2.phyl.tree.plot %>" class="phyl_tree_img" />
<div id="phyl_tree_caption_div" class="phyl_tree_caption">
Phylogenetic maximum likelihood tree for <%= query.accession %>. Node
labels are Shimodaira-Hasegawa local support values.
</div>
<div id="function_annotation_abbrevs_div">
<% for ( go.type in names( anno.abbrevs ) ) { %>
<p>
<h4><%= go.type %></h4>
<% print( xtable( anno.abbrevs[[ go.type ]] ), "html" ) %>
</p>
<% } %>
</div>
</div>
</body>
</html>
'
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.