Skip to content

Commit

Permalink
feat(#662): drugs do not inhibit themselves
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Oct 16, 2023
1 parent 1da7ae2 commit 22a43f3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/lib/common/models/drug/drug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ extension DrugMatchesQuery on Drug {
extension DrugWithUserGuideline on Drug {
Guideline? userGuideline() => guidelines.firstOrNullWhere(
(guideline) => guideline.lookupkey.all((geneSymbol, geneResults) =>
geneResults.contains(UserData.lookupFor(geneSymbol))),
geneResults.contains(UserData.lookupFor(geneSymbol, drug: name))),
);
}

Expand Down
38 changes: 28 additions & 10 deletions app/lib/common/models/userdata/userdata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ class UserData {
static PhenotypeInformation phenotypeFor(
String gene,
BuildContext context,
{ String userSalutation = 'you' }
{
String? drug,
String userSalutation = 'you',
}
) {
final originalPhenotype = UserData.instance.diplotypes?[gene]?.phenotype;
if (originalPhenotype == null) {
return PhenotypeInformation();
}
final overwrittenLookup = UserData.overwrittenLookup(gene);
final activeInhibitors = UserData.activeInhibitorsFor(gene);
final overwrittenLookup = UserData.overwrittenLookup(gene, drug: drug);
final activeInhibitors = UserData.activeInhibitorsFor(gene, drug: drug);
if (activeInhibitors.isEmpty) {
return PhenotypeInformation(phenotype: originalPhenotype);
}
Expand Down Expand Up @@ -107,17 +110,30 @@ class UserData {
@HiveField(1)
Map<String, CpicPhenotype>? lookups;

static MapEntry<String, String>? overwrittenLookup(String gene) {
static MapEntry<String, String>? overwrittenLookup(
String gene,
{ String? drug }
) {
final inhibitors = strongDrugInhibitors[gene];
if (inhibitors == null) return null;
final lookup = inhibitors.entries.firstWhereOrNull((entry) =>
UserData.instance.activeDrugNames?.contains(entry.key) ?? false);
final lookup = inhibitors.entries.firstWhereOrNull((entry) {
final isActiveInhitor =
UserData.instance.activeDrugNames?.contains(entry.key) ?? false;
final wouldInhibitItself = drug == entry.key;
return isActiveInhitor && !wouldInhibitItself;
});
if (lookup == null) return null;
return lookup;
}

static String? lookupFor(String gene, {bool useOverwrite = true}) {
final overwrittenLookup = UserData.overwrittenLookup(gene);
static String? lookupFor(
String gene,
{
String? drug,
bool useOverwrite = true,
}
) {
final overwrittenLookup = UserData.overwrittenLookup(gene, drug: drug);
if (useOverwrite && overwrittenLookup != null) {
return overwrittenLookup.value;
}
Expand All @@ -128,11 +144,13 @@ class UserData {
@HiveField(2)
List<String>? activeDrugNames;

static List<String> activeInhibitorsFor(String gene) {
static List<String> activeInhibitorsFor(String gene, { String? drug }) {
return UserData.instance.activeDrugNames == null
? <String>[]
: UserData.instance.activeDrugNames!.filter(
(drug) => inhibitorsFor(gene).contains(drug)
(activeDrug) =>
inhibitorsFor(gene).contains(activeDrug) &&
activeDrug != drug
).toList();
}
}
Expand Down
12 changes: 5 additions & 7 deletions app/lib/common/pages/drug/drug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ class DrugPage extends StatelessWidget {
tooltip: context.l10n.drugs_page_tooltip_guideline,
),
SizedBox(height: 12),
...(userGuideline != null)
? [
Disclaimer(),
SizedBox(height: 12),
GuidelineAnnotationCard(userGuideline)
]
: [GuidelineAnnotationCard(userGuideline, drug: drug)],
if (userGuideline != null) ...[
Disclaimer(),
SizedBox(height: 12),
],
GuidelineAnnotationCard(userGuideline, drug),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import '../../../../utilities/guideline_utils.dart';
import '../sub_header.dart';

class GuidelineAnnotationCard extends StatelessWidget {
const GuidelineAnnotationCard(this.guideline, { this.drug });
const GuidelineAnnotationCard(this.guideline, this.drug);

final Guideline? guideline;
final Drug? drug;
Expand Down Expand Up @@ -81,7 +81,11 @@ class GuidelineAnnotationCard extends StatelessWidget {
final genes = guideline?.lookupkey.keys ??
drug!.guidelines.first.lookupkey.keys;
final geneDescriptions = genes.map((geneSymbol) {
final phenotypeInformation = UserData.phenotypeFor(geneSymbol, context);
final phenotypeInformation = UserData.phenotypeFor(
geneSymbol,
context,
drug: drug?.name,
);
var description =
'$geneSymbol: ${
phenotypeInformation.phenotype ??
Expand Down
25 changes: 17 additions & 8 deletions app/lib/common/utilities/pdf_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ List<pw.Widget> _buildDrugPart(Drug drug, BuildContext buildContext) {
];
}

String? _getPhenotypeInfo(String gene, BuildContext context) {
String? _getPhenotypeInfo(String gene, Drug drug, BuildContext context) {
final phenotypeInformation = UserData.phenotypeFor(
gene,
context,
drug: drug.name,
userSalutation: context.l10n.general_the_user,
);
if (phenotypeInformation.adaptionText.isNullOrBlank) {
Expand All @@ -138,9 +139,17 @@ String? _getPhenotypeInfo(String gene, BuildContext context) {
'${phenotypeInformation.adaptionText})';
}

String? _getActivityScoreInfo(String gene, BuildContext context) {
final originalLookup = UserData.lookupFor(gene, useOverwrite: false);
final overwrittenLookup = UserData.lookupFor(gene, useOverwrite: true);
String? _getActivityScoreInfo(String gene, Drug drug, BuildContext context) {
final originalLookup = UserData.lookupFor(
gene,
drug: drug.name,
useOverwrite: false,
);
final overwrittenLookup = UserData.lookupFor(
gene,
drug: drug.name,
useOverwrite: true,
);
if (originalLookup == overwrittenLookup) return originalLookup;
return '$overwrittenLookup '
'(${context.l10n.pdf_activity_score_overwrite(
Expand All @@ -150,13 +159,13 @@ String? _getActivityScoreInfo(String gene, BuildContext context) {

String _userInfoPerGene(
Drug drug,
String? Function(String, BuildContext) getInfo,
String? Function(String, Drug, BuildContext) getInfo,
BuildContext buildContext,
) {
if (drug.guidelines.isEmpty) return buildContext.l10n.pdf_no_value;
final guidelineGenes = drug.guidelines.first.lookupkey.keys.toList();
return guidelineGenes.map((gene) =>
'$gene: ${getInfo(gene, buildContext) ?? buildContext.l10n.pdf_no_value}'
'$gene: ${getInfo(gene, drug, buildContext) ?? buildContext.l10n.pdf_no_value}'
).join(', ');
}

Expand All @@ -169,7 +178,7 @@ List<pw.Widget> _buildUserPart(
final userGuideline = drug.userGuideline();
final patientGenotype = _userInfoPerGene(
drug,
(gene, context) => UserData.genotypeFor(gene),
(gene, drug, context) => UserData.genotypeFor(gene),
buildContext,
);
final patientPhenotype = _userInfoPerGene(
Expand All @@ -184,7 +193,7 @@ List<pw.Widget> _buildUserPart(
);
final allelesTested = _userInfoPerGene(
drug,
(gene, context) => UserData.allelesTestedFor(gene),
(gene, drug, context) => UserData.allelesTestedFor(gene),
buildContext,
);
final warningLevelIcons = {
Expand Down

0 comments on commit 22a43f3

Please sign in to comment.