aboutsummaryrefslogtreecommitdiff
path: root/scripts/download_genbank_data
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/download_genbank_data')
-rwxr-xr-xscripts/download_genbank_data/from_genbank_to_fasta_and_yaml.py117
1 files changed, 62 insertions, 55 deletions
diff --git a/scripts/download_genbank_data/from_genbank_to_fasta_and_yaml.py b/scripts/download_genbank_data/from_genbank_to_fasta_and_yaml.py
index 4bb4964..cb94787 100755
--- a/scripts/download_genbank_data/from_genbank_to_fasta_and_yaml.py
+++ b/scripts/download_genbank_data/from_genbank_to_fasta_and_yaml.py
@@ -173,7 +173,9 @@ for path_metadata_xxx_xml in [os.path.join(dir_metadata, name_metadata_xxx_xml)
# submitter info
GBSeq_references = GBSeq.find('GBSeq_references')
if GBSeq_references is not None:
- info_for_yaml_dict['submitter']['authors'] = ["{}".format(x.text) for x in GBSeq_references.iter('GBAuthor')]
+ author_list = ["{}".format(x.text) for x in GBSeq_references.iter('GBAuthor')]
+ if len(author_list) > 0:
+ info_for_yaml_dict['submitter']['authors'] = author_list
GBReference = GBSeq_references.find('GBReference')
if GBReference is not None:
@@ -222,7 +224,8 @@ for path_metadata_xxx_xml in [os.path.join(dir_metadata, name_metadata_xxx_xml)
new_seq_tec_list.append(seq_tec)
- info_for_yaml_dict['technology']['sample_sequencing_technology'] = [x for x in new_seq_tec_list]
+ if len(new_seq_tec_list) > 0:
+ info_for_yaml_dict['technology']['sample_sequencing_technology'] = [x for x in new_seq_tec_list]
else:
info_for_yaml_dict['technology'][field_in_yaml] = tech_info_to_parse
@@ -240,58 +243,62 @@ for path_metadata_xxx_xml in [os.path.join(dir_metadata, name_metadata_xxx_xml)
GBQualifier_name_text = GBQualifier.find('GBQualifier_name').text
if GBQualifier_name_text == 'host':
- GBQualifier_value_text_list = GBQualifier_value_text.split('; ')
-
- if GBQualifier_value_text_list[0] in term_to_uri_dict:
- info_for_yaml_dict['host']['host_species'] = term_to_uri_dict[GBQualifier_value_text_list[0]]
- elif GBQualifier_value_text_list[0] and ('MT215193' in accession_version or 'MT270814' in accession_version):
- # Information checked manually from NCBI Virus
- info_for_yaml_dict['host']['host_species'] = term_to_uri_dict['Canis lupus familiaris']
+ if GBQualifier_value_text in term_to_uri_dict:
+ # Cases like 'Felis catus; Domestic Shorthair'
+ info_for_yaml_dict['host']['host_species'] = term_to_uri_dict[GBQualifier_value_text]
else:
- missing_value_list.append('\t'.join([accession_version, 'host_species', GBQualifier_value_text_list[0]]))
-
- # Possible cases:
- # - Homo sapiens --> ['Homo sapiens']
- # - Homo sapiens; female --> ['Homo sapiens', 'female']
- # - Homo sapiens; female 63 --> ['Homo sapiens', 'female 63']
- # - Homo sapiens; female; age 40 --> ['Homo sapiens', 'female', 'age 40']
- # - Homo sapiens; gender: F; age: 61 --> ['Homo sapiens', 'gender: F', 'age: 61']
- # - Homo sapiens; gender: M; age: 68 --> ['Homo sapiens', 'gender: M', 'age: 68']
- # - Homo sapiens; hospitalized patient --> ['Homo sapiens', 'hospitalized patient']
- # - Homo sapiens; male --> ['Homo sapiens', 'male']
- # - Homo sapiens; male; 63 --> ['Homo sapiens', 'male', '63']
- # - Homo sapiens; male; age 29 --> ['Homo sapiens', 'male', 'age 29']
- # - Homo sapiens; symptomatic --> ['Homo sapiens', 'symptomatic']
- if len(GBQualifier_value_text_list) > 1:
- host_sex = ''
- if 'female' in GBQualifier_value_text_list[1]:
- host_sex = 'female'
- elif 'male' in GBQualifier_value_text_list[1]:
- host_sex = 'male'
- elif 'gender' in GBQualifier_value_text_list[1]:
- host_sex_one_lecter = GBQualifier_value_text_list[1].split(':')[-1].strip()
- if host_sex_one_lecter in ['F', 'M']:
- host_sex = 'female' if host_sex_one_lecter == 'F' else 'male'
-
- if host_sex in ['male', 'female']:
- info_for_yaml_dict['host']['host_sex'] = "http://purl.obolibrary.org/obo/PATO_0000384" if host_sex == 'male' else "http://purl.obolibrary.org/obo/PATO_0000383"
- elif GBQualifier_value_text_list[1] in term_to_uri_dict:
- info_for_yaml_dict['host']['host_health_status'] = term_to_uri_dict[GBQualifier_value_text_list[1]]
+ GBQualifier_value_text_list = GBQualifier_value_text.split('; ')
+
+ if GBQualifier_value_text_list[0] in term_to_uri_dict:
+ info_for_yaml_dict['host']['host_species'] = term_to_uri_dict[GBQualifier_value_text_list[0]]
+ elif GBQualifier_value_text_list[0] and ('MT215193' in accession_version or 'MT270814' in accession_version):
+ # Information checked manually from NCBI Virus
+ info_for_yaml_dict['host']['host_species'] = term_to_uri_dict['Canis lupus familiaris']
else:
- missing_value_list.append('\t'.join([accession_version, 'host_sex or host_health_status', GBQualifier_value_text_list[1]]))
-
- # Host age
- host_age = -1
- if len(GBQualifier_value_text_list[1].split(' ')) > 1 and is_integer(GBQualifier_value_text_list[1].split(' ')[-1]):
- host_age = int(GBQualifier_value_text_list[1].split(' ')[-1])
- elif len(GBQualifier_value_text_list) > 2 and is_integer(GBQualifier_value_text_list[2].split(' ')[-1]):
- host_age = int(GBQualifier_value_text_list[2].split(' ')[-1])
-
- if host_age > -1:
- info_for_yaml_dict['host']['host_age'] = host_age
- info_for_yaml_dict['host']['host_age_unit'] = 'http://purl.obolibrary.org/obo/UO_0000036'
- elif len(GBQualifier_value_text_list) > 2:
- missing_value_list.append('\t'.join([accession_version, 'host_age', GBQualifier_value_text_list[2]]))
+ missing_value_list.append('\t'.join([accession_version, 'host_species', GBQualifier_value_text_list[0]]))
+
+ # Possible cases:
+ # - Homo sapiens --> ['Homo sapiens']
+ # - Homo sapiens; female --> ['Homo sapiens', 'female']
+ # - Homo sapiens; female 63 --> ['Homo sapiens', 'female 63']
+ # - Homo sapiens; female; age 40 --> ['Homo sapiens', 'female', 'age 40']
+ # - Homo sapiens; gender: F; age: 61 --> ['Homo sapiens', 'gender: F', 'age: 61']
+ # - Homo sapiens; gender: M; age: 68 --> ['Homo sapiens', 'gender: M', 'age: 68']
+ # - Homo sapiens; hospitalized patient --> ['Homo sapiens', 'hospitalized patient']
+ # - Homo sapiens; male --> ['Homo sapiens', 'male']
+ # - Homo sapiens; male; 63 --> ['Homo sapiens', 'male', '63']
+ # - Homo sapiens; male; age 29 --> ['Homo sapiens', 'male', 'age 29']
+ # - Homo sapiens; symptomatic --> ['Homo sapiens', 'symptomatic']
+ if len(GBQualifier_value_text_list) > 1:
+ host_sex = ''
+ if 'female' in GBQualifier_value_text_list[1]:
+ host_sex = 'female'
+ elif 'male' in GBQualifier_value_text_list[1]:
+ host_sex = 'male'
+ elif 'gender' in GBQualifier_value_text_list[1]:
+ host_sex_one_lecter = GBQualifier_value_text_list[1].split(':')[-1].strip()
+ if host_sex_one_lecter in ['F', 'M']:
+ host_sex = 'female' if host_sex_one_lecter == 'F' else 'male'
+
+ if host_sex in ['male', 'female']:
+ info_for_yaml_dict['host']['host_sex'] = "http://purl.obolibrary.org/obo/PATO_0000384" if host_sex == 'male' else "http://purl.obolibrary.org/obo/PATO_0000383"
+ elif GBQualifier_value_text_list[1] in term_to_uri_dict:
+ info_for_yaml_dict['host']['host_health_status'] = term_to_uri_dict[GBQualifier_value_text_list[1]]
+ else:
+ missing_value_list.append('\t'.join([accession_version, 'host_sex or host_health_status', GBQualifier_value_text_list[1]]))
+
+ # Host age
+ host_age = -1
+ if len(GBQualifier_value_text_list[1].split(' ')) > 1 and is_integer(GBQualifier_value_text_list[1].split(' ')[-1]):
+ host_age = int(GBQualifier_value_text_list[1].split(' ')[-1])
+ elif len(GBQualifier_value_text_list) > 2 and is_integer(GBQualifier_value_text_list[2].split(' ')[-1]):
+ host_age = int(GBQualifier_value_text_list[2].split(' ')[-1])
+
+ if host_age > -1:
+ info_for_yaml_dict['host']['host_age'] = host_age
+ info_for_yaml_dict['host']['host_age_unit'] = 'http://purl.obolibrary.org/obo/UO_0000036'
+ elif len(GBQualifier_value_text_list) > 2:
+ missing_value_list.append('\t'.join([accession_version, 'host_age', GBQualifier_value_text_list[2]]))
elif GBQualifier_name_text == 'collected_by':
if any([x in GBQualifier_value_text.lower() for x in ['institute', 'hospital', 'city', 'center']]):
info_for_yaml_dict['sample']['collecting_institution'] = GBQualifier_value_text
@@ -364,9 +371,9 @@ for path_metadata_xxx_xml in [os.path.join(dir_metadata, name_metadata_xxx_xml)
info_for_yaml_dict['virus']['virus_species'] = "http://purl.obolibrary.org/obo/NCBITaxon_"+GBQualifier_value_text.split('taxon:')[1]
- # Remove technology key if empty!
- if (info_for_yaml_dict['technology']=={}):
- del info_for_yaml_dict['technology']
+ if 'sample_sequencing_technology' not in info_for_yaml_dict['technology']:
+ print(accession_version, ' - technology not found')
+ continue
with open(os.path.join(dir_fasta_and_yaml, '{}.fasta'.format(accession_version)), 'w') as fw:
fw.write('>{}\n{}'.format(accession_version, GBSeq_sequence.text.upper()))