suzuki-2001 commited on
Commit
d739c9a
·
verified ·
1 Parent(s): 7f6a69a

Update rnacentral-non-coding-rna.py

Browse files
Files changed (1) hide show
  1. rnacentral-non-coding-rna.py +32 -30
rnacentral-non-coding-rna.py CHANGED
@@ -3,30 +3,33 @@ import datasets
3
  from Bio import SeqIO
4
  import os
5
  import gzip
6
- import urllib.request
7
  import logging
8
  import tqdm
9
  from collections import defaultdict
 
10
 
11
- _CITATION = """\
12
- @article{the2019rnacentral,
13
- title={RNAcentral: a hub of information for non-coding RNA sequences},
14
- author={The RNAcentral Consortium},
15
- journal={Nucleic acids research},
16
- volume={47},
17
- number={D1},
18
- pages={D221--D229},
19
- year={2019},
20
- publisher={Oxford University Press}
21
- }
22
- """
23
-
24
- _DESCRIPTION = """\
25
- RNAcentral corpus for pre-training, containing sequences and basic metadata.
26
- """
 
 
27
 
28
  class RNACentralMinimalConfig(datasets.BuilderConfig):
29
- """BuilderConfig for RNACentral minimal corpus."""
30
  def __init__(
31
  self,
32
  *args,
@@ -34,6 +37,7 @@ class RNACentralMinimalConfig(datasets.BuilderConfig):
34
  max_length: int = 10000,
35
  max_sequences: Optional[int] = None,
36
  include_metadata: bool = True,
 
37
  **kwargs,
38
  ):
39
  super().__init__(*args, **kwargs)
@@ -41,6 +45,7 @@ class RNACentralMinimalConfig(datasets.BuilderConfig):
41
  self.max_length = max_length
42
  self.max_sequences = max_sequences
43
  self.include_metadata = include_metadata
 
44
 
45
  class RNACentralMinimalCorpus(datasets.GeneratorBasedBuilder):
46
  """Minimal RNAcentral corpus for pre-training."""
@@ -58,6 +63,7 @@ class RNACentralMinimalCorpus(datasets.GeneratorBasedBuilder):
58
  max_sequences=1000000,
59
  include_metadata=True
60
  )
 
61
 
62
  def _info(self):
63
  features = {
@@ -94,25 +100,21 @@ class RNACentralMinimalCorpus(datasets.GeneratorBasedBuilder):
94
  return rfam_families
95
 
96
  def _split_generators(self, dl_manager: datasets.DownloadManager):
97
- base_url = "https://ftp.ebi.ac.uk/pub/databases/RNAcentral/current_release"
98
- urls = {
99
- 'sequences': f"{base_url}/sequences/rnacentral_active.fasta.gz",
100
- }
101
 
102
- if self.config.include_metadata:
103
- urls['rfam'] = f"{base_url}/rfam/rfam_annotations.tsv.gz"
104
-
105
- downloaded_files = dl_manager.download(urls)
106
-
107
  rfam_families = {}
108
  if self.config.include_metadata:
109
- rfam_families = self._load_minimal_rfam(downloaded_files['rfam'])
 
 
110
 
111
  return [
112
  datasets.SplitGenerator(
113
  name=datasets.Split.TRAIN,
114
  gen_kwargs={
115
- "filepath": downloaded_files['sequences'],
116
  "rfam_families": rfam_families,
117
  "split": "train",
118
  },
@@ -120,7 +122,7 @@ class RNACentralMinimalCorpus(datasets.GeneratorBasedBuilder):
120
  datasets.SplitGenerator(
121
  name=datasets.Split.VALIDATION,
122
  gen_kwargs={
123
- "filepath": downloaded_files['sequences'],
124
  "rfam_families": rfam_families,
125
  "split": "validation",
126
  },
 
3
  from Bio import SeqIO
4
  import os
5
  import gzip
6
+ import wget
7
  import logging
8
  import tqdm
9
  from collections import defaultdict
10
+ import tempfile
11
 
12
+ class RNACentralDownloader:
13
+ """Helper class to manage RNAcentral downloads"""
14
+ BASE_URL = "https://ftp.ebi.ac.uk/pub/databases/RNAcentral/current_release"
15
+
16
+ @staticmethod
17
+ def download_file(url: str, output_dir: str) -> str:
18
+ """Download a file with progress bar and return local path"""
19
+ os.makedirs(output_dir, exist_ok=True)
20
+ output_path = os.path.join(output_dir, os.path.basename(url))
21
+
22
+ if os.path.exists(output_path):
23
+ logging.info(f"File already exists: {output_path}")
24
+ return output_path
25
+
26
+ logging.info(f"Downloading {url} to {output_path}")
27
+ wget.download(url, output_path)
28
+ print() # New line after wget progress bar
29
+ return output_path
30
 
31
  class RNACentralMinimalConfig(datasets.BuilderConfig):
32
+ """BuilderConfig for RNAcentral minimal corpus."""
33
  def __init__(
34
  self,
35
  *args,
 
37
  max_length: int = 10000,
38
  max_sequences: Optional[int] = None,
39
  include_metadata: bool = True,
40
+ cache_dir: Optional[str] = None,
41
  **kwargs,
42
  ):
43
  super().__init__(*args, **kwargs)
 
45
  self.max_length = max_length
46
  self.max_sequences = max_sequences
47
  self.include_metadata = include_metadata
48
+ self.cache_dir = cache_dir or tempfile.gettempdir()
49
 
50
  class RNACentralMinimalCorpus(datasets.GeneratorBasedBuilder):
51
  """Minimal RNAcentral corpus for pre-training."""
 
63
  max_sequences=1000000,
64
  include_metadata=True
65
  )
66
+ self.downloader = RNACentralDownloader()
67
 
68
  def _info(self):
69
  features = {
 
100
  return rfam_families
101
 
102
  def _split_generators(self, dl_manager: datasets.DownloadManager):
103
+ # Direct download using wget instead of datasets downloader
104
+ sequences_url = f"{self.downloader.BASE_URL}/sequences/rnacentral_active.fasta.gz"
105
+ sequences_path = self.downloader.download_file(sequences_url, self.config.cache_dir)
 
106
 
 
 
 
 
 
107
  rfam_families = {}
108
  if self.config.include_metadata:
109
+ rfam_url = f"{self.downloader.BASE_URL}/rfam/rfam_annotations.tsv.gz"
110
+ rfam_path = self.downloader.download_file(rfam_url, self.config.cache_dir)
111
+ rfam_families = self._load_minimal_rfam(rfam_path)
112
 
113
  return [
114
  datasets.SplitGenerator(
115
  name=datasets.Split.TRAIN,
116
  gen_kwargs={
117
+ "filepath": sequences_path,
118
  "rfam_families": rfam_families,
119
  "split": "train",
120
  },
 
122
  datasets.SplitGenerator(
123
  name=datasets.Split.VALIDATION,
124
  gen_kwargs={
125
+ "filepath": sequences_path,
126
  "rfam_families": rfam_families,
127
  "split": "validation",
128
  },