3

I'm trying to use haploNet function of {pegas} to plot a haplotype network, but i`m having trouble putting equal haplotypes from different populations in a same piechart. I can build a haplotype net with the following script:

x <- read.dna(file="x.fas",format="fasta")
h <- haplotype(x)
net <- haploNet(h)
plot(net)

I'd like to set in the dnabin data the label of the original population of each taxa, so i could have piecharts of different colors (of haplotypes from different populations) in the resulting network. I'd like also to remove overlapping circles in the resulting haplotype network.

Thanks for any help!

An example:

> data(woodmouse)
> x <- woodmouse[sample(15, size = 110, replace = TRUE), ]
> h <- haplotype(x)
> net <- haploNet(h)
> plot(net, size=attr(net, "freq"), scale.ratio = 2, cex = 0.8)

This script is used to build an haplotype network using {pegas}. The bigger circles represent much more haplotypes of some type. I`d like to know how I could set in the dnabin matrix the origin of the haplotypes, so they would appear with different colors in the network.

C_Z_
  • 7,427
  • 5
  • 44
  • 81
  • What is the `dnabin matrix`? What are the populations in your sample? Again, being as specific as possible as to what the desired output should be. Are you sure these functions calculate the values you wish to plot? Try to explain that data to some one unfamiliar with the package (because `pegas` doesn't seem to be a very common one here) because often users can modify a plotting function to create whatever you want, as long as you are very specific about what you want and where the data is coming from. I still think this question is lacking in sufficient detail to be answerable. – MrFlick Sep 10 '14 at 01:47
  • Pegas is a R package for Population and Evolutionary Genetics Analysis System.A dnabin is a matrix or a list of DNA sequences with the names of the taxa read in the file as rownames or names,respectively.By default, the sequences are stored in binary format. This dnabin file is build with the function read.dna in the {pegas} package. When I have a dataset of DNA, I have sequences from individuals of different populations(e.g. 100 individuals from 5 populations, 20 individuals from each). I would like to know how to tag each individual in the dnabin matrix to say which population it came from. – Guilherme De Rezende Dias Sep 10 '14 at 01:55

1 Answers1

5

Ok, trying to make sense from your example. It appears the populations you have are 15 populations with anywhere from 3-13 samples per population.

table(rownames(x))

# No0906S No0908S No0909S No0910S No0912S No0913S No1007S 
#      10       8       6       3       3       7       6 
# No1103S No1114S No1202S No1206S No1208S   No304   No305 
#       4      13       9       6       9      13       7 
#   No306 
#       6

When you run haplotype(x), you get (unsurprisingly) 15 haplotypes representing a 1:1 mapping from population to haplotype. We can create a table showing the relationship between the populations and haplotypes with

ind.hap<-with(
    stack(setNames(attr(h, "index"), rownames(h))), 
    table(hap=ind, pop=rownames(x)[values])
)
ind.hap[1:10, 1:9]  #print just a chunk

#       pop
# hap    No0906S No0908S No0909S No0910S No0912S No0913S No1007S No1103S No1114S
#   I          0       0       0       0       0       0       0       0       0
#   II         0       0       0       0       0       0       6       0       0
#   III        0       0       0       0       0       0       0       4       0
#   IV        10       0       0       0       0       0       0       0       0
#   IX         0       0       0       0       0       0       0       0       0
#   V          0       0       6       0       0       0       0       0       0
#   VI         0       0       0       0       0       0       0       0       0
#   VII        0       0       0       0       0       7       0       0       0
#   VIII       0       0       0       0       0       0       0       0      13
#   X          0       0       0       0       0       0       0       0       0

We can use this table during plotting to draw pic chars at each of the nodes.

plot(net, size=attr(net, "freq"), scale.ratio = 2, cex = 0.8, pie=ind.hap)
legend(50,50, colnames(ind.hap), col=rainbow(ncol(ind.hap)), pch=20)

enter image description here

To better show off the pie charts, we can assign incorrect populations to each of the samples

wrong.pop<-rep(letters[1:5], each=22)
ind.hap2<-with(
    stack(setNames(attr(h, "index"), rownames(h))), 
    table(hap=ind, pop=wrong.pop[values])
)

plot(net, size=attr(net, "freq"), scale.ratio = 2, cex = 0.8, pie=ind.hap2)
legend(50,50, colnames(ind.hap2), col=rainbow(ncol(ind.hap2)), pch=20)

enter image description here

Here you can see we have more diversity at each haplotype because we've incorrectly labeled the populations with artificial names so they don't clump as nicely.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Excelent! Thank you very much! I still have an issue: Dd you know how you can plot different individuals with the same color?(e.g. if you want to treat No0906S No0908S No0909S No0910S as a same population, maintaining their names but plotting them in the same color on the network) I`m just starting to learn how does R works, your help has been very useful! Thank you very much. – Guilherme De Rezende Dias Sep 10 '14 at 04:04
  • That will not be very direct as far as I can tell. That's certainly outside the scope a comment. If you like, you may post a new question so that others may answer. – MrFlick Sep 10 '14 at 04:14
  • 2
    @MrFlick please note that this code has a bug. Notice in your first plot haplotype "VIII" should occur in sample "No1114S", but that is not how it's coloured in the plot. I've answered to this issue [here](http://stackoverflow.com/questions/31220586/r-how-to-plot-correct-pie-charts-in-haplonet-haplotyp-networks-pegas-ape-a/33107267#33107267). In short, you need to sort the haplotype object by label `h <- sort(h, what = "label")` before proceeding. – hugot Oct 13 '15 at 15:59
  • 1
    Is there a way to change the color of the pie charts? `col` affects the color of the legend, but not the color of the pie charts in the plot. – GabrielMontenegro Jun 08 '17 at 08:52