Reputation: 606
I have the following code:
refseq_dict = {'NM': 0, 'NC': 1, 'NG': 2, 'NP': 3, 'NR': 4, 'XM': 5, 'XP': 6, 'XR': 7, 'WP': 8}
ensembl_dict = {'transcript_ablation': 0, 'splice_acceptor_variant': 1, 'splice_donor_variant': 2, 'stop_gained': 3, 'frameshift_variant': 4, 'stop_lost': 5, 'start_lost': 6, 'transcript_amplification': 7,'inframe_insertion': 8, 'inframe_deletion': 9, 'missense_variant': 10, 'protein_altering_variant': 11, 'splice_region_variant': 12, 'incomplete_terminal_codon_variant': 13, 'start_retained_variant': 14, 'stop_retained_variant': 15, 'synonymous_variant': 16, 'coding_sequence_variant': 17, 'mature_miRNA_variant': 18, '5_prime_UTR_variant': 19, '3_prime_UTR_variant': 20, 'non_coding_transcript_exon_variant': 21, 'intron_variant': 22, 'NMD_transcript_variant': 23, 'non_coding_transcript_variant': 24, 'upstream_gene_variant': 25, 'downstream_gene_variant': 26, 'TFBS_ablation': 27,'TFBS_amplification': 28, 'TF_binding_site_variant': 29, 'regulatory_region_ablation': 30, 'regulatory_region_amplification': 31,'feature_elongation': 32, 'regulatory_region_variant': 33, 'feature_truncation': 34, 'intergenic_variant': 35}
def get_index(s):
by_tabs = s.split("\t")
if "_" in by_tabs[4]:
by_underscore = by_tabs[4].split("_")
return refseq_dict[by_underscore[0]]
elif "," not in by_tabs[6]:
return ensembl_dict[by_tabs[6]]
elif "," in by_tabs[6]:
by_commas = by_tabs[6].split(",")
by_commas = sorted(by_commas, key = lambda x: list(ensembl_dict.keys()).index(x))[0]
return ensembl_dict[by_commas]
d = {'rs570876887': ['rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000439127\tTranscript\tsplice_acceptor_variant,intron_variant,non_coding_transcript_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tHIGH\t-\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\ttranscribed_unprocessed_pseudogene\t-\t-\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t-\t7/7\t-\tENST00000439127.6:n.1189-3_1189-2del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000580113\tTranscript\tdownstream_gene_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tMODIFIER\t773\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\tprocessed_transcript\t-\t3\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000580621\tTranscript\tsplice_acceptor_variant,intron_variant,non_coding_transcript_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tHIGH\t-\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\tretained_intron\tYES\t1\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t-\t6/6\t-\tENST00000580621.5:n.1689-3_1689-2del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\tENSG00000214822\tENST00000584759\tTranscript\tnon_coding_transcript_exon_variant\t428-429\t-\t-\t-\t-\trs570876887\tTG\tMODIFIER\t-\t-1\t-\tdeletion\tKRT16P3\tHGNC\tHGNC:37808\tretained_intron\t-\t5\t-\t-\t-\t-\t-\t-\t-\tEnsembl\tTG\tTG\t-\t-\t-\t-\t2/2\t-\t-\tENST00000584759.1:n.428_429del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n', 'rs570876887\tchr17:20501765-20501766\t-\t644945\tNR_029393.1\tTranscript\tsplice_acceptor_variant,intron_variant,non_coding_transcript_variant\t-\t-\t-\t-\t-\trs570876887\tTG\tHIGH\t-\t-1\t-\tdeletion\tKRT16P3\tEntrezGene\tHGNC:37808\ttranscribed_pseudogene\t-\t-\t-\t-\t-\t-\t-\t-\t-\tRefSeq\tTG\tTG\tOK\t-\t-\t-\t-\t6/6\t-\tNR_029393.1:n.1706-3_1706-2del\t-\t-\t0.0140\t0.0272\t0.0086\t0.001\t0.0109\t0.0164\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t-\t0.0272\tAFR\t-\t-\t-\t-\n']}
for key, value in d.items():
ll = sorted(value, key = get_index)
for i in ll:
print(i)
which is supposed to sort the value
list according to refseq_dict
and ensembl_dict
. I save it under script.py
and launch it using python3 script.py
. However, I noticed that the output is random. What is expected it the following output:
rs570876887 chr17:20501765-20501766 - 644945 NR_029393.1 Transcript splice_acceptor_variant,intron_variant,non_coding_transcript_variant - - - - - rs570876887 TG HIGH - -1 - deletion KRT16P3 EntrezGene HGNC:37808 transcribed_pseudogene - - - - - - - - - RefSeq TG TG OK - - - - 6/6 - NR_029393.1:n.1706-3_1706-2del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000584759 Transcript non_coding_transcript_exon_variant 428-429 - - - - rs570876887 TG MODIFIER - -1 - deletion KRT16P3 HGNC HGNC:37808 retained_intron - 5 - - - - - - - Ensembl TG TG - - - - 2/2 - - ENST00000584759.1:n.428_429del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000439127 Transcript splice_acceptor_variant,intron_variant,non_coding_transcript_variant - - - - - rs570876887 TG HIGH - -1 - deletion KRT16P3 HGNC HGNC:37808 transcribed_unprocessed_pseudogene - - - - - - - - - Ensembl TG TG - - - - - 7/7 - ENST00000439127.6:n.1189-3_1189-2del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000580621 Transcript splice_acceptor_variant,intron_variant,non_coding_transcript_variant - - - - - rs570876887 TG HIGH - -1 - deletion KRT16P3 HGNC HGNC:37808 retained_intron YES 1 - - - - - - - Ensembl TG TG - - - - - 6/6 - ENST00000580621.5:n.1689-3_1689-2del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000580113 Transcript downstream_gene_variant - - - - - rs570876887 TG MODIFIER 773 -1 - deletion KRT16P3 HGNC HGNC:37808 processed_transcript - 3 - - - - - - - Ensembl TG TG - - - - - - - - - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
but sometimes I get:
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000439127 Transcript splice_acceptor_variant,intron_variant,non_coding_transcript_variant - - - - - rs570876887 TG HIGH - -1 - deletion KRT16P3 HGNC HGNC:37808 transcribed_unprocessed_pseudogene - - - - - - - - - Ensembl TG TG - - - - - 7/7 - ENST00000439127.6:n.1189-3_1189-2del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000580621 Transcript splice_acceptor_variant,intron_variant,non_coding_transcript_variant - - - - - rs570876887 TG HIGH - -1 - deletion KRT16P3 HGNC HGNC:37808 retained_intron YES 1 - - - - - - - Ensembl TG TG - - - - - 6/6 - ENST00000580621.5:n.1689-3_1689-2del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - 644945 NR_029393.1 Transcript splice_acceptor_variant,intron_variant,non_coding_transcript_variant - - - - - rs570876887 TG HIGH - -1 - deletion KRT16P3 EntrezGene HGNC:37808 transcribed_pseudogene - - - - - - - - - RefSeq TG TG OK - - - - 6/6 - NR_029393.1:n.1706-3_1706-2del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000584759 Transcript non_coding_transcript_exon_variant 428-429 - - - - rs570876887 TG MODIFIER - -1 - deletion KRT16P3 HGNC HGNC:37808 retained_intron - 5 - - - - - - - Ensembl TG TG - - - - 2/2 - - ENST00000584759.1:n.428_429del - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
rs570876887 chr17:20501765-20501766 - ENSG00000214822 ENST00000580113 Transcript downstream_gene_variant - - - - - rs570876887 TG MODIFIER 773 -1 - deletion KRT16P3 HGNC HGNC:37808 processed_transcript - 3 - - - - - - - Ensembl TG TG - - - - - - - - - - 0.0140 0.0272 0.0086 0.001 0.0109 0.0164 - - - - - - - - - - - 0.0272 AFR - - - -
What is the issue in my code ?
Upvotes: 0
Views: 37
Reputation: 4879
The get_index
function - which is the key for sorting - returns the same value for 2 values in your list-of-list
v = list(d.values())
len(v)#returns 1
len(*v)#returns 5
[get_index(x) for x in v[0]]#returns [1, 26, 1, 21, 4]
As the first and third entries return the same key
value - you get inconsistent results. You might want to update the get_index
function to differentiate between the 2
Upvotes: 1