├── README.md ├── plotTree.R ├── plotTree.py ├── plotTreeShiny ├── plotTree.R ├── server.R └── ui.R ├── server_modified ├── server.R └── ui.R ├── shiny_practice ├── print_statemtn.r ├── reactive │ ├── plot.pdf │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── reactive_stable │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── runPlotTree.download │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── runPlotTree │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── runPlotTree_conditional │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── runPlotTree_conditional_clustering │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── runPlotTree_tabs │ ├── plotTree.R │ ├── server.R │ └── ui.R ├── uploadTree2 │ ├── server.R │ └── ui.R ├── variable_relationships.txt └── wan │ ├── server.R │ └── ui.R └── tree_example_april2015 ├── .Rhistory ├── alleles.csv ├── bar.csv ├── blocks.csv ├── blocks.txt ├── info.csv ├── info.pdf ├── info.png ├── myplot.pdf ├── myplot4.pdf ├── pan.csv ├── pan.pdf ├── pan.png ├── res_genes.csv ├── res_genes.pdf ├── res_genes.png ├── tree.nwk └── tree_examples.txt /README.md: -------------------------------------------------------------------------------- 1 | Plotting trees with data using R and Python 2 | ==== 3 | [R code - plotTree.R](#r-code) 4 | 5 | [Python code (using ete2) - plotTree.py](#python-code) 6 | 7 | This is rough code that we use sometimes for making figures in my lab (http://holtlab.net). 8 | 9 | There will definitely be bugs and there's lots more features one could add, and probably many ways these features could be implemented differently/better. 10 | 11 | So basically, I am putting this up here to share the love and save other people's time messing around with R, Python and ETE2, and so that others can use the code as the basis for their own functions or to learn how to do various handy things with these packages. If you find it useful and expand on it - please share! 12 | 13 | Kat Holt - @DrKatHolt - http://holtlab.net 14 | 15 | R code 16 | == 17 | 18 | # plotTree.R 19 | 20 | This is R code for plotting a phylogenetic tree and annotating the leaves with various information, including: 21 | - colouring tips according to some variable (provided in infoFile; CSV format with column 1 = tip names) 22 | - printing columns of text next to the leaves (provided in infoFile; CSV format with column 1 = tip names) 23 | - printing heatmaps of data (provided in heatmapData; numerical data in CSV format with column 1 = tip names) 24 | - printing horizontal bar graphs next to the tips (provided in barData; numerical data in CSV format with column 1 = tip names) 25 | - printing the location of SNPs (snpFile; allele data in CSV format with row 1 = tip names; SNPs relative to reference, either column 1 or a specified strain) 26 | - printing the location of genome blocks (blockFile; tab delimited file with col 1 = tip name, col 2 = start, col 3 = stop) 27 | 28 | There are also options to: 29 | - cluster the heatmap data using any method available in hclust 30 | - perform ancestral discrete trait reconstruction using ace and plot the results as pie graphs on each node of the tree 31 | 32 | # Overview 33 | 34 | 1) Prepare your tree file (to be passed to the function via tree="tree.nwk") 35 | -- 36 | - newick format 37 | - no hashes in the strain names (trees can't be read into R if they have hashes) 38 | - alternatively, this can be an R object of the class 'phylo' (can convert hclust object to this format using as.phylo()) 39 | 40 | 2) Prepare your data files 41 | -- 42 | - you can provide any or all of strain info, data to be plotted as a heatmap, data to be plotted as a bar chart, snp allele table in the order: tree | info | heatmap | barplot | SNPS/blocks 43 | - CSV format, one row per strain (EXCEPT SNP allele table or blocks files, which contain coordinates of SNPs and blocks, see above) 44 | - column 1 contains strain names, precisely matching those in the tree file 45 | - row 1 contains variable names 46 | - alternatively, these can be R objects of class 'matrix' or 'data.frame' 47 | 48 | 3) Optional input data files (any combination can be provided, but they will always be plotted in this order across the page, with the tree on the left): 49 | -- 50 | (i) Strain info / metadata (to be passed to the function via infoFile="info.csv") 51 | - the values in the columns will be printed (in columns) next to the tree 52 | - optionally, if you have lots of columns and only want to print some of them, you can specify the names of the columns to print using infoCols=c("variable1","variable2"); otherwise all columns will be printed 53 | - optionally, if you want to colour the tree tips according to the value of one of the data columns, specify the name of the variable via colourNodesBy="variable"; you can also perform ancestral trait reconstruction on this variable and plot the results as pie graphs, to turn this on use ancestral.reconstruction=T 54 | 55 | (ii) Numeric values to plot as a heatmap (to be passed to the function via heatmapData="data.csv") 56 | 57 | (iii) One column of numeric values to plot as a barplot (to be passed to the function via barData="bar.csv") 58 | 59 | (iv) SNP allele table (to be passed to the function via snpFile="alleles.csv") 60 | will plotted to indicate the position of SNPs in each strain, where SNPs are defined as differences COMPARED TO THE ALLELES IN COLUMN 1. So, your alleles in column one should be the inferred ancestral alleles (e.g. those of an outgroup). 61 | - note you need to specify the total size of the genome in base pairs, to set up the appropriate X-axis; set using genome_size 62 | - unknown alleles or gaps should not be plotted as SNPs compared to the ancestral; the gap character is assumed to be '?', but often this will be '-' if your data comes direct from the mapping pipe, so you will need to change to gapChar="-" 63 | 64 | (v) Blocks file (to be passed to the function via blockFile="blocksByStrain.txt") 65 | - note you need to specify the total size of the genome in base pairs, to set up the appropriate X-axis; set using genome_size 66 | 67 | Tree plotting function 68 | -- 69 | p <- plotTree(tree="tree.nwk",heatmapData="data.csv",infoFile="info.csv",barData="bar.csv",snpFile="alleles.csv", blockFile="blocksByStrain.txt") 70 | 71 | 72 | Optionally, output to PDF: 73 | -- 74 | outputPDF="out.pdf" 75 | 76 | (specify width in inches via w=X, specify height in inches via h=X) 77 | 78 | OR output to PNG: 79 | 80 | outputPNG="out.png" 81 | 82 | (specify width in pixels via w=X, specify height in pixels via h=X) 83 | 84 | Spacing options 85 | -- 86 | You can provide any or all of strain info, data to be plotted as a heatmap, data to be plotted as a bar chart, SNPs and/or blocks. 87 | 88 | The order will be: 89 | 90 | [ tree | info | heatmap | barplot | SNPs/blocks] 91 | 92 | • Relative widths of the components can be changed in the function; by default they are: 93 | 94 | 95 | left & right spacing framing the whole page: edgeWidth = 1 96 | 97 | tree plotting space: treeWidth = 10 98 | 99 | info printing space: infoWidth = 10 100 | 101 | heatmap printing space: dataWidth = 30 102 | 103 | barplot plotting space: barDataWidth = 10 104 | 105 | SNP/blocks plotting space: blockPlotWidth = 10 106 | 107 | • Relative heights of the components can be changed in the function; by default they are: 108 | 109 | height of plotting spaces: mainHeight = 100 110 | 111 | top & bottom spacing: labelHeight = 10 112 | 113 | - if heatmap provided, this will be the height of the area in which the column names are printed above the heatmap; otherwise the top edge height will be taken from edgeWidth 114 | 115 | - if barplot provided, this will be the height of the area in which the x-axis is printed below the barplot; otherwise the bottom edge height will be taken from edgeWidth 116 | 117 | 118 | 119 | Tree options 120 | -- 121 | (see ?plot.phylo in R for more info) 122 | 123 | • tip.labels = T turns on printing strain names at the tips 124 | 125 | • tipLabelSize = 1 change the size of printed strain names (only relevant if tip.labels=T) 126 | 127 | • offset=0 change the spacing between the end of the tip and the printed strain name (only relevant if tip.labels=T) 128 | 129 | • tip.colour.cex=0.5 change the size of the coloured circles at the tips (only relevant if infoFile is provided AND colourNodesBy is specified) 130 | 131 | • tipColours = c("blue","red","black") specify colours to use for colouring nodes (otherwise will use rainbow(n)). RColourBrewer palettes are a good option to help with colour selection 132 | 133 | • lwd=1.5 change the line width of tree branches 134 | 135 | • edge.color="black" change the colour of the tree branches 136 | 137 | • axis=F,axisPos=3 add and position an axis for branch lengths 138 | 139 | 140 | 141 | Info options 142 | -- 143 | • colourNodesBy = "column name" colour the nodes according to the discrete values in this column. additional options: 144 | 145 | - legend=T, legend.pos="bottomleft" plot legend of node colour values, specify location (possible values: "topleft","topright","bottomleft" or "bottomright") 146 | 147 | - ancestral.reconstruction=T reconstruct ancestral states for this discrete variable, results will be returned as $mat and plotted as pie graphs on the tree 148 | 149 | • infoCex=0.8 Change the size of the printed text 150 | 151 | 152 | Heatmap options 153 | -- 154 | • heatmap.colours= 155 | 156 | - if not specified, uses white -> black 157 | 158 | - colorRampPalette is a good option, eg: 159 | 160 | heatmap.colours=colorRampPalette(c("white","yellow","blue"),space="rgb")(100) 161 | 162 | - note the legend/scale will be plotted above the tree 163 | 164 | • colLabelCex=0.8 change the size of the column labels 165 | 166 | • cluster Cluster matrix columns? (Default is no clustering.) 167 | 168 | - Set cluster=T to use default hclust clustering method ("ward.D"), or specify a different method to pass to hclust (see ?hclust for options). 169 | 170 | - Alternatively, if you have a square matrix (i.e. strain x strain) and you want to order columns the same as rows to keep it square, set cluster="square" 171 | 172 | 173 | Barplot options 174 | -- 175 | • barDataCol=2 Colour for the barplot (can be numeric, 1=black, 2=red, etc; or text, "red", "black", etc) 176 | 177 | 178 | SNP plot options 179 | -- 180 | • genome_size Sets the length of the x-axis that represents the length of the genome. This is REQUIRED when plotting SNPs/blocks. 181 | 182 | • gapChar="-" Character used to indicate gaps/unknown alleles in the SNP file (will not be counted as SNPs). 183 | 184 | • snp_colour Sets the colour of the lines indicating SNPs (default is red) 185 | 186 | 187 | Block plot options 188 | -- 189 | • genome_size Sets the length of the x-axis that represents the length of the genome. This is REQUIRED when plotting SNPs/blocks. 190 | 191 | • block_colour Sets the colour of the lines indicating blocks (default is black). Blocks are drawn after SNPs, so may obscure SNPs. 192 | 193 | • blwd Sets the height of the lines indicating blocks (default is 5). 194 | 195 | Ancestral trait reconstruction 196 | -- 197 | To perform ancestral discrete trait reconstruction using ace, and plot the results as pie graphs on each node of the tree: 198 | 199 | (i) specify the variable in the infoFile that you want to analyse: colourNodesBy="Variable_name" 200 | 201 | (ii) set ancestral.reconstruction = T 202 | 203 | (iii) to change the size of the pie graphs, change pie.cex (default value is 0.5) 204 | 205 | Outputs 206 | -- 207 | Primary output is the rendered tree figure (in the R drawing device or in a PDF/PNG file if specified) 208 | The plotTree() function also returns an R object with the following: 209 | 210 | $info: infoFile input file, re-ordered as per tree 211 | 212 | $anc: result of ancestral discrete trait reconstruction using ace 213 | 214 | $mat: heatmap data file, with rows re-ordered as per tree and columns re-ordered as per clustering (if cluster=T) 215 | 216 | $strain_order: order of leaves in the tree 217 | 218 | 219 | # Examples 220 | 221 | Data (trees and tables) used in this example are available in the subdirectory /tree_example_april2015 222 | 223 | Basic strain info 224 | --- 225 | Plot tree, colour tips by city of isolation, specify colours for each city manually, print strain details as table next to tree. 226 | 227 | v <- plotTree(tree="tree.nwk",ancestral.reconstruction=F,tip.colour.cex=1,cluster=T,tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=10,infoWidth=10,infoCols=c("name","location","year")) 228 | 229 | ![](tree_example_april2015/info.png?raw=true) 230 | 231 | Pan genome heatmap 232 | --- 233 | Plot tree, colour tips by location (as above), cluster a gene content matrix and plot as heatmap next to the tree (white = 0% coverage of gene, black = 100% coverage of the gene). 234 | 235 | v <- plotTree(tree="tree.nwk",heatmapData="pan.csv",ancestral.reconstruction=F,tip.colour.cex=1,cluster=T,tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=5,dataWidth=20,infoCols=NA) 236 | 237 | ![](tree_example_april2015/pan.png?raw=true) 238 | 239 | Curated genes, coloured 240 | --- 241 | Plot tree, colour tips by location (as above), plot curated resistance gene information next to the tree as a heatmap... 242 | 243 | Here the gene information in the heatmapData file is coded so that 0 represents absence, and different numbers are used to indicate presence of each gene/variant (e.g. in the gyrA column, one mutation is coded as 2 and the other is coded as 4). 244 | 245 | We then specify which colour to use for each number, using heatmap.colours... here 0 (ie absent) is white; 2 (ie gyrA mutant 1) is "seagreen3"; 4 (ie gyrA mutant 2) is "darkgreen", etc etc. 246 | 247 | heatmap.colours=c("white","grey","seagreen3","darkgreen","green","brown","tan","red","orange","pink","magenta","purple","blue","skyblue3","blue","skyblue2") 248 | 249 | v <- plotTree(tree="tree.nwk",heatmapData="res_genes.csv",ancestral.reconstruction=F,tip.colour.cex=1,cluster=F,heatmap.colours=c("white","grey","seagreen3","darkgreen","green","brown","tan","red","orange","pink","magenta","purple","blue","skyblue3","blue","skyblue2"),tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=10,dataWidth=10,infoCols=c("name","year"),infoWidth=8) 250 | 251 | ![](tree_example_april2015/res_genes.png?raw=true) 252 | 253 | 254 | Python code 255 | == 256 | 257 | # plotTree.py 258 | 259 | This is a Python script, that uses the Python package ETE2 (see http://ete.cgenomics.org/) which in turn requires pyqt4 and numpy. If you have BioPython then you will already have numpy. To install the rest, follow these steps: 260 | 261 | 1. Download sip from http://www.riverbankcomputing.com/software/sip/download 262 | 263 | 2. Unpack it (tar -xzvf sip.tar.gz) and cd into the directory (cd sip...) 264 | 265 | 3. Run these commands: 266 | 267 | python configure.py -d /Library/Python/2.7/site-packages --arch x86_64 268 | 269 | make 270 | 271 | sudo make install 272 | 273 | 4. Download the Qt4 binary from http://qt.nokia.com/downloads/sdk-mac-os-cpp 274 | 275 | 5. download PyQt4 from http://pyqt.sourceforge.net/Docs/PyQt4/installation.html 276 | 277 | 6. unpack it and cd into the directory 278 | 279 | 7. Run these commands: 280 | 281 | python configure.py -q /usr/bin/qmake-4.8 -d /Library/Python/2.7/site-packages/ --use-arch x86_64 282 | 283 | make 284 | 285 | sudo make install 286 | 287 | 8. Now you can install ETE2 288 | 289 | sudo easy_install -U ete2 290 | 291 | To use ETE2 on your own, see http://ete.cgenomics.org/ 292 | 293 | Running on contagion or merri 294 | 295 | The script will work on a remote server, but ONLY IF you add "-Y" to the ssh login command. Eg: 296 | 297 | ssh -Y you@server.com 298 | 299 | 300 | plotTree.py - Options 301 | == 302 | 303 | # Required inputs 304 | 305 | --output tree.pdf 306 | 307 | - Name of output file 308 | - must end in .pdf or .png 309 | - the file type for the ouput figure is taken from this extension 310 | 311 | --tree tree.nwk 312 | 313 | - Tree file (newick format) 314 | 315 | --info info.csv 316 | 317 | - Data file in CSV format; column 1 matches leaf names, other columns contain data for labelling or plotting. 318 | 319 | Labelling and colour options (text or categorical values) 320 | == 321 | # (i) Print values directly as columns of text 322 | --labels LABELS [LABELS ...] 323 | 324 | - labels to print as text columns next to the tree (must match column headers in the info file) 325 | 326 | --tags 327 | 328 | - Switch on colour labelling of backgrounds to indicate values 329 | 330 | --padding PADDING 331 | 332 | - padding between label columns (pixels, default 20) 333 | 334 | # (ii) Colour the leaf nodes 335 | --colour_nodes_by COLOUR_NODES_BY 336 | 337 | - label to use for colouring nodes 338 | 339 | --node_size NODE_SIZE 340 | 341 | -size for node shapes (default 10) 342 | 343 | # (iii) Use colour blocks to represent column values 344 | 345 | --colour_tags COLOUR_TAGS [COLOUR_TAGS ...] 346 | 347 | - labels to use to tag each element by colour code 348 | 349 | # (iv) Specify colours directly (otherwise expanded ColorBrewer Set1 is used) 350 | 351 | --colour_dict COLOUR_DICT 352 | 353 | - manually specify dictionary of values -> colours 354 | 355 | Data matrix options (numerical data) 356 | == 357 | 358 | --data DATA 359 | 360 | - Data matrix (tab delmited; header row starts with "#Names col1 col2 etc...", column 1 matches leaf names, other columns should be numerical values for plotting) 361 | 362 | --data_type DATA_TYPE 363 | 364 | - Type of data plot ([heatmap], line_profiles, bars, cbars) 365 | - See ete2 docs for options 366 | 367 | --data_width DATA_WIDTH 368 | 369 | Total width of data plot for each strain (mm, default 200) 370 | 371 | --data_height DATA_HEIGHT 372 | 373 | 374 | - Total height of data plot for each strain (mm, default 20) 375 | 376 | --mindata MINDATA Minimum data value for plotting scale (-1) 377 | 378 | --maxdata MAXDATA Maximum data value for plotting scale (1) 379 | 380 | --centervalue CENTERVALUE 381 | 382 | - Central data value for plotting scale (0) 383 | 384 | Tree formatting options 385 | == 386 | --midpoint Midpoint root the tree 387 | 388 | --outgroup OUTGROUP Outgroup to root the tree 389 | 390 | --no_ladderize Switch off ladderizing 391 | 392 | --show_leaf_names Print leaf names as well as labels 393 | 394 | --branch_support Print branch supports 395 | 396 | --no_guiding_lines Turn off linking nodes to data with guiding lines 397 | 398 | --fan Plot tree as fan dendrogram 399 | 400 | --length_scale LENGTH_SCALE scale (pixels per branch length unit) 401 | 402 | --branch_padding BRANCH_PADDING branch (pixels between each branch, ie vertical padding) 403 | 404 | Branch supports 405 | == 406 | --branch_support_print Print branch supports 407 | 408 | --branch_support_colour Colour branch leading to node by branch supports (scale is 0=red -> 100=black) 409 | 410 | --branch_support_cutoff BRANCH_SUPPORT_CUTOFF 411 | 412 | - Colour branches with support lower than this value (= cutoff = black) 413 | 414 | Colour branches or clade backgrounds by value of descendant leaves 415 | == 416 | --colour_branches_by COLOUR_BRANCHES_BY variable to use for colouring branches 417 | 418 | --colour_backgrounds_by COLOUR_BACKGROUNDS_BY variable to use for colouring clade backgrounds 419 | 420 | Plotting options 421 | == 422 | --title TITLE Title for plot 423 | 424 | --width WIDTH width of output image pile (mm, default 200) 425 | 426 | --interactive Switch on interactive view (after printing tree to file) 427 | 428 | 429 | plotTree script - examples 430 | == 431 | COMING SOON 432 | -------------------------------------------------------------------------------- /plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,ladderise=NULL,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE, CHOOSE LADDERISATION OR NOT, AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | if (is.null(ladderise)) 86 | { 87 | tl<-t 88 | } 89 | else if (ladderise=="descending") 90 | { 91 | tl<-ladderize(t, T) 92 | } 93 | else if (ladderise=="ascending") 94 | { 95 | tl<-ladderize(t, F) 96 | } 97 | else if (!is.null(ladderise)) 98 | { 99 | print("Ladderise option should be exactly 'ascending' or 'descending'. Any other command will raise this error. Leave option empty to order branches as per input tree.") 100 | } 101 | tips<-tl$edge[,2] 102 | tip.order<-tips[tips<=length(tl$tip.label)] 103 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 104 | 105 | 106 | # PREPARE HEATMAP DATA 107 | if (!is.null(heatmapData)) { 108 | 109 | # read heatmap data and convert to data frame 110 | x<-readMatrix(heatmapData) 111 | 112 | # order rows of heatmap matrix to match tree 113 | y.ordered<-x[tip.label.order,] 114 | 115 | # reorder columns? 116 | if (!is.null(cluster)) { 117 | if (!(cluster==FALSE)) { 118 | 119 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 120 | # order columns to match row order 121 | original_order<-1:nrow(x) 122 | names(original_order)<-rownames(x) 123 | reordered<-original_order[tip.label.order] 124 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 125 | } 126 | 127 | else { 128 | # cluster columns 129 | if (cluster==TRUE) {cluster="ward"} # set default clustering algorithm 130 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 131 | y.ordered<-y.ordered[,h$order] 132 | } 133 | 134 | }} # finished reordering columns 135 | 136 | } # finished setting up heatmap data 137 | 138 | 139 | # PREPARE BAR PLOT 140 | if (!is.null(barData)) { 141 | b<-readMatrix(barData) 142 | barData<-b[,1] 143 | names(barData)<-rownames(b) 144 | } 145 | 146 | # PREPARE INFO TO PRINT 147 | if (!is.null(infoFile)) { 148 | info<-readMatrix(infoFile) 149 | info.ordered<-info[rev(tip.label.order),] 150 | } 151 | else {info.ordered=NULL} 152 | 153 | 154 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 155 | ancestral=NULL 156 | nodeColourSuccess=NULL 157 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 158 | 159 | if (colourNodesBy %in% colnames(info.ordered)) { 160 | nodeColourSuccess = TRUE 161 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 162 | 163 | # assign values 164 | tipLabelSet <- character(length(loc1)) 165 | names(tipLabelSet) <- rownames(info.ordered) 166 | groups<-table(loc1,exclude="") 167 | n<-length(groups) 168 | groupNames<-names(groups) 169 | 170 | # set colours 171 | if (is.null(tipColours)){ colours<-rainbow(n) } 172 | else{ colours<-tipColours } 173 | 174 | # assign colours based on values 175 | for (i in 1:n) { 176 | g<-groupNames[i] 177 | tipLabelSet[loc1==g]<-colours[i] 178 | } 179 | tipLabelSet <- tipLabelSet[tl$tip] 180 | 181 | # ancestral reconstruction 182 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 183 | 184 | }} 185 | # finished with trait labels and ancestral reconstruction 186 | 187 | 188 | # OPEN EXTERNAL DEVICE FOR DRAWING 189 | # open PDF for drawing 190 | if (!is.null(outputPDF)) { 191 | pdf(width=w,height=h,file=outputPDF) 192 | } 193 | # open PNG for drawing 194 | if (!is.null(outputPNG)) { 195 | png(width=w,height=h,file=outputPNG) 196 | } 197 | 198 | 199 | # SET UP LAYOUT FOR PLOTTING 200 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 201 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 202 | layout(l$m, widths=l$w, heights=l$h) 203 | 204 | 205 | # PLOT TREE 206 | par(mar=rep(0,4)) 207 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 208 | 209 | # colour by trait 210 | if (!is.null(nodeColourSuccess)) { 211 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 212 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 213 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 214 | } 215 | 216 | if (axis) { axisPhylo(axisPos) } 217 | 218 | # PLOT INFO 219 | if (!is.null(infoFile)) { # info is provided 220 | 221 | printCols = TRUE 222 | if (!is.null(infoCols)) { 223 | if (is.na(infoCols)) { 224 | printCols = FALSE 225 | }} 226 | 227 | if (printCols) { 228 | 229 | par(mar=rep(0,4)) 230 | 231 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 232 | else { infoColNumbers = 1:ncol(info.ordered)} 233 | 234 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 235 | 236 | # plot all info columns 237 | for (i in 1:length(infoColNumbers)) { 238 | j<-infoColNumbers[i] 239 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 240 | } 241 | 242 | } 243 | } 244 | 245 | 246 | # PLOT HEATMAP 247 | if (!is.null(heatmapData)) { 248 | 249 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 250 | 251 | # plot heatmap 252 | par(mar=rep(0,4), xpd=TRUE) 253 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 254 | 255 | # draw vertical lines over heatmap 256 | if (!is.null(vlines.heatmap)) { 257 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 258 | } 259 | 260 | # overlay blocks on heatmap 261 | if (!is.null(heatmap.blocks)) { 262 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 263 | } 264 | 265 | 266 | # data labels for heatmap 267 | par(mar=rep(0,4)) 268 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 269 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 270 | 271 | # scale for heatmap 272 | par(mar=c(2,0,0,2)) 273 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 274 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 275 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 276 | } 277 | 278 | # BARPLOT 279 | if (!is.null(barData)) { 280 | par(mar=rep(0,4)) 281 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 282 | 283 | # scale for barData plot 284 | par(mar=c(2,0,0,0)) 285 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 286 | } 287 | 288 | # SNPS AND RECOMBINATION BLOCKS 289 | if (doBlocks) { 290 | par(mar=rep(0,4)) 291 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 292 | 293 | # plot snps 294 | if (!is.null(snpFile)) { 295 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 296 | snps_strainCols <- snps[1,] # column names = strain names 297 | snps<-snps[-1,] # drop strain names 298 | 299 | for (strain in tip.label.order){ 300 | # print SNPs compared to ancestral alleles in column 1 301 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 302 | y <- which(tip.label.order==strain) 303 | if (length(s)>0) { 304 | for (x in s) { 305 | points(x,y,pch="|",col=snp_colour,cex=0.25) 306 | } 307 | } 308 | } 309 | } 310 | 311 | # plot blocks 312 | if (!is.null(blockFile)){ 313 | blocks<-read.delim(blockFile,header=F) 314 | for (i in 1:nrow(blocks)) { 315 | if (as.character(blocks[i,1]) %in% tip.label.order) { 316 | y <- which(tip.label.order==as.character(blocks[i,1])) 317 | x1 <- blocks[i,2] 318 | x2 <- blocks[i,3] 319 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 320 | } 321 | } 322 | } 323 | 324 | } # finished with SNPs and recomb blocks 325 | 326 | # CLOSE EXTERNAL DRAWING DEVICE 327 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 328 | dev.off() 329 | } 330 | 331 | # RETURN ordered info and ancestral reconstruction object 332 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 333 | else {mat=NULL} 334 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 335 | } 336 | -------------------------------------------------------------------------------- /plotTreeShiny/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,ladderise=NULL,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE, CHOOSE LADDERISATION OR NOT, AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | if (is.null(ladderise)) 86 | { 87 | tl<-t 88 | } 89 | else if (ladderise=="descending") 90 | { 91 | tl<-ladderize(t, T) 92 | } 93 | else if (ladderise=="ascending") 94 | { 95 | tl<-ladderize(t, F) 96 | } 97 | else if (!is.null(ladderise)) 98 | { 99 | print("Ladderise option should be exactly 'ascending' or 'descending'. Any other command will raise this error. Leave option empty to order branches as per input tree.") 100 | } 101 | tips<-tl$edge[,2] 102 | tip.order<-tips[tips<=length(tl$tip.label)] 103 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 104 | 105 | 106 | # PREPARE HEATMAP DATA 107 | if (!is.null(heatmapData)) { 108 | 109 | # read heatmap data and convert to data frame 110 | x<-readMatrix(heatmapData) 111 | 112 | # order rows of heatmap matrix to match tree 113 | y.ordered<-x[tip.label.order,] 114 | 115 | # reorder columns? 116 | if (!is.null(cluster)) { 117 | if (!(cluster==FALSE)) { 118 | 119 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 120 | # order columns to match row order 121 | original_order<-1:nrow(x) 122 | names(original_order)<-rownames(x) 123 | reordered<-original_order[tip.label.order] 124 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 125 | } 126 | 127 | else { 128 | # cluster columns 129 | if (cluster==TRUE) {cluster="ward.D2"} # set default clustering algorithm 130 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 131 | y.ordered<-y.ordered[,h$order] 132 | } 133 | 134 | }} # finished reordering columns 135 | 136 | } # finished setting up heatmap data 137 | 138 | 139 | # PREPARE BAR PLOT 140 | if (!is.null(barData)) { 141 | b<-readMatrix(barData) 142 | barData<-b[,1] 143 | names(barData)<-rownames(b) 144 | } 145 | 146 | # PREPARE INFO TO PRINT 147 | if (!is.null(infoFile)) { 148 | info<-readMatrix(infoFile) 149 | info.ordered<-info[rev(tip.label.order),] 150 | } 151 | else {info.ordered=NULL} 152 | 153 | 154 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 155 | ancestral=NULL 156 | nodeColourSuccess=NULL 157 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 158 | 159 | if (colourNodesBy %in% colnames(info.ordered)) { 160 | nodeColourSuccess = TRUE 161 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 162 | 163 | # assign values 164 | tipLabelSet <- character(length(loc1)) 165 | names(tipLabelSet) <- rownames(info.ordered) 166 | groups<-table(loc1,exclude="") 167 | n<-length(groups) 168 | groupNames<-names(groups) 169 | 170 | # set colours 171 | if (is.null(tipColours)){ colours<-rainbow(n) } 172 | else{ colours<-tipColours } 173 | 174 | # assign colours based on values 175 | for (i in 1:n) { 176 | g<-groupNames[i] 177 | tipLabelSet[loc1==g]<-colours[i] 178 | } 179 | tipLabelSet <- tipLabelSet[tl$tip] 180 | 181 | # ancestral reconstruction 182 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 183 | 184 | }} 185 | # finished with trait labels and ancestral reconstruction 186 | 187 | 188 | # OPEN EXTERNAL DEVICE FOR DRAWING 189 | # open PDF for drawing 190 | if (!is.null(outputPDF)) { 191 | pdf(width=w,height=h,file=outputPDF) 192 | } 193 | # open PNG for drawing 194 | if (!is.null(outputPNG)) { 195 | png(width=w,height=h,file=outputPNG) 196 | } 197 | 198 | 199 | # SET UP LAYOUT FOR PLOTTING 200 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 201 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 202 | layout(l$m, widths=l$w, heights=l$h) 203 | 204 | 205 | # PLOT TREE 206 | par(mar=rep(0,4)) 207 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 208 | 209 | # colour by trait 210 | if (!is.null(nodeColourSuccess)) { 211 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 212 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 213 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 214 | } 215 | 216 | if (axis) { axisPhylo(axisPos) } 217 | 218 | # PLOT INFO 219 | if (!is.null(infoFile)) { # info is provided 220 | 221 | printCols = TRUE 222 | if (!is.null(infoCols)) { 223 | if (is.na(infoCols)) { 224 | printCols = FALSE 225 | }} 226 | 227 | if (printCols) { 228 | 229 | par(mar=rep(0,4)) 230 | 231 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 232 | else { infoColNumbers = 1:ncol(info.ordered)} 233 | 234 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 235 | 236 | # plot all info columns 237 | for (i in 1:length(infoColNumbers)) { 238 | j<-infoColNumbers[i] 239 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 240 | } 241 | 242 | } 243 | } 244 | 245 | 246 | # PLOT HEATMAP 247 | if (!is.null(heatmapData)) { 248 | 249 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 250 | 251 | # plot heatmap 252 | par(mar=rep(0,4), xpd=TRUE) 253 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 254 | 255 | # draw vertical lines over heatmap 256 | if (!is.null(vlines.heatmap)) { 257 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 258 | } 259 | 260 | # overlay blocks on heatmap 261 | if (!is.null(heatmap.blocks)) { 262 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 263 | } 264 | 265 | 266 | # data labels for heatmap 267 | par(mar=rep(0,4)) 268 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 269 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 270 | 271 | # scale for heatmap 272 | par(mar=c(2,0,0,2)) 273 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 274 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 275 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 276 | } 277 | 278 | # BARPLOT 279 | if (!is.null(barData)) { 280 | par(mar=rep(0,4)) 281 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 282 | 283 | # scale for barData plot 284 | par(mar=c(2,0,0,0)) 285 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 286 | } 287 | 288 | # SNPS AND RECOMBINATION BLOCKS 289 | if (doBlocks) { 290 | par(mar=rep(0,4)) 291 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 292 | 293 | # plot snps 294 | if (!is.null(snpFile)) { 295 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 296 | snps_strainCols <- snps[1,] # column names = strain names 297 | snps<-snps[-1,] # drop strain names 298 | 299 | for (strain in tip.label.order){ 300 | # print SNPs compared to ancestral alleles in column 1 301 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 302 | y <- which(tip.label.order==strain) 303 | if (length(s)>0) { 304 | for (x in s) { 305 | points(x,y,pch="|",col=snp_colour,cex=0.25) 306 | } 307 | } 308 | } 309 | } 310 | 311 | # plot blocks 312 | if (!is.null(blockFile)){ 313 | blocks<-read.delim(blockFile,header=F) 314 | for (i in 1:nrow(blocks)) { 315 | if (as.character(blocks[i,1]) %in% tip.label.order) { 316 | y <- which(tip.label.order==as.character(blocks[i,1])) 317 | x1 <- blocks[i,2] 318 | x2 <- blocks[i,3] 319 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 320 | } 321 | } 322 | } 323 | 324 | } # finished with SNPs and recomb blocks 325 | 326 | # CLOSE EXTERNAL DRAWING DEVICE 327 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 328 | dev.off() 329 | } 330 | 331 | # RETURN ordered info and ancestral reconstruction object 332 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 333 | else {mat=NULL} 334 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 335 | } 336 | -------------------------------------------------------------------------------- /plotTreeShiny/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer( function(input, output, session) { 6 | 7 | # An event observer for changes to INFO CSV file 8 | observeEvent(input$info_file, 9 | { 10 | # read the CSV file and get the column names. 11 | # re-reading this file repeatedly is inefficient 12 | df = read.table(input$info_file$datapath, header=TRUE, sep=',') 13 | # build a list of values, this is what is required by update methods 14 | info_cols = list() 15 | for (v in colnames(df)) { 16 | info_cols[v] = v 17 | } 18 | # update the two input widgets using the column names 19 | updateSelectInput(session, inputId='colour_tips_by', choices=c('(none)',info_cols[-1])) 20 | updateSelectInput(session, inputId='print_column', choices=c(info_cols[-1])) 21 | 22 | # switch on the meta data plotting option 23 | updateCheckboxInput(session, inputId='info_data', value=TRUE) 24 | } 25 | ) 26 | 27 | # An event observer for changes to HEATMAP file 28 | observeEvent(input$heatmap, 29 | { 30 | # switch on the heatmap plotting option 31 | updateCheckboxInput(session, inputId='chk_heatmap', value=TRUE) 32 | } 33 | ) 34 | 35 | # An event observer for changes to BAR DATA file 36 | observeEvent(input$barData, 37 | { 38 | # switch on the heatmap plotting option 39 | updateCheckboxInput(session, inputId='chk_barplot', value=TRUE) 40 | } 41 | ) 42 | 43 | # An event observer for changes to BLOCKS file 44 | observeEvent(input$blockFile, 45 | { 46 | # switch on the heatmap plotting option 47 | updateCheckboxInput(session, inputId='chk_blocks', value=TRUE) 48 | } 49 | ) 50 | 51 | # An event observer for changes to SNPs file 52 | observeEvent(input$snpFile, 53 | { 54 | # switch on the heatmap plotting option 55 | updateCheckboxInput(session, inputId='chk_snps', value=TRUE) 56 | } 57 | ) 58 | 59 | output$Tree <- renderPlot({ 60 | 61 | input$drawButton == 0 62 | 63 | ### ALL VARIABLES PULLED FROM 'input' GO INSIDE HERE 64 | isolate ( { 65 | 66 | l<-input$Layout 67 | t<-input$Tree 68 | i<-input$Info 69 | o<-input$Other 70 | d<-input$Data 71 | 72 | treeFile <- input$tree$datapath 73 | 74 | # tree plotting options 75 | label_tips <- input$label_tips 76 | tree_line_width <- as.integer(input$tree_line_width) 77 | branch_colour <- input$branch_colour 78 | tipLabelSize <- as.integer(input$tipLabelSize) 79 | offset <- as.integer(input$offset) 80 | 81 | # metadata variables 82 | infoFile <- input$info_file$datapath 83 | tip_size <- input$tip_size 84 | colour_tips_by <- input$colour_tips_by 85 | if (colour_tips_by == '(none)') {colour_tips_by <- NULL} 86 | ancestral <- input$ancestral 87 | pie_size <- input$pie_size 88 | legend <- input$legend 89 | legend_pos <- input$legend_pos 90 | print_column <- input$print_column 91 | print_metadata <- input$print_metadata 92 | if (!print_metadata) { print_column <- NA } 93 | 94 | # heatmap variables 95 | heatmapFile <- input$heatmap$datapath 96 | cluster <- input$clustering 97 | heatmapDecimalPlaces <- as.integer(input$heatmapDecimalPlaces) 98 | colLabelCex <- as.integer(input$colLabelCex) 99 | vlines_heatmap_col <-input$vlines_heatmap_col 100 | vlines_heatmap <- input$vlines_heatmap 101 | 102 | # heatColoursSpecify <- input$heatColoursSpecify 103 | 104 | # if (heatColoursSpecify) { 105 | # heatmap_colours <- input$heatmap_colour_vector 106 | # } 107 | # else { 108 | heatmap_colours <- colorRampPalette(c(input$start_col,input$middle_col,input$end_col),space="rgb")(as.integer(input$heatmap_breaks)) 109 | # } 110 | 111 | # barplot variables 112 | barDataFile <- input$barData$datapath 113 | barDataCol <- input$barDataCol 114 | 115 | # block plot variables 116 | blockFile <- input$blockFile$datapath 117 | block_colour <- input$block_colour 118 | blwd <- input$blwd 119 | genome_size <- input$genome_size 120 | 121 | snpFile <- input$snpFile$datapath 122 | snp_colour <- input$snp_colour 123 | 124 | # Layout/spacing 125 | tree_width <- as.numeric(input$tree_width) 126 | info_width <- as.numeric(input$info_width) 127 | heatmap_width <- as.numeric(input$heatmap_width) 128 | bar_width <- as.numeric(input$bar_width) 129 | genome_width <- as.numeric(input$genome_width) 130 | main_height <- as.numeric(input$main_height) 131 | label_height <- as.numeric(input$label_height) 132 | edge_width <- as.numeric(input$edge_width) 133 | 134 | # TRACK DATA TYPES TO PLOT 135 | chk_heatmap <- input$chk_heatmap 136 | info_data <- input$info_data 137 | chk_barplot <- input$chk_barplot 138 | chk_blocks <- input$chk_blocks 139 | chk_snps <- input$chk_snps 140 | 141 | if (is.null(treeFile)) { return(NULL) } 142 | 143 | if (!info_data) { infoFile <- NULL } 144 | else { infoFile <- infoFile } 145 | 146 | if (!chk_heatmap) { heatmapFile <- NULL } 147 | else { heatmapFile <- heatmapFile } 148 | 149 | if (!chk_barplot) { barDataFile <- NULL } 150 | else { barDataFile <- barDataFile } 151 | 152 | if (!chk_blocks) { blockFile <- NULL } 153 | else { blockFile <- blockFile } 154 | 155 | if (!chk_snps) { snpFile <- NULL } 156 | else { snpFile <- snpFile } 157 | 158 | }) # end isolate 159 | 160 | 161 | ### PLOT THE TREE 162 | 163 | # main plotting function 164 | doPlotTree <-function() { 165 | 166 | # underlying call to plotTree(), drawn to screen and to file 167 | plotTree(tree=treeFile, 168 | tip.labels=label_tips, tipLabelSize=tipLabelSize, offset=offset, 169 | lwd=tree_line_width, edge.color=branch_colour, 170 | infoFile=infoFile, infoCols=print_column, 171 | colourNodesBy=colour_tips_by, tip.colour.cex=tip_size, 172 | ancestral.reconstruction=ancestral, pie.cex=pie_size, 173 | legend=legend, legend.pos=legend_pos, 174 | heatmapData=heatmapFile, cluster=cluster, 175 | heatmap.colours=heatmap_colours, 176 | heatmapDecimalPlaces=heatmapDecimalPlaces, colLabelCex=colLabelCex, 177 | vlines.heatmap=vlines_heatmap, vlines.heatmap.col=vlines_heatmap_col, 178 | barData=barDataFile, barDataCol=barDataCol, 179 | blockFile=blockFile, block_colour=block_colour, blwd=blwd, 180 | genome_size=genome_size, 181 | snpFile=snpFile, snp_colour=snp_colour, 182 | treeWidth=tree_width, infoWidth=info_width, dataWidth=heatmap_width, 183 | barDataWidth=bar_width, blockPlotWidth=genome_width, 184 | mainHeight=main_height, labelHeight=label_height, edgeWidth=edge_width 185 | ) 186 | } 187 | 188 | doPlotTree() 189 | 190 | }) # end render plot 191 | 192 | }) # shinyServer -------------------------------------------------------------------------------- /plotTreeShiny/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | library(RLumShiny) 4 | shinyUI(fluidPage( 5 | #titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | tabsetPanel( 10 | 11 | tabPanel("Tree", 12 | 13 | ### UPLOAD TREE 14 | br(), 15 | fileInput('tree', 'Upload tree file (nwk)', multiple=F, 16 | accept=c('biotree/newick','.nwk', '.tree')), 17 | 18 | checkboxInput("label_tips", "Label tree tips?", value=FALSE), 19 | conditionalPanel( 20 | condition = "input.label_tips", 21 | textInput("tipLabelSize", label = "Text size", value = "1"), 22 | textInput("offset", label = "Offset", value = "0") 23 | ), 24 | 25 | textInput("tree_line_width", label = "Branch width", value = "1.5"), 26 | jscolorInput(inputId="branch_colour", label="Branch colour:", value="#000000", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 27 | br() 28 | ), # finished tree tab 29 | 30 | tabPanel("Info", 31 | 32 | ### METADATA (info file) 33 | br(), 34 | fileInput('info_file', 'Upload metadata (CSV)'), 35 | checkboxInput('info_data', 'Use metadata', value = FALSE), 36 | conditionalPanel( 37 | condition = "input.info_data", 38 | checkboxInput('print_metadata', 'Print columns', value = FALSE), 39 | conditionalPanel( 40 | condition = "input.print_metadata", 41 | selectInput('print_column', 'Metadata columns to print:', c(''), multiple=TRUE) 42 | ), 43 | "--------", 44 | selectInput('colour_tips_by', 'Colour tips by:', c('')), 45 | # options if colouring by tips 46 | conditionalPanel( 47 | condition = "input.colour_tips_by != '(none)'", 48 | sliderInput("tip_size", label = "Tip size", min = 0.1, max = 20, value = 0.5), 49 | ### COLOUR PANELS 50 | checkboxInput("legend", "Legend for node colours?", value=TRUE), 51 | selectInput("legend_pos", label = "Position for legend", 52 | choices = list( "bottomleft"="bottomleft", "bottomright"="bottomright", 53 | "top-left"="topleft", "topright"="topright") 54 | ), 55 | "--------", 56 | checkboxInput("ancestral", "Ancestral state reconstruction?", value=FALSE), 57 | sliderInput("pie_size", label = "Pie graph size", min = 0.1, max = 20, value = 0.5) 58 | ) 59 | ) 60 | ), # finished metadata tab 61 | 62 | 63 | tabPanel("Data", 64 | 65 | ### HEATMAP DATA 66 | br(), 67 | fileInput('heatmap', 'Upload heatmap file (CSV)', multiple = F, accept = c('text/csv', '.csv')), 68 | checkboxInput('chk_heatmap', 'Plot heatmap', value = FALSE), 69 | 70 | conditionalPanel( 71 | condition = "input.chk_heatmap", h4("Heatmap options"), 72 | selectInput("clustering", label = h5("Clustering:"), 73 | choices = list("Select..."=F, "Cluster columns by values"=T, "Square matrix"="square"), 74 | selected = "Select"), 75 | "--------", 76 | 77 | # OPTIONALLY DISPLAY COLOUR OPTIONS 78 | checkboxInput("heatColoursPrompt", "Change heatmap colour ramp", value=FALSE), 79 | conditionalPanel( 80 | condition = "input.heatColoursPrompt", 81 | jscolorInput(inputId="start_col", label="Start colour:", value="FFFFFF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 82 | jscolorInput(inputId="middle_col", label="Middle colour:", value="FFF94D", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 83 | jscolorInput(inputId="end_col", label="End colour:", value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 84 | textInput("heatmap_breaks", label = "Breaks:", value = "100") 85 | ), 86 | # checkboxInput("heatColoursSpecify", "Specify heatmap colours manually", value=FALSE), 87 | # conditionalPanel( 88 | # condition = "input.heatColoursSpecify", 89 | # textInput("heatmap_colour_vector", label = "R code (vector), e.g. rev(gray(seq(0,1,0.1)))", value = "") 90 | # ), 91 | "--------", 92 | textInput("heatmapDecimalPlaces", label = "Decimal places to show in heatmap legend:", value = "1"), 93 | textInput("colLabelCex", label = "Text size for column labels:", value = "0.75") 94 | # textInput("vlines_heatmap", label = "y-coordinates for vertical lines (e.g. c(2,5)):", value = ""), 95 | # jscolorInput(inputId="vlines_heatmap_col", label=h5("Colour for vertical lines:"), value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T) 96 | ) 97 | ), # finished heatmap options 98 | 99 | tabPanel("Other", 100 | tabsetPanel( 101 | tabPanel("Barplots", 102 | br(), 103 | # bar plots 104 | fileInput('barData', 'Upload data for bar plots (CSV)', multiple = F, accept = c('text/csv', '.csv')), 105 | checkboxInput('chk_barplot', 'Plot bar graphs', value = FALSE), 106 | 107 | conditionalPanel( 108 | condition = "input.chk_barplot", h5("Barplot options"), 109 | jscolorInput(inputId="barDataCol", label="Colour for barplots:", value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T) 110 | ) 111 | ), 112 | 113 | tabPanel("Genome blocks", 114 | br(), 115 | # genome blocks 116 | fileInput('blockFile', 'Upload genome block coordinates', multiple = F, accept = c('text/tab', '.txt')), 117 | checkboxInput('chk_blocks', 'Plot genome blocks', value = FALSE), 118 | 119 | conditionalPanel( 120 | condition = "input.chk_blocks", h5("Genome block plotting options"), 121 | textInput("genome_size", label = "Genome size (bp):", value = "5E6"), 122 | jscolorInput(inputId="block_colour", label="Colour for blocks:", value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 123 | sliderInput("blwd", label = "Block size", min = 0.1, max = 20, value = 5) 124 | ) 125 | ), 126 | 127 | tabPanel("SNPs", 128 | br(), 129 | # snps 130 | fileInput('snpFile', 'Upload SNP allele table (CSV)', multiple = F, accept = c('text/csv', '.csv')), 131 | checkboxInput('chk_snps', 'Plot SNPs', value = FALSE), 132 | 133 | conditionalPanel( 134 | condition = "input.chk_snps", h5("SNP plotting options"), 135 | textInput("genome_size", label = "Genome size (bp):", value = "5E6"), # make this linked to previous conditional 136 | jscolorInput(inputId="snp_colour", label="Colour for SNPs:", value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T) 137 | ) 138 | ) 139 | ) #finished other data subtabs 140 | ), # finished other data tab 141 | 142 | tabPanel("Layout", 143 | br(), 144 | h5("Relative widths"), 145 | textInput("tree_width", label = "Tree", value = 10), 146 | textInput("info_width", label = "Info columns", value = 10), 147 | textInput("heatmap_width", label = "Heatmap", value = 30), 148 | textInput("bar_width", label = "Bar plots", value = 10), 149 | textInput("genome_width", label = "Genome data (blocks, SNPs)", value = 10), 150 | br(), 151 | h5("Relative heights"), 152 | textInput("main_height", label = "Main panels", value = 100), 153 | textInput("label_height", label = "Heatmap labels", value = 10), 154 | br(), 155 | h5("Borders"), 156 | textInput("edge_width", label = "Border width/height", value = 1) 157 | ) 158 | 159 | ), # finish tabpanel 160 | 161 | ### DRAW BUTTON 162 | br(), 163 | actionButton("drawButton", "Draw!") 164 | 165 | # ADD PRINT BUTTON HERE 166 | 167 | ), # finished sidebarPanel 168 | 169 | mainPanel( 170 | plotOutput("Tree", height=800) 171 | ) 172 | 173 | ) # finished sidebarLayout 174 | ) # fluidPage 175 | ) # shinyUI -------------------------------------------------------------------------------- /server_modified/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | 3 | shinyServer( 4 | 5 | function(input, output, session) { 6 | 7 | # An event observer for changes to INFO CSV file 8 | observeEvent(input$info_file, 9 | { 10 | # read the CSV file and get the column names. 11 | # re-reading this file repeatedly is inefficient 12 | df = read.table(input$info_file$datapath, header=TRUE, sep=',') 13 | 14 | # build a list of values, this is what is required by update methods 15 | info_cols = list() 16 | for (v in colnames(df)) { 17 | info_cols[v] = v 18 | } 19 | 20 | # update the two input widgets using the column names 21 | updateSelectInput(session, 'highlight_column', choices=info_cols) 22 | updateSelectInput(session, 'show_column', choices=info_cols) 23 | } 24 | ) 25 | 26 | } 27 | ) -------------------------------------------------------------------------------- /server_modified/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | 3 | shinyUI( 4 | fluidPage( 5 | titlePanel('tester'), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | fileInput('treeFile', 'Tree'), 10 | 11 | # Test widgets for selecting INFO CSV file and 12 | # display column selection options. 13 | checkboxInput("info_data", "Info Data"), 14 | conditionalPanel( 15 | condition = "input.info_data", 16 | fileInput('info_file', 'Info CSV'), 17 | selectInput('show_column', 'Show Columns', c(''), multiple=TRUE), 18 | selectInput('highlight_column', 'Highlight By', c('')) 19 | ), 20 | 21 | actionButton("update", "Update") 22 | ), 23 | 24 | mainPanel() 25 | ) 26 | ) 27 | ) 28 | -------------------------------------------------------------------------------- /shiny_practice/print_statemtn.r: -------------------------------------------------------------------------------- 1 | sidebarPanel( 2 | checkboxInput('returnDownload', 'Download figure?', FALSE), 3 | conditionalPanel( 4 | condition = "input.returnDownload == true", 5 | radioButtons("download_type", "Download type:", 6 | c("PDF" = "PDF", 7 | "PNG" = "PNG")), 8 | sliderInput(inputId="w", label = "width (A4=210mm):", min=60, max=600, value=210, width='80%', ticks=F), 9 | sliderInput(inputId="h", label = "height (A4=297mm):", min=60, max=600, value=297, width='80%', ticks=F), 10 | br(), 11 | downloadLink('pdflink') 12 | ), 13 | actionButton("printButton", "Update print settings") 14 | ), -------------------------------------------------------------------------------- /shiny_practice/reactive/plot.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/shiny_practice/reactive/plot.pdf -------------------------------------------------------------------------------- /shiny_practice/reactive/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward.D2"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/reactive/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer(function(input, output, session) { 6 | 7 | tree <- input$tree 8 | info <- input$info_file 9 | heatmap <- input$heatmap 10 | cluster <- input$clustering 11 | colour_nodes <- input$colour_nodes 12 | tip_size <- input$tip_size 13 | 14 | # heatmap colours 15 | start_col <- input$start_col 16 | middle_col <- input$middle_col 17 | end_col <- input$end_col 18 | heatmap_breaks <- as.integer(input$heatmap_breaks) 19 | 20 | highlight_column <- input$highlight_column 21 | show_column <- input$show_column 22 | 23 | # track data types 24 | chk_heatmap <- input$chk_heatmap 25 | info_data <- input$info_data 26 | 27 | # An event observer for changes to INFO CSV file 28 | observeEvent(input$info_file, 29 | { 30 | # read the CSV file and get the column names. 31 | # re-reading this file repeatedly is inefficient 32 | df = read.table(input$info_file$datapath, header=TRUE, sep=',') 33 | 34 | # build a list of values, this is what is required by update methods 35 | info_cols = list() 36 | for (v in colnames(df)) { 37 | info_cols[v] = v 38 | } 39 | 40 | # update the two input widgets using the column names 41 | updateSelectInput(session, inputId='highlight_column', choices=info_cols) 42 | updateSelectInput(session, inputId='show_column', choices=info_cols) 43 | } 44 | ) 45 | 46 | # we don't do anything if there's no tree file 47 | if (is.null(treeFile)) { return(NULL) } 48 | 49 | # switch off metadata plotting if the box is unchecked 50 | if (!info_data) { infoFile <- NULL } 51 | else { infoFile <- info$datapath } 52 | 53 | # switch off heatmap plotting if the box is unchecked 54 | if (!chk_heatmap) { heatmapFile <- NULL } 55 | else { heatmapFile <- heatmap$datapath } 56 | 57 | # plotTree wrapping (to allow calling for plotting to screen or file) 58 | doPlotTree <-function() { 59 | plotTree(tree=tree$datapath, 60 | infoFile=infoFile,colourNodesBy=highlight_column,tip.colour.cex=tip_size, 61 | infoCols=show_column, 62 | heatmapData=heatmapFile,cluster=cluster, 63 | heatmap.colours=colorRampPalette(c(start_col,middle_col,end_col),space="rgb")(heatmap_breaks) 64 | ) 65 | } 66 | 67 | # PLOT THE TREE when button is pressed 68 | 69 | plot_tree <- F 70 | plot_tree <- eventReactive(input$drawButton, function() { 71 | plot_tree <- T 72 | }) 73 | 74 | output$Tree <- renderPlot({ 75 | if (!plot_tree) { return (NULL) } 76 | doPlotTree() 77 | plot_tree <- F 78 | }) 79 | 80 | }) 81 | -------------------------------------------------------------------------------- /shiny_practice/reactive/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | library(RLumShiny) 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | fileInput('tree', 'Choose tree file', multiple=F, 9 | accept=c('biotree/newick','.nwk', '.tree')), 10 | checkboxInput("info_data", "Info Data"), 11 | conditionalPanel( 12 | condition = "input.info_data", 13 | fileInput('info_file', 'Info CSV'), 14 | selectInput('show_column', 'Show Columns', c(''), multiple=TRUE), 15 | selectInput('highlight_column', 'Highlight By', c('location')), 16 | sliderInput("tip_size", label = h4("Tip size"), min = 0.1, 17 | max = 20, value = 0.5) 18 | ), 19 | ### HEATMAP DATA 20 | checkboxInput("chk_heatmap", "Heatmap file", value=FALSE), 21 | 22 | conditionalPanel( 23 | condition = "input.chk_heatmap", "Heatmap", 24 | fileInput('heatmap', 'Choose heatmap file', multiple = F, accept = c('text/csv', '.csv')), 25 | 26 | # HEATMAP OPTIONS 27 | checkboxInput("optionsPrompt", "Check box if you wish to not use the default values.", value=FALSE), 28 | conditionalPanel( 29 | condition = "input.optionsPrompt", 30 | selectInput("clustering", label = "Columns clustering:", 31 | choices = list("Select"=F, "Cluster based on density"=T, "Cluster according to tree"="square"), selected = "Select"), 32 | "Note: You can only cluster according to tree if your rows are equal to your tree tips. I.e. if you're viewing the dataset against itself.", 33 | 34 | jscolorInput(inputId="start_col", label="Start colour", value="FFFFFF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 35 | jscolorInput(inputId="middle_col", label="Middle colour", value="FFF94D", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 36 | jscolorInput(inputId="end_col", label="End colour", value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 37 | textInput("heatmap_breaks", label = "Breaks", value = "100") 38 | ) 39 | ), 40 | actionButton("drawButton", "Draw!") 41 | ), 42 | 43 | mainPanel( 44 | plotOutput("Tree", height=800) 45 | ) 46 | ) 47 | ) 48 | ) -------------------------------------------------------------------------------- /shiny_practice/reactive_stable/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward.D2"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/reactive_stable/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer(function(input, output, session) { 6 | 7 | tree <- eventReactive(input$drawButton, { 8 | input$tree 9 | }) 10 | 11 | info <- eventReactive(input$drawButton, { 12 | input$info_file 13 | }) 14 | 15 | heatmap <- eventReactive(input$drawButton, { 16 | input$heatmap 17 | }) 18 | 19 | cluster <- eventReactive(input$drawButton, { 20 | input$clustering 21 | }) 22 | 23 | colour_nodes <- eventReactive(input$drawButton, { 24 | input$colour_nodes 25 | }) 26 | 27 | tip_size <- eventReactive(input$drawButton, { 28 | input$tip_size 29 | }) 30 | 31 | 32 | # heatmap colours 33 | start_col <- eventReactive(input$drawButton, { 34 | input$start_col 35 | }) 36 | middle_col <- eventReactive(input$drawButton, { 37 | input$middle_col 38 | }) 39 | end_col <- eventReactive(input$drawButton, { 40 | input$end_col 41 | }) 42 | heatmap_breaks <- eventReactive(input$drawButton, { 43 | input$heatmap_breaks 44 | }) 45 | 46 | 47 | highlight_column <- eventReactive(input$drawButton, { 48 | input$highlight_column 49 | }) 50 | 51 | show_column <- eventReactive(input$drawButton, { 52 | input$show_column 53 | }) 54 | 55 | 56 | # track data types 57 | chk_heatmap <- eventReactive(input$drawButton, { 58 | input$chk_heatmap 59 | }) 60 | 61 | info_data <- eventReactive(input$drawButton, { 62 | input$info_data 63 | }) 64 | 65 | # An event observer for changes to INFO CSV file 66 | observeEvent(input$info_file, 67 | { 68 | # read the CSV file and get the column names. 69 | # re-reading this file repeatedly is inefficient 70 | df = read.table(input$info_file$datapath, header=TRUE, sep=',') 71 | 72 | # build a list of values, this is what is required by update methods 73 | info_cols = list() 74 | for (v in colnames(df)) { 75 | info_cols[v] = v 76 | } 77 | 78 | # update the two input widgets using the column names 79 | updateSelectInput(session, inputId='highlight_column', choices=info_cols) 80 | updateSelectInput(session, inputId='show_column', choices=info_cols) 81 | } 82 | ) 83 | 84 | 85 | output$Tree <- renderPlot({ 86 | 87 | highlight_column <- highlight_column() 88 | show_column <- show_column() 89 | treeFile <- tree() 90 | infoFile <- info() 91 | heatmapFile <- heatmap() 92 | cluster <- cluster() 93 | colour_nodes <- colour_nodes() 94 | tip_size <- tip_size() 95 | start_col <- start_col() 96 | middle_col <- middle_col() 97 | end_col <- end_col() 98 | heatmap_breaks <- as.integer(heatmap_breaks()) 99 | 100 | chk_heatmap <- chk_heatmap() 101 | info_data <- info_data() 102 | 103 | if (is.null(treeFile)) 104 | return(NULL) 105 | 106 | if (!info_data) { 107 | infoFile <- NULL 108 | } 109 | else { 110 | infoFile <- infoFile$datapath 111 | } 112 | 113 | if (!chk_heatmap) { 114 | heatmapFile <- NULL 115 | } else { 116 | heatmapFile <- heatmapFile$datapath 117 | } 118 | 119 | 120 | doPlotTree <-function(){ 121 | 122 | plotTree(tree=treeFile$datapath,infoFile=infoFile, 123 | heatmapData=heatmapFile,cluster=cluster,colourNodesBy=highlight_column, 124 | infoCols=show_column, 125 | tip.colour.cex=tip_size,heatmap.colours=colorRampPalette(c(start_col,middle_col,end_col),space="rgb")(heatmap_breaks)) 126 | } 127 | 128 | doPlotTree() 129 | 130 | }) 131 | 132 | }) 133 | -------------------------------------------------------------------------------- /shiny_practice/reactive_stable/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | library(RLumShiny) 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | fileInput('tree', 'Choose tree file', multiple=F, 9 | accept=c('biotree/newick','.nwk', '.tree')), 10 | checkboxInput("info_data", "Info Data"), 11 | conditionalPanel( 12 | condition = "input.info_data", 13 | fileInput('info_file', 'Info CSV'), 14 | selectInput('show_column', 'Show Columns', c(''), multiple=TRUE), 15 | selectInput('highlight_column', 'Highlight By', c('location')), 16 | sliderInput("tip_size", label = h4("Tip size"), min = 0.1, 17 | max = 20, value = 0.5) 18 | ), 19 | ### HEATMAP DATA 20 | checkboxInput("chk_heatmap", "Heatmap file", value=FALSE), 21 | 22 | conditionalPanel( 23 | condition = "input.chk_heatmap", "Heatmap", 24 | fileInput('heatmap', 'Choose heatmap file', multiple = F, accept = c('text/csv', '.csv')), 25 | 26 | # HEATMAP OPTIONS 27 | checkboxInput("optionsPrompt", "Check box if you wish to not use the default values.", value=FALSE), 28 | conditionalPanel( 29 | condition = "input.optionsPrompt", 30 | selectInput("clustering", label = "Columns clustering:", 31 | choices = list("Select"=F, "Cluster based on density"=T, "Cluster according to tree"="square"), selected = "Select"), 32 | "Note: You can only cluster according to tree if your rows are equal to your tree tips. I.e. if you're viewing the dataset against itself.", 33 | 34 | jscolorInput(inputId="start_col", label="Start colour", value="FFFFFF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 35 | jscolorInput(inputId="middle_col", label="Middle colour", value="FFF94D", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 36 | jscolorInput(inputId="end_col", label="End colour", value="1755FF", position = "bottom", color = "transparent", mode = "HSV", slider = T, close = T), 37 | textInput("heatmap_breaks", label = "Breaks", value = "100") 38 | ) 39 | ), 40 | actionButton("drawButton", "Draw!") 41 | ), 42 | 43 | mainPanel( 44 | plotOutput("Tree", height=800) 45 | ) 46 | ) 47 | ) 48 | ) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree.download/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree.download/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer(function(input, output) { 6 | output$Tree <- renderPlot({ 7 | 8 | treeFile <- input$tree 9 | infoFile <- input$info 10 | heatmapFile <- input$heatmap 11 | 12 | if (is.null(treeFile)) 13 | return(NULL) 14 | 15 | if(input$returnDownload){ 16 | if(input$type == 'PDF'){ 17 | pdf("plot.pdf", width=as.numeric(input$w*3.94), height=as.numeric(input$h*3.94)) 18 | plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath,heatmapData=heatmapFile$datapath) 19 | dev.off() 20 | 21 | output$pdflink <- downloadHandler( 22 | filename <- "myplot.pdf", 23 | content <- function(file) { 24 | file.copy("plot.pdf", file) 25 | }) 26 | #} else if (input$type == "SVG"){ 27 | #svg("myplot.svg", width=as.numeric(input$w*3.94), height=as.numeric(input$h*3.94)) 28 | # plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath,heatmapData=heatmapFile$datapath) 29 | # dev.off() 30 | # output$pdflink <- downloadHandler( 31 | # filename <- "myplot.svg", 32 | # content <- function(file) { 33 | # file.copy("plot.svg", file) 34 | # }) 35 | } else if (input$type == "PNG"){ 36 | png("plot.png", width=as.numeric(input$w*3.94), height=as.numeric(input$h*3.94)) 37 | plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath,heatmapData=heatmapFile$datapath) 38 | dev.off() 39 | output$pdflink <- downloadHandler( 40 | filename <- "myplot.png", 41 | content <- function(file) { 42 | file.copy("plot.png", file) 43 | }) 44 | } else { 45 | stop(paste("Unexpected type returned:", input$type)) 46 | } 47 | } 48 | plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath,heatmapData=heatmapFile$datapath) 49 | 50 | }) 51 | }) 52 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree.download/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | fileInput('tree', 'Choose tree file', multiple=F, 10 | accept=c('biotree/newick', 11 | '.nwk', '.tree')), 12 | 13 | fileInput('info', 'Choose info file', multiple=F, 14 | accept=c('text/csv', 15 | '.csv')), 16 | 17 | fileInput('heatmap', 'Choose heatmap file', multiple=F, 18 | accept=c('text/csv', 19 | '.csv')), 20 | checkboxInput('returnDownload', 'download?', FALSE), 21 | conditionalPanel( 22 | condition = "input.returnDownload == true", 23 | sliderInput(inputId="w", label = "width (A4=210mm):", min=60, max=600, value=210, width='80%', ticks=F), 24 | sliderInput(inputId="h", label = "height (A4=297mm):", min=60, max=600, value=297, width='80%', ticks=F), 25 | radioButtons("type", "Download type:", 26 | c(#"SVG" = "SVG", 27 | "PDF" = "PDF", 28 | "PNG" = "PNG")), 29 | br(), 30 | downloadLink('pdflink') 31 | ) 32 | 33 | ), 34 | mainPanel( 35 | plotOutput("Tree", height=2000)) 36 | ) 37 | ) 38 | ) 39 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer(function(input, output) { 6 | output$Tree <- renderPlot({ 7 | 8 | treeFile <- input$tree 9 | infoFile <- input$info 10 | heatmapFile <- input$heatmap 11 | 12 | if (is.null(treeFile)) 13 | return(NULL) 14 | 15 | plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath,heatmapData=heatmapFile$datapath) 16 | 17 | }) 18 | }) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | fileInput('tree', 'Choose tree file', multiple=F, 10 | accept=c('biotree/newick', 11 | '.nwk', '.tree')), 12 | 13 | fileInput('info', 'Choose info file', multiple=F, 14 | accept=c('text/csv', 15 | '.csv')), 16 | 17 | fileInput('heatmap', 'Choose heatmap file', multiple=F, 18 | accept=c('text/csv', 19 | '.csv')) 20 | 21 | ), 22 | mainPanel( 23 | plotOutput("Tree", height=2000)) 24 | ) 25 | ) 26 | ) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_conditional/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_conditional/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer(function(input, output) { 6 | output$Tree <- renderPlot({ 7 | 8 | treeFile <- input$tree 9 | infoFile <- input$info 10 | heatmapFile <- input$heatmap 11 | cluster <- input$heat_cluster 12 | colour_nodes <- input$colour_nodes 13 | tip_size <- input$tip_size 14 | 15 | if (is.null(treeFile)) 16 | return(NULL) 17 | 18 | plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath, 19 | heatmapData=heatmapFile$datapath,cluster=cluster,colourNodesBy=colour_nodes, 20 | tip.colour.cex=tip_size) 21 | 22 | }) 23 | }) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_conditional/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | fileInput('tree', 'Choose tree file', multiple=F, 10 | accept=c('biotree/newick', 11 | '.nwk', '.tree')), 12 | 13 | fileInput('info', 'Choose info file', multiple=F, 14 | accept=c('text/csv', 15 | '.csv')), 16 | 17 | textInput("colour_nodes", label = h4("Colour nodes by"), value = "Enter variable name"), 18 | 19 | sliderInput("tip_size", label = h4("Tip size"), min = 0.1, 20 | max = 20, value = 0.5), 21 | 22 | fileInput('heatmap', 'Choose heatmap file', multiple=F, 23 | accept=c('text/csv', 24 | '.csv')), 25 | 26 | checkboxInput("heat_cluster", label = "Cluster heatmap", value = TRUE) 27 | 28 | ), 29 | mainPanel( 30 | plotOutput("Tree", height=800)) 31 | ) 32 | ) 33 | ) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_conditional_clustering/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_conditional_clustering/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | ### END R PLOTTING CODE 6 | 7 | shinyServer(function(input, output) { 8 | 9 | output$Tree <- renderPlot({ 10 | 11 | treeFile <- input$tree$datapath 12 | heatmapFile = NULL 13 | infoFile = NULL 14 | clusteringOption = NULL 15 | 16 | if (is.null(treeFile)) 17 | return(NULL) 18 | 19 | if(input$chk_meta) { 20 | infoFile <- input$info$datapath 21 | if (input$chk_heatmap) { 22 | heatmapFile <- input$heatmap$datapath 23 | } 24 | } 25 | if (input$chk_heatmap) { 26 | heatmapFile <- input$heatmap$datapath 27 | } 28 | 29 | if(!is.null(heatmapFile)) { 30 | if(input$optionsPrompt) { 31 | if(input$clustering == "Cluster based on density") { 32 | clusteringOption = T 33 | } else 34 | if (input$clustering == "Cluster according to tree") { 35 | clusteringOption = "square" 36 | } 37 | } 38 | } 39 | 40 | plotTree(treeFile, heatmapFile, infoFile, cluster=clusteringOption) 41 | }) 42 | }) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_conditional_clustering/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | fileInput('tree', 'Choose tree file', multiple=F, 9 | accept=c('biotree/newick', '.nwk', '.tree')), 10 | # This prompts the user for metadata file 11 | checkboxInput("chk_meta", "Meta file"), 12 | conditionalPanel( 13 | condition = "input.chk_meta", 14 | fileInput('info', 'Choose info file', multiple = F, accept = c('text/csv', '.csv')) 15 | ), 16 | 17 | # This prompts the user for pan genome file 18 | checkboxInput("chk_heatmap", "Heatmap file"), 19 | conditionalPanel( 20 | condition = "input.chk_heatmap", "Heatmap", 21 | fileInput('heatmap', 'Choose heatmap file', multiple = F, accept = c('text/csv', '.csv')), 22 | 23 | # This displays a check box if the user wants to change tree options 24 | checkboxInput("optionsPrompt", "Check box if you wish to not use the default values.", value=FALSE), 25 | conditionalPanel( 26 | condition = "input.optionsPrompt", 27 | selectInput("clustering", label = "Columns clustering:", 28 | choices = c("Select", "Cluster based on density", "Cluster according to tree"), selected = "Select"), "Note: You can only cluster according to tree if your rows are equal to your tree tips. 29 | I.e. if you're viewing the dataset against itself.") 30 | ) 31 | ), 32 | 33 | mainPanel(plotOutput("Tree", height=2000)) 34 | ) 35 | ) 36 | ) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_tabs/plotTree.R: -------------------------------------------------------------------------------- 1 | # read data and convert to data frame 2 | readMatrix<-function(heatmapData){ 3 | if (is.matrix(heatmapData)) { 4 | x = data.frame(heatmapData) 5 | } 6 | else if (is.data.frame(heatmapData)) { 7 | x = heatmapData 8 | } 9 | else { 10 | x<-read.csv(heatmapData,row.names=1) 11 | } 12 | x 13 | } 14 | 15 | getLayout<-function(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10) { 16 | 17 | # m = layout matrix 18 | # w = layout widths vector 19 | # h = layout height vector 20 | 21 | # tree 22 | w = c(edgeWidth,treeWidth) 23 | m<-cbind(c(0,0,0),c(0,1,0)) # first two columns, edge + tree 24 | x = 1 25 | 26 | # info 27 | if (!is.null(infoFile)) { # info is provided 28 | 29 | printCols = TRUE 30 | if (!is.null(infoCols)) { 31 | if (is.na(infoCols)) { 32 | printCols = FALSE 33 | }} 34 | 35 | if (printCols) { 36 | x = x + 1 37 | m<-cbind(m,c(0,x,0)) 38 | w = c(w,infoWidth) 39 | } 40 | } 41 | 42 | # heatmap 43 | if (!is.null(heatmapData)) { 44 | x = x + 1 45 | m<-cbind(m,c(x+1,x,0)) # add heatmap & labels 46 | x = x + 2 47 | m[1,2] = x # add heatmap scale above tree 48 | w = c(w,dataWidth) 49 | } 50 | 51 | # barplot 52 | if (!is.null(barData)) { 53 | x = x + 1 54 | m<-cbind(m,c(0,x,x+1)) # barplot and scale bar 55 | x = x + 1 56 | w = c(w,barDataWidth) 57 | } 58 | 59 | if (doBlocks) { 60 | x = x + 1 61 | m<-cbind(m,c(0,x,0)) # recomb blocks 62 | w = c(w,blockPlotWidth) 63 | } 64 | 65 | # empty edge column 66 | m<-cbind(m,c(0,0,0)) 67 | w = c(w,edgeWidth) 68 | 69 | if (!is.null(heatmapData) | !is.null(barData)) { h = c(labelHeight,mainHeight,labelHeight) } 70 | else { h = c(edgeWidth,mainHeight,edgeWidth) } 71 | 72 | return(list(m=as.matrix(m),w=w,h=h)) 73 | } 74 | 75 | 76 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 77 | 78 | require(ape) 79 | 80 | # PREPARE TREE AND GET TIP ORDER 81 | if (is.character(tree)){ 82 | t<-read.tree(tree) 83 | } 84 | else t<-tree 85 | tl<-ladderize(t) 86 | tips<-tl$edge[,2] 87 | tip.order<-tips[tips<=length(tl$tip.label)] 88 | tip.label.order<-tl$tip.label[tip.order] # for ordering data. note that for tiplabel(), the order is the same as in t$tip (= tl$tip) 89 | 90 | 91 | # PREPARE HEATMAP DATA 92 | if (!is.null(heatmapData)) { 93 | 94 | # read heatmap data and convert to data frame 95 | x<-readMatrix(heatmapData) 96 | 97 | # order rows of heatmap matrix to match tree 98 | y.ordered<-x[tip.label.order,] 99 | 100 | # reorder columns? 101 | if (!is.null(cluster)) { 102 | if (!(cluster==FALSE)) { 103 | 104 | if (cluster=="square" & ncol(y.ordered)==nrow(y.ordered)) { 105 | # order columns to match row order 106 | original_order<-1:nrow(x) 107 | names(original_order)<-rownames(x) 108 | reordered<-original_order[tip.label.order] 109 | y.ordered<-y.ordered[,rev(as.numeric(reordered))] 110 | } 111 | 112 | else { 113 | # cluster columns 114 | if (cluster==TRUE) {cluster="ward"} # set default clustering algorithm 115 | h<-hclust(dist(t(na.omit(y.ordered))),cluster) 116 | y.ordered<-y.ordered[,h$order] 117 | } 118 | 119 | }} # finished reordering columns 120 | 121 | } # finished setting up heatmap data 122 | 123 | 124 | # PREPARE BAR PLOT 125 | if (!is.null(barData)) { 126 | b<-readMatrix(barData) 127 | barData<-b[,1] 128 | names(barData)<-rownames(b) 129 | } 130 | 131 | # PREPARE INFO TO PRINT 132 | if (!is.null(infoFile)) { 133 | info<-readMatrix(infoFile) 134 | info.ordered<-info[rev(tip.label.order),] 135 | } 136 | else {info.ordered=NULL} 137 | 138 | 139 | # PREPARE DISCRETE TRAIT FOR COLOURING NODES AND INFERRING ANCESTRAL STATES 140 | ancestral=NULL 141 | nodeColourSuccess=NULL 142 | if (!is.null(colourNodesBy) & !is.null(infoFile)) { 143 | 144 | if (colourNodesBy %in% colnames(info.ordered)) { 145 | nodeColourSuccess = TRUE 146 | loc1<-info.ordered[,which(colnames(info.ordered)==colourNodesBy)] 147 | 148 | # assign values 149 | tipLabelSet <- character(length(loc1)) 150 | names(tipLabelSet) <- rownames(info.ordered) 151 | groups<-table(loc1,exclude="") 152 | n<-length(groups) 153 | groupNames<-names(groups) 154 | 155 | # set colours 156 | if (is.null(tipColours)){ colours<-rainbow(n) } 157 | else{ colours<-tipColours } 158 | 159 | # assign colours based on values 160 | for (i in 1:n) { 161 | g<-groupNames[i] 162 | tipLabelSet[loc1==g]<-colours[i] 163 | } 164 | tipLabelSet <- tipLabelSet[tl$tip] 165 | 166 | # ancestral reconstruction 167 | if (ancestral.reconstruction) { ancestral<-ace(loc1,tl,type="discrete") } 168 | 169 | }} 170 | # finished with trait labels and ancestral reconstruction 171 | 172 | 173 | # OPEN EXTERNAL DEVICE FOR DRAWING 174 | # open PDF for drawing 175 | if (!is.null(outputPDF)) { 176 | pdf(width=w,height=h,file=outputPDF) 177 | } 178 | # open PNG for drawing 179 | if (!is.null(outputPNG)) { 180 | png(width=w,height=h,file=outputPNG) 181 | } 182 | 183 | 184 | # SET UP LAYOUT FOR PLOTTING 185 | doBlocks <- (!is.null(blockFile) | !is.null(snpFile)) 186 | l <- getLayout(infoFile,infoCols,heatmapData,barData,doBlocks,treeWidth=treeWidth,infoWidth=infoWidth,dataWidth=dataWidth,edgeWidth=edgeWidth,labelHeight=labelHeight,mainHeight=mainHeight,barDataWidth=barDataWidth,blockPlotWidth=blockPlotWidth) 187 | layout(l$m, widths=l$w, heights=l$h) 188 | 189 | 190 | # PLOT TREE 191 | par(mar=rep(0,4)) 192 | tlp<-plot.phylo(tl,no.margin=T,show.tip.label=tip.labels,label.offset=offset,edge.width=lwd,edge.color=edge.color,xaxs="i", yaxs="i", y.lim=c(0.5,length(tl$tip)+0.5),cex=tipLabelSize) 193 | 194 | # colour by trait 195 | if (!is.null(nodeColourSuccess)) { 196 | tiplabels(col= tipLabelSet,pch=16,cex=tip.colour.cex) 197 | if (ancestral.reconstruction) { nodelabels(pie=ancestral$lik.anc, cex=pie.cex, piecol=colours) } 198 | if (legend) { legend(legend.pos,legend=groupNames,fill=colours) } 199 | } 200 | 201 | if (axis) { axisPhylo(axisPos) } 202 | 203 | # PLOT INFO 204 | if (!is.null(infoFile)) { # info is provided 205 | 206 | printCols = TRUE 207 | if (!is.null(infoCols)) { 208 | if (is.na(infoCols)) { 209 | printCols = FALSE 210 | }} 211 | 212 | if (printCols) { 213 | 214 | par(mar=rep(0,4)) 215 | 216 | if (!is.null(infoCols)) {infoColNumbers = which(colnames(info.ordered) %in% infoCols)} 217 | else { infoColNumbers = 1:ncol(info.ordered)} 218 | 219 | plot(NA,axes=F,pch="",xlim=c(0,length(infoColNumbers)+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") 220 | 221 | # plot all info columns 222 | for (i in 1:length(infoColNumbers)) { 223 | j<-infoColNumbers[i] 224 | text(x=rep(i+1,nrow(info.ordered)+1),y=c((nrow(info.ordered)):1),info.ordered[,j],cex=infoCex) 225 | } 226 | 227 | } 228 | } 229 | 230 | 231 | # PLOT HEATMAP 232 | if (!is.null(heatmapData)) { 233 | 234 | if (is.null(heatmapBreaks)) { heatmapBreaks = seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1) } 235 | 236 | # plot heatmap 237 | par(mar=rep(0,4), xpd=TRUE) 238 | image((1:ncol(y.ordered))-0.5,(1:nrow(y.ordered))-0.5, as.matrix(t(y.ordered)),col=heatmap.colours,breaks=heatmapBreaks,axes=F,xaxs="i", yaxs="i", xlab="",ylab="") 239 | 240 | # draw vertical lines over heatmap 241 | if (!is.null(vlines.heatmap)) { 242 | for (v in vlines.heatmap) {abline(v=v, col=vlines.heatmap.col)} 243 | } 244 | 245 | # overlay blocks on heatmap 246 | if (!is.null(heatmap.blocks)) { 247 | for (coords in heatmap.blocks) {rect(xleft=coords[1], 0, coords[2], ncol(y.ordered), col=vlines.heatmap.col, border=NA)} 248 | } 249 | 250 | 251 | # data labels for heatmap 252 | par(mar=rep(0,4)) 253 | plot(NA, axes=F, xaxs="i", yaxs="i", ylim=c(0,2), xlim=c(0.5,ncol(y.ordered)+0.5)) 254 | text(1:ncol(y.ordered)-0.5,rep(0,ncol(x)),colnames(y.ordered), srt=90, cex=colLabelCex, pos=4) 255 | 256 | # scale for heatmap 257 | par(mar=c(2,0,0,2)) 258 | #image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",xlim=c(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T))) 259 | image(as.matrix(seq(min(y.ordered,na.rm=T),max(y.ordered,na.rm=T),length.out=length(heatmap.colours)+1)),col=heatmap.colours,yaxt="n",breaks=heatmapBreaks,axes=F) 260 | axis(1,at=heatmapBreaks[-length(heatmapBreaks)]/max(y.ordered,na.rm=T),labels=round(heatmapBreaks[-length(heatmapBreaks)],heatmapDecimalPlaces)) 261 | } 262 | 263 | # BARPLOT 264 | if (!is.null(barData)) { 265 | par(mar=rep(0,4)) 266 | barplot(barData[tip.label.order], horiz=T, axes=F, xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0.25,length(barData)+0.25),xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),col=barDataCol,border=0,width=0.5,space=1,names.arg=NA) 267 | 268 | # scale for barData plot 269 | par(mar=c(2,0,0,0)) 270 | plot(NA, yaxt="n", xaxs="i", yaxs="i", xlab="", ylab="", ylim=c(0,2), xlim=c((-1)*max(barData,na.rm=T)/20,max(barData,na.rm=T)),frame.plot=F) 271 | } 272 | 273 | # SNPS AND RECOMBINATION BLOCKS 274 | if (doBlocks) { 275 | par(mar=rep(0,4)) 276 | plot(NA,axes=F,pch="",xlim=c(genome_offset,genome_offset+genome_size+1.5),ylim=c(0.5,length(tl$tip)+0.5),xaxs="i",yaxs="i") # blank plotting area 277 | 278 | # plot snps 279 | if (!is.null(snpFile)) { 280 | snps<-read.csv(snpFile,header=F,row.names=1) # in case colnames start with numbers or contain dashes, which R does not like as column headers 281 | snps_strainCols <- snps[1,] # column names = strain names 282 | snps<-snps[-1,] # drop strain names 283 | 284 | for (strain in tip.label.order){ 285 | # print SNPs compared to ancestral alleles in column 1 286 | s<-rownames(snps)[(as.character(snps[,1]) != as.character(snps[,which(snps_strainCols==strain)])) & (as.character(snps[,which(snps_strainCols==strain)])!=gapChar) & (as.character(snps[,1])!=gapChar)] 287 | y <- which(tip.label.order==strain) 288 | if (length(s)>0) { 289 | for (x in s) { 290 | points(x,y,pch="|",col=snp_colour,cex=0.25) 291 | } 292 | } 293 | } 294 | } 295 | 296 | # plot blocks 297 | if (!is.null(blockFile)){ 298 | blocks<-read.delim(blockFile,header=F) 299 | for (i in 1:nrow(blocks)) { 300 | if (as.character(blocks[i,1]) %in% tip.label.order) { 301 | y <- which(tip.label.order==as.character(blocks[i,1])) 302 | x1 <- blocks[i,2] 303 | x2 <- blocks[i,3] 304 | lines(c(x1,x2),c(y,y),lwd=blwd,lend=2,col=block_colour) 305 | } 306 | } 307 | } 308 | 309 | } # finished with SNPs and recomb blocks 310 | 311 | # CLOSE EXTERNAL DRAWING DEVICE 312 | if (!is.null(outputPDF) | !is.null(outputPNG)) { 313 | dev.off() 314 | } 315 | 316 | # RETURN ordered info and ancestral reconstruction object 317 | if (!is.null(heatmapData)){mat=as.matrix(t(y.ordered))} 318 | else {mat=NULL} 319 | return(list(info=info.ordered,anc=ancestral,mat=mat,strain_order=tip.label.order)) 320 | } 321 | -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_tabs/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | shinyServer(function(input, output) { 6 | output$Tree <- renderPlot({ 7 | 8 | treeFile <- input$tree 9 | infoFile <- input$info 10 | heatmapFile <- input$heatmap 11 | 12 | if (is.null(treeFile)) 13 | return(NULL) 14 | 15 | plotTree(tree=treeFile$datapath,infoFile=infoFile$datapath,heatmapData=heatmapFile$datapath) 16 | 17 | }) 18 | }) -------------------------------------------------------------------------------- /shiny_practice/runPlotTree_tabs/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | tabsetPanel( 10 | 11 | tabPanel("tree", 12 | fileInput('tree', 'Choose tree file', multiple=F, 13 | accept=c('biotree/newick', '.nwk', '.tree')) 14 | ), 15 | 16 | tabPanel("metadata", 17 | fileInput('info', 'Choose info file', multiple=F, 18 | accept=c('text/csv', '.csv')) 19 | ), 20 | 21 | tabPanel("heatmap", 22 | fileInput('heatmap', 'Choose heatmap file', multiple=F, 23 | accept=c('text/csv', 24 | '.csv')) 25 | ) 26 | ) 27 | 28 | ), 29 | mainPanel( 30 | plotOutput("Tree", height=2000)) 31 | ) 32 | ) 33 | ) -------------------------------------------------------------------------------- /shiny_practice/uploadTree2/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyServer(function(input, output) { 5 | output$Tree <- renderPlot({ 6 | 7 | # input$file1 will be NULL initially. After the user selects 8 | # and uploads a file, it will be a data frame with 'name', 9 | # 'size', 'type', and 'datapath' columns. The 'datapath' 10 | # column will contain the local filenames where the data can 11 | # be found. 12 | 13 | inFile <- input$tree 14 | 15 | if (is.null(inFile)) 16 | return(NULL) 17 | 18 | t<-read.tree(inFile$datapath) 19 | plot(t) 20 | }) 21 | }) -------------------------------------------------------------------------------- /shiny_practice/uploadTree2/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | 9 | fileInput('tree', 'Choose tree file', multiple=F, 10 | accept=c('biotree/newick', 11 | '.nwk', '.tree')) 12 | 13 | fileInput('info', 'Choose info file', multiple=F, 14 | accept=c('text/csv', 15 | '.csv')) 16 | 17 | ), 18 | mainPanel( 19 | plotOutput("Tree", height=2000)) 20 | ) 21 | ) 22 | ) -------------------------------------------------------------------------------- /shiny_practice/variable_relationships.txt: -------------------------------------------------------------------------------- 1 | TO DO: choose colour palettes for node colours 2 | 3 | manually specifying R code for colour palette in heatmap isn't working... 4 | 5 | bar plots are not lined up 6 | 7 | plotting area for blocks isn't working properly 8 | 9 | 10 | INPUTS: 11 | 12 | plotTree<-function(tree,heatmapData=NULL,barData=NULL,infoFile=NULL,blockFile=NULL,snpFile=NULL,gapChar="?",genome_size=5E6,blwd=5,block_colour="black",snp_colour="red",genome_offset=0,colourNodesBy=NULL,infoCols=NULL,outputPDF=NULL,outputPNG=NULL,w,h,heatmap.colours=rev(gray(seq(0,1,0.1))),tip.labels=F,tipLabelSize=1,offset=0,tip.colour.cex=0.5,legend=T,legend.pos="bottomleft",ancestral.reconstruction=F,cluster=NULL,tipColours=NULL,lwd=1.5,axis=F,axisPos=3,edge.color="black",infoCex=0.8,colLabelCex=0.8,treeWidth=10,infoWidth=10,dataWidth=30,edgeWidth=1,labelHeight=10,mainHeight=100,barDataWidth=10,blockPlotWidth=10,barDataCol=2,heatmapBreaks=NULL,heatmapDecimalPlaces=1,vlines.heatmap=NULL,vlines.heatmap.col=2,heatmap.blocks=NULL,pie.cex=0.5) { 13 | 14 | 15 | REQUIRED INPUT, NOT CONDITIONAL: 16 | tree -> require input file in newick format. 17 | 18 | tree options: 19 | 20 | (1) lwd=1.5 21 | [width of lines in tree] 22 | 23 | (2) edge.color="black" 24 | [colour of tree branches] 25 | 26 | (3) tip.labels=F 27 | print tip labels next to the leaves 28 | if TRUE then can change: 29 | tipLabelSize=1 30 | offset=0 31 | 32 | CONDITIONAL RELATIONSHIPS: 33 | 34 | (1) metadata -> if this is checked, then display: 35 | 36 | - input file to select table (to be passed to the function as infoFile=) 37 | 38 | - name of column to use to colour tree tips [optional] 39 | - to be passed to colourNodesBy= 40 | - only valid options are column names in the info file, so ideally would load this file first and then display the column names as a dropdown list to select from) 41 | 42 | => if this is selected, display more options: 43 | -> change size of the leaf node circles by setting tip.colour.cex=0.5 44 | -> perform ancestral reconstruction? ancestral.reconstruction=F 45 | -> if yes, optionally change pie.cex=0.5 46 | -> change the default colour panel by setting tipColours= 47 | -> turn off legend (set legend=F) 48 | -> legend.pos 49 | 50 | - by default, if input file is provided, all columns will be printed next to the tree (infoCols=NULL) 51 | 52 | => options: 53 | - select names of columns to display (check boxes? or type list) infoCols=c() 54 | - switch off (set to infoCols=NA) 55 | 56 | 57 | (2) heatmap data -> if this is checked, then display: 58 | 59 | - input file to select datatable (to be passed to the function as heatmapData=) 60 | 61 | - option to change colour scheme (default heatmap.colours=rev(gray(seq(0,1,0.1)))) 62 | 63 | - option to switch on clustering of columns (set cluster=T) 64 | OR 65 | option to switch on ordering of columns according to the tree 66 | (IF the matrix is a square matrix, i.e. with columns names = tree tips as well as 67 | row names = tree tips) (set cluster="square") 68 | 69 | EXPERT OPTIONS 70 | - option to manually supply vector of breakpoints for heatmap values (heatmapBreaks=NULL) 71 | 72 | 73 | 74 | - option to specify decimal places to display in heatmap legend heatmapDecimalPlaces=1 75 | 76 | - option to change size of column labels (colLabelCex=) 77 | 78 | - option to draw vertical lines after columns 79 | e.g. lines after column 5 and 10, vlines.heatmap=c(5,10) 80 | - set colour for these lines: vlines.heatmap.col=2 81 | 82 | - manually specify colour scheme 83 | 84 | (3) bar plots -> if this is checked, then display: 85 | 86 | - input file to select table of data to be plotted as barplot (barData=) 87 | 88 | - set colour to use for plotting bar graphs: barDataCol= 89 | 90 | (4) genome blocks -> if this is checked, then display: 91 | 92 | - input file to select table of blocks (blockFile=NULL) 93 | - MUST specify genome size for plotting (genome_size=) [*same as for snps] 94 | 95 | - optionally: 96 | - line width for drawing blocks (blwd=5) 97 | - change colour for blocks (block_colour="black") 98 | 99 | <== DONE ==> 100 | 101 | (5) SNPs -> if this is checked, then display: 102 | 103 | - input file to select table of blocks (snpFile=NULL) 104 | - MUST specify genome size for plotting (genome_size=) [*same as for blocks] 105 | 106 | - optionally: 107 | - character representing unknown bases/baps (gapChar="?") 108 | - change colour for SNPs (snp_colour="red") 109 | 110 | 111 | 112 | CONTROLLING THE RELATIVE SIZES OF THE ELEMENTS: 113 | 114 | WIDTHS (this is the order displayed) 115 | treeWidth=10 116 | infoWidth=10 117 | dataWidth=30 118 | barDataWidth=10 119 | blockPlotWidth=10 120 | 121 | HEIGHTS: 122 | mainHeight=100 123 | labelHeight=10 124 | 125 | EDGES (TOP, BOTTOM, LEFT, RIGHT) 126 | edgeWidth=1 127 | 128 | -------------------------------------------------------------------------------- /shiny_practice/wan/server.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | source("plotTree.R") 4 | 5 | ### END R PLOTTING CODE 6 | 7 | shinyServer(function(input, output) { 8 | output$Tree <- renderPlot({ 9 | 10 | treeFile <- input$tree 11 | 12 | if (is.null(treeFile)) 13 | return(NULL) 14 | 15 | if (input$chk_metadata) { 16 | infoFile <- input$info 17 | } else { 18 | infoFile <- NULL 19 | } 20 | 21 | if (input$chk_heatmap) { 22 | heatmapFile <- input$heatmap 23 | } else { 24 | heatmapFile <- NULL 25 | } 26 | 27 | if (input$chk_barplot) { 28 | barplotFile <- input$barplot 29 | } else { 30 | barplotFile <- NULL 31 | } 32 | 33 | plotTree(tree=treeFile$datapath, heatmapData = heatmapFile$datapath, infoFile = infoFile$datapath, 34 | barData = barplotFile$datapath) 35 | }) 36 | }) -------------------------------------------------------------------------------- /shiny_practice/wan/ui.R: -------------------------------------------------------------------------------- 1 | library(shiny) 2 | library(ape) 3 | 4 | shinyUI(fluidPage( 5 | titlePanel("Plot tree"), 6 | sidebarLayout( 7 | sidebarPanel( 8 | fileInput('tree', 'Choose tree file', multiple=F, 9 | accept=c('biotree/newick', '.nwk', '.tree')), 10 | 11 | checkboxInput("chk_metadata", "Metadata file", value = FALSE), 12 | 13 | conditionalPanel( 14 | condition = "input.chk_metadata", 15 | fileInput('info', 'Choose metadata file', multiple = FALSE, accept = c('text/csv', '.csv')) 16 | ), 17 | 18 | checkboxInput("chk_heatmap", "Heatmap file", value = FALSE), 19 | conditionalPanel( 20 | condition = "input.chk_heatmap", 21 | fileInput('heatmap', 'Choose heatmap file', multiple = F, accept = c('text/csv', '.csv')) 22 | ) 23 | ), 24 | 25 | mainPanel(plotOutput("Tree", height=2000)) 26 | ) 27 | ) 28 | ) -------------------------------------------------------------------------------- /tree_example_april2015/.Rhistory: -------------------------------------------------------------------------------- 1 | d<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv",header=T) 2 | d[1:5,1:5] 3 | d<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv") 4 | d<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet.csv") 5 | d[1:5,1:5] 6 | d<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet.csv",row.names=1,header=T) 7 | d[1:5,1:5] 8 | d<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet.csv",row.names=1,header=F) 9 | write.csv(t(d),file="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv") 10 | write.csv(t(d),file="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv",row.names=F) 11 | d<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv",row.names=1,header=T) 12 | d[1:5,1:5] 13 | plotTree(tree,infoFile=info_file,colourNodesBy="K",tip.colour.cex=2,infoCols=c("K","OC"),treeWidth=10,infoWidth=2,tipColours=c(KL1,KL12,KL15,KL17,KL1a,KL1b,KL1c,KL20,KL25,KL4,KL40),tip.label=T,offset=0.0001,edge.color=c(KL1,KL1,KL12,KL15,KL1a,KL1b,KL25,KL4,KL1)[as.factor(edge_colours)],lwd=2,heatmapData="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv",heatmap.colours=c("grey","red"),dataWidth=20,cluster=F) 14 | dd<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv") 15 | head(dd) 16 | dd[1:5,1:5] 17 | dd<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose.csv",header=T,row.names=1) 18 | image(dd) 19 | image(as.matrix(dd)) 20 | image(as.matrix(dd),col=rev(grey(seq(0,10,1)/10))) 21 | image(as.matrix(dd),col=(grey(seq(0,10,1)/10))) 22 | image(as.matrix(dd),col=rev(grey(seq(0,10,1)/10))) 23 | image(as.matrix(dd),col=(grey(seq(0,10,1)/10))) 24 | plotTree(tree,infoFile=info_file,colourNodesBy="K",tip.colour.cex=2,infoCols=c("K","OC"),treeWidth=10,infoWidth=2,tipColours=c(KL1,KL12,KL15,KL17,KL1a,KL1b,KL1c,KL20,KL25,KL4,KL40),tip.label=T,offset=0.0001,edge.color=c(KL1,KL1,KL12,KL15,KL1a,KL1b,KL25,KL4,KL1)[as.factor(edge_colours)],lwd=2,heatmapData=dd,heatmap.colours=c("grey","red"),dataWidth=20,cluster=F) 25 | plotTree(tree,infoFile=info_file,colourNodesBy="K",tip.colour.cex=2,infoCols=c("K","OC"),treeWidth=10,infoWidth=2,tipColours=c(KL1,KL12,KL15,KL17,KL1a,KL1b,KL1c,KL20,KL25,KL4,KL40),tip.label=T,offset=0.0001,edge.color=c(KL1,KL1,KL12,KL15,KL1a,KL1b,KL25,KL4,KL1)[as.factor(edge_colours)],lwd=2,heatmapData="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose_raxID.csv",heatmap.colours=c("grey","red"),dataWidth=20,cluster=F) 26 | plotTree(tree,infoFile=info_file,colourNodesBy="K",tip.colour.cex=2,infoCols=c("K","OC"),treeWidth=10,infoWidth=2,tipColours=c(KL1,KL12,KL15,KL17,KL1a,KL1b,KL1c,KL20,KL25,KL4,KL40),tip.label=T,offset=0.0001,edge.color=c(KL1,KL1,KL12,KL15,KL1a,KL1b,KL25,KL4,KL1)[as.factor(edge_colours)],lwd=2,heatmapData="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose_raxID.csv",dataWidth=20,cluster=F) 27 | plotTree(tree,infoFile=info_file,colourNodesBy="K",tip.colour.cex=2,infoCols=c("K","OC"),treeWidth=10,infoWidth=2,tipColours=c(KL1,KL12,KL15,KL17,KL1a,KL1b,KL1c,KL20,KL25,KL4,KL40),tip.label=T,offset=0.0001,edge.color=c(KL1,KL1,KL12,KL15,KL1a,KL1b,KL25,KL4,KL1)[as.factor(edge_colours)],lwd=2,heatmapData="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose_raxID.csv",heatmap.colours=(grey(seq(0,10,1)/10)),dataWidth=20,cluster=F) 28 | plotTree(tree,infoFile=info_file,colourNodesBy="K",tip.colour.cex=2,infoCols=c("OC"),treeWidth=10,infoWidth=2,tipColours=c(KL1,KL12,KL15,KL17,KL1a,KL1b,KL1c,KL20,KL25,KL4,KL40),tip.label=T,offset=0.0001,edge.color=c(KL1,KL1,KL12,KL15,KL1a,KL1b,KL25,KL4,KL1)[as.factor(edge_colours)],lwd=2,heatmapData="/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/mapping_final/KTA1_GeneSummary_finalStrainSet_transpose_raxID.csv",heatmap.colours=(grey(seq(0,10,1)/10)),dataWidth=20,cluster=F) 29 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/ISAba1_insertSites.csv") 30 | abar 31 | head(abar) 32 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/ISAba1_insertSites.csv",row.names=1) 33 | abar[1:5,1:5] 34 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/AbaR_detail",row.names=1) 35 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/AbaR_detail.csv",row.names=1) 36 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/plotting/AbaR_detail.csv",row.names=1) 37 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/plotting/AbaR_detail.csv") 38 | head(abar) 39 | nrow(abar) 40 | ncol <- 100# 41 | ns <- 44# 42 | plot(c(0:ncol),0:(ns+1),pch="") 43 | ncol <- 100# 44 | ns <- 44# 45 | plot(c(0,ncol),c(0,ns+1),pch="") 46 | abar<-read.csv("/Users/kat/Documents/acinetobacter/GC1/GC1_paper_july2014/resistance/plotting/AbaR_detail.csv") 47 | for (i in 1:nrow(abar)) {# 48 | if (abar$AbaR_group[i] != "na") {# 49 | plot(1,i,pch=16,cex=3,col=abar$AbaR_group[i])# 50 | }# 51 | } 52 | plot(c(0,ncol),c(0,ns+1),pch="") 53 | for (i in 1:nrow(abar)) {# 54 | if (abar$AbaR_group[i] != "na") {# 55 | points(1,i,pch=16,cex=3,col=abar$AbaR_group[i])# 56 | }# 57 | } 58 | set up plotting frame# 59 | ncol <- 100# 60 | ns <- 44# 61 | plot(c(0,ncol),c(0,ns+1),pch="")# 62 | # 63 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 64 | for (i in 1:nrow(abar)) {# 65 | if (abar$AbaR_group[i] != "na") {# 66 | points(1,abar$tree.order,pch=16,cex=3,col=abar$AbaR_group[i])# 67 | }# 68 | } 69 | plot(c(0,ncol),c(0,ns+1),pch="")# 70 | # 71 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 72 | for (i in 1:nrow(abar)) {# 73 | if (abar$AbaR_group[i] != "na") {# 74 | points(1,abar$tree.order[i],pch=16,cex=2,col=abar$AbaR_group[i])# 75 | }# 76 | } 77 | plot(c(0,ncol),c(0,ns+1),pch="")# 78 | # 79 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 80 | for (i in 1:nrow(abar)) {# 81 | if (abar$AbaR_group[i] != "na") {# 82 | points(1,abar$tree.order[i],pch=16,cex=1,col=abar$AbaR_group[i])# 83 | }# 84 | } 85 | plot(c(0,ncol),c(0,ns+1),pch="")# 86 | # 87 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 88 | for (i in 1:nrow(abar)) {# 89 | if (abar$AbaR_group[i] != "na") {# 90 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 91 | }# 92 | } 93 | label with number# 94 | for (i in 1:nrow(abar)) {# 95 | if (abar$AbaR_group[i] != "na") {# 96 | text(abar$AbaR_group[i],2,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 97 | }# 98 | } 99 | for (i in 1:nrow(abar)) {# 100 | if (abar$AbaR_group[i] != "na") {# 101 | text(abar$AbaR_group[i],2,ns-i,pch=16,cex=1)# 102 | }# 103 | } 104 | plot circle to indicate AbaR presence, colour by main group, label with specific variant# 105 | for (i in 1:nrow(abar)) {# 106 | if (abar$AbaR_group[i] != "na") {# 107 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 108 | }# 109 | }# 110 | # 111 | # label with number# 112 | for (i in 1:nrow(abar)) {# 113 | if (abar$AbaR_group[i] != "na") {# 114 | text(abar$AbaR_group[i],x=2,y=ns-i,pch=16,cex=1)# 115 | }# 116 | } 117 | plot(c(0,ncol),c(0,ns+1),pch="")# 118 | # 119 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 120 | for (i in 1:nrow(abar)) {# 121 | if (abar$AbaR_group[i] != "na") {# 122 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 123 | }# 124 | }# 125 | # 126 | # label with number# 127 | for (i in 1:nrow(abar)) {# 128 | if (abar$AbaR_group[i] != "na") {# 129 | text(abar$type[i],x=2,y=ns-i,pch=16,cex=1)# 130 | }# 131 | } 132 | plot(c(0,ncol),c(0,ns+1),pch="")# 133 | # 134 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 135 | for (i in 1:nrow(abar)) {# 136 | if (abar$AbaR_group[i] != "na") {# 137 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 138 | }# 139 | }# 140 | # 141 | # label with number# 142 | for (i in 1:nrow(abar)) {# 143 | if (abar$AbaR_group[i] != "na") {# 144 | text(abar$type[i],x=4,y=ns-i,pch=16,cex=1)# 145 | }# 146 | } 147 | plot(c(0,ncol),c(0,ns+1),pch="")# 148 | # 149 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 150 | for (i in 1:nrow(abar)) {# 151 | if (abar$AbaR_group[i] != "na") {# 152 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 153 | }# 154 | }# 155 | # 156 | # label with number# 157 | for (i in 1:nrow(abar)) {# 158 | if (abar$AbaR_group[i] != "na") {# 159 | text(abar$type[i],x=4,y=ns-i,pch=16,cex=1,cex=0.5)# 160 | }# 161 | } 162 | label with number# 163 | for (i in 1:nrow(abar)) {# 164 | if (abar$AbaR_group[i] != "na") {# 165 | text(abar$type[i],x=4,y=ns-i,pch=16,cex=0.5)# 166 | }# 167 | } 168 | label AbaR type with number# 169 | for (i in 1:nrow(abar)) {# 170 | if (abar$AbaR_group[i] != "na") {# 171 | text(abar$type[i],x=4,y=ns-i,pch=16,cex=0.5,col=abar$AbaR_group[i])# 172 | }# 173 | } 174 | add sul1# 175 | sul_col <- 3# 176 | for (i in 1:nrow(abar)) {# 177 | if (abar$SulI_Sul[i] != "-") {# 178 | points(7,ns-i,pch=17,cex=1,col=sul_col)# 179 | }# 180 | } 181 | add sul1# 182 | sul_col <- 3# 183 | for (i in 1:nrow(abar)) {# 184 | if (abar$SulI_Sul[i] != "-") {# 185 | points(7,ns-i,pch=15,cex=1,col=sul_col)# 186 | }# 187 | } 188 | plot(c(0,ncol),c(0,ns+1),pch="")# 189 | # 190 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 191 | for (i in 1:nrow(abar)) {# 192 | if (abar$AbaR_group[i] != "na") {# 193 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 194 | }# 195 | }# 196 | # 197 | # label AbaR type with number# 198 | for (i in 1:nrow(abar)) {# 199 | if (abar$AbaR_group[i] != "na") {# 200 | text(abar$type[i],x=4,y=ns-i,pch=16,cex=0.5,col=abar$AbaR_group[i])# 201 | }# 202 | }# 203 | # 204 | # add sul1# 205 | sul_col <- 3# 206 | for (i in 1:nrow(abar)) {# 207 | if (abar$SulI_Sul[i] != "-") {# 208 | points(7,ns-i,pch=15,cex=1,col=sul_col)# 209 | }# 210 | } 211 | plot(c(0,ncol),c(0,ns+1),pch="")# 212 | # 213 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 214 | for (i in 1:nrow(abar)) {# 215 | if (abar$AbaR_group[i] != "na") {# 216 | points(1,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 217 | }# 218 | }# 219 | # 220 | # label AbaR type with number# 221 | for (i in 1:nrow(abar)) {# 222 | if (abar$AbaR_group[i] != "na") {# 223 | text(abar$type[i],x=4,y=ns-i,pch=16,cex=0.5)# 224 | }# 225 | }# 226 | # 227 | # add sul1# 228 | sul_col <- 3# 229 | for (i in 1:nrow(abar)) {# 230 | if (abar$SulI_Sul[i] != "-") {# 231 | points(7,ns-i,pch=15,cex=1,col=sul_col)# 232 | }# 233 | } 234 | plot(c(0,ncol),c(0,ns+1),pch="")# 235 | # 236 | # plot circle to indicate AbaR presence, colour by main group, label with specific variant# 237 | for (i in 1:nrow(abar)) {# 238 | if (abar$AbaR_group[i] != "na") {# 239 | points(4,ns-i,pch=16,cex=1,col=abar$AbaR_group[i])# 240 | }# 241 | }# 242 | # 243 | # label AbaR type with number# 244 | for (i in 1:nrow(abar)) {# 245 | if (abar$AbaR_group[i] != "na") {# 246 | text(abar$type[i],x=1,y=ns-i,pch=16,cex=0.5)# 247 | }# 248 | }# 249 | # 250 | # add sul1# 251 | sul_col <- 3# 252 | for (i in 1:nrow(abar)) {# 253 | if (abar$SulI_Sul[i] != "-") {# 254 | points(7,ns-i,pch=15,cex=1,col=sul_col)# 255 | }# 256 | } 257 | aminogly_col <- 4# 258 | for (i in 1:nrow(abar)) {if (abar$Aac3-I_AGly[i] != "-") {# 259 | points(9,ns-i,pch=15,cex=1,col=aminogly_col) } } 260 | aminogly_col <- 4# 261 | for (i in 1:nrow(abar)) {if (abar$Aac3.I_AGly[i] != "-") {# 262 | points(9,ns-i,pch=15,cex=1,col=aminogly_col) } } 263 | for (i in 1:nrow(abar)) {if (abar$AadA.AGly[i] != "-") {# 264 | points(11,ns-i,pch=15,cex=1,col=aminogly_col) } } 265 | head(abar) 266 | for (i in 1:nrow(abar)) {if (abar$AadA_AGly[i] != "-") {# 267 | points(11,ns-i,pch=15,cex=1,col=aminogly_col) } } 268 | 90*8+3200 269 | 4000/8 270 | 400*500 271 | 7+9+5+8 272 | 29*50 273 | 360/5 274 | 72+324 275 | 324-72 276 | 72+36 277 | 129+90 278 | 109*2 279 | 132.62*2 280 | 132.62*2*10 281 | 2610/9 282 | 176/2 283 | install.packages('knitr', dependencies = TRUE) 284 | library(apoe) 285 | library(ape) 286 | library(knitr) 287 | ?knit 288 | 236/4 289 | load("/Users/kat/Documents/PROJECTS/CAS_NHMCR_2013/virus_bact_data_221014_yr1paper/virus_bacteria_forKat_22oct.Rdata") 290 | ls() 291 | head(virus_bacteria) 292 | table(virus_bacteria$MPG,virus_bacteria$Category) 293 | 14+34 294 | 48/(174+333) 295 | 489/(174+333) 296 | 4/174 297 | 360/5 298 | 324-72 299 | 324+72 300 | 324+72-360 301 | 4804510-354000-180000 302 | 0.6*0.6 303 | m <- "/Users/kat/Downloads/VRE_metadataInclClinical_12022015.xlsx\ -\ baseline\ screening.csv" 304 | t <- "/Users/kat/Documents/Efaecium/VRE_Alfred/reddog/run2/CP006620_CP006620_alleles_var_cons0.95_noOutgroups.tree" 305 | source('~/code/holtlab/Rcode/plotTree.R', chdir = TRUE) 306 | getwd() 307 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST")) 308 | colnames(m) 309 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn2549")) 310 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549")) 311 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Swab.date")) 312 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Swab_date")) 313 | d<-read.csv(m) 314 | head(m) 315 | head(d) 316 | colnames(d) 317 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number")) 318 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community")) 319 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community") 320 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community",colours=c(2,1)) 321 | plotTree 322 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community", tipColours =c(2,1)) 323 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community", tipColours =c(2,1),tipColourCex=3) 324 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community", tipColours =c(2,1),tip.colour.cex=3) 325 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community", tipColours =c(2,1),tip.colour.cex=2) 326 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community", tipColours =c(2,1),tip.colour.cex=2) 327 | colnames(d) 328 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549.S.L","Swab_date","Swab.Number","community"),colourNodesBy="community", tipColours =c(2,1),tip.colour.cex=2,"H_admission_to_swab","abtmt") 329 | plotTree(t,infoFile=m,infoCols=c("Study.id","Name.code","MLST","Tn1549.S.L","Swab_date","Swab.Number","community","H_admission_to_swab","abtmt"),colourNodesBy="community", tipColours =c(2,1),tip.colour.cex=2) 330 | 694+789+478+742+651+379+600 331 | 60+81+38+10 332 | 4333+ 189 333 | 27+42 334 | 5/6 335 | 0.23*22 336 | 17/23 337 | 17/22 338 | /5 339 | 96/5 340 | 48/5 341 | 20/4 342 | 96*2 343 | 26*5 344 | 130/48 345 | 5*52 346 | r<-read.csv("~/Downloads/Aussie conferences (Responses) - Form Responses.csv") 347 | colnames(r) 348 | plot(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.) 349 | lm(r$Invited.talks~r$Contributed.talks..selected.from.abstacts.) 350 | summary(lm(r$Invited.talks~r$Contributed.talks..selected.from.abstacts.)) 351 | cor.test(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.) 352 | cor.test(r$Invited.talks,r$Organisers) 353 | cor.test(r$Contributed.talks..selected.from.abstacts.,r$Organisers) 354 | plot(r$Contributed.talks..selected.from.abstacts.,r$Organisers) 355 | cor.test(r$Invited.talks,r$Organisers) 356 | lm(r$Invited.talks~r$Contributed.talks..selected.from.abstacts.) 357 | cor.test(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.) 358 | plot(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.,pch=16) 359 | abline(coef(lm(r$Contributed.talks..selected.from.abstacts.~r$Invited.talks))) 360 | plot(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.,pch=16,ylab="Contributed",xlab="Invited") 361 | abline(coef(lm(r$Contributed.talks..selected.from.abstacts.~r$Invited.talks))) 362 | plot(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.,pch=16,ylab="% Female speakers (Contributed talks)",xlab="% Fmale speakers (Invited talks)") 363 | plot(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.,pch=16,ylab="% Female speakers (Contributed talks)",xlab="% Fmale speakers (Invited talks)",col=2) 364 | abline(coef(lm(r$Contributed.talks..selected.from.abstacts.~r$Invited.talks))) 365 | text(x=70,y=30,"Correlation=0.5, p=0.03") 366 | text(x=70,y=30,"Correlation=0.5, p=0.03")e 367 | plot(r$Invited.talks,r$Contributed.talks..selected.from.abstacts.,pch=16,ylab="% Female speakers (Contributed talks)",xlab="% Female speakers (Invited talks)",col=2) 368 | text(x=70,y=30,"R2=0.2, p=0.03") 369 | abline(coef(lm(r$Contributed.talks..selected.from.abstacts.~r$Invited.talks))) 370 | lm(r$Contributed.talks..selected.from.abstacts.~r$Invited.talks) 371 | summary(lm(r$Contributed.talks..selected.from.abstacts.~r$Invited.talks)) 372 | summary(lm(r$Contributed.talks..selected.from.abstacts.+r$Invited.talks~r$Organisers)) 373 | plot(r$Organisers,r$Contributed.talks..selected.from.abstacts.,pch=16,ylab="% Female speakers (Contributed talks)",xlab="% Female organisers",col=2) 374 | head(r) 375 | 14/(14+38) 376 | 13/(13+430) 377 | 38/(14+38) 378 | 430/(13+430) 379 | 5/7 380 | 2/7 381 | 9/45 382 | 24+87+46+36 383 | 193-4-15 384 | 140/0.35 385 | 35/2 386 | 3.1*6.9 387 | install.packages("shiny") 388 | library(shiny) 389 | runExample("01_hello") 390 | source("~/code/plotTree/plotTree.R") 391 | plotTree(tree="tree.nwk",infoFile="info.csv") 392 | plotTree(tree="tree.nwk") 393 | ls() 394 | plotTree(tree="tree.nwk",barData="bar.csv") 395 | plotTree(tree="tree.nwk",barData="bar.csv") 396 | plotTree(tree="tree.nwk",barData="bar.csv",blockFile="blocks.csv"genome_size=5E6) 397 | plotTree(tree="tree.nwk",barData="bar.csv",blockFile="blocks.csv",genome_size=5E6) 398 | plotTree(tree="tree.nwk",barData="bar.csv",blockFile="blocks.csv",genome_size=5E6) 399 | plotTree(tree="tree.nwk",barData="bar.csv",blockFile="blocks.txt",genome_size=5E6) 400 | plotTree(tree="tree.nwk",barData="bar.csv",blockFile="blocks.txt",genome_size=5E6) 401 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5E6) 402 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5E6) 403 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5E7) 404 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5E1) 405 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5E) 406 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5) 407 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=5) 408 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10) 409 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=10,blwd=10) 410 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=100,blwd=10) 411 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=50,blwd=10) 412 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=20,blwd=10) 413 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=10,blwd=10) 414 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10) 415 | source("~/code/plotTree/plotTree2.R") 416 | source("~/code/holtlab/Rcode/plotTree2.R") 417 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10) 418 | ?lines 419 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10,snpFile="alleles.csv") 420 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10,snpFile="alleles.csv") 421 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10,snpFile="alleles.csv") 422 | plotTree(tree="tree.nwk",genome_size=5E6,blwd=10,snpFile="alleles.csv") 423 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5,blwd=10,snpFile="alleles.csv") 424 | plotTree(tree="tree.nwk",blockFile="blocks.txt",genome_size=5E6,blwd=10,snpFile="alleles.csv") 425 | runApp("~/code/plotTree/shiny_practice/reactive/") 426 | library(shiny) 427 | runApp("~/code/plotTree/shiny_practice/reactive/") 428 | runApp("~/code/plotTree/shiny_practice/reactive/") 429 | runApp("~/code/plotTree/shiny_practice/reactive/") 430 | runApp("~/code/plotTree/shiny_practice/reactive/") 431 | runApp("~/code/plotTree/shiny_practice/reactive/") 432 | runApp("~/code/plotTree/shiny_practice/reactive/") 433 | runApp("~/code/plotTree/shiny_practice/reactive/") 434 | runApp("~/code/plotTree/shiny_practice/reactive/") 435 | runApp("~/code/plotTree/shiny_practice/reactive/") 436 | runApp("~/code/plotTree/shiny_practice/reactive/") 437 | runApp("~/code/plotTree/shiny_practice/reactive/") 438 | runApp("~/code/plotTree/shiny_practice/reactive/") 439 | runApp("~/code/plotTree/shiny_practice/reactive/") 440 | runApp("~/code/plotTree/shiny_practice/reactive/") 441 | runApp("~/code/plotTree/shiny_practice/reactive/") 442 | runApp("~/code/plotTree/shiny_practice/reactive/") 443 | runApp("~/code/plotTree/shiny_practice/reactive/") 444 | runApp("~/code/plotTree/shiny_practice/reactive/") 445 | runApp("~/code/plotTree/shiny_practice/reactive/") 446 | runApp("~/code/plotTree/shiny_practice/reactive/") 447 | runApp("~/code/plotTree/shiny_practice/reactive/") 448 | runApp("~/code/plotTree/shiny_practice/reactive/") 449 | runApp("~/code/plotTree/shiny_practice/reactive/") 450 | runApp("~/code/plotTree/shiny_practice/reactive/") 451 | -------------------------------------------------------------------------------- /tree_example_april2015/bar.csv: -------------------------------------------------------------------------------- 1 | id,dummy_bar_value CS14_Brazil_2001,5 CS2_Brazil_1997,10 CS20_Brazil_2002,2 CS6_Brazil_2000,7 CS7_Brazil_2000,8 373_Cameroun_1973,10 32222_Cuba_2003,7 54185_Denmark_1945,2 20061758_Dominican,6 20051272_Egypt_2005,1 20060018_Egypt_2006,7 20061309_Egypt_2006,8 20062087_Egypt_Tunisia_2006,8 259_France_1959,2 476_France_1976,8 658_France_1958,9 1166_France_1966,3 1167_France_1967,4 1173_France_1973,2 1263_France_1963,7 1265_France_1965,2 1267_France_1967,10 1274_France_1974,4 1460_France_1960,9 1461_France_1961,4 1761_France_1961,5 2073_France_1973,9 2574_France_1974,4 4374_France_1974,10 4474_France_1974,1 8883_France_1983,3 54213_France_1945,7 998911_France_1999,8 IB48279_France_NA,9 pWR105_France_NA,1 2225_French,4 988743_French,3 10014_HCMC_2009,10 10021_HCMC_2009,1 10031_HCMC_2009,4 10035_HCMC_2009,3 10060_HCMC_2009,2 10063_HCMC_2009,2 10071_HCMC_2009,10 10073_HCMC_2009,2 10083_HCMC_2009,8 10093_HCMC_2009,6 10102_HCMC_2009,5 10111_HCMC_2009,4 10115_HCMC_2009,3 10134_HCMC_2009,7 10135_HCMC_2009,5 10152_HCMC_2009,9 10159_HCMC_2009,9 10188_HCMC_2009,2 10263_HCMC_2009,1 10320_HCMC_2010,5 10365_HCMC_2010,9 20006_HCMC_2009,5 20021_HCMC_2009,7 20023_HCMC_2009,9 20037_HCMC_2009,7 20070_HCMC_2009,3 20094_HCMC_2009,8 20228_HCMC_2009,3 20261_HCMC_2009,6 20263_HCMC_2009,3 20343_HCMC_2009,10 30003_HCMC_2009,1 30008_HCMC_2009,3 30010_HCMC_2009,1 30037_HCMC_2009,10 30054_HCMC_2009,5 30059_HCMC_2009,5 30071_HCMC_2009,7 30073_HCMC_2009,8 30100_HCMC_2009,8 30112_HCMC_2009,1 30124_HCMC_2009,8 30162_HCMC_2009,7 30164_HCMC_2009,7 30169_HCMC_2009,1 30172_HCMC_2009,2 30174_HCMC_2009,3 30233_HCMC_2009,2 30293_HCMC_2009,8 30366_HCMC_2009,10 30371_HCMC_2009,7 30387_HCMC_2010,8 30450_HCMC_2010,4 30451_HCMC_2010,2 DE0115_HCMC_2000,5 DE0127_HCMC_2000,10 DE0199_HCMC_2000,5 DE0248_HCMC_2000,3 DE0295_HCMC_2000,3 DE0303_HCMC_2000,6 DE0306_HCMC_2000,10 DE0330_HCMC_2000,5 DE0427_HCMC_2000,5 DE0477_HCMC_2000,2 DE0489_HCMC_2000,5 DE0490_HCMC_2000,5 DE0579_HCMC_2001,4 DE0611_HCMC_2001,1 DE0654_HCMC_2001,8 DE0655_HCMC_2001,8 DE0685_HCMC_2001,6 DE0816_HCMC_2001,7 DE0846_HCMC_2001,7 DE0885_HCMC_2001,4 DE0891_HCMC_2001,8 DE0900_HCMC_2001,2 DE0965_HCMC_2001,9 DE1063_HCMC_2002,7 DE1140_HCMC_2002,2 DE1150_HCMC_2002,2 DE1165_HCMC_2002,7 DE1191_HCMC_2002,9 DE1198_HCMC_2002,9 DE1208_HCMC_2002,2 DE1209_HCMC_2002,7 DE1256_HCMC_2002,1 DE1318_HCMC_2002,3 DE1336_HCMC_2002,2 DE1404_HCMC_2002,1 DE1486_HCMC_2002,2 EG0129_HCMC_2007,2 EG0159_HCMC_2007,3 EG0204_HCMC_2008,2 EG0255_HCMC_2008,1 EG0304_HCMC_2006,7 EG0309_HCMC_2006,1 EG0313_HCMC_2006,2 EG0315_HCMC_2006,4 EG0318_HCMC_2006,4 EG0352_HCMC_2007,7 EG0357_HCMC_2007,5 EG0362_HCMC_2007,5 EG0365_HCMC_2007,6 EG0369_HCMC_2007,3 EG0372_HCMC_2007,6 EG0373_HCMC_2007,8 EG0375_HCMC_2007,3 EG0379_HCMC_2007,5 EG0383_HCMC_2007,9 EG0385_HCMC_2007,5 EG0386_HCMC_2007,8 EG0388_HCMC_2007,7 EG0390_HCMC_2007,10 EG0392_HCMC_2007,6 EG0393_HCMC_2007,4 EG0394_HCMC_2007,8 EG0395_HCMC_2007,3 EG0401_HCMC_2007,2 EG0404_HCMC_2007,10 EG0410_HCMC_2007,4 EG0425_HCMC_2008,7 EG0430_HCMC_2008,7 EG0451_HCMC_2008,9 EG0467_HCMC_2008,4 EG0472_HCMC_2008,8 EG1001_HCMC_2008,9 EG1004_HCMC_2008,5 EG1008_HCMC_2008,10 EG1014_HCMC_2009,4 EG1015_HCMC_2009,3 EG1016_HCMC_2009,4 EG1017_HCMC_2009,10 EG1018_HCMC_2009,10 EG1019_HCMC_2009,9 EG1020_HCMC_2009,8 EG1021_HCMC_2009,4 EG1022_HCMC_2009,5 EG1023_HCMC_2009,6 EG1024_HCMC_2009,6 EG1025_HCMC_2009,9 EG1026_HCMC_0,7 EG1027_HCMC_0,6 EG1028_HCMC_0,5 EG1029_HCMC_0,4 HUE01_HCMC_2008,1 HUE02_HCMC_2008,3 HUE05_HCMC_2008,7 HUE16_HCMC_2009,7 HUE17_HCMC_2009,5 HUE19_HCMC_2009,5 HUE20_HCMC_2009,10 HUE21_HCMC_2009,6 HUE22_HCMC_2009,1 HUE23_HCMC_2009,10 HUE24_HCMC_2009,8 HUE25_HCMC_2009,1 HUE26_HCMC_2009,8 HUE27_HCMC_2009,3 HUE29_HCMC_2009,10 HUE30_HCMC_2009,9 HUE31_HCMC_2009,2 HUE32_HCMC_2009,6 HUE33_HCMC_2009,7 HUE34_HCMC_2009,1 HUE40_HCMC_2009,2 HUE42_HCMC_2009,2 HUE43_HCMC_2009,1 HUE46_HCMC_2009,5 HUE47_HCMC_2009,4 HUE48_HCMC_2009,4 HUE50_HCMC_2009,6 HUE53_HCMC_2010,1 HUE55_HCMC_2010,7 HUE57_HCMC_2010,1 HUE58_HCMC_2010,6 HUE60_HCMC_2010,6 HUE62_HCMC_2010,6 HUE64_HCMC_2010,4 HUE67_HCMC_2010,3 HUE68_HCMC_2010,2 KH02_HCMC_2009,8 KH04_HCMC_2009,3 KH05_HCMC_2009,7 KH06_HCMC_2009,10 KH07_HCMC_2009,10 KH09_HCMC_2009,3 KH10_HCMC_2009,5 KH11_HCMC_2009,1 KH12_HCMC_2009,6 KH13_HCMC_2009,9 KH14_HCMC_2009,10 KH15_HCMC_2009,4 KH16_HCMC_2009,5 KH17_HCMC_2009,4 KH18_HCMC_2009,6 KH19_HCMC_2009,8 KH20_HCMC_2009,3 KH21_HCMC_2009,2 KH23_HCMC_2009,4 KH24_HCMC_2009,5 KH25_HCMC_2009,9 KH26_HCMC_2009,3 KH27_HCMC_2009,7 KH28_HCMC_2009,9 KH29_HCMC_2009,3 KH30_HCMC_2009,9 KH32_HCMC_2009,8 KH33_HCMC_2009,7 KH34_HCMC_2009,5 KH35_HCMC_2009,10 KH37_HCMC_2009,3 KH38_HCMC_2009,4 KH40_HCMC_2009,6 KH41_HCMC_2009,9 KH42_HCMC_2010,6 KH43_HCMC_2010,7 KH45_HCMC_2010,2 KH53_HCMC_2010,6 KH54_HCMC_2010,1 KH55_HCMC_2010,2 KH57_HCMC_2010,8 MS0004_HCMC_1995,5 MS0011_HCMC_1995,4 MS0032_HCMC_1995,9 MS0034_HCMC_1995,9 MS0035_HCMC_1995,4 MS0039_HCMC_1995,2 MS0042_HCMC_1995,4 MS0043_HCMC_1995,5 MS0048_HCMC_1995,7 MS0063_HCMC_1995,5 MS0065_HCMC_1995,5 MS0069_HCMC_1995,6 MS0070_HCMC_1995,7 MS0080_HCMC_1995,10 MS0083_HCMC_1996,3 MS0094_HCMC_1996,2 MS0102_HCMC_1996,8 MS0110_HCMC_1996,6 MS0111_HCMC_1996,3 MS0119_HCMC_1996,9 MS0122_HCMC_1996,4 MS0127_HCMC_1996,7 MS0128_HCMC_1996,10 20031275_Iran_2003,7 31382_Israel_2003,8 20040924_Kenya_Egypt_2004,8 IB1_Korea_2003,1 IB10_Korea_2003,10 IB2_Korea_2003,1 IB2493_Korea_NA,10 IB3_Korea_2003,6 IB681_Korea_1991,6 IB683_Korea_1994,8 IB687_Korea_1998,5 IB690_Korea_2000,3 IB691_Korea_1999,6 IB694_Korea_1979,7 IB695_Korea_1983,3 IB696_Korea_1980,9 IB697_Korea_1982,5 IB698_Korea_1983,1 IB713_Korea_1981,5 IB716_Korea_1981,7 IB717_Korea_1982,1 IB739_Korea_1985,9 IB748_Korea_1987,8 53G_Korea_2000,1 5827_Madagascar_2000,2 989560_Madagascar_1998,9 9810267_Madagascar_1998,3 19984123_Mexico_1998,7 20062313_Nepal_2006,8 970044_New,10 IB3277_Pakistan_2002,9 IB3300_Pakistan_2002,3 IB3374_Pakistan_2002,10 IB3488_Pakistan_2003,8 IB3507_Pakistan_2003,4 IB3580_Pakistan_2003,4 IB3599_Pakistan_2003,5 20052631_Peru_2005,2 273_Senegal_1973,4 1567_Senegal_1967,8 36224_Senegal_2003,3 20040880_SriLanka_2004,5 Ss046_Ref_1950,2 54178_Sweden_1945,6 54179_Sweden_1944,6 54181_Sweden_1945,9 54184_Sweden_1945,4 54190_Sweden_1945,6 54210_Sweden_1943,1 54216_Sweden_1946,1 54228_Sweden_1947,3 19904011_unk_1990,5 19910761_unk_1991,1 19911483_unk_1991,1 19920319_unk_1992,5 20003593_unk_2000,1 20010007_unk_2001,3 20011685_unk_2001,3 20021122_unk_2002,8 20040489_unk_2004,2 20041367_unk_2004,4 20071599_unk_2007,9 20081885_unk_2008,7 20051541_Uzbekistan_2005,7 IB1970_Vietnam_2001,2 IB1976_Vietnam_2002,9 IB1980_Vietnam_2002,2 IB1985_Vietnam_2002,1 IB1987_Vietnam_2002,3 IB1990_Vietnam_2003,2 IB1993_Vietnam_2003,3 IB1995_Vietnam_2003,2 IB1997_Vietnam_2003,1 IB2000_Vietnam_2003,10 IB2004_Vietnam_2003,7 IB2008_Vietnam_2003,4 IB2009_Vietnam_2003,3 IB2012_Vietnam_2001,3 IB2013_Vietnam_2001,5 IB2015_Vietnam_2002,3 IB2018_Vietnam_2002,8 IB2024_Vietnam_2002,5 IB2026_Vietnam_2003,1 #N/A,9 10083_HCMC_NA,7 10093_HCMC_NA,9 10102_HCMC_NA,8 10111_HCMC_NA,8 10115_HCMC_NA,4 10134_HCMC_NA,3 10135_HCMC_NA,5 10152_HCMC_NA,1 10159_HCMC_NA,7 10188_HCMC_NA,4 10014_HCMC_NA,5 10263_HCMC_NA,7 10320_HCMC_NA,6 10365_HCMC_NA,8 10021_HCMC_NA,4 10031_HCMC_NA,1 10035_HCMC_NA,2 10060_HCMC_NA,4 10063_HCMC_NA,10 10071_HCMC_NA,1 10073_HCMC_NA,6 -------------------------------------------------------------------------------- /tree_example_april2015/blocks.csv: -------------------------------------------------------------------------------- 1 | id,start,stop 30037_HCMC_2009,1,20000 DE0685_HCMC_2001,1.00E+06,1.50E+06 DE1140_HCMC_2002,3.00E+06,3.20E+06 EG0369_HCMC_2007,5.00E+05,8.00E+05 HUE20_HCMC_2009,3.40E+06,3.50E+06 HUE62_HCMC_2010,2.10E+06,2.40E+06 KH14_HCMC_2009,1.80E+06,2.00E+06 KH45_HCMC_2010,9.00E+05,1.50E+06 -------------------------------------------------------------------------------- /tree_example_april2015/blocks.txt: -------------------------------------------------------------------------------- 1 | id start stop 30037_HCMC_2009 1 20000 DE0685_HCMC_2001 1.00E+06 1.50E+06 DE1140_HCMC_2002 3.00E+06 3.20E+06 EG0369_HCMC_2007 5.00E+05 8.00E+05 HUE20_HCMC_2009 3.40E+06 3.50E+06 HUE62_HCMC_2010 2.10E+06 2.40E+06 KH14_HCMC_2009 1.80E+06 2.00E+06 KH45_HCMC_2010 9.00E+05 1.50E+06 -------------------------------------------------------------------------------- /tree_example_april2015/info.csv: -------------------------------------------------------------------------------- 1 | id,location,name,country,year, CS14_Brazil_2001,other,CS14,Brazil,2001, CS2_Brazil_1997,other,CS2,Brazil,1997, CS20_Brazil_2002,other,CS20,Brazil,2002, CS6_Brazil_2000,other,CS6,Brazil,2000, CS7_Brazil_2000,other,CS7,Brazil,2000, 373_Cameroun_1973,other,373,Cameroun,1973, 32222_Cuba_2003,other,32222,Cuba,2003, 54185_Denmark_1945,other,54185,Denmark,1945, 20061758_Dominican,other,20061758,Dominican,, 20051272_Egypt_2005,other,20051272,Egypt,2005, 20060018_Egypt_2006,other,20060018,Egypt,2006, 20061309_Egypt_2006,other,20061309,Egypt,2006, 20062087_Egypt_Tunisia_2006,other,20062087,Egypt,Tunisia,2006 259_France_1959,other,259,France,1959, 476_France_1976,other,476,France,1976, 658_France_1958,other,658,France,1958, 1166_France_1966,other,1166,France,1966, 1167_France_1967,other,1167,France,1967, 1173_France_1973,other,1173,France,1973, 1263_France_1963,other,1263,France,1963, 1265_France_1965,other,1265,France,1965, 1267_France_1967,other,1267,France,1967, 1274_France_1974,other,1274,France,1974, 1460_France_1960,other,1460,France,1960, 1461_France_1961,other,1461,France,1961, 1761_France_1961,other,1761,France,1961, 2073_France_1973,other,2073,France,1973, 2574_France_1974,other,2574,France,1974, 4374_France_1974,other,4374,France,1974, 4474_France_1974,other,4474,France,1974, 8883_France_1983,other,8883,France,1983, 54213_France_1945,other,54213,France,1945, 998911_France_1999,other,998911,France,1999, IB48279_France_NA,other,IB48279,France,NA, pWR105_France_NA,other,pWR105,France,NA, 2225_French,other,2225,French,, 988743_French,other,988743,French,, 10014_HCMC_2009,HCMC,10014,HCMC,2009, 10021_HCMC_2009,HCMC,10021,HCMC,2009, 10031_HCMC_2009,HCMC,10031,HCMC,2009, 10035_HCMC_2009,HCMC,10035,HCMC,2009, 10060_HCMC_2009,HCMC,10060,HCMC,2009, 10063_HCMC_2009,HCMC,10063,HCMC,2009, 10071_HCMC_2009,HCMC,10071,HCMC,2009, 10073_HCMC_2009,HCMC,10073,HCMC,2009, 10083_HCMC_2009,HCMC,10083,HCMC,2009, 10093_HCMC_2009,HCMC,10093,HCMC,2009, 10102_HCMC_2009,HCMC,10102,HCMC,2009, 10111_HCMC_2009,HCMC,10111,HCMC,2009, 10115_HCMC_2009,HCMC,10115,HCMC,2009, 10134_HCMC_2009,HCMC,10134,HCMC,2009, 10135_HCMC_2009,HCMC,10135,HCMC,2009, 10152_HCMC_2009,HCMC,10152,HCMC,2009, 10159_HCMC_2009,HCMC,10159,HCMC,2009, 10188_HCMC_2009,HCMC,10188,HCMC,2009, 10263_HCMC_2009,HCMC,10263,HCMC,2009, 10320_HCMC_2010,HCMC,10320,HCMC,2010, 10365_HCMC_2010,HCMC,10365,HCMC,2010, 20006_HCMC_2009,HCMC,20006,HCMC,2009, 20021_HCMC_2009,HCMC,20021,HCMC,2009, 20023_HCMC_2009,HCMC,20023,HCMC,2009, 20037_HCMC_2009,HCMC,20037,HCMC,2009, 20070_HCMC_2009,HCMC,20070,HCMC,2009, 20094_HCMC_2009,HCMC,20094,HCMC,2009, 20228_HCMC_2009,HCMC,20228,HCMC,2009, 20261_HCMC_2009,HCMC,20261,HCMC,2009, 20263_HCMC_2009,HCMC,20263,HCMC,2009, 20343_HCMC_2009,HCMC,20343,HCMC,2009, 30003_HCMC_2009,HCMC,30003,HCMC,2009, 30008_HCMC_2009,HCMC,30008,HCMC,2009, 30010_HCMC_2009,HCMC,30010,HCMC,2009, 30037_HCMC_2009,HCMC,30037,HCMC,2009, 30054_HCMC_2009,HCMC,30054,HCMC,2009, 30059_HCMC_2009,HCMC,30059,HCMC,2009, 30071_HCMC_2009,HCMC,30071,HCMC,2009, 30073_HCMC_2009,HCMC,30073,HCMC,2009, 30100_HCMC_2009,HCMC,30100,HCMC,2009, 30112_HCMC_2009,HCMC,30112,HCMC,2009, 30124_HCMC_2009,HCMC,30124,HCMC,2009, 30162_HCMC_2009,HCMC,30162,HCMC,2009, 30164_HCMC_2009,HCMC,30164,HCMC,2009, 30169_HCMC_2009,HCMC,30169,HCMC,2009, 30172_HCMC_2009,HCMC,30172,HCMC,2009, 30174_HCMC_2009,HCMC,30174,HCMC,2009, 30233_HCMC_2009,HCMC,30233,HCMC,2009, 30293_HCMC_2009,HCMC,30293,HCMC,2009, 30366_HCMC_2009,HCMC,30366,HCMC,2009, 30371_HCMC_2009,HCMC,30371,HCMC,2009, 30387_HCMC_2010,HCMC,30387,HCMC,2010, 30450_HCMC_2010,HCMC,30450,HCMC,2010, 30451_HCMC_2010,HCMC,30451,HCMC,2010, DE0115_HCMC_2000,HCMC,DE0115,HCMC,2000, DE0127_HCMC_2000,HCMC,DE0127,HCMC,2000, DE0199_HCMC_2000,HCMC,DE0199,HCMC,2000, DE0248_HCMC_2000,HCMC,DE0248,HCMC,2000, DE0295_HCMC_2000,HCMC,DE0295,HCMC,2000, DE0303_HCMC_2000,HCMC,DE0303,HCMC,2000, DE0306_HCMC_2000,HCMC,DE0306,HCMC,2000, DE0330_HCMC_2000,HCMC,DE0330,HCMC,2000, DE0427_HCMC_2000,HCMC,DE0427,HCMC,2000, DE0477_HCMC_2000,HCMC,DE0477,HCMC,2000, DE0489_HCMC_2000,HCMC,DE0489,HCMC,2000, DE0490_HCMC_2000,HCMC,DE0490,HCMC,2000, DE0579_HCMC_2001,HCMC,DE0579,HCMC,2001, DE0611_HCMC_2001,HCMC,DE0611,HCMC,2001, DE0654_HCMC_2001,HCMC,DE0654,HCMC,2001, DE0655_HCMC_2001,HCMC,DE0655,HCMC,2001, DE0685_HCMC_2001,HCMC,DE0685,HCMC,2001, DE0816_HCMC_2001,HCMC,DE0816,HCMC,2001, DE0846_HCMC_2001,HCMC,DE0846,HCMC,2001, DE0885_HCMC_2001,HCMC,DE0885,HCMC,2001, DE0891_HCMC_2001,HCMC,DE0891,HCMC,2001, DE0900_HCMC_2001,HCMC,DE0900,HCMC,2001, DE0965_HCMC_2001,HCMC,DE0965,HCMC,2001, DE1063_HCMC_2002,HCMC,DE1063,HCMC,2002, DE1140_HCMC_2002,HCMC,DE1140,HCMC,2002, DE1150_HCMC_2002,HCMC,DE1150,HCMC,2002, DE1165_HCMC_2002,HCMC,DE1165,HCMC,2002, DE1191_HCMC_2002,HCMC,DE1191,HCMC,2002, DE1198_HCMC_2002,HCMC,DE1198,HCMC,2002, DE1208_HCMC_2002,HCMC,DE1208,HCMC,2002, DE1209_HCMC_2002,HCMC,DE1209,HCMC,2002, DE1256_HCMC_2002,HCMC,DE1256,HCMC,2002, DE1318_HCMC_2002,HCMC,DE1318,HCMC,2002, DE1336_HCMC_2002,HCMC,DE1336,HCMC,2002, DE1404_HCMC_2002,HCMC,DE1404,HCMC,2002, DE1486_HCMC_2002,HCMC,DE1486,HCMC,2002, EG0129_HCMC_2007,HCMC,EG0129,HCMC,2007, EG0159_HCMC_2007,HCMC,EG0159,HCMC,2007, EG0204_HCMC_2008,HCMC,EG0204,HCMC,2008, EG0255_HCMC_2008,HCMC,EG0255,HCMC,2008, EG0304_HCMC_2006,HCMC,EG0304,HCMC,2006, EG0309_HCMC_2006,HCMC,EG0309,HCMC,2006, EG0313_HCMC_2006,HCMC,EG0313,HCMC,2006, EG0315_HCMC_2006,HCMC,EG0315,HCMC,2006, EG0318_HCMC_2006,HCMC,EG0318,HCMC,2006, EG0352_HCMC_2007,HCMC,EG0352,HCMC,2007, EG0357_HCMC_2007,HCMC,EG0357,HCMC,2007, EG0362_HCMC_2007,HCMC,EG0362,HCMC,2007, EG0365_HCMC_2007,HCMC,EG0365,HCMC,2007, EG0369_HCMC_2007,HCMC,EG0369,HCMC,2007, EG0372_HCMC_2007,HCMC,EG0372,HCMC,2007, EG0373_HCMC_2007,HCMC,EG0373,HCMC,2007, EG0375_HCMC_2007,HCMC,EG0375,HCMC,2007, EG0379_HCMC_2007,HCMC,EG0379,HCMC,2007, EG0383_HCMC_2007,HCMC,EG0383,HCMC,2007, EG0385_HCMC_2007,HCMC,EG0385,HCMC,2007, EG0386_HCMC_2007,HCMC,EG0386,HCMC,2007, EG0388_HCMC_2007,HCMC,EG0388,HCMC,2007, EG0390_HCMC_2007,HCMC,EG0390,HCMC,2007, EG0392_HCMC_2007,HCMC,EG0392,HCMC,2007, EG0393_HCMC_2007,HCMC,EG0393,HCMC,2007, EG0394_HCMC_2007,HCMC,EG0394,HCMC,2007, EG0395_HCMC_2007,HCMC,EG0395,HCMC,2007, EG0401_HCMC_2007,HCMC,EG0401,HCMC,2007, EG0404_HCMC_2007,HCMC,EG0404,HCMC,2007, EG0410_HCMC_2007,HCMC,EG0410,HCMC,2007, EG0425_HCMC_2008,HCMC,EG0425,HCMC,2008, EG0430_HCMC_2008,HCMC,EG0430,HCMC,2008, EG0451_HCMC_2008,HCMC,EG0451,HCMC,2008, EG0467_HCMC_2008,HCMC,EG0467,HCMC,2008, EG0472_HCMC_2008,HCMC,EG0472,HCMC,2008, EG1001_HCMC_2008,HCMC,EG1001,HCMC,2008, EG1004_HCMC_2008,HCMC,EG1004,HCMC,2008, EG1008_HCMC_2008,HCMC,EG1008,HCMC,2008, EG1014_HCMC_2009,HCMC,EG1014,HCMC,2009, EG1015_HCMC_2009,HCMC,EG1015,HCMC,2009, EG1016_HCMC_2009,HCMC,EG1016,HCMC,2009, EG1017_HCMC_2009,HCMC,EG1017,HCMC,2009, EG1018_HCMC_2009,HCMC,EG1018,HCMC,2009, EG1019_HCMC_2009,HCMC,EG1019,HCMC,2009, EG1020_HCMC_2009,HCMC,EG1020,HCMC,2009, EG1021_HCMC_2009,HCMC,EG1021,HCMC,2009, EG1022_HCMC_2009,HCMC,EG1022,HCMC,2009, EG1023_HCMC_2009,HCMC,EG1023,HCMC,2009, EG1024_HCMC_2009,HCMC,EG1024,HCMC,2009, EG1025_HCMC_2009,HCMC,EG1025,HCMC,2009, EG1026_HCMC_0,HCMC,EG1026,HCMC,0, EG1027_HCMC_0,HCMC,EG1027,HCMC,0, EG1028_HCMC_0,HCMC,EG1028,HCMC,0, EG1029_HCMC_0,HCMC,EG1029,HCMC,0, HUE01_HCMC_2008,Hue,HUE01,HCMC,2008, HUE02_HCMC_2008,Hue,HUE02,HCMC,2008, HUE05_HCMC_2008,Hue,HUE05,HCMC,2008, HUE16_HCMC_2009,Hue,HUE16,HCMC,2009, HUE17_HCMC_2009,Hue,HUE17,HCMC,2009, HUE19_HCMC_2009,Hue,HUE19,HCMC,2009, HUE20_HCMC_2009,Hue,HUE20,HCMC,2009, HUE21_HCMC_2009,Hue,HUE21,HCMC,2009, HUE22_HCMC_2009,Hue,HUE22,HCMC,2009, HUE23_HCMC_2009,Hue,HUE23,HCMC,2009, HUE24_HCMC_2009,Hue,HUE24,HCMC,2009, HUE25_HCMC_2009,Hue,HUE25,HCMC,2009, HUE26_HCMC_2009,Hue,HUE26,HCMC,2009, HUE27_HCMC_2009,Hue,HUE27,HCMC,2009, HUE29_HCMC_2009,Hue,HUE29,HCMC,2009, HUE30_HCMC_2009,Hue,HUE30,HCMC,2009, HUE31_HCMC_2009,Hue,HUE31,HCMC,2009, HUE32_HCMC_2009,Hue,HUE32,HCMC,2009, HUE33_HCMC_2009,Hue,HUE33,HCMC,2009, HUE34_HCMC_2009,Hue,HUE34,HCMC,2009, HUE40_HCMC_2009,Hue,HUE40,HCMC,2009, HUE42_HCMC_2009,Hue,HUE42,HCMC,2009, HUE43_HCMC_2009,Hue,HUE43,HCMC,2009, HUE46_HCMC_2009,Hue,HUE46,HCMC,2009, HUE47_HCMC_2009,Hue,HUE47,HCMC,2009, HUE48_HCMC_2009,Hue,HUE48,HCMC,2009, HUE50_HCMC_2009,Hue,HUE50,HCMC,2009, HUE53_HCMC_2010,Hue,HUE53,HCMC,2010, HUE55_HCMC_2010,Hue,HUE55,HCMC,2010, HUE57_HCMC_2010,Hue,HUE57,HCMC,2010, HUE58_HCMC_2010,Hue,HUE58,HCMC,2010, HUE60_HCMC_2010,Hue,HUE60,HCMC,2010, HUE62_HCMC_2010,Hue,HUE62,HCMC,2010, HUE64_HCMC_2010,Hue,HUE64,HCMC,2010, HUE67_HCMC_2010,Hue,HUE67,HCMC,2010, HUE68_HCMC_2010,Hue,HUE68,HCMC,2010, KH02_HCMC_2009,KH,KH02,HCMC,2009, KH04_HCMC_2009,KH,KH04,HCMC,2009, KH05_HCMC_2009,KH,KH05,HCMC,2009, KH06_HCMC_2009,KH,KH06,HCMC,2009, KH07_HCMC_2009,KH,KH07,HCMC,2009, KH09_HCMC_2009,KH,KH09,HCMC,2009, KH10_HCMC_2009,KH,KH10,HCMC,2009, KH11_HCMC_2009,KH,KH11,HCMC,2009, KH12_HCMC_2009,KH,KH12,HCMC,2009, KH13_HCMC_2009,KH,KH13,HCMC,2009, KH14_HCMC_2009,KH,KH14,HCMC,2009, KH15_HCMC_2009,KH,KH15,HCMC,2009, KH16_HCMC_2009,KH,KH16,HCMC,2009, KH17_HCMC_2009,KH,KH17,HCMC,2009, KH18_HCMC_2009,KH,KH18,HCMC,2009, KH19_HCMC_2009,KH,KH19,HCMC,2009, KH20_HCMC_2009,KH,KH20,HCMC,2009, KH21_HCMC_2009,KH,KH21,HCMC,2009, KH23_HCMC_2009,KH,KH23,HCMC,2009, KH24_HCMC_2009,KH,KH24,HCMC,2009, KH25_HCMC_2009,KH,KH25,HCMC,2009, KH26_HCMC_2009,KH,KH26,HCMC,2009, KH27_HCMC_2009,KH,KH27,HCMC,2009, KH28_HCMC_2009,KH,KH28,HCMC,2009, KH29_HCMC_2009,KH,KH29,HCMC,2009, KH30_HCMC_2009,KH,KH30,HCMC,2009, KH32_HCMC_2009,KH,KH32,HCMC,2009, KH33_HCMC_2009,KH,KH33,HCMC,2009, KH34_HCMC_2009,KH,KH34,HCMC,2009, KH35_HCMC_2009,KH,KH35,HCMC,2009, KH37_HCMC_2009,KH,KH37,HCMC,2009, KH38_HCMC_2009,KH,KH38,HCMC,2009, KH40_HCMC_2009,KH,KH40,HCMC,2009, KH41_HCMC_2009,KH,KH41,HCMC,2009, KH42_HCMC_2010,KH,KH42,HCMC,2010, KH43_HCMC_2010,KH,KH43,HCMC,2010, KH45_HCMC_2010,KH,KH45,HCMC,2010, KH53_HCMC_2010,KH,KH53,HCMC,2010, KH54_HCMC_2010,KH,KH54,HCMC,2010, KH55_HCMC_2010,KH,KH55,HCMC,2010, KH57_HCMC_2010,KH,KH57,HCMC,2010, MS0004_HCMC_1995,HCMC,MS0004,HCMC,1995, MS0011_HCMC_1995,HCMC,MS0011,HCMC,1995, MS0032_HCMC_1995,HCMC,MS0032,HCMC,1995, MS0034_HCMC_1995,HCMC,MS0034,HCMC,1995, MS0035_HCMC_1995,HCMC,MS0035,HCMC,1995, MS0039_HCMC_1995,HCMC,MS0039,HCMC,1995, MS0042_HCMC_1995,HCMC,MS0042,HCMC,1995, MS0043_HCMC_1995,HCMC,MS0043,HCMC,1995, MS0048_HCMC_1995,HCMC,MS0048,HCMC,1995, MS0063_HCMC_1995,HCMC,MS0063,HCMC,1995, MS0065_HCMC_1995,HCMC,MS0065,HCMC,1995, MS0069_HCMC_1995,HCMC,MS0069,HCMC,1995, MS0070_HCMC_1995,HCMC,MS0070,HCMC,1995, MS0080_HCMC_1995,HCMC,MS0080,HCMC,1995, MS0083_HCMC_1996,HCMC,MS0083,HCMC,1996, MS0094_HCMC_1996,HCMC,MS0094,HCMC,1996, MS0102_HCMC_1996,HCMC,MS0102,HCMC,1996, MS0110_HCMC_1996,HCMC,MS0110,HCMC,1996, MS0111_HCMC_1996,HCMC,MS0111,HCMC,1996, MS0119_HCMC_1996,HCMC,MS0119,HCMC,1996, MS0122_HCMC_1996,HCMC,MS0122,HCMC,1996, MS0127_HCMC_1996,HCMC,MS0127,HCMC,1996, MS0128_HCMC_1996,HCMC,MS0128,HCMC,1996, 20031275_Iran_2003,other,20031275,Iran,2003, 31382_Israel_2003,other,31382,Israel,2003, 20040924_Kenya_Egypt_2004,other,20040924,Kenya,Egypt,2004 IB1_Korea_2003,other,IB1,Korea,2003, IB10_Korea_2003,other,IB10,Korea,2003, IB2_Korea_2003,other,IB2,Korea,2003, IB2493_Korea_NA,other,IB2493,Korea,NA, IB3_Korea_2003,other,IB3,Korea,2003, IB681_Korea_1991,other,IB681,Korea,1991, IB683_Korea_1994,other,IB683,Korea,1994, IB687_Korea_1998,other,IB687,Korea,1998, IB690_Korea_2000,other,IB690,Korea,2000, IB691_Korea_1999,other,IB691,Korea,1999, IB694_Korea_1979,other,IB694,Korea,1979, IB695_Korea_1983,other,IB695,Korea,1983, IB696_Korea_1980,other,IB696,Korea,1980, IB697_Korea_1982,other,IB697,Korea,1982, IB698_Korea_1983,other,IB698,Korea,1983, IB713_Korea_1981,other,IB713,Korea,1981, IB716_Korea_1981,other,IB716,Korea,1981, IB717_Korea_1982,other,IB717,Korea,1982, IB739_Korea_1985,other,IB739,Korea,1985, IB748_Korea_1987,other,IB748,Korea,1987, 53G_Korea_2000,other,53G,Korea,2000, 5827_Madagascar_2000,other,5827,Madagascar,2000, 989560_Madagascar_1998,other,989560,Madagascar,1998, 9810267_Madagascar_1998,other,9810267,Madagascar,1998, 19984123_Mexico_1998,other,19984123,Mexico,1998, 20062313_Nepal_2006,other,20062313,Nepal,2006, 970044_New,other,970044,New,, IB3277_Pakistan_2002,other,IB3277,Pakistan,2002, IB3300_Pakistan_2002,other,IB3300,Pakistan,2002, IB3374_Pakistan_2002,other,IB3374,Pakistan,2002, IB3488_Pakistan_2003,other,IB3488,Pakistan,2003, IB3507_Pakistan_2003,other,IB3507,Pakistan,2003, IB3580_Pakistan_2003,other,IB3580,Pakistan,2003, IB3599_Pakistan_2003,other,IB3599,Pakistan,2003, 20052631_Peru_2005,other,20052631,Peru,2005, 273_Senegal_1973,other,273,Senegal,1973, 1567_Senegal_1967,other,1567,Senegal,1967, 36224_Senegal_2003,other,36224,Senegal,2003, 20040880_SriLanka_2004,other,20040880,SriLanka,2004, Ss046_Ref_1950,other,Ss046,Ref,1950, 54178_Sweden_1945,other,54178,Sweden,1945, 54179_Sweden_1944,other,54179,Sweden,1944, 54181_Sweden_1945,other,54181,Sweden,1945, 54184_Sweden_1945,other,54184,Sweden,1945, 54190_Sweden_1945,other,54190,Sweden,1945, 54210_Sweden_1943,other,54210,Sweden,1943, 54216_Sweden_1946,other,54216,Sweden,1946, 54228_Sweden_1947,other,54228,Sweden,1947, 19904011_unk_1990,other,19904011,unk,1990, 19910761_unk_1991,other,19910761,unk,1991, 19911483_unk_1991,other,19911483,unk,1991, 19920319_unk_1992,other,19920319,unk,1992, 20003593_unk_2000,other,20003593,unk,2000, 20010007_unk_2001,other,20010007,unk,2001, 20011685_unk_2001,other,20011685,unk,2001, 20021122_unk_2002,other,20021122,unk,2002, 20040489_unk_2004,other,20040489,unk,2004, 20041367_unk_2004,other,20041367,unk,2004, 20071599_unk_2007,other,20071599,unk,2007, 20081885_unk_2008,other,20081885,unk,2008, 20051541_Uzbekistan_2005,other,20051541,Uzbekistan,2005, IB1970_Vietnam_2001,VN,IB1970,Vietnam,2001, IB1976_Vietnam_2002,VN,IB1976,Vietnam,2002, IB1980_Vietnam_2002,VN,IB1980,Vietnam,2002, IB1985_Vietnam_2002,VN,IB1985,Vietnam,2002, IB1987_Vietnam_2002,VN,IB1987,Vietnam,2002, IB1990_Vietnam_2003,VN,IB1990,Vietnam,2003, IB1993_Vietnam_2003,VN,IB1993,Vietnam,2003, IB1995_Vietnam_2003,VN,IB1995,Vietnam,2003, IB1997_Vietnam_2003,VN,IB1997,Vietnam,2003, IB2000_Vietnam_2003,VN,IB2000,Vietnam,2003, IB2004_Vietnam_2003,VN,IB2004,Vietnam,2003, IB2008_Vietnam_2003,VN,IB2008,Vietnam,2003, IB2009_Vietnam_2003,VN,IB2009,Vietnam,2003, IB2012_Vietnam_2001,VN,IB2012,Vietnam,2001, IB2013_Vietnam_2001,VN,IB2013,Vietnam,2001, IB2015_Vietnam_2002,VN,IB2015,Vietnam,2002, IB2018_Vietnam_2002,VN,IB2018,Vietnam,2002, IB2024_Vietnam_2002,VN,IB2024,Vietnam,2002, IB2026_Vietnam_2003,VN,IB2026,Vietnam,2003, #N/A,other,#N/A,,, 10083_HCMC_NA,other,10083,HCMC,NA, 10093_HCMC_NA,other,10093,HCMC,NA, 10102_HCMC_NA,other,10102,HCMC,NA, 10111_HCMC_NA,other,10111,HCMC,NA, 10115_HCMC_NA,other,10115,HCMC,NA, 10134_HCMC_NA,other,10134,HCMC,NA, 10135_HCMC_NA,other,10135,HCMC,NA, 10152_HCMC_NA,other,10152,HCMC,NA, 10159_HCMC_NA,other,10159,HCMC,NA, 10188_HCMC_NA,other,10188,HCMC,NA, 10014_HCMC_NA,other,10014,HCMC,NA, 10263_HCMC_NA,other,10263,HCMC,NA, 10320_HCMC_NA,other,10320,HCMC,NA, 10365_HCMC_NA,other,10365,HCMC,NA, 10021_HCMC_NA,other,10021,HCMC,NA, 10031_HCMC_NA,other,10031,HCMC,NA, 10035_HCMC_NA,other,10035,HCMC,NA, 10060_HCMC_NA,other,10060,HCMC,NA, 10063_HCMC_NA,other,10063,HCMC,NA, 10071_HCMC_NA,other,10071,HCMC,NA, 10073_HCMC_NA,other,10073,HCMC,NA, -------------------------------------------------------------------------------- /tree_example_april2015/info.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/info.pdf -------------------------------------------------------------------------------- /tree_example_april2015/info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/info.png -------------------------------------------------------------------------------- /tree_example_april2015/myplot.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/myplot.pdf -------------------------------------------------------------------------------- /tree_example_april2015/myplot4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/myplot4.pdf -------------------------------------------------------------------------------- /tree_example_april2015/pan.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/pan.pdf -------------------------------------------------------------------------------- /tree_example_april2015/pan.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/pan.png -------------------------------------------------------------------------------- /tree_example_april2015/res_genes.csv: -------------------------------------------------------------------------------- 1 | id,gyrA,pEK204,CT-X-M-15,pEG356,CTX-M-14,colicinPlasmid1,pMG828-3 KH09_HCMC_2009,4,0,0,0,0,5,0 KH11_HCMC_2009,4,0,0,0,0,5,0 DE0579_HCMC_2001,4,0,0,0,0,5,6 EG0159_HCMC_2007,4,0,0,0,0,5,6 KH54_HCMC_2010,4,0,0,0,0,5,6 EG0392_HCMC_2007,4,0,0,0,0,5,6 EG0372_HCMC_2007,4,0,0,0,0,5,6 KH02_HCMC_2009,4,0,0,0,0,5,6 EG0385_HCMC_2007,4,0,0,0,0,5,6 KH53_HCMC_2010,4,0,0,0,0,5,6 HUE58_HCMC_2010,4,0,0,0,0,5,6 EG0404_HCMC_2007,4,0,0,0,0,5,6 30010_HCMC_2009,4,0,0,0,0,5,6 KH27_HCMC_2009,4,0,0,14,15,5,6 KH20_HCMC_2009,4,0,0,14,15,5,6 KH26_HCMC_2009,4,0,0,14,15,5,6 KH24_HCMC_2009,4,0,0,14,15,5,6 KH15_HCMC_2009,4,0,0,0,0,5,6 KH18_HCMC_2009,4,0,0,0,0,5,6 KH23_HCMC_2009,4,0,0,0,0,5,6 20261_HCMC_2009,4,0,0,0,0,5,6 IB1997_Vietnam_2003,4,0,0,0,0,5,6 IB1987_Vietnam_2002,4,0,0,0,0,5,6 30293_HCMC_2009,4,7,8,14,15,5,0 KH55_HCMC_2010,4,0,0,0,0,5,0 10073_HCMC_2009,4,0,0,0,15,5,0 KH41_HCMC_2009,4,0,0,0,0,5,0 KH37_HCMC_2009,4,0,0,0,0,5,0 KH35_HCMC_2009,4,0,0,0,0,5,0 30450_HCMC_2010,4,0,0,0,0,5,0 DE1150_HCMC_2002,3,0,0,0,0,5,6 DE1336_HCMC_2002,3,0,0,0,0,5,6 DE1209_HCMC_2002,3,0,0,0,0,5,6 IB2024_Vietnam_2002,3,0,0,0,0,5,6 IB2026_Vietnam_2003,3,0,0,0,0,5,6 IB1995_Vietnam_2003,3,0,0,0,0,5,6 EG1015_HCMC_2009,2,7,8,0,0,0,0 10111_HCMC_2009,2,7,8,0,0,5,0 EG0129_HCMC_2007,2,0,0,0,0,5,0 EG0401_HCMC_2007,2,0,0,0,0,5,0 EG0383_HCMC_2007,2,0,0,0,0,5,0 EG0204_HCMC_2008,2,7,8,0,0,5,0 EG0255_HCMC_2008,2,0,0,0,0,5,0 30124_HCMC_2009,2,7,8,0,0,5,0 10115_HCMC_2009,2,7,8,0,0,5,0 10135_HCMC_2009,2,7,8,0,0,5,0 EG1001_HCMC_2008,2,7,8,0,0,5,0 EG1023_HCMC_2009,2,7,8,0,0,5,0 10134_HCMC_2009,2,7,8,0,0,5,0 10152_HCMC_2009,2,7,8,0,0,5,0 KH07_HCMC_2009,2,0,0,14,15,5,0 EG1008_HCMC_2008,2,7,8,0,0,5,0 EG1024_HCMC_2009,2,7,8,0,0,5,0 EG0393_HCMC_2007,2,0,0,0,0,5,0 20037_HCMC_2009,2,7,8,0,0,5,0 EG1004_HCMC_2008,2,7,8,0,0,5,0 KH10_HCMC_2009,2,0,0,0,0,5,0 KH19_HCMC_2009,2,0,0,14,15,5,0 30008_HCMC_2009,2,7,8,0,0,5,0 KH12_HCMC_2009,2,0,0,14,15,5,0 10083_HCMC_2009,2,7,8,0,0,5,0 30054_HCMC_2009,2,7,8,0,0,5,0 KH17_HCMC_2009,2,0,0,0,0,5,0 EG1025_HCMC_2009,2,7,8,0,0,5,0 KH21_HCMC_2009,2,0,0,0,0,5,0 10093_HCMC_2009,2,7,8,0,0,5,0 20070_HCMC_2009,2,7,8,0,0,5,0 30059_HCMC_2009,2,7,8,0,0,5,0 10102_HCMC_2009,2,7,8,0,0,5,0 20021_HCMC_2009,2,7,8,0,0,5,0 EG0430_HCMC_2008,2,7,8,0,0,5,0 EG0357_HCMC_2007,2,0,0,0,0,5,0 10159_HCMC_2009,2,7,8,0,0,5,0 KH30_HCMC_2009,2,0,0,14,15,5,0 20023_HCMC_2009,2,7,8,0,0,5,0 30037_HCMC_2009,2,7,8,0,0,5,0 30071_HCMC_2009,2,7,8,0,0,5,0 EG1026_HCMC_0,2,7,8,0,0,5,0 30100_HCMC_2009,2,7,8,0,0,5,0 20228_HCMC_2009,2,7,8,0,0,5,0 KH25_HCMC_2009,2,0,0,14,15,5,6 30371_HCMC_2009,2,7,8,0,0,5,6 KH05_HCMC_2009,2,0,0,0,0,5,6 KH06_HCMC_2009,2,0,0,14,15,5,6 20006_HCMC_2009,2,7,8,0,0,5,6 KH14_HCMC_2009,2,0,0,14,15,5,6 HUE55_HCMC_2010,2,0,0,0,0,5,6 EG0318_HCMC_2006,2,0,0,0,0,5,6 HUE29_HCMC_2009,2,0,0,0,0,5,6 HUE50_HCMC_2009,2,0,0,14,15,5,6 EG0315_HCMC_2006,2,0,0,0,0,5,6 EG0313_HCMC_2006,2,0,0,0,0,5,6 EG0467_HCMC_2008,2,0,0,0,0,5,6 KH04_HCMC_2009,2,0,0,14,15,5,6 KH43_HCMC_2010,2,7,0,14,15,5,6 EG0375_HCMC_2007,2,0,0,0,0,5,6 EG0365_HCMC_2007,2,0,0,0,0,5,6 EG0362_HCMC_2007,2,0,0,0,0,5,6 EG0369_HCMC_2007,2,0,0,0,0,5,6 30073_HCMC_2009,2,0,0,0,0,5,6 EG0394_HCMC_2007,2,0,0,0,0,5,6 EG0388_HCMC_2007,2,0,0,0,0,5,6 KH16_HCMC_2009,2,0,0,0,0,5,6 EG0395_HCMC_2007,2,7,8,0,0,5,6 EG0390_HCMC_2007,2,7,8,0,0,5,6 KH28_HCMC_2009,2,0,0,0,0,5,6 KH29_HCMC_2009,2,0,0,0,0,5,6 20094_HCMC_2009,2,0,0,0,15,5,6 KH13_HCMC_2009,2,0,0,0,0,5,6 HUE53_HCMC_2010,2,0,0,0,0,5,6 EG0309_HCMC_2006,2,0,0,0,0,5,6 EG0451_HCMC_2008,2,0,0,0,0,5,6 EG0425_HCMC_2008,2,0,0,0,0,5,6 30112_HCMC_2009,2,7,8,0,0,5,6 EG0472_HCMC_2008,2,7,8,0,0,5,6 HUE17_HCMC_2009,2,7,8,0,0,5,6 HUE22_HCMC_2009,2,7,8,0,0,5,6 KH45_HCMC_2010,2,7,8,0,15,5,6 10021_HCMC_2009,2,0,0,0,0,5,0 30366_HCMC_2009,2,0,0,0,0,5,0 KH33_HCMC_2009,2,0,0,0,0,5,0 KH40_HCMC_2009,2,0,0,0,15,5,0 KH32_HCMC_2009,2,0,0,14,15,5,0 KH34_HCMC_2009,2,0,0,14,15,5,0 30003_HCMC_2009,2,0,0,0,0,5,0 KH57_HCMC_2010,2,0,0,0,0,5,0 30451_HCMC_2010,2,0,0,0,0,5,0 HUE46_HCMC_2009,2,0,0,0,0,5,0 EG1017_HCMC_2009,2,7,8,0,0,5,0 EG1014_HCMC_2009,2,7,8,0,0,5,0 EG1018_HCMC_2009,2,7,8,0,0,5,0 EG1022_HCMC_2009,2,7,8,0,0,5,0 EG1019_HCMC_2009,2,7,8,0,0,5,0 10060_HCMC_2009,2,7,8,0,0,5,0 30174_HCMC_2009,2,7,8,0,0,5,0 EG1027_HCMC_0,2,7,8,0,0,5,0 10035_HCMC_2009,2,7,8,0,0,5,0 30162_HCMC_2009,2,7,8,0,0,5,0 10365_HCMC_2010,2,7,8,0,0,5,0 10320_HCMC_2010,2,7,8,0,0,5,0 20343_HCMC_2009,2,7,8,0,0,5,0 30164_HCMC_2009,2,7,8,0,0,5,0 EG1028_HCMC_0,2,7,8,0,0,5,0 EG1016_HCMC_2009,2,7,8,0,0,5,0 30172_HCMC_2009,2,7,8,0,0,5,0 HUE20_HCMC_2009,2,7,8,0,0,5,0 10063_HCMC_2009,2,7,8,0,0,5,0 10014_HCMC_2009,2,7,8,0,0,5,0 EG1020_HCMC_2009,2,7,8,0,0,5,0 EG1021_HCMC_2009,2,7,8,0,0,5,0 HUE19_HCMC_2009,2,7,8,0,0,5,0 20263_HCMC_2009,2,7,8,0,0,5,0 30169_HCMC_2009,2,7,8,0,0,5,0 EG1029_HCMC_0,2,7,8,0,0,5,0 30387_HCMC_2010,2,7,8,0,0,5,0 KH38_HCMC_2009,2,7,8,0,0,5,0 30233_HCMC_2009,2,7,8,0,0,5,0 10188_HCMC_2009,2,7,8,0,0,5,0 10263_HCMC_2009,2,7,8,0,0,5,0 DE0303_HCMC_2000,0,0,0,0,0,5,6 DE0816_HCMC_2001,0,0,0,0,0,5,6 DE0248_HCMC_2000,0,0,0,0,0,5,6 DE0685_HCMC_2001,0,0,0,0,0,5,6 DE0611_HCMC_2001,0,0,0,0,15,5,6 DE0489_HCMC_2000,0,0,0,0,0,5,6 DE0477_HCMC_2000,0,0,0,0,0,5,6 MS0110_HCMC_1996,0,0,0,0,0,5,6 DE0654_HCMC_2001,0,0,0,0,0,5,6 DE0900_HCMC_2001,0,0,0,0,0,5,6 DE0965_HCMC_2001,0,0,0,0,0,5,6 DE1140_HCMC_2002,0,0,0,0,0,5,6 DE0846_HCMC_2001,0,0,0,0,0,5,6 IB2008_Vietnam_2003,0,0,0,0,0,5,6 IB2000_Vietnam_2003,0,0,0,0,0,5,6 DE1318_HCMC_2002,0,0,0,0,0,5,6 MS0128_HCMC_1996,0,0,0,0,0,5,6 DE0127_HCMC_2000,0,0,0,0,0,5,6 DE1256_HCMC_2002,0,0,0,0,0,5,6 DE0115_HCMC_2000,0,0,0,0,0,5,6 DE0427_HCMC_2000,0,0,0,0,0,5,6 DE0295_HCMC_2000,0,0,0,0,0,5,6 IB2004_Vietnam_2003,0,0,0,0,0,5,6 IB1970_Vietnam_2001,0,0,0,0,0,5,6 IB2009_Vietnam_2003,0,0,0,0,0,5,6 IB2015_Vietnam_2002,0,0,0,0,0,5,6 DE0199_HCMC_2000,0,0,0,0,0,5,6 DE1486_HCMC_2002,0,0,0,0,0,5,6 DE1198_HCMC_2002,0,0,0,0,0,5,6 DE0306_HCMC_2000,0,0,0,0,0,5,6 DE1165_HCMC_2002,0,0,0,0,0,5,6 IB2018_Vietnam_2002,0,0,0,0,0,5,6 IB1993_Vietnam_2003,0,0,0,0,0,5,6 IB1990_Vietnam_2003,0,0,0,0,0,5,6 DE1404_HCMC_2002,0,0,0,0,0,5,6 EG0304_HCMC_2006,0,0,0,0,0,5,6 EG0410_HCMC_2007,0,0,0,0,0,5,6 EG0379_HCMC_2007,0,0,0,0,0,5,6 DE1063_HCMC_2002,0,0,0,0,0,5,6 DE1191_HCMC_2002,0,0,0,0,0,5,6 DE1208_HCMC_2002,0,0,0,0,0,5,6 EG0386_HCMC_2007,0,0,0,0,0,5,6 KH42_HCMC_2010,0,0,0,0,15,5,6 HUE25_HCMC_2009,0,0,0,0,0,5,6 EG0373_HCMC_2007,0,7,8,0,0,5,6 HUE26_HCMC_2009,0,0,0,0,0,5,6 HUE47_HCMC_2009,0,0,0,0,0,5,6 HUE16_HCMC_2009,0,0,0,0,0,5,6 HUE23_HCMC_2009,0,0,0,0,0,5,6 HUE21_HCMC_2009,0,0,0,0,0,5,6 HUE67_HCMC_2010,0,0,0,0,0,5,6 HUE64_HCMC_2010,0,0,0,0,0,5,6 HUE27_HCMC_2009,0,0,0,0,0,5,6 HUE24_HCMC_2009,0,0,0,0,0,5,6 HUE48_HCMC_2009,0,0,0,0,0,5,6 HUE60_HCMC_2010,0,0,0,0,0,5,6 HUE57_HCMC_2010,0,0,0,0,0,5,6 HUE68_HCMC_2010,0,0,0,0,0,5,6 HUE62_HCMC_2010,0,0,0,0,0,5,6 HUE43_HCMC_2009,0,0,0,0,0,5,0 HUE33_HCMC_2009,0,0,0,0,15,5,0 10071_HCMC_2009,0,0,0,0,0,5,0 10031_HCMC_2009,0,0,0,0,0,5,0 HUE30_HCMC_2009,0,0,0,0,0,5,0 HUE01_HCMC_2008,0,0,0,0,0,5,0 HUE02_HCMC_2008,0,0,0,0,0,5,0 HUE05_HCMC_2008,0,0,0,0,0,5,0 HUE34_HCMC_2009,0,0,0,0,15,5,0 HUE32_HCMC_2009,0,0,0,0,0,5,0 HUE40_HCMC_2009,0,0,0,0,0,5,0 HUE42_HCMC_2009,0,0,0,0,0,5,0 HUE31_HCMC_2009,0,0,0,0,0,5,0 DE0655_HCMC_2001,0,0,0,0,0,0,0 MS0111_HCMC_1996,0,0,0,0,0,0,0 MS0063_HCMC_1995,0,0,0,0,0,0,0 DE0490_HCMC_2000,4,0,0,0,0,0,0 DE0885_HCMC_2001,4,0,0,0,0,0,0 DE0891_HCMC_2001,4,0,0,0,0,0,0 MS0119_HCMC_1996,0,0,0,0,0,0,0 MS0069_HCMC_1995,0,0,0,0,0,0,0 MS0065_HCMC_1995,0,0,0,0,0,0,0 MS0070_HCMC_1995,0,0,0,0,0,0,0 MS0048_HCMC_1995,0,0,0,0,0,0,0 MS0083_HCMC_1996,0,0,0,0,0,0,0 MS0102_HCMC_1996,0,0,0,0,0,0,0 MS0032_HCMC_1995,0,0,0,0,0,0,0 MS0034_HCMC_1995,0,0,0,0,0,0,0 MS0039_HCMC_1995,0,0,0,0,0,0,0 MS0004_HCMC_1995,0,0,0,0,0,0,0 -------------------------------------------------------------------------------- /tree_example_april2015/res_genes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/res_genes.pdf -------------------------------------------------------------------------------- /tree_example_april2015/res_genes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/katholt/plotTree/c28dfec87fb5430dab2daaf44c3e41dba210d8d8/tree_example_april2015/res_genes.png -------------------------------------------------------------------------------- /tree_example_april2015/tree.nwk: -------------------------------------------------------------------------------- 1 | ((((DE0655_HCMC_2001:5.8032794,MS0111_HCMC_1996:0.8032793):3.2539742,(MS0063_HCMC_1995:0.5929808,(DE0490_HCMC_2000:0.6927232,(DE0885_HCMC_2001:0.66264933,DE0891_HCMC_2001:0.66264933):1.0300739):4.9002576):2.4642727):7.377318,((MS0119_HCMC_1996:5.44911,(MS0069_HCMC_1995:0.46310937,(MS0065_HCMC_1995:0.2289469,MS0070_HCMC_1995:0.2289469):0.23416248):3.9860005):4.526132,(MS0048_HCMC_1995:7.537512,(MS0083_HCMC_1996:8.017781,(MS0102_HCMC_1996:4.3871775,(MS0032_HCMC_1995:1.725267,(MS0034_HCMC_1995:1.3453876,(MS0039_HCMC_1995:0.7213195,MS0004_HCMC_1995:0.7213195):0.62406814):0.37987942):1.6619103):3.630604):0.5197302):1.4377306):1.4593295):2.3088996,(((DE0816_HCMC_2001:4.3169746,DE0303_HCMC_2000:3.3169746):1.7689667,((DE1318_HCMC_2002:2.248374,(DE1209_HCMC_2002:1.3276712,(DE1336_HCMC_2002:0.5616553,DE1150_HCMC_2002:0.5616553):0.7660158):0.92070293):4.566584,((MS0110_HCMC_1996:0.38845667,(DE0654_HCMC_2001:1.0745678,DE0900_HCMC_2001:1.0745678):4.313889):0.09005634,(DE0965_HCMC_2001:4.2770276,(DE0846_HCMC_2001:1.3503177,DE1140_HCMC_2002:2.3503177):2.92671):1.2014855):0.33644536):0.27098292):0.36677948,((DE0685_HCMC_2001:3.3104615,(DE0248_HCMC_2000:2.1315928,(DE0611_HCMC_2001:2.1621506,(DE0477_HCMC_2000:0.8892436,DE0489_HCMC_2000:0.8892436):0.2729069):0.96944225):0.17886876):2.8215983,((DE1486_HCMC_2002:5.19946,(DE1198_HCMC_2002:3.4406295,DE0306_HCMC_2000:1.4406294):1.7588307):1.684639,((MS0128_HCMC_1996:0.73830974,((DE0579_HCMC_2001:4.4574614,DE0199_HCMC_2000:3.457461):0.9459294,((DE1256_HCMC_2002:3.7247095,DE0127_HCMC_2000:1.7247095):2.327542,(DE0115_HCMC_2000:2.7405453,(DE0295_HCMC_2000:0.14105976,DE0427_HCMC_2000:0.14105976):2.5994856):1.3117063):0.35113895):0.33491915):0.050606824,(DE1165_HCMC_2002:5.093656,((DE1404_HCMC_2002:0.557506,((KH02_HCMC_2009:2.5800672,EG0385_HCMC_2007:0.5800673):3.9254208,((EG0392_HCMC_2007:1.2242377,EG0372_HCMC_2007:1.2242377):2.93899,(EG0159_HCMC_2007:1.7672435,(30293_HCMC_2009:2.617739,(KH55_HCMC_2010:0.276287,KH54_HCMC_2010:0.276287):3.341452):1.1495047):2.3959842):0.34226042):1.0520179):1.3412366,((EG0304_HCMC_2006:4.813705,(EG0410_HCMC_2007:4.2868924,((10073_HCMC_2009:2.6962647,(KH53_HCMC_2010:2.1526585,(KH41_HCMC_2009:0.29114425,HUE58_HCMC_2010:1.2911443):0.8615141):1.5436063):0.41412598,(EG0404_HCMC_2007:0.7696753,(((KH11_HCMC_2009:0.078025736,KH09_HCMC_2009:0.078025736):0.65184283,(KH27_HCMC_2009:0.35741767,(KH24_HCMC_2009:0.20970763,(KH26_HCMC_2009:0.15865457,KH20_HCMC_2009:0.15865457):0.051053073):0.14771004):0.37245086):1.1103305,(30010_HCMC_2009:1.5896724,(KH37_HCMC_2009:1.1502044,((20261_HCMC_2009:0.08516318,30450_HCMC_2010:1.0851632):0.8404743,(KH35_HCMC_2009:0.7951829,(KH15_HCMC_2009:0.60741717,(KH23_HCMC_2009:0.42464504,KH18_HCMC_2009:0.42464504):0.18277211):0.1877657):0.13045461):0.22456689):0.4394681):0.25052658):0.9294763):0.34071532):3.176502):1.5268124):0.7114763,(((HUE43_HCMC_2009:0.36201376,HUE33_HCMC_2009:0.36201376):7.1241865,((KH42_HCMC_2010:3.3679163,EG0386_HCMC_2007:0.3679164):0.70920086,(10071_HCMC_2009:2.6898785,(10031_HCMC_2009:0.47469062,(HUE30_HCMC_2009:0.0918009,HUE25_HCMC_2009:0.0918009):0.38288972):2.2151878):0.3872388):4.4090834):0.6226607,((EG0379_HCMC_2007:5.4546046,(DE1208_HCMC_2002:0.20387767,(DE1191_HCMC_2002:0.093292266,DE1063_HCMC_2002:0.093292266):0.1105854):0.25072718):0.5096697,((EG0373_HCMC_2007:4.596729,((HUE05_HCMC_2008:0.5661828,(HUE01_HCMC_2008:0.45300022,HUE02_HCMC_2008:0.45300022):0.11318255):0.7151998,((HUE26_HCMC_2009:0.5150966,HUE47_HCMC_2009:0.5150966):1.3821286,((HUE34_HCMC_2009:0.32973218,(HUE16_HCMC_2009:0.075720444,HUE23_HCMC_2009:0.075720444):0.25401172):0.6174645,((HUE42_HCMC_2009:0.6127716,(HUE24_HCMC_2009:0.39078307,HUE48_HCMC_2009:0.39078307):0.2219885):0.12479895,((HUE60_HCMC_2010:0.4141967,(HUE62_HCMC_2010:0.25284857,(HUE68_HCMC_2010:0.1128484,HUE57_HCMC_2010:0.1128484):0.14000016):0.16134813):1.0245577,((HUE40_HCMC_2009:0.18138531,HUE31_HCMC_2009:0.18138531):0.16259845,((HUE27_HCMC_2009:0.09743744,HUE21_HCMC_2009:0.09743744):0.14581622,(HUE32_HCMC_2009:0.16771278,(HUE67_HCMC_2010:0.12057354,HUE64_HCMC_2010:0.12057354):1.0471393):0.07554087):0.10073009):0.094770655):0.29881608):0.20962612):0.9500286):0.38415742):4.3153462):0.70938575,((((EG0318_HCMC_2006:0.7907195,EG1015_HCMC_2009:3.7907195):1.2927706,(EG0315_HCMC_2006:0.6976699,EG0313_HCMC_2006:0.6976699):1.3858202):0.06450212,(EG0129_HCMC_2007:2.1558528,((10021_HCMC_2009:1.5945473,EG0467_HCMC_2008:0.5945472):1.0374268,(30366_HCMC_2009:1.6741632,(KH33_HCMC_2009:1.214116,(((KH17_HCMC_2009:0.20019487,KH21_HCMC_2009:0.20019487):0.46664792,(KH40_HCMC_2009:0.48855478,KH05_HCMC_2009:0.48855478):0.178288):0.36136207,(((KH32_HCMC_2009:0.28143814,(KH19_HCMC_2009:0.09562999,KH14_HCMC_2009:0.09562999):0.18580814):0.2679375,(KH04_HCMC_2009:0.30604878,(KH43_HCMC_2010:1.0811216,KH34_HCMC_2009:0.08112162):0.22492714):0.24332689):0.25284138,(KH30_HCMC_2009:0.6326716,(KH25_HCMC_2009:0.52252483,(KH12_HCMC_2009:0.42703867,(KH07_HCMC_2009:0.24101046,KH06_HCMC_2009:0.24101046):0.1860282):0.095486164):0.11014678):0.16954544):0.2259878):0.18591116):0.46004727):0.9578108):1.5238787):0.9921395):0.32343355,((HUE50_HCMC_2009:0.9875876,HUE29_HCMC_2009:0.9875876):4.093982,(((EG0383_HCMC_2007:1.0930482,(EG0369_HCMC_2007:0.8584623,(30073_HCMC_2009:2.7792876,((EG0362_HCMC_2007:0.5311965,(EG0365_HCMC_2007:0.43008167,EG0375_HCMC_2007:0.43008167):0.10111479):0.15812446,(EG0430_HCMC_2008:1.3735616,(EG0357_HCMC_2007:0.27279392,EG1001_HCMC_2008:1.2727939):0.10076768):0.3157593):0.089966774):0.07917458):0.23458594):0.38436875,((EG0394_HCMC_2007:0.77970237,EG0388_HCMC_2007:0.77970237):0.37730196,(KH16_HCMC_2009:2.8120885,(EG0395_HCMC_2007:0.60353893,(EG0390_HCMC_2007:0.44508734,((KH29_HCMC_2009:0.34328976,KH28_HCMC_2009:0.34328976):1.8971763,((KH10_HCMC_2009:0.083643876,KH13_HCMC_2009:0.083643876):0.8767341,(30003_HCMC_2009:0.72657967,20094_HCMC_2009:0.72657967):0.2337983):1.2800881):0.20462132):0.15845159):0.20854953):0.3449159):0.3204126):1.3297911,((((KH57_HCMC_2010:1.7996426,30451_HCMC_2010:1.7996426):1.505258,(HUE46_HCMC_2009:0.10950728,(HUE53_HCMC_2010:0.40261135,HUE55_HCMC_2010:0.40261135):0.70689595):2.1953933):1.7387503,(EG0309_HCMC_2006:0.5710684,((EG0451_HCMC_2008:0.5512447,EG0425_HCMC_2008:0.5512447):1.8021553,(EG0401_HCMC_2007:1.0234818,(EG0255_HCMC_2008:1.2049158,EG0393_HCMC_2007:0.20491578):0.8185661):0.32991803):0.21766852):0.47258243):0.38384464,((EG1017_HCMC_2009:0.42428076,EG1014_HCMC_2009:0.42428076):3.1158812,(((EG0472_HCMC_2008:1.3247443,(10063_HCMC_2009:1.6660695,(10014_HCMC_2009:1.0079165,(EG1025_HCMC_2009:0.4257418,EG1024_HCMC_2009:0.4257418):0.5821746):0.6581531):0.6586748):0.3036106,((EG1020_HCMC_2009:0.32170457,EG1023_HCMC_2009:0.32170457):1.8825513,((EG1021_HCMC_2009:0.7762185,(HUE17_HCMC_2009:0.1977663,HUE19_HCMC_2009:0.1977663):0.57845217):1.0972601,(EG1008_HCMC_2008:0.7484997,(30169_HCMC_2009:0.42480412,20263_HCMC_2009:0.42480412):1.3236955):0.12497892):0.33077726):0.42409903):0.30794248,((((EG1004_HCMC_2008:0.7502813,(EG1016_HCMC_2009:1.3527408,(10083_HCMC_2009:1.3309634,30100_HCMC_2009:1.3309634):0.021777324):0.39754057):0.20406148,(20006_HCMC_2009:0.5885145,(10159_HCMC_2009:0.46098584,(20021_HCMC_2009:0.09170538,30071_HCMC_2009:0.09170538):0.36928046):0.12752864):1.3658283):0.13290036,(30008_HCMC_2009:0.9339169,(30172_HCMC_2009:0.46886787,((30059_HCMC_2009:0.06837999,10093_HCMC_2009:0.06837999):0.15934758,(10111_HCMC_2009:0.0736825,30054_HCMC_2009:0.0736825):0.15404508):0.24114032):0.46504906):1.1533262):0.488436,(((HUE22_HCMC_2009:0.6245181,(30037_HCMC_2009:0.35643455,(10134_HCMC_2009:0.102605924,30387_HCMC_2010:1.1026059):0.25382864):0.26808354):1.4696217,((30112_HCMC_2009:0.62682027,(10188_HCMC_2009:0.598717,(30371_HCMC_2009:0.12725024,10263_HCMC_2009:0.12725024):0.47146675):0.028103273):0.9045901,((20023_HCMC_2009:1.0931172,((20037_HCMC_2009:0.30523556,30124_HCMC_2009:0.30523556):0.64873445,(KH45_HCMC_2010:1.7549813,20070_HCMC_2009:0.7549813):0.19898874):0.13914724):0.25823042,(10115_HCMC_2009:1.0115757,(KH38_HCMC_2009:0.78829896,(20228_HCMC_2009:0.30395183,30233_HCMC_2009:0.30395183):0.4843471):0.22327676):0.33977196):0.18006267):0.56272936):0.32713786,(((EG1018_HCMC_2009:0.50615525,EG1022_HCMC_2009:0.50615525):0.56690115,(EG1019_HCMC_2009:0.7863105,(10102_HCMC_2009:0.5945545,(30174_HCMC_2009:0.37238792,10060_HCMC_2009:0.37238792):0.22216657):0.19175601):0.2867459):1.1382207,(EG0204_HCMC_2008:1.0480666,(HUE20_HCMC_2009:2.046511,((10152_HCMC_2009:0.8673694,30164_HCMC_2009:0.8673694):0.28193346,((10320_HCMC_2010:1.3126699,20343_HCMC_2009:0.3126699):0.3879042,((10035_HCMC_2009:0.09392745,10135_HCMC_2009:0.09392745):0.33818772,(10365_HCMC_2010:1.1629158,30162_HCMC_2009:0.16291583):0.26919934):0.26845893):0.44872877):0.89720803):0.0015557038):0.16321044):0.21000053):0.15440157):0.36061823):0.6038646):0.88733345):0.37971255):0.27436194):0.3898559):1.8346889):0.6581598):0.14458658):0.41632017):0.3735612):3.1949134):1.6952608):0.09518246):0.24796072):0.320661):12.2907505); 2 | -------------------------------------------------------------------------------- /tree_example_april2015/tree_examples.txt: -------------------------------------------------------------------------------- 1 | # SHIGELLA - R plotting 2 | 3 | # basic strain info 4 | v<-plotTree(tree="tree.nwk",ancestral.reconstruction=F,tip.colour.cex=1,cluster=T,tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=10,infoWidth=10,infoCols=c("name","location","year")) 5 | 6 | # pan genome heatmap 7 | v<-plotTree(tree="tree.nwk",heatmapData="pan.csv",ancestral.reconstruction=F,tip.colour.cex=1,cluster=T,tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=5,dataWidth=20,infoCols=NA) 8 | 9 | # curated genes, coloured 10 | v<-plotTree(tree="tree.nwk",heatmapData="res_genes.csv",ancestral.reconstruction=F,tip.colour.cex=1,cluster=F,heatmap.colours=c("white","grey","seagreen3","darkgreen","green","brown","tan","red","orange","pink","magenta","purple","blue","skyblue3","blue","skyblue2"),tipColours=c("black","purple2","skyblue2","grey"),lwd=1,infoFile="info.csv",colourNodesBy="location",treeWidth=10,dataWidth=10,infoCols=c("name","year"),infoWidth=8) --------------------------------------------------------------------------------