1414
1515FUNCTION_ORDER = [
1616 'No Function' ,
17+ 'Severely Decreased Function' ,
1718 'Decreased Function' ,
1819 'Possible Decreased Function' ,
1920 'Increased Function' ,
2021 'Possible Increased Function' ,
22+ 'Class I (Deficient with CNSHA)' ,
23+ 'Class II (Deficient)' ,
24+ 'Class III (Deficient)' ,
2125 'Uncertain Function' ,
2226 'Unknown Function' ,
2327 'Normal Function' ,
28+ 'Class IV (Normal)' ,
2429]
2530
2631class AlleleNotFoundError (Exception ):
@@ -202,6 +207,24 @@ def has_score(gene):
202207
203208 return gene in df [df .PhenotypeMethod == 'Score' ].Gene .unique ()
204209
210+ def is_legit_allele (gene , allele ):
211+ """
212+ Return True if specified allele exists in the allele table.
213+
214+ Parameters
215+ ----------
216+ gene : str
217+ Target gene.
218+ allele : str
219+ Allele to be tested.
220+
221+ Returns
222+ -------
223+ bool
224+ True if the allele is legit.
225+ """
226+ return allele in list_alleles (gene )
227+
205228def is_target_gene (gene ):
206229 """
207230 Return True if specified gene is one of the target genes.
@@ -465,7 +488,7 @@ def get_score(gene, allele):
465488 df = df [(df .Gene == gene ) & (df .StarAllele == allele )]
466489
467490 if df .empty :
468- raise AlleleNotFoundError (gene + allele )
491+ raise AlleleNotFoundError (gene , allele )
469492
470493 return df .ActivityScore .values [0 ]
471494
@@ -960,6 +983,10 @@ def one_row(r, score):
960983 elif phenotype_method == 'Diplotype' :
961984 df = load_diplotype_table ()
962985 df = df [df .Gene == gene ]
986+ if not is_legit_allele (gene , a ):
987+ raise AlleleNotFoundError (gene , a )
988+ if not is_legit_allele (gene , b ):
989+ raise AlleleNotFoundError (gene , b )
963990 l = [f'{ a } /{ b } ' , f'{ b } /{ a } ' ]
964991 i = df .Diplotype .isin (l )
965992 try :
@@ -1095,7 +1122,7 @@ def sort_alleles(
10951122
10961123 >>> alleles = ['*1', '*2', '*4', '*10']
10971124
1098- We can sort them by their prioirty with ``method='priority'``:
1125+ We can sort the alleles by their prioirty with ``method='priority'``:
10991126
11001127 >>> import pypgx
11011128 >>> alleles = pypgx.sort_alleles(alleles, by='priority', gene='CYP2D6', assembly='GRCh37')
@@ -1107,8 +1134,17 @@ def sort_alleles(
11071134 >>> alleles = pypgx.sort_alleles(alleles, by='name')
11081135 >>> alleles
11091136 ['*1', '*2', '*4', '*10']
1137+
1138+ Note that we can also sort alleles by name for genes that do not use the
1139+ star allele nomenclature (e.g. the *DPYD* gene):
1140+
1141+ >>> alleles = ['c.557A>G', 'c.2194G>A (*6)', 'c.496A>G', 'Reference', 'c.1627A>G (*5)']
1142+ >>> pypgx.sort_alleles(alleles, by='name')
1143+ ['Reference', 'c.496A>G', 'c.557A>G', 'c.1627A>G (*5)', 'c.2194G>A (*6)']
11101144 """
11111145 def func1 (allele ):
1146+ if gene is None :
1147+ raise ValueError ('Gene is required when sorting by priority' )
11121148 if not is_target_gene (gene ):
11131149 raise NotTargetGeneError (gene )
11141150 function = get_function (gene , allele )
@@ -1122,13 +1158,18 @@ def func1(allele):
11221158 return (a , b , c , d )
11231159
11241160 def func2 (allele ):
1161+ n = 99999
11251162 cn = 1
1126- if '*' not in allele :
1127- n = 999
1163+ if allele == 'Reference' :
1164+ n = 0
1165+ elif 'c.' in allele : # For the DPYD gene
1166+ n = int ('' .join ([x for x in allele .split ('>' )[0 ] if x .isdigit ()]))
1167+ elif '*' not in allele :
1168+ pass
11281169 else :
11291170 _ = allele .split ('+' )[0 ].split ('x' )[0 ].replace ('*' , '' )
11301171 if not _ [0 ].isdigit ():
1131- n = 999
1172+ pass
11321173 else :
11331174 n = int ('' .join ([x for x in _ if x .isdigit ()]))
11341175 if 'x' in allele .split ('+' )[0 ]:
0 commit comments