From 0f28002e2089635d7e3cbbf6bd950ad11b6663a9 Mon Sep 17 00:00:00 2001 From: Dimas Ciputra Date: Wed, 24 Jul 2024 13:38:27 +0100 Subject: [PATCH] Fix taxa upload test (#4102) --- bims/scripts/taxa_upload.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/bims/scripts/taxa_upload.py b/bims/scripts/taxa_upload.py index 8b2743b5a..6aba11fe7 100644 --- a/bims/scripts/taxa_upload.py +++ b/bims/scripts/taxa_upload.py @@ -287,33 +287,56 @@ def get_taxonomy(self, taxon_name, scientific_name, rank): return parent def get_parent(self, row, current_rank=GENUS): + # Retrieve the taxon name based on the current rank from the row data taxon_name = DataCSVUpload.row_value(row, current_rank) if not taxon_name: return None + + # Handle concatenation for SPECIES rank if current_rank == SPECIES: genus_name = DataCSVUpload.row_value(row, GENUS) if genus_name not in taxon_name: - taxon_name = DataCSVUpload.row_value(row, GENUS) + ' ' + taxon_name + taxon_name = genus_name + ' ' + taxon_name + + # Handle concatenation for VARIETY rank if current_rank == VARIETY: - taxon_name = ( - DataCSVUpload.row_value(row, GENUS) + ' ' + - DataCSVUpload.row_value(row, SPECIES) + ' ' + - DataCSVUpload.row_value(row, VARIETY) - ) + genus_name = DataCSVUpload.row_value(row, GENUS) + species_name = DataCSVUpload.row_value(row, SPECIES) + if species_name not in taxon_name: + taxon_name = species_name + ' ' + taxon_name + if genus_name not in taxon_name: + taxon_name = genus_name + ' ' + taxon_name + + # Fetch the taxon using the constructed taxon name and current rank taxon = self.get_taxonomy( taxon_name, taxon_name, current_rank.upper() ) - while not taxon.gbif_key or not taxon.parent and taxon.parent.rank != 'KINGDOM': + + # If the taxon already has a parent, return it + if taxon.parent and taxon.parent.rank: + return taxon + + # Loop to find and assign the parent taxon until the conditions are met + while not taxon.gbif_key or (taxon.parent and taxon.parent.rank != 'KINGDOM'): + # Exit the loop if the current taxon rank is 'KINGDOM' + if taxon.rank == 'KINGDOM': + break + + # Determine the parent rank name parent_rank_name = parent_rank(current_rank) if not parent_rank_name: break + + # Recursively get the parent taxon parent = self.get_parent(row, parent_rank_name) if parent: taxon.parent = parent taxon.save() break + + # Update the current rank for the next iteration current_rank = parent_rank_name return taxon