wyxu commited on
Commit
ef26f19
·
1 Parent(s): 71a7e50

Update Genome_database.py

Browse files
Files changed (1) hide show
  1. Genome_database.py +37 -31
Genome_database.py CHANGED
@@ -1,12 +1,13 @@
1
- from datasets import DatasetBuilder, GenerateMode
2
  from Bio import SeqIO
3
- from typing import Any, Dict, List, Tuple
4
  from Bio.SeqUtils import gc_fraction
 
5
  import os
6
  import gzip
 
7
 
8
- class GenomeDataset(DatasetBuilder):
9
- VERSION = datasets.Version("1.0.0")
10
 
11
  def _info(self):
12
  return datasets.DatasetInfo(
@@ -20,41 +21,45 @@ class GenomeDataset(DatasetBuilder):
20
  "sequence": datasets.Value("string"),
21
  "gc_content": datasets.Value("float"),
22
  "translation_code": datasets.Value("string"),
 
 
23
  })
24
  )
25
 
26
  def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
27
- downloaded_files = dl_manager.download_and_extract('https://ftp.ncbi.nih.gov/genbank/')
28
- train_files = downloaded_files[:int(len(downloaded_files) * 0.8)] # first 80% for training
29
- test_files = downloaded_files[int(len(downloaded_files) * 0.8):] # last 20% for testing
30
-
31
- return [
32
- datasets.SplitGenerator(
33
- name=datasets.Split.TRAIN,
34
- gen_kwargs={"filepaths": train_files}
35
- ),
36
- datasets.SplitGenerator(
37
- name=datasets.Split.TEST,
38
- gen_kwargs={"filepaths": test_files}
39
- )
40
- ]
41
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  def _generate_examples(self, filepaths: List[str]) -> Tuple[str, Dict[str, Any]]:
 
44
  for filepath in filepaths:
45
  if filepath.endswith(".seq.gz"):
46
  with gzip.open(filepath, 'rt') as handle:
47
  for record in SeqIO.parse(handle, "genbank"):
48
  if 'molecule_type' in record.annotations and record.annotations['molecule_type'] == 'DNA':
49
  organism = record.annotations.get('organism', 'unknown')
50
- collection_date = record.annotations.get('date', 'unkown')
51
- year = collection_date.split('-')[-1] if '-' in collection_date else collection_date
52
  for feature in record.features:
53
- if feature.type in ['rRNA', 'tRNA','CDS','tmRNA']:
 
 
 
 
54
  region_type = 'coding'
55
  product = feature.qualifiers.get('product', ['Unknown'])[0]
56
- seq = feature.extract(record.seq)
57
- gc_content = gc_fraction(seq)
58
  if feature.type == 'CDS':
59
  translation = feature.qualifiers.get('translation', [''])[0]
60
  specific_class = 'Protein'
@@ -64,20 +69,18 @@ class GenomeDataset(DatasetBuilder):
64
  elif feature.type == 'regulatory':
65
  region_type = feature.type
66
  specific_class = feature.qualifiers.get('regulatory_class', ['regulatory'])[0]
67
- seq = feature.extract(record.seq)
68
- gc_content = gc_fraction(seq)
69
- #gene_tag = feature.qualifiers.get('locus_tag', [''])[0]
70
  translation = 'NA'
71
  product = 'NA'
72
  elif feature.type == 'gene':
73
  continue
74
  else:
 
 
 
 
75
  region_type = feature.type
76
- seq = feature.extract(record.seq)
77
- gc_content = gc_fraction(seq)
78
  specific_class = 'NA'
79
  translation = 'NA'
80
- product = 'NA'
81
  yield record.id, {
82
  'DNA_id': record.id,
83
  'organism': organism,
@@ -87,5 +90,8 @@ class GenomeDataset(DatasetBuilder):
87
  'product': product,
88
  'sequence': str(seq),
89
  'gc_content': gc_content,
90
- 'translation_code': translation
91
- }
 
 
 
 
1
+ import datasets
2
  from Bio import SeqIO
 
3
  from Bio.SeqUtils import gc_fraction
4
+ from typing import Any, Dict, List, Tuple
5
  import os
6
  import gzip
7
+ import re
8
 
9
+ class GenomeDataset(datasets.GeneratorBasedBuilder):
10
+ VERSION = datasets.Version("1.1.0")
11
 
12
  def _info(self):
13
  return datasets.DatasetInfo(
 
21
  "sequence": datasets.Value("string"),
22
  "gc_content": datasets.Value("float"),
23
  "translation_code": datasets.Value("string"),
24
+ "start_postion": datasets.Value("int32"),
25
+ "end_position": datasets.Value("int32"),
26
  })
27
  )
28
 
29
  def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
30
+ downloaded_files = dl_manager.download_and_extract('https://ftp.ncbi.nih.gov/genbank/')
31
+ train_files = downloaded_files[:int(len(downloaded_files) * 0.8)] # first 80% for training
32
+ test_files = downloaded_files[int(len(downloaded_files) * 0.8):] # last 20% for testing
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ return [
35
+ datasets.SplitGenerator(
36
+ name=datasets.Split.TRAIN,
37
+ gen_kwargs={"filepaths": train_files},
38
+ ),
39
+ datasets.SplitGenerator(
40
+ name=datasets.Split.TEST,
41
+ gen_kwargs={"filepaths": test_files},
42
+ ),
43
+ ]
44
 
45
  def _generate_examples(self, filepaths: List[str]) -> Tuple[str, Dict[str, Any]]:
46
+ split_regex = re.compile('-')
47
  for filepath in filepaths:
48
  if filepath.endswith(".seq.gz"):
49
  with gzip.open(filepath, 'rt') as handle:
50
  for record in SeqIO.parse(handle, "genbank"):
51
  if 'molecule_type' in record.annotations and record.annotations['molecule_type'] == 'DNA':
52
  organism = record.annotations.get('organism', 'unknown')
53
+ collection_date = record.annotations.get('date', 'unknown')
54
+ year = split_regex.split(collection_date)[-1] if '-' in collection_date else collection_date
55
  for feature in record.features:
56
+ seq = feature.extract(record.seq)
57
+ gc_content = gc_fraction(seq)
58
+ start_position = int(feature.location.start)
59
+ end_position = int(feature.location.end)
60
+ if feature.type in ['rRNA', 'tRNA','CDS','tmRNA','mRNA','mat_peptide','sig_peptide','propeptide']:
61
  region_type = 'coding'
62
  product = feature.qualifiers.get('product', ['Unknown'])[0]
 
 
63
  if feature.type == 'CDS':
64
  translation = feature.qualifiers.get('translation', [''])[0]
65
  specific_class = 'Protein'
 
69
  elif feature.type == 'regulatory':
70
  region_type = feature.type
71
  specific_class = feature.qualifiers.get('regulatory_class', ['regulatory'])[0]
 
 
 
72
  translation = 'NA'
73
  product = 'NA'
74
  elif feature.type == 'gene':
75
  continue
76
  else:
77
+ if feature.qualifiers.get('product') != ['']:
78
+ product = feature.qualifiers.get('product', ['NA'])[0]
79
+ else:
80
+ product = 'NA'
81
  region_type = feature.type
 
 
82
  specific_class = 'NA'
83
  translation = 'NA'
 
84
  yield record.id, {
85
  'DNA_id': record.id,
86
  'organism': organism,
 
90
  'product': product,
91
  'sequence': str(seq),
92
  'gc_content': gc_content,
93
+ 'translation_code': translation,
94
+ 'start_postion': start_position,
95
+ 'end_position': end_position
96
+ }
97
+