
eu.interedition.collatex.util.VariantGraphRanking Maven / Gradle / Ivy
/*
* Copyright (c) 2013 The Interedition Development Group.
*
* This file is part of CollateX.
*
* CollateX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CollateX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CollateX. If not, see .
*/
package eu.interedition.collatex.util;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.annotation.Nullable;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Ordering;
import com.google.common.collect.RowSortedTable;
import com.google.common.collect.Sets;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.TreeBasedTable;
import com.google.common.collect.TreeMultimap;
import eu.interedition.collatex.Token;
import eu.interedition.collatex.VariantGraph;
import eu.interedition.collatex.VariantGraph.Vertex;
import eu.interedition.collatex.Witness;
/**
* @author Gregor Middell
* @author Ronald Haentjens Dekker
*/
public class VariantGraphRanking implements Iterable>, Function, Comparator {
private final Map byVertex = Maps.newHashMap();
private final SortedSetMultimap byRank = TreeMultimap.create(Ordering.natural(), Ordering.arbitrary());
private final VariantGraph graph;
private final Set witnesses;
VariantGraphRanking(VariantGraph graph, Set witnesses) {
this.graph = graph;
this.witnesses = witnesses;
}
public static VariantGraphRanking of(VariantGraph graph) {
return of(graph, null);
}
public static VariantGraphRanking of(VariantGraph graph, Set witnesses) {
final VariantGraphRanking ranking = new VariantGraphRanking(graph, witnesses);
for (VariantGraph.Vertex v : graph.vertices(witnesses)) {
int rank = -1;
for (VariantGraph.Edge e : v.incoming(witnesses)) {
rank = Math.max(rank, ranking.byVertex.get(e.from()));
}
rank++;
ranking.byVertex.put(v, rank);
ranking.byRank.put(rank, v);
}
return ranking;
}
public static VariantGraphRanking ofOnlyCertainVertices(VariantGraph graph, Set witnesses, Set vertices) {
final VariantGraphRanking ranking = new VariantGraphRanking(graph, witnesses);
for (VariantGraph.Vertex v : graph.vertices(witnesses)) {
int rank = -1;
for (VariantGraph.Edge e : v.incoming(witnesses)) {
rank = Math.max(rank, ranking.byVertex.get(e.from()));
}
if (vertices.contains(v)) {
rank++;
}
ranking.byVertex.put(v, rank);
ranking.byRank.put(rank, v);
}
return ranking;
}
public Set witnesses() {
return Objects.firstNonNull(witnesses, graph.witnesses());
}
public Map getByVertex() {
return Collections.unmodifiableMap(byVertex);
}
public SortedSetMultimap getByRank() {
return Multimaps.unmodifiableSortedSetMultimap(byRank);
}
public int size() {
return byRank.keySet().size();
}
@Override
public Iterator> iterator() {
return new AbstractIterator>() {
private final Iterator it = byRank.keySet().iterator();
@Override
protected Set computeNext() {
return (it.hasNext() ? byRank.get(it.next()) : endOfData());
}
};
}
public RowSortedTable> asTable() {
final TreeBasedTable> table = TreeBasedTable.create(Ordering.natural(), Witness.SIGIL_COMPARATOR);
for (Map.Entry rank : byVertex.entrySet()) {
final int row = rank.getValue();
for (Token token : rank.getKey().tokens(witnesses)) {
final Witness column = token.getWitness();
Set cell = table.get(row, column);
if (cell == null) {
table.put(row, column, cell = Sets.newHashSet());
}
cell.add(token);
}
}
return table;
}
@Override
public Integer apply(@Nullable VariantGraph.Vertex vertex) {
return byVertex.get(vertex);
}
@Override
public int compare(VariantGraph.Vertex o1, VariantGraph.Vertex o2) {
final Integer o1Rank = byVertex.get(o1);
final Integer o2Rank = byVertex.get(o2);
Preconditions.checkState(o1Rank != null, o1);
Preconditions.checkState(o2Rank != null, o2);
return (o1Rank.intValue() - o2Rank.intValue());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy