Skip to content

Commit

Permalink
Added support for moving to debris
Browse files Browse the repository at this point in the history
  • Loading branch information
AxisAlexNT committed Jul 31, 2023
1 parent 5d37da5 commit b7902da
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,47 @@ public void unscaffoldRegion(final long startIncl, final long endExcl, final @No
}
}

public void moveRegionToDebris(final long startIncl, final long endExcl, final @NotNull ResolutionDescriptor resolutionDescriptor, final @NotNull QueryLengthUnit units) {
assert (startIncl < endExcl) : "Unscaffolding: start >= end??";

final var contigTree = this.chunkedFile.getContigTree();
final var scaffoldTree = this.chunkedFile.getScaffoldTree();

try {
contigTree.getRootLock().writeLock().lock();
scaffoldTree.getRootLock().writeLock().lock();

final long startBp = this.chunkedFile.convertUnits(
startIncl,
resolutionDescriptor,
units,
ResolutionDescriptor.fromResolutionOrder(0),
QueryLengthUnit.BASE_PAIRS
);
final long endBp = this.chunkedFile.convertUnits(
endExcl,
resolutionDescriptor,
units,
ResolutionDescriptor.fromResolutionOrder(0),
QueryLengthUnit.BASE_PAIRS
);

final var leftScaffoldBorders = scaffoldTree.getScaffoldBordersAtBp(startBp);
final var rightScaffoldBorders = scaffoldTree.getScaffoldBordersAtBp(endBp - 1);

final var trueStartBpIncl = leftScaffoldBorders.endBP();
final var trueEndBpExcl = rightScaffoldBorders.startBP();

if (trueStartBpIncl < trueEndBpExcl) {
scaffoldRegion(trueStartBpIncl, trueEndBpExcl, ResolutionDescriptor.fromResolutionOrder(0), QueryLengthUnit.BASE_PAIRS, id -> new ScaffoldDescriptor(id, String.format("scaffold_debris_%d", id), 1000));
moveSelectionRangeBp(trueStartBpIncl, trueEndBpExcl, this.chunkedFile.getMatrixSizeBins()[0]);
}
} finally {
scaffoldTree.getRootLock().writeLock().unlock();
contigTree.getRootLock().writeLock().unlock();
}
}

public void splitContigAtBin(final long splitPosition, final @NotNull @NonNull ResolutionDescriptor resolutionDescriptor, final @NotNull @NonNull QueryLengthUnit units) {
assert !QueryLengthUnit.BASE_PAIRS.equals(units) || (resolutionDescriptor.getResolutionOrderInArray() == 0) : "In bp query resolution should be set to 0";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ public void commitExposedSegment(final @NotNull Node.ExposedSegment exposedSegme
}
}

public @NotNull ScaffoldDescriptor.ScaffoldBordersBP getScaffoldBordersAtBp(final long queriedBp) {
try {
var leftBp = queriedBp;
var rightBp = queriedBp;
this.rootLock.readLock().lock();

final var es = Node.expose(this.root, leftBp, 1 + rightBp);
final var sg = es.segment();
if ((sg != null) && sg.scaffoldDescriptor != null) {
leftBp = Optional.ofNullable(es.less()).map(n -> n.subtreeLengthBp).orElse(0L);
rightBp = leftBp + sg.subtreeLengthBp;
}
return new ScaffoldDescriptor.ScaffoldBordersBP(leftBp, rightBp);
} finally {
this.rootLock.readLock().unlock();
}
}

/**
* @param startBp Starting BP of new scaffold.
* @param endBp Last (exclusive) BP of new scaffold.
Expand Down Expand Up @@ -329,7 +347,7 @@ public static Node.SplitResult splitNodeBp(final Node t, final long expectedLeft
final var rightPartLengthBp = newTree.subtreeLengthBp - t1.subtreeLengthBp;
final Node t2;
if (rightPartLengthBp > 0) {
t2 = newTree.cloneBuilder().nodeLengthBp(rightPartLengthBp).left(null).yPriority(rnd.nextLong(Long.min(1L+newTree.yPriority, Long.MAX_VALUE-1), Long.MAX_VALUE)).build().updateSizes();
t2 = newTree.cloneBuilder().nodeLengthBp(rightPartLengthBp).left(null).yPriority(rnd.nextLong(Long.min(1L + newTree.yPriority, Long.MAX_VALUE - 1), Long.MAX_VALUE)).build().updateSizes();
} else {
t2 = newTree.right;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.itmo.ctlab.hict.hict_server.dto.request.scaffolding;

import io.vertx.core.json.JsonObject;
import org.jetbrains.annotations.NotNull;

public record MoveSelectionToDebrisRequestDTO(
long startBP,
long endBP
) {

public static @NotNull MoveSelectionToDebrisRequestDTO fromJSONObject(final @NotNull JsonObject json) {
return new MoveSelectionToDebrisRequestDTO(
json.getLong("startBP"),
json.getLong("endBP")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ public void addHandlersToRouter(final @NotNull Router router) {

chunkedFile.scaffoldingOperations().unscaffoldRegion(request.startBP(), request.endBP(), ResolutionDescriptor.fromResolutionOrder(0), QueryLengthUnit.BASE_PAIRS);

ctx.response().end(Json.encode(AssemblyInfoDTO.generateFromChunkedFile(chunkedFile)));
});
router.post("/move_selection_to_debris").blockingHandler(ctx -> {
final @NotNull var requestBody = ctx.body();
final @NotNull var requestJSON = requestBody.asJsonObject();

final @NotNull @NonNull var request = MoveSelectionToDebrisRequestDTO.fromJSONObject(requestJSON);

final var map = vertx.sharedData().getLocalMap("hict_server");
log.debug("Got map");
final var chunkedFileWrapper = ((ShareableWrappers.ChunkedFileWrapper) (map.get("chunkedFile")));
if (chunkedFileWrapper == null) {
ctx.fail(new RuntimeException("Chunked file is not present in the local map, maybe the file is not yet opened?"));
return;
}
final var chunkedFile = chunkedFileWrapper.getChunkedFile();
log.debug("Got ChunkedFile from map");

chunkedFile.scaffoldingOperations().moveRegionToDebris(request.startBP(), request.endBP(), ResolutionDescriptor.fromResolutionOrder(0), QueryLengthUnit.BASE_PAIRS);

ctx.response().end(Json.encode(AssemblyInfoDTO.generateFromChunkedFile(chunkedFile)));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ public void addHandlersToRouter(final @NotNull Router router) {
return;
}

final var currentVersion = stats.versionCounter().get();
if ((version < currentVersion) || ((version > currentVersion) && !stats.versionCounter().compareAndSet(currentVersion, version))) {
ctx.response().setStatusCode(204).end(String.format("Current version is %d and request version is %d", currentVersion, version));
var currentVersion = stats.versionCounter().get();
if (version < currentVersion) {
log.debug(String.format("Current version is %d and request version is %d", currentVersion, version));
ctx.response().setStatusCode(204).putHeader("Content-Type", "text/plain").end(String.format("Current version is %d and request version is %d", currentVersion, version));
return;
}
do {
currentVersion = stats.versionCounter().get();
} while ((currentVersion < version) && !stats.versionCounter().compareAndSet(currentVersion, version));

final long startRowPx, startColPx, endRowPx, endColPx;
if (format == TileFormat.PNG_BY_PIXELS) {
Expand Down

0 comments on commit b7902da

Please sign in to comment.