Skip to content

Commit

Permalink
fix bug #15 even for python2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ggautreau committed Mar 5, 2018
1 parent c992a6e commit 2661c06
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 1 addition & 2 deletions ppanggolin/ppanggolin.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ def __neighborhood_computation(self, directed = False):#,light = False, untangle
if contig in self.circular_contig_size:#circularization
self.__add_link(gene_info_start[FAMILY],family_id_nei,organism, (self.circular_contig_size[contig] - end_family_nei) + gene_info_start[START])

contig_annot[gene_start]=gene_info_start
contig_annot.move_to_end(gene_start, last=False)#move to the beginning
ordered_dict_prepend(contig_annot,gene_start,gene_info_start)#insert at the top
# if light:
# del self.annotations[organism]

Expand Down
23 changes: 21 additions & 2 deletions ppanggolin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import gzip
from decimal import Decimal
from collections import defaultdict
from collections import defaultdict, OrderedDict
import math
from random import sample
from io import TextIOWrapper
Expand Down Expand Up @@ -79,4 +79,23 @@ def median(numbers):
return numbers[n//2]
else:
i = n//2
return (numbers[i - 1] + numbers[i])/2
return (numbers[i - 1] + numbers[i])/2

"""insertion of element at the top of an OrderedDict (for compatibility with python 2.7)
from : https://stackoverflow.com/questions/16664874/how-can-i-add-an-element-at-the-top-of-an-ordereddict-in-python/18326914
"""
def ordered_dict_prepend(dct, key, value, dict_setitem=dict.__setitem__):
root = dct._OrderedDict__root
first = root[1]

if key in dct:
link = dct._OrderedDict__map[key]
link_prev, link_next, _ = link
link_prev[1] = link_next
link_next[0] = link_prev
link[0] = root
link[1] = first
root[1] = first[0] = link
else:
root[1] = first[0] = dct._OrderedDict__map[key] = [root, first, key]
dict_setitem(dct, key, value)

0 comments on commit 2661c06

Please sign in to comment.