diff options
author | LLTommy | 2020-06-12 22:44:28 +0200 |
---|---|---|
committer | GitHub | 2020-06-12 22:44:28 +0200 |
commit | 9931e5d6ddf02c1f0f4a27e8321a37078e1c060d (patch) | |
tree | d342f22f5091e9a28f12ba0b7bd772bed97d770a /scripts | |
parent | a8674b7e5c726994a102ea8c47d0e82c12e23d51 (diff) | |
parent | f3d27f234b008a1faaeea428f5bd935ea3a438d2 (diff) | |
download | bh20-seq-resource-9931e5d6ddf02c1f0f4a27e8321a37078e1c060d.tar.gz bh20-seq-resource-9931e5d6ddf02c1f0f4a27e8321a37078e1c060d.tar.lz bh20-seq-resource-9931e5d6ddf02c1f0f4a27e8321a37078e1c060d.zip |
Merge pull request #82 from AndreaGuarracino/patch-17
the script is more robust and verbose
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/dict_ontology_standardization/ncbi_host_species.csv | 6 | ||||
-rwxr-xr-x | scripts/from_genbank_to_fasta_and_yaml.py | 435 |
2 files changed, 227 insertions, 214 deletions
diff --git a/scripts/dict_ontology_standardization/ncbi_host_species.csv b/scripts/dict_ontology_standardization/ncbi_host_species.csv new file mode 100644 index 0000000..0d2120c --- /dev/null +++ b/scripts/dict_ontology_standardization/ncbi_host_species.csv @@ -0,0 +1,6 @@ +Homo sapiens,http://purl.obolibrary.org/obo/NCBITaxon_9606 +Mustela lutreola,http://purl.obolibrary.org/obo/NCBITaxon_9666 +Manis javanica,http://purl.obolibrary.org/obo/NCBITaxon_9974 +Felis catus,http://purl.obolibrary.org/obo/NCBITaxon_9685 +Panthera tigris jacksoni,http://purl.obolibrary.org/obo/NCBITaxon_419130 +Canis lupus familiaris,http://purl.obolibrary.org/obo/NCBITaxon_9615 diff --git a/scripts/from_genbank_to_fasta_and_yaml.py b/scripts/from_genbank_to_fasta_and_yaml.py index 65adb00..87e99d4 100755 --- a/scripts/from_genbank_to_fasta_and_yaml.py +++ b/scripts/from_genbank_to_fasta_and_yaml.py @@ -123,18 +123,12 @@ for path_dict_xxx_csv in [os.path.join(dir_dict_ontology_standardization, name_x else: term, uri = line.strip('\n').split(',') + if term in term_to_uri_dict: + print('Warning: in the dictionaries there are more entries for the same term ({}).'.format(term)) + continue + term_to_uri_dict[term] = uri -species_to_taxid_dict = { - 'Homo sapiens': 'http://purl.obolibrary.org/obo/NCBITaxon_9606', - 'Mustela lutreola': 'http://purl.obolibrary.org/obo/NCBITaxon_9666', - 'Manis javanica': 'http://purl.obolibrary.org/obo/NCBITaxon_9974', - 'Felis catus': 'http://purl.obolibrary.org/obo/NCBITaxon_9685', - 'Panthera tigris jacksoni': 'http://purl.obolibrary.org/obo/NCBITaxon_419130', - 'Canis lupus familiaris': 'http://purl.obolibrary.org/obo/NCBITaxon_9615' -} - - if not os.path.exists(dir_fasta_and_yaml): os.makedirs(dir_fasta_and_yaml) @@ -142,6 +136,7 @@ min_len_to_count = 27500 num_seq_with_len_ge_X_bp = 0 missing_value_list = [] +accession_with_errors_list = [] for path_metadata_xxx_xml in [os.path.join(dir_metadata, name_metadata_xxx_xml) for name_metadata_xxx_xml in os.listdir(dir_metadata) if name_metadata_xxx_xml.endswith('.xml')]: tree = ET.parse(path_metadata_xxx_xml) @@ -155,232 +150,244 @@ for path_metadata_xxx_xml in [os.path.join(dir_metadata, name_metadata_xxx_xml) print(accession_version, ' - sequence not found') continue - #print(path_metadata_xxx_xml, accession_version) - - # A general default-empty yaml could be read from the definitive one - info_for_yaml_dict = { - 'id': 'placeholder', - 'host': {}, - 'sample': {}, - 'virus': {}, - 'technology': {}, - 'submitter': {} - } - - - info_for_yaml_dict['sample']['sample_id'] = accession_version - info_for_yaml_dict['sample']['source_database_accession'] = ["http://identifiers.org/insdc/"+accession_version+"#sequence"] #accession is turned into resolvable URL/URI now - - - # 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')] - - GBReference = GBSeq_references.find('GBReference') - if GBReference is not None: - GBReference_journal = GBReference.find('GBReference_journal') - - if GBReference_journal is not None and GBReference_journal.text != 'Unpublished': - if 'Submitted' in GBReference_journal.text: - info_for_yaml_dict['submitter']['submitter_name'] = ["{}".format(GBReference_journal.text.split(') ')[1].split(',')[0].strip())] - info_for_yaml_dict['submitter']['submitter_address'] = ','.join(GBReference_journal.text.split(') ')[1].split(',')[1:]).strip() - else: - info_for_yaml_dict['submitter']['additional_submitter_information'] = GBReference_journal.text - - - GBSeq_comment = GBSeq.find('GBSeq_comment') - if GBSeq_comment is not None and 'Assembly-Data' in GBSeq_comment.text: - prefix_split_string = '##Genome-Assembly' if GBSeq_comment.text.startswith('##Genome-') else '##Assembly' - - GBSeq_comment_text = GBSeq_comment.text.split( - '{}-Data-START## ; '.format(prefix_split_string) - )[1].split(' ; {}-Data-END##'.format(prefix_split_string))[0] - - for info_to_check, field_in_yaml in zip( - ['Assembly Method', 'Coverage', 'Sequencing Technology'], - ['sequence_assembly_method', 'sequencing_coverage', 'sample_sequencing_technology'] - ): - if info_to_check in GBSeq_comment_text: - tech_info_to_parse = GBSeq_comment_text.split('{} :: '.format(info_to_check))[1].split(' ;')[0] - - if field_in_yaml == 'sequencing_coverage': - # A regular expression would be better! - try: - info_for_yaml_dict['technology'][field_in_yaml] = [ - float(tech_info_to_parse.strip('(average)').strip("reads/nt").strip('(average for 6 sequences)').replace(',', '.').strip(' xX>')) - ] - except ValueError: - print(accession_version, "Couldn't make sense of Coverage '%s'" % tech_info_to_parse) - pass - elif field_in_yaml == 'sample_sequencing_technology': - new_seq_tec_list = [] - for seq_tec in tech_info_to_parse.split(';'): - seq_tec = seq_tec.strip() - if seq_tec in term_to_uri_dict: - seq_tec = term_to_uri_dict[seq_tec] - else: - missing_value_list.append('\t'.join([accession_version, 'sample_sequencing_technology', seq_tec])) + try: + #print(path_metadata_xxx_xml, accession_version) - new_seq_tec_list.append(seq_tec) + # A general default-empty yaml could be read from the definitive one + info_for_yaml_dict = { + 'id': 'placeholder', + 'host': {}, + 'sample': {}, + 'virus': {}, + 'technology': {}, + 'submitter': {} + } - 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 + info_for_yaml_dict['sample']['sample_id'] = accession_version + info_for_yaml_dict['sample']['source_database_accession'] = ["http://identifiers.org/insdc/"+accession_version+"#sequence"] #accession is turned into resolvable URL/URI now - for GBFeature in GBSeq.iter('GBFeature'): - if GBFeature.find('GBFeature_key').text != 'source': - continue - for GBQualifier in GBFeature.iter('GBQualifier'): - GBQualifier_value = GBQualifier.find('GBQualifier_value') - if GBQualifier_value is None: - continue - GBQualifier_value_text = GBQualifier_value.text - - 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 species_to_taxid_dict: - info_for_yaml_dict['host']['host_species'] = species_to_taxid_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'] = species_to_taxid_dict['Canis lupus familiaris'] - 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]] - 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 - else: - info_for_yaml_dict['sample']['collector_name'] = GBQualifier_value_text - elif GBQualifier_name_text == 'isolation_source': - if GBQualifier_value_text.upper() in term_to_uri_dict: - GBQualifier_value_text = GBQualifier_value_text.upper() # For example, in case of 'usa: wa' - - # Little cleaning - GBQualifier_value_text = GBQualifier_value_text.strip("/'") - - if GBQualifier_value_text in term_to_uri_dict: - info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict[GBQualifier_value_text]] - else: - if GBQualifier_value_text.lower() in ['np/op', 'np/op swab', 'np/np swab', 'nasopharyngeal and oropharyngeal swab', 'nasopharyngeal/oropharyngeal swab']: - info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict['nasopharyngeal swab'], term_to_uri_dict['oropharyngeal swab']] - elif GBQualifier_value_text in ['nasopharyngeal swab/throat swab', 'nasopharyngeal/throat swab', 'nasopharyngeal swab and throat swab', 'nasal swab and throat swab']: - info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict['nasopharyngeal swab'], term_to_uri_dict['throat swab']] - elif GBQualifier_value_text in ['nasopharyngeal aspirate/throat swab']: - info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict['nasopharyngeal aspirate'], term_to_uri_dict['throat swab']] + # 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')] + + GBReference = GBSeq_references.find('GBReference') + if GBReference is not None: + GBReference_journal = GBReference.find('GBReference_journal') + + if GBReference_journal is not None and GBReference_journal.text != 'Unpublished': + if 'Submitted' in GBReference_journal.text: + info_for_yaml_dict['submitter']['submitter_name'] = ["{}".format(GBReference_journal.text.split(') ')[1].split(',')[0].strip())] + info_for_yaml_dict['submitter']['submitter_address'] = ','.join(GBReference_journal.text.split(') ')[1].split(',')[1:]).strip() else: - missing_value_list.append('\t'.join([accession_version, 'specimen_source', GBQualifier_value_text])) - elif GBQualifier_name_text == 'collection_date': - # TO_DO: which format we will use? - date_to_write = GBQualifier_value_text - - if len(GBQualifier_value_text.split('-')) == 1: - if int(GBQualifier_value_text) < 2020: - date_to_write = "{}-12-15".format(GBQualifier_value_text) + info_for_yaml_dict['submitter']['additional_submitter_information'] = GBReference_journal.text + + + GBSeq_comment = GBSeq.find('GBSeq_comment') + if GBSeq_comment is not None and 'Assembly-Data' in GBSeq_comment.text: + prefix_split_string = '##Genome-Assembly' if GBSeq_comment.text.startswith('##Genome-') else '##Assembly' + + GBSeq_comment_text = GBSeq_comment.text.split( + '{}-Data-START## ; '.format(prefix_split_string) + )[1].split(' ; {}-Data-END##'.format(prefix_split_string))[0] + + for info_to_check, field_in_yaml in zip( + ['Assembly Method', 'Coverage', 'Sequencing Technology'], + ['sequence_assembly_method', 'sequencing_coverage', 'sample_sequencing_technology'] + ): + if info_to_check in GBSeq_comment_text: + tech_info_to_parse = GBSeq_comment_text.split('{} :: '.format(info_to_check))[1].split(' ;')[0] + + if field_in_yaml == 'sequencing_coverage': + # A regular expression would be better! + try: + info_for_yaml_dict['technology'][field_in_yaml] = [ + float(tech_info_to_parse.strip('(average)').strip("reads/nt").strip('(average for 6 sequences)').replace(',', '.').strip(' xX>')) + ] + except ValueError: + print(accession_version, "Couldn't make sense of Coverage '%s'" % tech_info_to_parse) + pass + elif field_in_yaml == 'sample_sequencing_technology': + new_seq_tec_list = [] + for seq_tec in tech_info_to_parse.split(';'): + seq_tec = seq_tec.strip() + if seq_tec in term_to_uri_dict: + seq_tec = term_to_uri_dict[seq_tec] + else: + missing_value_list.append('\t'.join([accession_version, 'sample_sequencing_technology', seq_tec])) + + new_seq_tec_list.append(seq_tec) + + info_for_yaml_dict['technology']['sample_sequencing_technology'] = [x for x in new_seq_tec_list] else: - date_to_write = "{}-01-15".format(GBQualifier_value_text) + info_for_yaml_dict['technology'][field_in_yaml] = tech_info_to_parse - if 'additional_collection_information' in info_for_yaml_dict['sample']: - info_for_yaml_dict['sample']['additional_collection_information'] += "; The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) + + for GBFeature in GBSeq.iter('GBFeature'): + if GBFeature.find('GBFeature_key').text != 'source': + continue + + for GBQualifier in GBFeature.iter('GBQualifier'): + GBQualifier_value = GBQualifier.find('GBQualifier_value') + if GBQualifier_value is None: + continue + GBQualifier_value_text = GBQualifier_value.text + + 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'] else: - info_for_yaml_dict['sample']['additional_collection_information'] = "The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) - elif len(GBQualifier_value_text.split('-')) == 2: - date_to_write += '-15' - - if 'additional_collection_information' in info_for_yaml_dict['sample']: - info_for_yaml_dict['sample']['additional_collection_information'] += "; The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) + 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 else: - info_for_yaml_dict['sample']['additional_collection_information'] = "The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) - elif len(GBQualifier_value_text.split('-')) == 3: - GBQualifier_value_text_list = GBQualifier_value_text.split('-') + info_for_yaml_dict['sample']['collector_name'] = GBQualifier_value_text + elif GBQualifier_name_text == 'isolation_source': + if GBQualifier_value_text.upper() in term_to_uri_dict: + GBQualifier_value_text = GBQualifier_value_text.upper() # For example, in case of 'usa: wa' - if GBQualifier_value_text_list[1].isalpha(): - date_to_write = parse(GBQualifier_value_text).strftime('%Y-%m-%d') + # Little cleaning + GBQualifier_value_text = GBQualifier_value_text.strip("/'") - info_for_yaml_dict['sample']['collection_date'] = date_to_write - elif GBQualifier_name_text in ['lat_lon', 'country']: - if GBQualifier_value_text == 'Hong Kong': - GBQualifier_value_text = 'China: Hong Kong' + if GBQualifier_value_text in term_to_uri_dict: + info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict[GBQualifier_value_text]] + else: + if GBQualifier_value_text.lower() in ['np/op', 'np/op swab', 'np/np swab', 'nasopharyngeal and oropharyngeal swab', 'nasopharyngeal/oropharyngeal swab']: + info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict['nasopharyngeal swab'], term_to_uri_dict['oropharyngeal swab']] + elif GBQualifier_value_text in ['nasopharyngeal swab/throat swab', 'nasopharyngeal/throat swab', 'nasopharyngeal swab and throat swab', 'nasal swab and throat swab']: + info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict['nasopharyngeal swab'], term_to_uri_dict['throat swab']] + elif GBQualifier_value_text in ['nasopharyngeal aspirate/throat swab']: + info_for_yaml_dict['sample']['specimen_source'] = [term_to_uri_dict['nasopharyngeal aspirate'], term_to_uri_dict['throat swab']] + else: + missing_value_list.append('\t'.join([accession_version, 'specimen_source', GBQualifier_value_text])) + elif GBQualifier_name_text == 'collection_date': + # TO_DO: which format we will use? + date_to_write = GBQualifier_value_text + + if len(GBQualifier_value_text.split('-')) == 1: + if int(GBQualifier_value_text) < 2020: + date_to_write = "{}-12-15".format(GBQualifier_value_text) + else: + date_to_write = "{}-01-15".format(GBQualifier_value_text) - if GBQualifier_value_text in term_to_uri_dict: - info_for_yaml_dict['sample']['collection_location'] = term_to_uri_dict[GBQualifier_value_text] - else: - missing_value_list.append('\t'.join([accession_version, GBQualifier_name_text, GBQualifier_value_text])) - elif GBQualifier_name_text == 'note': - if 'additional_collection_information' in info_for_yaml_dict['sample']: - info_for_yaml_dict['sample']['additional_collection_information'] += '; ' + GBQualifier_value_text - else: - info_for_yaml_dict['sample']['additional_collection_information'] = GBQualifier_value_text - elif GBQualifier_name_text == 'isolate': - info_for_yaml_dict['virus']['virus_strain'] = GBQualifier_value_text - elif GBQualifier_name_text == 'db_xref': - info_for_yaml_dict['virus']['virus_species'] = "http://purl.obolibrary.org/obo/NCBITaxon_"+GBQualifier_value_text.split('taxon:')[1] + if 'additional_collection_information' in info_for_yaml_dict['sample']: + info_for_yaml_dict['sample']['additional_collection_information'] += "; The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) + else: + info_for_yaml_dict['sample']['additional_collection_information'] = "The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) + elif len(GBQualifier_value_text.split('-')) == 2: + date_to_write += '-15' + if 'additional_collection_information' in info_for_yaml_dict['sample']: + info_for_yaml_dict['sample']['additional_collection_information'] += "; The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) + else: + info_for_yaml_dict['sample']['additional_collection_information'] = "The 'collection_date' is estimated (the original date was: {})".format(GBQualifier_value_text) + elif len(GBQualifier_value_text.split('-')) == 3: + GBQualifier_value_text_list = GBQualifier_value_text.split('-') - # Remove technology key if empty! - if (info_for_yaml_dict['technology']=={}): - del info_for_yaml_dict['technology'] + if GBQualifier_value_text_list[1].isalpha(): + date_to_write = parse(GBQualifier_value_text).strftime('%Y-%m-%d') - 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())) + info_for_yaml_dict['sample']['collection_date'] = date_to_write + elif GBQualifier_name_text in ['lat_lon', 'country']: + if GBQualifier_value_text == 'Hong Kong': + GBQualifier_value_text = 'China: Hong Kong' + + if GBQualifier_value_text in term_to_uri_dict: + info_for_yaml_dict['sample']['collection_location'] = term_to_uri_dict[GBQualifier_value_text] + else: + missing_value_list.append('\t'.join([accession_version, GBQualifier_name_text, GBQualifier_value_text])) + elif GBQualifier_name_text == 'note': + if 'additional_collection_information' in info_for_yaml_dict['sample']: + info_for_yaml_dict['sample']['additional_collection_information'] += '; ' + GBQualifier_value_text + else: + info_for_yaml_dict['sample']['additional_collection_information'] = GBQualifier_value_text + elif GBQualifier_name_text == 'isolate': + info_for_yaml_dict['virus']['virus_strain'] = GBQualifier_value_text + elif GBQualifier_name_text == 'db_xref': + info_for_yaml_dict['virus']['virus_species'] = "http://purl.obolibrary.org/obo/NCBITaxon_"+GBQualifier_value_text.split('taxon:')[1] - with open(os.path.join(dir_fasta_and_yaml, '{}.yaml'.format(accession_version)), 'w') as fw: - json.dump(info_for_yaml_dict, fw, indent=2) + # Remove technology key if empty! + if (info_for_yaml_dict['technology']=={}): + del info_for_yaml_dict['technology'] - if(len(GBSeq_sequence.text) >= min_len_to_count): - num_seq_with_len_ge_X_bp += 1 + 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())) + with open(os.path.join(dir_fasta_and_yaml, '{}.yaml'.format(accession_version)), 'w') as fw: + json.dump(info_for_yaml_dict, fw, indent=2) + + + if(len(GBSeq_sequence.text) >= min_len_to_count): + num_seq_with_len_ge_X_bp += 1 + except: + print("Unexpected error for the ID {}: {}".format(accession_version, sys.exc_info()[0])) + accession_with_errors_list.append(accession_version) + continue if len(missing_value_list) > 0: - with open('missing_terms.tsv', 'w') as fw: + path_missing_terms_tsv = 'missing_terms.tsv' + print('Written missing terms in {}'.format(path_missing_terms_tsv)) + with open(path_missing_terms_tsv, 'w') as fw: fw.write('\n'.join(missing_value_list)) +if len(accession_with_errors_list) > 0: + path_accession_with_errors_tsv = 'accession_with_errors.tsv' + print('Written the accession with errors in {}'.format(path_accession_with_errors_tsv)) + with open(path_accession_with_errors_tsv, 'w') as fw: + fw.write('\n'.join(accession_with_errors_list)) + print('Num. new sequences with length >= {} bp: {}'.format(min_len_to_count, num_seq_with_len_ge_X_bp)) |