From fe8c73bc4ae8ea7b5e3db8bddc051b6072b3b206 Mon Sep 17 00:00:00 2001 From: Tamara Slosarek Date: Wed, 4 Sep 2024 16:53:48 +0200 Subject: [PATCH] refactor(#706): move inhibitor code to inhibitor file --- .../common/models/drug/drug_inhibitors.dart | 110 +++++++++++++++++ app/lib/common/models/userdata/userdata.dart | 112 +----------------- app/lib/common/utilities/pdf_utils.dart | 2 +- .../widgets/annotation_cards/guideline.dart | 2 +- 4 files changed, 113 insertions(+), 113 deletions(-) diff --git a/app/lib/common/models/drug/drug_inhibitors.dart b/app/lib/common/models/drug/drug_inhibitors.dart index b7f74a5c..2cb24691 100644 --- a/app/lib/common/models/drug/drug_inhibitors.dart +++ b/app/lib/common/models/drug/drug_inhibitors.dart @@ -5,6 +5,8 @@ // structure: gene symbol -> drug name -> overwriting lookupkey +import 'package:collection/collection.dart'; + import '../../module.dart'; // Inhibit phenotype for gene by overwriting with poor metabolizer @@ -82,3 +84,111 @@ List inhibitedGenes(Drug drug) { List inhibitorsFor(String gene) { return _drugInhibitorsPerGene[gene] ?? []; } + +String possiblyAdaptedPhenotype(GenotypeResult genotypeResult) { + final originalPhenotype = genotypeResult.phenotypeDisplayString; + if (!isInhibited(genotypeResult)) { + return originalPhenotype; + } + final overwrittenLookup = getOverwrittenLookup(genotypeResult.gene); + if (overwrittenLookup == null) { + return '$originalPhenotype$drugInteractionIndicator'; + } + return '$overwritePhenotype$drugInteractionIndicator'; +} + +bool isInhibited( + GenotypeResult genotypeResult, + { String? drug } +) { + final activeInhibitors = activeInhibitorsFor( + genotypeResult.gene, + drug: drug, + ); + final originalPhenotype = genotypeResult.phenotypeDisplayString; + final phenotypeCanBeInhibited = + originalPhenotype.toLowerCase() != overwritePhenotype.toLowerCase(); + return activeInhibitors.isNotEmpty && phenotypeCanBeInhibited; +} + +List activeInhibitorsFor(String gene, { String? drug }) { + return UserData.instance.activeDrugNames == null + ? [] + : UserData.instance.activeDrugNames!.filter( + (activeDrug) => + inhibitorsFor(gene).contains(activeDrug) && + activeDrug != drug + ).toList(); +} + +PhenotypeInformation phenotypeInformationFor( + GenotypeResult genotypeResult, + BuildContext context, + { + String? drug, + bool thirdPerson = false, + bool useLongPrefix = false, + } +) { + final userSalutation = thirdPerson + ? context.l10n.drugs_page_inhibitor_third_person_salutation + : context.l10n.drugs_page_inhibitor_direct_salutation; + final strongInhibitorTextPrefix = useLongPrefix + ? context.l10n.strong_inhibitor_long_prefix + : context.l10n.gene_page_phenotype.toLowerCase(); + final originalPhenotype = genotypeResult.phenotypeDisplayString; + final activeInhibitors = activeInhibitorsFor( + genotypeResult.gene, + drug: drug, + ); + if (!isInhibited(genotypeResult, drug: drug)) { + return PhenotypeInformation(phenotype: originalPhenotype); + } + final overwrittenLookup = getOverwrittenLookup( + genotypeResult.gene, + drug: drug, + ); + if (overwrittenLookup == null) { + return PhenotypeInformation( + phenotype: originalPhenotype, + adaptionText: context.l10n.drugs_page_moderate_inhibitors( + userSalutation, + enumerationWithAnd( + activeInhibitors, + context + ), + ), + ); + } + final originalPhenotypeText = context.l10n.drugs_page_original_phenotype( + thirdPerson + ? context.l10n.drugs_page_inhibitor_third_person_salutation_genitive + : context.l10n.drugs_page_inhibitor_direct_salutation_genitive, + originalPhenotype, + ); + return PhenotypeInformation( + phenotype: overwritePhenotype, + adaptionText: context.l10n.drugs_page_strong_inhibitors( + strongInhibitorTextPrefix, + userSalutation, + enumerationWithAnd(activeInhibitors, context), + ), + overwrittenPhenotypeText: originalPhenotypeText, + ); +} + +MapEntry? getOverwrittenLookup ( + String gene, + { String? drug } +) { + final inhibitors = strongDrugInhibitors[gene]; + if (inhibitors == null) return null; + final lookup = inhibitors.entries.firstWhereOrNull((entry) { + final isActiveInhibitor = + UserData.instance.activeDrugNames?.contains(entry.key) ?? false; + final wouldInhibitItself = drug == entry.key; + return isActiveInhibitor && !wouldInhibitItself; + }); + if (lookup == null) return null; + return lookup; +} \ No newline at end of file diff --git a/app/lib/common/models/userdata/userdata.dart b/app/lib/common/models/userdata/userdata.dart index da3b0c57..1f8ede96 100644 --- a/app/lib/common/models/userdata/userdata.dart +++ b/app/lib/common/models/userdata/userdata.dart @@ -1,6 +1,5 @@ import 'dart:convert'; -import 'package:collection/collection.dart'; import 'package:hive/hive.dart'; import 'package:http/http.dart'; @@ -43,84 +42,12 @@ class UserData { @HiveField(2) Map? genotypeResults; - static PhenotypeInformation phenotypeInformationFor( - GenotypeResult genotypeResult, - BuildContext context, - { - String? drug, - bool thirdPerson = false, - bool useLongPrefix = false, - } - ) { - final userSalutation = thirdPerson - ? context.l10n.drugs_page_inhibitor_third_person_salutation - : context.l10n.drugs_page_inhibitor_direct_salutation; - final strongInhibitorTextPrefix = useLongPrefix - ? context.l10n.strong_inhibitor_long_prefix - : context.l10n.gene_page_phenotype.toLowerCase(); - final originalPhenotype = genotypeResult.phenotypeDisplayString; - final activeInhibitors = UserData.activeInhibitorsFor( - genotypeResult.gene, - drug: drug, - ); - if (!isInhibited(genotypeResult, drug: drug)) { - return PhenotypeInformation(phenotype: originalPhenotype); - } - final overwrittenLookup = UserData.overwrittenLookup( - genotypeResult.gene, - drug: drug, - ); - if (overwrittenLookup == null) { - return PhenotypeInformation( - phenotype: originalPhenotype, - adaptionText: context.l10n.drugs_page_moderate_inhibitors( - userSalutation, - enumerationWithAnd( - activeInhibitors, - context - ), - ), - ); - } - final originalPhenotypeText = context.l10n.drugs_page_original_phenotype( - thirdPerson - ? context.l10n.drugs_page_inhibitor_third_person_salutation_genitive - : context.l10n.drugs_page_inhibitor_direct_salutation_genitive, - originalPhenotype, - ); - return PhenotypeInformation( - phenotype: overwritePhenotype, - adaptionText: context.l10n.drugs_page_strong_inhibitors( - strongInhibitorTextPrefix, - userSalutation, - enumerationWithAnd(activeInhibitors, context), - ), - overwrittenPhenotypeText: originalPhenotypeText, - ); - } - static String? variantFor(String genotypeKey) => UserData.instance.genotypeResults?[genotypeKey]?.variant; static String? allelesTestedFor(String genotypeKey) => UserData.instance.genotypeResults?[genotypeKey]?.allelesTested; - static MapEntry? overwrittenLookup( - String gene, - { String? drug } - ) { - final inhibitors = strongDrugInhibitors[gene]; - if (inhibitors == null) return null; - final lookup = inhibitors.entries.firstWhereOrNull((entry) { - final isActiveInhibitor = - UserData.instance.activeDrugNames?.contains(entry.key) ?? false; - final wouldInhibitItself = drug == entry.key; - return isActiveInhibitor && !wouldInhibitItself; - }); - if (lookup == null) return null; - return lookup; - } - static String? lookupFor( String genotypeKey, { @@ -129,22 +56,12 @@ class UserData { } ) { final overwrittenLookup = - UserData.overwrittenLookup(genotypeKey, drug: drug); + getOverwrittenLookup(genotypeKey, drug: drug); if (useOverwrite && overwrittenLookup != null) { return overwrittenLookup.value; } return UserData.instance.genotypeResults?[genotypeKey]?.lookupkey; } - - static List activeInhibitorsFor(String gene, { String? drug }) { - return UserData.instance.activeDrugNames == null - ? [] - : UserData.instance.activeDrugNames!.filter( - (activeDrug) => - inhibitorsFor(gene).contains(activeDrug) && - activeDrug != drug - ).toList(); - } } // Wrapper of UserData.instance.activeDrugNames that manages changes; used to @@ -229,30 +146,3 @@ List activeDrugsFromHTTPResponse(Response resp) { } return activeDrugs; } - -String possiblyAdaptedPhenotype(GenotypeResult genotypeResult) { - final originalPhenotype = genotypeResult.phenotypeDisplayString; - if (!isInhibited(genotypeResult)) { - return originalPhenotype; - } - final overwrittenLookup = UserData.overwrittenLookup(genotypeResult.gene); - if (overwrittenLookup == null) { - return '$originalPhenotype$drugInteractionIndicator'; - } - return '$overwritePhenotype$drugInteractionIndicator'; -} - -bool isInhibited( - GenotypeResult genotypeResult, - { String? drug } -) { - final activeInhibitors = UserData.activeInhibitorsFor( - genotypeResult.gene, - drug: drug, - ); - final originalPhenotype = genotypeResult.phenotypeDisplayString; - final phenotypeCanBeInhibited = - originalPhenotype.toLowerCase() != overwritePhenotype.toLowerCase(); - return activeInhibitors.isNotEmpty && phenotypeCanBeInhibited; -} - diff --git a/app/lib/common/utilities/pdf_utils.dart b/app/lib/common/utilities/pdf_utils.dart index ddd5cbde..08842d9e 100644 --- a/app/lib/common/utilities/pdf_utils.dart +++ b/app/lib/common/utilities/pdf_utils.dart @@ -133,7 +133,7 @@ List _buildDrugPart(Drug drug, BuildContext buildContext) { } String? _getPhenotypeInfo(String genotypeKey, Drug drug, BuildContext context) { - final phenotypeInformation = UserData.phenotypeInformationFor( + final phenotypeInformation = phenotypeInformationFor( UserData.instance.genotypeResults!.findOrMissing(genotypeKey, context), context, drug: drug.name, diff --git a/app/lib/drug/widgets/annotation_cards/guideline.dart b/app/lib/drug/widgets/annotation_cards/guideline.dart index a6238bf7..31be158d 100644 --- a/app/lib/drug/widgets/annotation_cards/guideline.dart +++ b/app/lib/drug/widgets/annotation_cards/guideline.dart @@ -189,7 +189,7 @@ class GuidelineAnnotationCard extends StatelessWidget { ); } else { final geneDescriptions = drug.guidelineGenotypes.map((genotypeKey) { - final phenotypeInformation = UserData.phenotypeInformationFor( + final phenotypeInformation = phenotypeInformationFor( UserData.instance.genotypeResults!.findOrMissing( genotypeKey, context,