From 6fda75fc4cd58d50e3db73cb9918d2193432bc84 Mon Sep 17 00:00:00 2001 From: tamslo Date: Fri, 6 Dec 2024 09:52:28 +0100 Subject: [PATCH] refactor(app): create drug inhibitor utils --- .../common/models/drug/drug_inhibitors.dart | 115 ------------------ app/lib/common/utilities/drug_inhibitors.dart | 110 +++++++++++++++++ app/lib/common/utilities/module.dart | 1 + 3 files changed, 111 insertions(+), 115 deletions(-) create mode 100644 app/lib/common/utilities/drug_inhibitors.dart diff --git a/app/lib/common/models/drug/drug_inhibitors.dart b/app/lib/common/models/drug/drug_inhibitors.dart index 11796f49..e2a833db 100644 --- a/app/lib/common/models/drug/drug_inhibitors.dart +++ b/app/lib/common/models/drug/drug_inhibitors.dart @@ -5,10 +5,6 @@ // structure: gene symbol -> drug name -> overwriting lookupkey -import 'package:collection/collection.dart'; - -import '../../module.dart'; - // Inhibit phenotype for gene by overwriting with poor metabolizer const Map> strongDrugInhibitors = { 'CYP2D6': { @@ -33,118 +29,7 @@ const Map> moderateDrugInhibitors = { }, }; -// Private helper functions - -final _drugInhibitorsPerGene = { - for (final gene in inhibitableGenes) gene: [ - ...?strongDrugInhibitors[gene]?.keys, - ...?moderateDrugInhibitors[gene]?.keys, - ] -}; - -List _inhibitorsFor(String gene) { - return _drugInhibitorsPerGene[gene] ?? []; -} - -bool _isInhibitorOfType( - String drugName, - Map> inhibitorDefinition -) { - final influencingDrugs = inhibitorDefinition.keys.flatMap( - (gene) => inhibitorDefinition[gene]!.keys); - return influencingDrugs.contains(drugName); -} - -// Public helper functions - -bool isModerateInhibitor(String drugName) { - return _isInhibitorOfType(drugName, moderateDrugInhibitors); -} - -List activeInhibitorsFor(String gene, { required String? drug }) { - return UserData.instance.activeDrugNames == null - ? [] - : UserData.instance.activeDrugNames!.filter( - (activeDrug) => - _inhibitorsFor(gene).contains(activeDrug) && - activeDrug != drug - ).toList(); -} - final inhibitableGenes = List.from({ ...strongDrugInhibitors.keys, ...moderateDrugInhibitors.keys, }); - -bool isInhibitor(String drugName) { - var drugIsInhibitor = false; - for (final gene in _drugInhibitorsPerGene.keys) { - final influencingDrugs = _drugInhibitorsPerGene[gene]; - // WARNING: this does not work for non-unique genes, such as HLA-B - final originalLookup = UserData.lookupFor( - gene, - drug: drugName, - useOverwrite: false, - ); - if (influencingDrugs!.contains(drugName) && originalLookup != '0.0') { - drugIsInhibitor = true; - break; - } - } - return drugIsInhibitor; -} - -bool isInhibited( - GenotypeResult genotypeResult, - { required String? drug } -) { - final activeInhibitors = activeInhibitorsFor( - genotypeResult.gene, - drug: drug, - ); - final originalPhenotype = genotypeResult.phenotype; - final phenotypeCanBeInhibited = - originalPhenotype?.toLowerCase() != overwritePhenotype.toLowerCase(); - return activeInhibitors.isNotEmpty && phenotypeCanBeInhibited; -} - -List inhibitedGenes(Drug drug) { - return _drugInhibitorsPerGene.keys.filter( - (gene) => _drugInhibitorsPerGene[gene]!.contains(drug.name) - ).toList(); -} - -MapEntry? getOverwrittenLookup ( - String gene, - { required 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; -} - -String possiblyAdaptedPhenotype( - BuildContext context, - GenotypeResult genotypeResult, - { required String? drug } -) { - final originalPhenotype = genotypeResult.phenotypeDisplayString(context); - if (!isInhibited(genotypeResult, drug: drug)) { - return originalPhenotype; - } - final overwrittenLookup = getOverwrittenLookup( - genotypeResult.gene, - drug: drug, - ); - if (overwrittenLookup == null) { - return '$originalPhenotype$drugInteractionIndicator'; - } - return '$overwritePhenotype$drugInteractionIndicator'; -} diff --git a/app/lib/common/utilities/drug_inhibitors.dart b/app/lib/common/utilities/drug_inhibitors.dart new file mode 100644 index 00000000..af73c8d6 --- /dev/null +++ b/app/lib/common/utilities/drug_inhibitors.dart @@ -0,0 +1,110 @@ +import 'package:collection/collection.dart'; + +import '../module.dart'; + +final _drugInhibitorsPerGene = { + for (final gene in inhibitableGenes) gene: [ + ...?strongDrugInhibitors[gene]?.keys, + ...?moderateDrugInhibitors[gene]?.keys, + ] +}; + +List _inhibitorsFor(String gene) { + return _drugInhibitorsPerGene[gene] ?? []; +} + +bool _isInhibitorOfType( + String drugName, + Map> inhibitorDefinition +) { + final influencingDrugs = inhibitorDefinition.keys.flatMap( + (gene) => inhibitorDefinition[gene]!.keys); + return influencingDrugs.contains(drugName); +} + +bool isModerateInhibitor(String drugName) { + return _isInhibitorOfType(drugName, moderateDrugInhibitors); +} + +List activeInhibitorsFor(String gene, { required String? drug }) { + return UserData.instance.activeDrugNames == null + ? [] + : UserData.instance.activeDrugNames!.filter( + (activeDrug) => + _inhibitorsFor(gene).contains(activeDrug) && + activeDrug != drug + ).toList(); +} + +bool isInhibitor(String drugName) { + var drugIsInhibitor = false; + for (final gene in _drugInhibitorsPerGene.keys) { + final influencingDrugs = _drugInhibitorsPerGene[gene]; + // WARNING: this does not work for non-unique genes, such as HLA-B + final originalLookup = UserData.lookupFor( + gene, + drug: drugName, + useOverwrite: false, + ); + if (influencingDrugs!.contains(drugName) && originalLookup != '0.0') { + drugIsInhibitor = true; + break; + } + } + return drugIsInhibitor; +} + +bool isInhibited( + GenotypeResult genotypeResult, + { required String? drug } +) { + final activeInhibitors = activeInhibitorsFor( + genotypeResult.gene, + drug: drug, + ); + final originalPhenotype = genotypeResult.phenotype; + final phenotypeCanBeInhibited = + originalPhenotype?.toLowerCase() != overwritePhenotype.toLowerCase(); + return activeInhibitors.isNotEmpty && phenotypeCanBeInhibited; +} + +List inhibitedGenes(Drug drug) { + return _drugInhibitorsPerGene.keys.filter( + (gene) => _drugInhibitorsPerGene[gene]!.contains(drug.name) + ).toList(); +} + +MapEntry? getOverwrittenLookup ( + String gene, + { required 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; +} + +String possiblyAdaptedPhenotype( + BuildContext context, + GenotypeResult genotypeResult, + { required String? drug } +) { + final originalPhenotype = genotypeResult.phenotypeDisplayString(context); + if (!isInhibited(genotypeResult, drug: drug)) { + return originalPhenotype; + } + final overwrittenLookup = getOverwrittenLookup( + genotypeResult.gene, + drug: drug, + ); + if (overwrittenLookup == null) { + return '$originalPhenotype$drugInteractionIndicator'; + } + return '$overwritePhenotype$drugInteractionIndicator'; +} diff --git a/app/lib/common/utilities/module.dart b/app/lib/common/utilities/module.dart index a076f0a2..2055a3ca 100644 --- a/app/lib/common/utilities/module.dart +++ b/app/lib/common/utilities/module.dart @@ -1,5 +1,6 @@ export 'color_utils.dart'; export 'contact.dart'; +export 'drug_inhibitors.dart'; export 'drug_utils.dart'; export 'genome_data.dart'; export 'material_colors.dart';