From 2661c06d5fccd35be1820eefccf34ecebb2157d9 Mon Sep 17 00:00:00 2001 From: GAUTREAU Guillaume Date: Mon, 5 Mar 2018 15:09:12 +0100 Subject: [PATCH] fix bug #15 even for python2.7 --- ppanggolin/ppanggolin.py | 3 +-- ppanggolin/utils.py | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ppanggolin/ppanggolin.py b/ppanggolin/ppanggolin.py index e07fbed..d4f52d7 100755 --- a/ppanggolin/ppanggolin.py +++ b/ppanggolin/ppanggolin.py @@ -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] diff --git a/ppanggolin/utils.py b/ppanggolin/utils.py index 343e572..bd5d4b6 100755 --- a/ppanggolin/utils.py +++ b/ppanggolin/utils.py @@ -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 @@ -79,4 +79,23 @@ def median(numbers): return numbers[n//2] else: i = n//2 - return (numbers[i - 1] + numbers[i])/2 \ No newline at end of file + 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) \ No newline at end of file