Skip to content

Commit

Permalink
sort by descending weight
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed Jan 24, 2025
1 parent 43ef6c9 commit 96b3c3a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/jcvi/projects/sugarcane.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,29 @@ def make(cls, name: str, chromosomes: List[Chromosome]) -> "Genome":
def _sort_key(self, x: Chromosome):
return x.subgenome, x.chrom

def pair_chromosomes(
self, inplace=False
) -> Tuple[List[List[Chromosome]], List[Chromosome]]:
def pair_with_weights(self) -> List[List[str]]:
"""
Pair chromosomes by similarity.
Pair chromosomes by similarity, results are sorted in descending similarity.
"""
G = nx.Graph()
scores = {}
self.chromosomes.sort(key=self._sort_key)
for _, chromosomes in groupby(self.chromosomes, key=self._sort_key):
for a, b in combinations(chromosomes, 2):
weight = a.num_matching_genes(b) + 1 # +1 so we don't have zero weight
G.add_edge(a.uuid, b.uuid, weight=weight)
scores[tuple(sorted((a.uuid, b.uuid)))] = weight
# Find the maximum matching
matching = nx.max_weight_matching(G)
return sorted(matching, key=lambda x: scores[tuple(sorted(x))], reverse=True)

def pair_chromosomes(
self, inplace=False
) -> Tuple[List[List[Chromosome]], List[Chromosome]]:
"""
Pair chromosomes by similarity.
"""
matching = self.pair_with_weights()
# Partition the chromosomes into paired and singleton
paired = set()
pairs = []
Expand Down

0 comments on commit 96b3c3a

Please sign in to comment.