Skip to content

Commit

Permalink
refactor(app): create drug inhibitor utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Dec 6, 2024
1 parent c89f418 commit 6fda75f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 115 deletions.
115 changes: 0 additions & 115 deletions app/lib/common/models/drug/drug_inhibitors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, String>> strongDrugInhibitors = {
'CYP2D6': {
Expand All @@ -33,118 +29,7 @@ const Map<String, Map<String, double>> moderateDrugInhibitors = {
},
};

// Private helper functions

final _drugInhibitorsPerGene = {
for (final gene in inhibitableGenes) gene: [
...?strongDrugInhibitors[gene]?.keys,
...?moderateDrugInhibitors[gene]?.keys,
]
};

List<String> _inhibitorsFor(String gene) {
return _drugInhibitorsPerGene[gene] ?? [];
}

bool _isInhibitorOfType(
String drugName,
Map<String, Map<String, dynamic>> 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<String> activeInhibitorsFor(String gene, { required String? drug }) {
return UserData.instance.activeDrugNames == null
? <String>[]
: UserData.instance.activeDrugNames!.filter(
(activeDrug) =>
_inhibitorsFor(gene).contains(activeDrug) &&
activeDrug != drug
).toList();
}

final inhibitableGenes = List<String>.from(<String>{
...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<String> inhibitedGenes(Drug drug) {
return _drugInhibitorsPerGene.keys.filter(
(gene) => _drugInhibitorsPerGene[gene]!.contains(drug.name)
).toList();
}

MapEntry<String, String>? 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';
}
110 changes: 110 additions & 0 deletions app/lib/common/utilities/drug_inhibitors.dart
Original file line number Diff line number Diff line change
@@ -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<String> _inhibitorsFor(String gene) {
return _drugInhibitorsPerGene[gene] ?? [];
}

bool _isInhibitorOfType(
String drugName,
Map<String, Map<String, dynamic>> inhibitorDefinition
) {
final influencingDrugs = inhibitorDefinition.keys.flatMap(
(gene) => inhibitorDefinition[gene]!.keys);
return influencingDrugs.contains(drugName);
}

bool isModerateInhibitor(String drugName) {
return _isInhibitorOfType(drugName, moderateDrugInhibitors);
}

List<String> activeInhibitorsFor(String gene, { required String? drug }) {
return UserData.instance.activeDrugNames == null
? <String>[]
: 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<String> inhibitedGenes(Drug drug) {
return _drugInhibitorsPerGene.keys.filter(
(gene) => _drugInhibitorsPerGene[gene]!.contains(drug.name)
).toList();
}

MapEntry<String, String>? 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';
}
1 change: 1 addition & 0 deletions app/lib/common/utilities/module.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 6fda75f

Please sign in to comment.