File size: 4,777 Bytes
6b29baa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c329a72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b29baa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
## NOTE: The data is currently on /lts/mblab/downloaded_data/barkai_checseq
## and the parquet dataset is on the brentlab-strides aws at s3://yeast-binding-perturbation-data/barkai_checkseq

library(tidyverse)
library(here)
library(arrow)

# genomic feature harmonization table ----
# see https://huggingface.co/datasets/BrentLab/yeast_genome_resources
genomefeatures = arrow::open_dataset(here("data/genome_files/hf/features")) %>%
  as_tibble() %>%
  mutate(rownum = row_number())

chrmap = read_csv("~/projects/parsing_yeast_database_data/data/genome_files/chrmap.csv.gz")

# scp -r chasem@login.htcf.wustl.edu:/lts/mblab/downloaded_data/chipexo/paper_mimic_tag_count_20250718 .
genomecov_files = list.files(here("data/chip_exo/paper_mimic_tag_count_20250718"), full.names = TRUE)

genomecov_df = tibble(
  path = genomecov_files,
  tmp = str_remove(basename(genomecov_files), ".mLb.mkD.sorted_r1_5p_cov.txt")) %>%
  separate(tmp,c('sample', 'replicate'), extra = "merge", remove = FALSE) %>%
  mutate(replicate = ifelse(sample != "control",
                            as.integer(str_extract(str_extract(replicate, "REP\\d"), "\\d")),
                            as.integer(str_extract(str_extract(replicate, "T\\d+"), "\\d+"))))

nf_core_meta = read_csv(here("data/chip_exo/nfcore_chipseq_full_samplesheet.csv")) %>%
  group_by(sample) %>%
  mutate(tmp = row_number()) %>%
  ungroup() %>%
  mutate(replicate = ifelse(sample == "control", tmp, replicate)) %>%
  select(-tmp)

genomecov_df_meta = genomecov_df %>%
  left_join(nf_core_meta)

pugh_genomecov_tmp <- genomecov_df_meta %>%
  select(sample, replicate, run_accession, yeastepigenome_id, path) %>%
  mutate(sample = tolower(sample)) %>%
  left_join(
    genomefeatures %>%
      select(symbol, locus_tag) %>%
      distinct() %>%
      mutate(sample = tolower(symbol)),
    by = "sample"
  )

filled_missing_locus_tag =  pugh_genomecov_tmp %>%
  filter(!complete.cases(.)) %>%
  mutate(
    alias_rownum = map_int(sample, function(s) {
      idx <- which(str_detect(genomefeatures$alias, fixed(toupper(s))))
      if (length(idx) > 0) idx[1] else NA_integer_
    }),
    alias_rownum = ifelse(
      is.na(alias_rownum) & str_starts(sample, "y"),
      map_int(sample, function(s) {
        idx <- which(str_detect(genomefeatures$locus_tag, fixed(toupper(s))))
        if (length(idx) > 0) idx[1] else NA_integer_
      }),
      alias_rownum
    )
  ) %>%
  select(-c(symbol, locus_tag)) %>%
  left_join(genomefeatures %>%
              select(rownum, symbol, locus_tag) %>%
              dplyr::rename(alias_rownum=rownum)) %>%
  select(-alias_rownum)

pugh_genomecov_meta = pugh_genomecov_tmp %>%
  filter(!is.na(locus_tag)) %>%
  bind_rows(
    filled_missing_locus_tag) %>%
  mutate(regulator_symbol = symbol, regulator_locus_tag = locus_tag) %>%
    arrange(regulator_locus_tag) %>%
    group_by(regulator_locus_tag) %>%
    mutate(sample_id = cur_group_id())

# pugh_genomecov_meta %>%
#   select(regulator_locus_tag, regulator_symbol,
#          run_accession, yeastepigenome_id) %>%
# write_parquet("~/code/hf/rossi_2021/rossi_2021_metadata.parquet",
#               compression = "zstd",
#               write_statistics = TRUE,
#               use_dictionary = c(
#                 regulator_locus_tag = TRUE,
#                 regulator_symbol = TRUE
#               )
# )

process_chipexo_genomecov_file = function(covpath, accession_str){

  data.table::fread(covpath, sep = "\t", col.names=c('chr', 'pos', 'pileup')) %>%
    left_join(chrmap %>% select(refseq, ucsc) %>%
                dplyr::rename(chr=refseq)) %>%
    mutate(chr = ucsc,
           accession = accession_str) %>%
    select(chr, pos, pileup, accession)

}


# Output base directory for partitioned dataset
output_parquet_dir = file.path(here("data/chip_exo/"), "genome_map")
dir.create(output_parquet_dir)

# Write incrementally
# for (accession_str in pugh_genomecov_meta$run_accession) {
#
#   sample = pugh_genomecov_meta %>% filter(run_accession == accession_str) %>% pull(sample)
#   replicate_str = pugh_genomecov_meta %>% filter(run_accession == accession_str) %>% pull(replicate)
#   path = pugh_genomecov_meta %>% filter(run_accession == accession_str) %>% pull(path)
#
#
#     message(glue::glue("Processing {sample} ({replicate_str})"))
#
#     df <- process_chipexo_genomecov_file(
#       path,
#       accession_str
#     )
#
#     # Write just this sample's data to the appropriate partition
#     arrow::write_dataset(
#       df,
#       path = output_parquet_dir,
#       format = "parquet",
#       partitioning = c("accession"),
#       existing_data_behavior = "overwrite",
#       compression = "zstd",
#       write_statistics = TRUE,
#       use_dictionary = c(
#         chr = TRUE
#       )
#     )
# }