Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugs in the main function #24

Closed
GabrieleNocchi opened this issue Sep 26, 2024 · 9 comments
Closed

Bugs in the main function #24

GabrieleNocchi opened this issue Sep 26, 2024 · 9 comments
Assignees

Comments

@GabrieleNocchi
Copy link

GabrieleNocchi commented Sep 26, 2024

Hi there,

I was trying to give a try to your tool but I think there are some issues.

These lines in your code causes problems, and I am not sure why are there:
Check coordinate site IDs exactly match admixture site IDs

  if ( all(coords_df[[1]] == unique(admixture_df[[1]])) == FALSE ) {
    stop("Site names in coordinates data frame do not match site names in admixture data frame. Check site names are not empty or NA and that they match.")
  }

Why simply not:
Check coordinate site IDs exactly match admixture site IDs

  if ( unique(coords_df[[1]] == unique(admixture_df[[1]])) == FALSE ) {
    stop("Site names in coordinates data frame do not match site names in admixture data frame. Check site names are not empty or NA and that they match.")
  }

My site names are exactly the same between coordinates and admixture file, but I can t go past this error. This however does not happen with your data. If I remove those lines above from the function, it works. Also note sure why the warning about longer object having to be a multiple.
Also, if the sampling area is small, I can t find a way for the pie not to overlap even when I decrease the pie size and map size, they overlap. If I adjust the limits of the map to make it much smaller (ie 1 degree Lat and 1 degree Lon), it still makes it bigger than specified.

I'd love to use your package but at the moment is much easier to do this from scratch using mapplots draw.pie function:
https://www.rdocumentation.org/packages/mapplots/versions/1.5.2/topics/draw.pie

@Tom-Jenkins
Copy link
Owner

Hi @GabrieleNocchi,

  1. Please can you provide me with some example data for me to test your error. It would be ideal if you could also copy the exact code you are using for me to test on my R. The all() part means if any of the comparisons are FALSE then print the stop message. I am happy to test your option to see if that is a better solution.

  2. Overlapping pies. This is not trivial, see overlapping pies  #20. This would be a great feature but unfortunately I currently don't have the time to invest in a neat solution (if it exists). If you adjust the limits and make the pie charts smaller does that not work? Or are they still too close together? I can explore this if you provide an example of your code.

  3. I'm not sure what you mean by "Also not sure why the warning about longer object having to be a multiple". Please let me know which line of code you are referring to.

Thanks, Tom

@Tom-Jenkins Tom-Jenkins self-assigned this Sep 26, 2024
@GabrieleNocchi
Copy link
Author

Hi Tom,

  1. I have attached input files and the script I used. Note that in my script, the first few lines, are to format the data exactly as yours. In short, I have 380 individuals divided in 24 populations (24 unique sites). The final map should have 24 pies. I have attached the expected results (file pie_map.svg, which I coded myself using mapplots and other libraries back then). The reason I was interested in your function is because it uses ggplot, and then it make easier to collate map + structure plot using gridExtra -- and of course because if it works it is probably much quicker and cleaner than my method.

So, as we are here. I also think that the mapmixture function should have options to specify the column names where sites are stored, where admixture proportion are stored and where lat and long are stored. Maybe it still works or I missed it (in that case my bad), but to be safe I reformatted everything like you. It is a bit tedious to have to reformat the data exactly like yours, it defeat the purpose of the tool (speeding up and ease how to make those maps which can already be done using other things). I don t want to sound over critic, this tool is a great idea, just trying to help.

  1. In reply to your second point, no, if I adjust the limits and try to reduce size of pies I still can't do what what I want. But maybe the issue is not in that as I see the map gets the correct limit but the pies still overlap. But I think it is caused by something else before, as I only see two pies in the final plot (rather than 24) . I am not sure what is going on.

  2. That's the error that it throws if I dont remove those two lines I flagged in my initial comment from your mapmixture function:

> mapmixture(admixture1, coordinates, crs = 4326, boundary = c(xmin=min(na.omit(coordinates$Lon)-0.05), xmax=max(na.omit(coordinates$Lon)+0.05), ymin=min(na.omit(coordinates$Lat)-0.05), ymax=max(na.omit(coordinates$Lat)+0.05)), pie_size = 0.1)
Error in mapmixture(admixture1, coordinates, crs = 4326, boundary = c(xmin = min(na.omit(coordinates$Lon) -  : 
  Site names in coordinates data frame do not match site names in admixture data frame. Check site names are not empty or NA and that they match.
In addition: Warning message:
**In coords_df[[1]] == unique(admixture_df[[1]]) :
  longer object length is not a multiple of shorter object length**

Note, my code in the attached file only work if you edit the mapmixture function removing those lines I flagged before, else it will throw the error I reported in point 3. Test.png is the "wrong" map produced with mapmixture, while pie_map is what it should look like (don't mind the extra features and graphical stuff, it is the one I coded with mapplots and other libraries).

data.zip

Cheers,
G.

@GabrieleNocchi
Copy link
Author

As a final note, I downloaded from CRAN. Had issues with dependencies when trying to download from GitHub the development version

@Tom-Jenkins
Copy link
Owner

As a final note, I downloaded from CRAN. Had issues with dependencies when trying to download from GitHub the development version

While I look into your main issue, on this point please can you let me know what version of R you are using. I recently pushed a change to the documentation because R version 4.2+ is needed for mapmixture to install. If you are using 4.2+ please can you also copy the error message so that I can see what's going on.

@GabrieleNocchi
Copy link
Author

GabrieleNocchi commented Sep 26, 2024

I found the issues:

I had a wrong coordinate for Cathedral N in the coordinates file. Also the coordinates file does not have to be per individual, but per site -- only the unique locations without repeating the rows. I fixed it with:

library(mapmixture)

admixture1 <- read.table("spruce_filtered_MAF_fast_output.2.meanQ")
coordinates <- read.table("ordered_metadata_with_coord.txt", sep = "\t")


admixture1 <- cbind(paste(coordinates$V3,coordinates$V4, sep ="_"), coordinates$V1, admixture1$V1,admixture1$V2)
colnames(admixture1) <- c("Site","Ind","Cluster1","Cluster2")

admixture1 <- as.data.frame(admixture1)


coordinates <-  cbind(paste(coordinates$V3,coordinates$V4, sep ="_"),coordinates$V6, coordinates$V5)
colnames(coordinates) <- c("Site","Lat","Lon")
coordinates <- as.data.frame(coordinates)


coordinates$Lat <- as.numeric(as.character(coordinates$Lat))
coordinates$Lon <- as.numeric(as.character(coordinates$Lon))

admixture1$Cluster1 <- as.numeric(as.character(admixture1$Cluster1))
admixture1$Cluster2 <- as.numeric(as.character(admixture1$Cluster2))






df <- coordinates
df_sorted <- df[order(df$Site, decreasing = TRUE), ]
coordinates <- unique(df_sorted)


mapmixture(admixture1, coordinates, crs = 4326, boundary = c(xmin=min(na.omit(coordinates$Lon)-0.05), xmax=max(na.omit(coordinates$Lon)+0.05), ymin=min(na.omit(coordinates$Lat)-0.05), ymax=max(na.omit(coordinates$Lat)+0.05)), pie_size = 0.1)

last lines before the mapmixture function is where I "fix" the coord file.

@GabrieleNocchi
Copy link
Author

So everything seems like it is working now. Now also making the map smaller and pie smalle make the pies not overlap -- great!

@Tom-Jenkins
Copy link
Owner

OK that's great to hear! Is there anything you want me to look at still or shall I close the GitHub issue?

Tom

@GabrieleNocchi
Copy link
Author

It works as intended now I think! Just testing a few of the other functions!

@GabrieleNocchi
Copy link
Author

It all seems good. Amazing, my bad the issue was on my end and also wrong formatting of my coordinates data. It is great now -- finally can do those pie maps using ggplot, I was limited to base R before as the libraries I was using were not compatible with ggplot. Did some quick tests, require a bit of tweaking, but very good results with no much effort:
test.zip

Amazing! Very happy to have found this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants