Search in sources :

Example 16 with TLongHashSet

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;
}
Also used : TLongHashSet(gnu.trove.set.hash.TLongHashSet) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) PriorityQueue(java.util.PriorityQueue)

Example 17 with TLongHashSet

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;
}
Also used : RoutingContext(net.osmand.router.RoutingContext) RoutingConfiguration(net.osmand.router.RoutingConfiguration) TLongHashSet(gnu.trove.set.hash.TLongHashSet) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) RouteDataObject(net.osmand.binary.RouteDataObject)

Example 18 with TLongHashSet

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);
}
Also used : TLongHashSet(gnu.trove.set.hash.TLongHashSet) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) RouteDataObject(net.osmand.binary.RouteDataObject)

Example 19 with TLongHashSet

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;
}
Also used : TLongHashSet(gnu.trove.set.hash.TLongHashSet)

Example 20 with TLongHashSet

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));
    }
}
Also used : ArrayList(java.util.ArrayList) NSequenceWithQuality(com.milaboratory.core.sequence.NSequenceWithQuality) NucleotideSequence(com.milaboratory.core.sequence.NucleotideSequence) TLongHashSet(gnu.trove.set.hash.TLongHashSet) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TLongHashSet (gnu.trove.set.hash.TLongHashSet)22 ArrayList (java.util.ArrayList)11 RouteDataObject (net.osmand.binary.RouteDataObject)6 TLongSet (gnu.trove.set.TLongSet)5 List (java.util.List)5 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)4 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 PriorityQueue (java.util.PriorityQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)2 Node (net.osmand.osm.edit.Node)2 BinaryRoutePlanner (net.osmand.router.BinaryRoutePlanner)2 RouteSegment (net.osmand.router.BinaryRoutePlanner.RouteSegment)2 Async (br.com.brjdevs.java.utils.async.Async)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DiscordBotsAPI (com.github.natanbc.discordbotsapi.DiscordBotsAPI)1