thewall commited on
Commit
c7b2a4a
1 Parent(s): 73ce77a

Update jolma_unique.py

Browse files
Files changed (1) hide show
  1. jolma_unique.py +27 -25
jolma_unique.py CHANGED
@@ -1,10 +1,9 @@
1
  import os
2
- import json
3
  import re
 
4
  import pandas as pd
5
  import datasets
6
 
7
-
8
  logger = datasets.logging.get_logger(__name__)
9
 
10
 
@@ -54,34 +53,26 @@ _URL = "ftp://ftp.sra.ebi.ac.uk/vol1/run/"
54
  # _REVERSE_PRIMER = "CCTATGCGTGCTAGTGTGA"
55
  # _DESIGN_LENGTH = 30
56
 
57
-
58
- config = datasets.load_dataset(path="thewall/deepbindweight", split="all")
59
- info = pd.read_excel(config['selex'][0], index_col=0)
60
-
61
- _URLS = {}
62
- _DESIGN_LENGTH = {}
63
  pattern = re.compile("(\d+)")
64
- for idx, row in info.iterrows():
65
- sra_id = idx
66
- file = row["file"]
67
- _URLS[sra_id] = "/".join([_URL, sra_id[:6], sra_id, file])
68
- _DESIGN_LENGTH[sra_id] = int(pattern.search(row["Ligand"]).group(0))
69
 
70
 
71
  class JolmaUniqueConfig(datasets.BuilderConfig):
72
- def __init__(self, url, sra_id="ERR173157", length_match=True, filter_N=True, design_length=None, file=None, **kwargs):
73
  super(JolmaUniqueConfig, self).__init__(**kwargs)
74
- self.url = url
75
- self.sra_id = sra_id
76
  self.length_match = length_match
77
- self.design_length = design_length
78
  self.filter_N = filter_N
79
  self.file = file
80
 
81
 
82
  class JolmaUnique(datasets.GeneratorBasedBuilder):
 
 
 
83
  BUILDER_CONFIGS = [
84
- JolmaUniqueConfig(name=key, url=_URLS[key], design_length=_DESIGN_LENGTH[key], file=info.loc[key]['file']) for key in _URLS
85
  ]
86
 
87
  DEFAULT_CONFIG_NAME = "ERR173157"
@@ -102,22 +93,35 @@ class JolmaUnique(datasets.GeneratorBasedBuilder):
102
  citation=_CITATION,
103
  )
104
 
 
 
 
 
 
 
 
 
 
105
  def _split_generators(self, dl_manager):
106
  downloaded_files = None
 
 
 
107
  if getattr(self.config, "data_dir") is not None:
108
- # downloaded_files = os.path.join(self.config.data_dir, self.config.sra_id, self.config.file)
109
- downloaded_files = dl_manager.extract(os.path.join(self.config.data_dir, self.config.sra_id, self.config.file))
110
  logger.info(f"Load from {downloaded_files}")
111
  if downloaded_files is None or not os.path.exists(downloaded_files):
112
- logger.info(f"Download from {self.config.url}")
113
- downloaded_files = dl_manager.download_and_extract(self.config.url)
 
114
  return [
115
  datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files}),
116
  ]
117
 
118
  def filter_fn(self, example):
119
  seq = example["seq"]
120
- if self.config.length_match and len(seq)!=self.config.design_length:
121
  return False
122
  if self.config.filter_N and "N" in seq:
123
  return False
@@ -154,5 +158,3 @@ if __name__=="__main__":
154
  from datasets import load_dataset
155
  dataset = load_dataset("jolma_unique.py", name="ERR173157", split="all")
156
  # dataset = load_dataset("jolma.py", name="ERR173157", split="all", data_dir="/root/autodl-fs/ERP001824-461")
157
-
158
-
 
1
  import os
 
2
  import re
3
+ from functools import cached_property
4
  import pandas as pd
5
  import datasets
6
 
 
7
  logger = datasets.logging.get_logger(__name__)
8
 
9
 
 
53
  # _REVERSE_PRIMER = "CCTATGCGTGCTAGTGTGA"
54
  # _DESIGN_LENGTH = 30
55
 
56
+ _DOWNLODE_MANAGER = datasets.DownloadManager()
57
+ _RESOURCE_URL = "https://huggingface.co/datasets/thewall/DeepBindWeight/resolve/main"
58
+ SELEX_INFO_FILE = _DOWNLODE_MANAGER.download(f"{_RESOURCE_URL}/ERP001824-deepbind.xlsx")
 
 
 
59
  pattern = re.compile("(\d+)")
 
 
 
 
 
60
 
61
 
62
  class JolmaUniqueConfig(datasets.BuilderConfig):
63
+ def __init__(self, length_match=True, filter_N=True, file=None, **kwargs):
64
  super(JolmaUniqueConfig, self).__init__(**kwargs)
 
 
65
  self.length_match = length_match
 
66
  self.filter_N = filter_N
67
  self.file = file
68
 
69
 
70
  class JolmaUnique(datasets.GeneratorBasedBuilder):
71
+
72
+ SELEX_INFO = pd.read_excel(SELEX_INFO_FILE, index_col=0)
73
+
74
  BUILDER_CONFIGS = [
75
+ JolmaUniqueConfig(name=key) for key in SELEX_INFO.index
76
  ]
77
 
78
  DEFAULT_CONFIG_NAME = "ERR173157"
 
93
  citation=_CITATION,
94
  )
95
 
96
+ @cached_property
97
+ def selex_info(self):
98
+ return self.SELEX_INFO.loc[self.config.name]
99
+
100
+ @cached_property
101
+ def design_length(self):
102
+ selex_info = self.selex_info
103
+ return int(pattern.search(selex_info["Ligand"]).group(0))
104
+
105
  def _split_generators(self, dl_manager):
106
  downloaded_files = None
107
+ sra_id = self.config.name
108
+ selex_info = self.selex_info
109
+ file = selex_info['file']
110
  if getattr(self.config, "data_dir") is not None:
111
+ path = os.path.join(self.config.data_dir, sra_id, file)
112
+ downloaded_files = dl_manager.extract(path)
113
  logger.info(f"Load from {downloaded_files}")
114
  if downloaded_files is None or not os.path.exists(downloaded_files):
115
+ downloaded_url = "/".join([_URL, sra_id[:6], sra_id, file])
116
+ logger.info(f"Download from {downloaded_url}")
117
+ downloaded_files = dl_manager.download_and_extract(downloaded_url)
118
  return [
119
  datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files}),
120
  ]
121
 
122
  def filter_fn(self, example):
123
  seq = example["seq"]
124
+ if self.config.length_match and len(seq)!=self.design_length:
125
  return False
126
  if self.config.filter_N and "N" in seq:
127
  return False
 
158
  from datasets import load_dataset
159
  dataset = load_dataset("jolma_unique.py", name="ERR173157", split="all")
160
  # dataset = load_dataset("jolma.py", name="ERR173157", split="all", data_dir="/root/autodl-fs/ERP001824-461")