MEA_analysis_plot_bursts_LAa.R 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Laura Aarnos 190515
  2. # Plotting burst detection results
  3. # attach packages from library
  4. #library(IGM.MEA)
  5. library(Hmisc)
  6. library(meaRtools) #attach meaRtools after IGM.MEA to get right parameter names
  7. # import data
  8. # location of RDS file (e.g. 'Z:\Public user documents\User\Folders between\Analysis\R objects\Filename')
  9. my.input <- 'Z:/Public user documents/paavitan/Varaosat2/Lundbeck erilaistus/MEA Analysis/Analysed files/meaRtools/Lundbeck_meaRtools/LundbeckE6/N2579_final/Analysis/R objects/N2579_02-26-19_14_17_31'
  10. # import function
  11. burst.set <- readRDS(my.input)
  12. # instructions to derive data of a single electrode:
  13. # with a click, open burst.set located in the environment
  14. # see which [[list number]] corresponds to desired DIV
  15. # set the information below
  16. my.e <- burst.set[[20]]$allb$C3_14 # burst.set[[i]]$allb$e, where i is the list number, e is electrode name
  17. my.bs <- burst.set[[20]]$bs['C3_14',] # burst.set[[i]]$bs['e',]
  18. my.spikes <- burst.set[[20]]$spikes$C3_14 # burst.set[[i]]$spikes$e
  19. name <- 'DIV 120, C3_14' # 'name', name is used in plots
  20. burst.set[[20]]$rec_time # bursts.set[[i]]$rec_time, printing recording time start and end in seconds (rec time is based on first and last spike detected)
  21. # printing mean and median burst durations
  22. mean(my.e[,'durn']) # printing mean burst duration
  23. median(my.e[,'durn']) # printing median burst duration
  24. # plot boxplots of IBI, number of spikes in a burst, burst duration and mean ISIs in burst
  25. par(mfrow = c(2,2)) # setting number of plots in display
  26. boxplot(my.e[,'ibi'],
  27. names = c(' '), main = paste0(name, ', IBI'), ylab = 'IBI (s)',
  28. col = c('lightseagreen'))
  29. boxplot(my.e[,'len'],
  30. names = c(' '), main = paste0(name, ', spikes in burst'), ylab = 'Number of spikes in burst',
  31. col = c('lightseagreen'))
  32. boxplot(my.e[,'durn'],
  33. names = c(' '), main = paste0(name, ', burst duration'), ylab = 'Burst duration (s)',
  34. col = c('lightseagreen'))
  35. boxplot(my.e[,'mean_isis'],
  36. names = c(' '), main = paste0(name, ', mean ISIs'), ylab = 'Mean ISIs (s)',
  37. col = c('lightseagreen'))
  38. # plot histogram: % of in-burst spikes of all spikes, number of bursts
  39. par(mfrow = c(1,2)) # setting number of plots in display
  40. my.nbursts <- my.bs[,'nbursts']
  41. barplot(my.nbursts,
  42. main = name, xlab = 'Number of bursts', ylim = c(0,max(my.nbursts)+10),
  43. col = c('lightseagreen'), beside = TRUE)
  44. my.per_spikes <- my.bs[,'per_spikes_in_burst']
  45. barplot(my.per_spikes,
  46. main = name, xlab = '% of spikes within bursts', ylim = c(0,100),
  47. col = c('lightseagreen'), beside = TRUE)
  48. # plotting burst detection - set start and end time below
  49. my.y <- c(rep(1, length(my.spikes))) # creating ready-to-plot spikes from time stamps
  50. my.logISI <- cbind(my.e[,'beg'], my.e[,'end']) # retrieving burst timing info
  51. par(mfrow = c(1,1)) # setting number of plots in display
  52. my.start <- 0 # SET START TIME FOR PLOT
  53. my.end <- 200 # SET END TIME FOR PLOT
  54. my.lwd <- 3 # line width for burst line
  55. my.nx = 5 # number of minor tick to draw between major ticks, good number depends on your plotted time interval
  56. # plot spikes of the specified time interval
  57. plot(my.spikes, my.y, xlim = c(my.start, my.end), ylim = c(0,2.5), type = 'h', xlab = 'Spike times (s)', ylab = '', main = name, yaxt = 'n')
  58. minor.tick(nx = my.nx, ny = 0, tick.ratio = 0.7)
  59. #plot bursts of the specified time interval
  60. for (i in 1:nrow(my.logISI)) {
  61. lines(my.spikes[my.logISI[i,]],c(1.1,1.1), col = 'lightseagreen', lwd = my.lwd)
  62. }
  63. # if you wish to save your plot, click export on top of the plot