use of gnu.trove.set.hash.TLongHashSet in project OsmAnd-tools by osmandapp.
the class ManyToOneRoadCalculation method calculateOneToMany.
private List<ManyToManySegment> calculateOneToMany(TLongObjectHashMap<ManyToManySegment> allSegments, List<ManyToManySegment> bottomIntersects, final int sbottom, final GeneralRouter router, ManyToManySegment oneTop) {
if (router.isOneWay(oneTop.road) > 0 && oneTop.segmentIndex == oneTop.road.getPointsLength() - 1) {
int s = oneTop.segmentIndex - 1;
long key = calcLong(oneTop.road.getPoint31XTile(s), oneTop.road.getPoint31YTile(s));
oneTop = find(allSegments.get(key), oneTop.road.id);
} else if (router.isOneWay(oneTop.road) < 0 && oneTop.segmentIndex == 0) {
int s = oneTop.segmentIndex + 1;
long key = calcLong(oneTop.road.getPoint31XTile(s), oneTop.road.getPoint31YTile(s));
oneTop = find(allSegments.get(key), oneTop.road.id);
}
PriorityQueue<ManyToManySegment> queue = new PriorityQueue<ManyToManySegment>(100, new Comparator<ManyToManySegment>() {
@Override
public int compare(ManyToManySegment arg0, ManyToManySegment arg1) {
// arg1.distanceFromStart + arg1.estimateDistanceEnd(router, sbottom));
return Double.compare(arg0.distanceFromStart, arg1.distanceFromStart);
}
});
TLongHashSet finalSegments = new TLongHashSet();
for (ManyToManySegment fs : bottomIntersects) {
finalSegments.add(fs.road.id);
}
oneTop.distanceFromStart = 0;
// oneTop.estimateDistanceToEnd = squareRootDist(0, stop, 0, sbottom) / router.getMaxDefaultSpeed();
queue.add(oneTop);
TLongHashSet visitedSegments = new TLongHashSet();
List<ManyToManySegment> finalSegmentResult = new ArrayList<ManyToOneRoadCalculation.ManyToManySegment>();
while (!queue.isEmpty()) {
ManyToManySegment seg = queue.poll();
if (finalSegments.contains(seg.road.id)) {
finalSegmentResult.add(seg);
finalSegments.remove(seg.road.id);
if (finalSegments.size() == 0) {
break;
} else {
continue;
}
}
int oneWay = router.isOneWay(seg.road);
if (oneWay >= 0) {
processRoadSegment(queue, router, sbottom, seg, true, allSegments, visitedSegments);
}
if (oneWay <= 0) {
processRoadSegment(queue, router, sbottom, seg, false, allSegments, visitedSegments);
}
// visitedRoads++;
}
return finalSegmentResult;
}
use of gnu.trove.set.hash.TLongHashSet in project OsmAnd-tools by osmandapp.
the class CheckRoadConnectivity method calculateDisconnectedRoadsToAddAndDelete.
private TLongObjectHashMap<RouteDataObject> calculateDisconnectedRoadsToAddAndDelete(TLongObjectHashMap<List<RouteDataObject>> mapOfObjectToCheck, TLongObjectHashMap<List<RouteDataObject>> all, BinaryMapIndexReader reader, TLongHashSet setToRemove, TLongHashSet registeredIds) {
RoutePlannerFrontEnd frontEnd = new RoutePlannerFrontEnd(false);
RoutingConfiguration config = RoutingConfiguration.getDefault().build("car", 1000);
long[] keys = mapOfObjectToCheck.keys();
TLongObjectHashMap<RouteDataObject> toAdd = new TLongObjectHashMap<RouteDataObject>();
TLongHashSet beginIsolated = new TLongHashSet();
TLongHashSet endIsolated = new TLongHashSet();
for (int k = 0; k < keys.length; k++) {
long point = keys[k];
if (all.get(point).size() == 1) {
RouteDataObject rdo = all.get(keys[k]).get(0);
boolean begin = calcPointId(rdo, 0) == point;
RoutingContext ctx = frontEnd.buildRoutingContext(config, null, new BinaryMapIndexReader[] { reader }, RouteCalculationMode.NORMAL);
List<RouteDataObject> result = findConnectedRoads(ctx, rdo, begin, all);
if (result == null) {
if (begin) {
beginIsolated.add(rdo.getId());
} else {
endIsolated.add(rdo.getId());
}
} else {
for (RouteDataObject obj : result) {
if (!registeredIds.contains(obj.id)) {
toAdd.put(obj.id, obj);
}
}
}
}
}
int begSize = beginIsolated.size();
int endSize = endIsolated.size();
beginIsolated.retainAll(endIsolated);
int intersectionSize = beginIsolated.size();
if (setToRemove != null) {
setToRemove.addAll(beginIsolated);
}
System.out.println("All objects in base file " + mapOfObjectToCheck.size() + " to keep isolated " + (begSize + endSize - 2 * intersectionSize) + " to add " + toAdd.size() + " to remove " + beginIsolated.size());
return toAdd;
}
use of gnu.trove.set.hash.TLongHashSet in project OsmAnd-tools by osmandapp.
the class CheckRoadConnectivity method collectDisconnectedRoads.
public TLongObjectHashMap<RouteDataObject> collectDisconnectedRoads(BinaryMapIndexReader reader) throws IOException {
TLongObjectHashMap<List<RouteDataObject>> all = new TLongObjectHashMap<List<RouteDataObject>>();
TLongObjectHashMap<List<RouteDataObject>> onlyRoads = new TLongObjectHashMap<List<RouteDataObject>>();
TLongHashSet registeredRoadIds = new TLongHashSet();
findAllBaseRoadIntersections(reader, all, onlyRoads, registeredRoadIds);
return calculateDisconnectedRoadsToAddAndDelete(onlyRoads, all, reader, null, registeredRoadIds);
}
use of gnu.trove.set.hash.TLongHashSet in project Osmand by osmandapp.
the class RoutingContext method getRoutingTile.
@SuppressWarnings("unused")
private long getRoutingTile(int x31, int y31, int memoryLimit, int loadOptions) {
// long now = System.nanoTime();
long xloc = x31 >> (31 - config.ZOOM_TO_LOAD_TILES);
long yloc = y31 >> (31 - config.ZOOM_TO_LOAD_TILES);
long tileId = (xloc << config.ZOOM_TO_LOAD_TILES) + yloc;
if (loadOptions != OPTION_NO_LOAD) {
if (memoryLimit == 0) {
memoryLimit = config.memoryLimitation;
}
if (getCurrentEstimatedSize() > 0.9 * memoryLimit) {
int sz1 = getCurrentEstimatedSize();
long h1 = 0;
if (SHOW_GC_SIZE && sz1 > 0.7 * memoryLimit) {
runGCUsedMemory();
h1 = runGCUsedMemory();
}
int clt = getCurrentlyLoadedTiles();
long us1 = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
unloadUnusedTiles(memoryLimit);
if (h1 != 0 && getCurrentlyLoadedTiles() != clt) {
int sz2 = getCurrentEstimatedSize();
runGCUsedMemory();
long h2 = runGCUsedMemory();
float mb = (1 << 20);
log.warn("Unload tiles : estimated " + (sz1 - sz2) / mb + " ?= " + (h1 - h2) / mb + " actual");
log.warn("Used after " + h2 / mb + " of " + Runtime.getRuntime().totalMemory() / mb + " max " + maxMemory() / mb);
} else {
float mb = (1 << 20);
int sz2 = getCurrentEstimatedSize();
log.warn("Unload tiles : occupied before " + sz1 / mb + " Mb - now " + sz2 / mb + "MB " + memoryLimit / mb + " limit MB " + config.memoryLimitation / mb);
long us2 = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory());
log.warn("Used memory before " + us1 / mb + "after " + us1 / mb + " of max " + maxMemory() / mb);
}
}
if (!indexedSubregions.containsKey(tileId)) {
List<RoutingSubregionTile> collection = loadTileHeaders(x31, y31);
indexedSubregions.put(tileId, collection);
}
List<RoutingSubregionTile> subregions = indexedSubregions.get(tileId);
if (subregions != null) {
boolean load = false;
for (RoutingSubregionTile ts : subregions) {
if (!ts.isLoaded()) {
load = true;
}
}
if (load) {
TLongHashSet excludeIds = new TLongHashSet();
for (RoutingSubregionTile ts : subregions) {
if (!ts.isLoaded()) {
loadSubregionTile(ts, loadOptions == OPTION_IN_MEMORY_LOAD, null, excludeIds);
} else {
if (ts.excludedIds != null) {
excludeIds.addAll(ts.excludedIds);
}
}
}
}
}
}
// timeToLoad += (System.nanoTime() - now);
return tileId;
}
use of gnu.trove.set.hash.TLongHashSet in project mixcr by milaboratory.
the class PartialAlignmentsAssembler method searchOverlaps.
@SuppressWarnings("unchecked")
private OverlapSearchResult searchOverlaps(final VDJCAlignments rightAl, final boolean allowChimeras) {
final Chains jChains = rightAl.getAllChains(GeneType.Joining);
int rightTargetId = getRightPartitionedSequence(rightAl);
if (rightTargetId == -1)
return null;
rightParts.incrementAndGet();
final VDJCPartitionedSequence rightTarget = rightAl.getPartitionedTarget(rightTargetId);
NSequenceWithQuality rightSeqQ = rightTarget.getSequence();
NucleotideSequence rightSeq = rightSeqQ.getSequence();
int stop = rightTarget.getPartitioning().getPosition(ReferencePoint.JBeginTrimmed);
if (stop == -1)
stop = rightTarget.getSequence().size();
else
stop -= kOffset;
// black list of left parts failed due to inconsistent overlapped alignments (failed AMerge)
TLongHashSet blackList = new TLongHashSet();
SEARCH_LEFT_PARTS: while (true) {
int maxOverlap = -1, maxDelta = -1, maxOverlapIndexInList = -1, maxBegin = -1, maxEnd = -1;
List<KMerInfo> maxOverlapList = null;
boolean isMaxOverOverlapped = false;
for (int rFrom = 0; rFrom < stop && rFrom + kValue < rightSeqQ.size(); rFrom++) {
long kMer = kMer(rightSeqQ.getSequence(), rFrom, kValue);
List<KMerInfo> match = kToIndexLeft.get(kMer);
if (match == null)
continue;
out: for (int i = 0; i < match.size(); i++) {
boolean isOverOverlapped = false;
final VDJCAlignments leftAl = match.get(i).getAlignments();
if (blackList.contains(leftAl.getAlignmentsIndex()))
continue;
if (// You shall not merge with yourself
leftAl.getAlignmentsIndex() == rightAl.getAlignmentsIndex() || alreadyMergedIds.contains(leftAl.getAlignmentsIndex()))
continue;
// Checking chains compatibility
if (!allowChimeras && jChains != null && !leftAl.getAllChains(GeneType.Variable).intersects(jChains))
continue;
// Check for the same V
if (leftAl.getBestHit(GeneType.Variable) != null && rightAl.getBestHit(GeneType.Variable) != null && !leftAl.hasCommonGenes(GeneType.Variable, rightAl))
continue;
final NucleotideSequence leftSeq = leftAl.getPartitionedTarget(getLeftPartitionedSequence(leftAl)).getSequence().getSequence();
int lFrom = match.get(i).kMerPositionFrom;
// begin and end in the coordinates of left
int delta, begin = delta = lFrom - rFrom;
if (begin < 0) {
begin = 0;
isOverOverlapped = true;
}
int end = leftSeq.size();
if (end >= rightSeq.size() + delta) {
end = rightSeq.size() + delta;
isOverOverlapped = true;
}
for (int j = begin; j < end; j++) if (leftSeq.codeAt(j) != rightSeq.codeAt(j - delta))
continue out;
int overlap = end - begin;
if (maxOverlap < overlap) {
maxOverlap = overlap;
maxOverlapList = match;
maxOverlapIndexInList = i;
maxDelta = delta;
maxBegin = begin;
maxEnd = end;
isMaxOverOverlapped = isOverOverlapped;
}
}
}
if (maxOverlapList == null)
return null;
if (maxOverlap < minimalAssembleOverlap)
return null;
final KMerInfo left = maxOverlapList.remove(maxOverlapIndexInList);
VDJCAlignments leftAl = left.alignments;
// final long readId = rightAl.getReadId();
ArrayList<AlignedTarget> leftTargets = extractAlignedTargets(leftAl);
ArrayList<AlignedTarget> rightTargets = extractAlignedTargets(rightAl);
AlignedTarget leftCentral = leftTargets.get(left.targetId);
AlignedTarget rightCentral = rightTargets.get(rightTargetId);
AlignedTarget central = targetMerger.merge(leftCentral, rightCentral, maxDelta, SequenceHistory.OverlapType.CDR3Overlap, 0);
// Setting overlap position
central = AlignedTarget.setOverlapRange(central, maxBegin, maxEnd);
final List<AlignedTarget> leftDescriptors = new ArrayList<>(2), rightDescriptors = new ArrayList<>(2);
for (int i = 0; i < left.targetId; ++i) leftDescriptors.add(leftTargets.get(i));
for (int i = left.targetId + 1; i < leftAl.numberOfTargets(); ++i) rightDescriptors.add(leftTargets.get(i));
for (int i = 0; i < rightTargetId; ++i) leftDescriptors.add(rightTargets.get(i));
for (int i = rightTargetId + 1; i < rightAl.numberOfTargets(); ++i) rightDescriptors.add(rightTargets.get(i));
// Merging to VJ junction
List<AlignedTarget>[] allDescriptors = new List[] { leftDescriptors, rightDescriptors };
TargetMerger.TargetMergingResult bestResult = TargetMerger.FAILED_RESULT;
int bestI;
// Trying to merge left and right reads to central one
for (List<AlignedTarget> descriptors : allDescriptors) do {
bestI = -1;
for (int i = 0; i < descriptors.size(); i++) {
TargetMerger.TargetMergingResult result = targetMerger.merge(descriptors.get(i), central);
if (result.failedDueInconsistentAlignments()) {
// Inconsistent alignments -> retry
blackList.add(leftAl.getAlignmentsIndex());
continue SEARCH_LEFT_PARTS;
}
if (bestResult.getScore() < result.getScore()) {
bestResult = result;
bestI = i;
}
}
if (bestI != -1) {
assert bestResult != TargetMerger.FAILED_RESULT;
central = bestResult.getResult();
descriptors.remove(bestI);
}
} while (bestI != -1);
// Merging left+left / right+right
outer: for (int d = 0; d < allDescriptors.length; d++) {
List<AlignedTarget> descriptors = allDescriptors[d];
for (int i = 0; i < descriptors.size(); i++) for (int j = i + 1; j < descriptors.size(); j++) {
TargetMerger.TargetMergingResult result = targetMerger.merge(descriptors.get(i), descriptors.get(j));
if (result.failedDueInconsistentAlignments()) {
// Inconsistent alignments -> retry
blackList.add(leftAl.getAlignmentsIndex());
continue SEARCH_LEFT_PARTS;
}
if (result.isSuccessful()) {
descriptors.set(i, result.getResult());
descriptors.remove(j);
--d;
continue outer;
}
}
}
if (isMaxOverOverlapped)
overoverlapped.incrementAndGet();
// Creating pre-list of resulting targets
List<AlignedTarget> result = new ArrayList<>();
result.addAll(leftDescriptors);
result.add(central);
result.addAll(rightDescriptors);
// Ordering and filtering final targets
return new OverlapSearchResult(maxOverlapList, left, AlignedTarget.orderTargets(result));
}
}
Aggregations