cells2numbers / migrationminer

MigrationmineR is a R package to analyse and profile in vitro cell tracking and migration data. It is belongs to the cytominer-verse used for morphological profiling and allows to create temporal or dynamic profiles.
Other
5 stars 0 forks source link

create vignette and explain sector analysis #3

Open cells2numbers opened 7 years ago

cells2numbers commented 5 years ago

Sector analysis can be calculated using

tracks %>% group_by(Track_Sector) %>% 
  count %>% 
  rename(n_per_sector  = n ) %>% 
  mutate(fraction = n_per_sector / nrow(tracks)) %>%
  print 

Result:

Track_Sector n_per_sector fraction
1 166 0.3971292    
2 112 0.2679426    
3 68 0.1626794    
4 71 0.1698565
cells2numbers commented 5 years ago

for multiple experiments use

df_debug %>% 
  ungroup() %>% 
  group_by(Metadata_experiment) %>% 
  count %>% 
  rename(n_per_sector  = n ) %>% 
  spread(key = Track_Sector, value = n_per_sector) %>% 
  rename('sector_1' = '1') %>% 
  rename('sector_2' = '2') %>% 
  rename('sector_3' = '3') %>% 
  rename('sector_4' = '4') %>% 
  mutate(sector_1_fraction = sector_1 / (sector_1 + sector_2 + sector_3 + sector_4)) %>% 
  mutate(sector_2_fraction = sector_2 / (sector_1 + sector_2 + sector_3 + sector_4)) %>% 
  mutate(sector_3_fraction = sector_3 / (sector_1 + sector_2 + sector_3 + sector_4)) %>% 
  mutate(sector_4_fraction = sector_4 / (sector_1 + sector_2 + sector_3 + sector_4)) %>% 
  print

Result:

Metadata_experiment Metadata_Frame_Subset Metadata_offset sector_1 sector_2 sector_3 sector_4 sector_1_fraction sector_2_fraction sector_3_fraction sector_4_fraction
10 0 6 3 18 3 2 0.1153846 0.6923077 0.1153846 0.07692308
10 0 12 6 16 5 1 0.2142857 0.5714286 0.1785714 0.03571429
10 10 6 8 7 7 5 0.2962963 0.2592593 0.2592593 0.18518519
10 10 12 4 8 5 2 0.2105263 0.4210526 0.2631579 0.10526316
cells2numbers commented 5 years ago

The sector analysis on experiment level is implemented summarize_sectors, see PR #23 Changes to sample code above

Still missing:

summarize_sectors <- function(tracks, strata) {
  results <- tracks %>%
    dplyr::ungroup() %>%
    dplyr::group_by_(.dots = c(strata,'Track_Sector')) %>%
    dplyr::count() %>%
    dplyr::rename(n_per_sector  = n ) %>%
    tidyr::spread(key = Track_Sector, value = n_per_sector)

  # replace na with 0
  results[is.na(results)] <- 0

  # add missing cols
  missing_cols <- setdiff(c("1","2","3","4"),setdiff(colnames(results),strata))
  results[missing_cols] <- 0

  sector_summary <- results %>%
    dplyr::rename('sector_left' = '1') %>%
    dplyr::rename('sector_right' = '2') %>%
    dplyr::rename('sector_up' = '3') %>%
    dplyr::rename('sector_down' = '4') %>%
    dplyr::mutate(sector_left_fraction =
        sector_left  / (sector_left + sector_right + sector_up + sector_down)) %>%
    dplyr::mutate(sector_right_fraction =
        sector_right / (sector_left + sector_right + sector_up + sector_down)) %>%
    dplyr::mutate(sector_up_fraction =
        sector_up    / (sector_left + sector_right + sector_up + sector_down)) %>%
    dplyr::mutate(sector_down_fraction =
        sector_down  / (sector_left + sector_right + sector_up + sector_down))
}