Skip to content

Commit

Permalink
update fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sa501428 committed Jan 18, 2023
1 parent c577cd7 commit ec78606
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class Main {

public static final String VERSION_NUM = "0.103.0";
public static final String VERSION_NUM = "0.104.0";
public static boolean printVerboseComments = false;

public static void printGeneralUsageAndExit(int exitCode, String cUsage) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public static void main(String[] argv) throws CmdLineParser.UnknownOptionExcepti
APA apa = new APA(args, parser, false);
apa.run();
} else if (command.startsWith("assign") && command.contains("motif")) {
MotifAssignment motif = new MotifAssignment(args, parser);
MotifAssignment motif = new MotifAssignment(args, parser, command);
motif.run();
} else if (command.startsWith("ata")) {
ATA ata = new ATA(args, parser);
Expand Down
44 changes: 9 additions & 35 deletions src/cli/clt/bedpe/AnchorFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
import cli.clt.CommandLineParser;
import cli.utils.clique.Node95;
import cli.utils.clique.SimpleClustering;
import cli.utils.peaks.Point1D;
import javastraw.feature2D.Feature2D;
import javastraw.feature2D.Feature2DList;
import javastraw.feature2D.Feature2DParser;
import javastraw.reader.basics.Chromosome;
import javastraw.reader.basics.ChromosomeHandler;
import javastraw.reader.basics.ChromosomeTools;
import org.apache.commons.math3.ml.clustering.Cluster;
import org.apache.commons.math3.ml.clustering.DBSCANClusterer;

import java.io.File;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AnchorFix {

private static final int widthToConnect = 2;
public static String usage = "anchor-fix[-clean] [-r resolution] <genomeID> <input.bedpe> <output.stem>\n" +
"\t\tdefault behavior will fix the hi-res shared anchors for loops\n" +
"\t\tclean avoids saving old attributes";
private static int resolution = 100;
public static int MAX_DIST = 250;
public static int CLUSTER_DIST = 100;

public static void run(String[] args, CommandLineParser parser, String command) {
if (args.length != 4) {
Expand All @@ -48,7 +46,6 @@ private static void fixAnchors(String inputBedpe, String genomeID, String outSte
if (Main.printVerboseComments) System.out.println("Processing " + chrom.getName());
List<Feature2D> loops = loopList.get(chrom.getIndex(), chrom.getIndex());
if (loops.size() > 0) {
if (true) System.out.println("Processing " + chrom.getName());
List<Feature2D> newLoops = recoverLoops(loops);
output.addByKey(Feature2DList.getKey(chrom, chrom), newLoops);
}
Expand All @@ -64,9 +61,9 @@ private static List<Feature2D> recoverLoops(List<Feature2D> loops) {
allAnchorBins.addAll(upStreamAnchorBins);
allAnchorBins.addAll(downStreamAnchorBins);

List<Node95> upStreamNodes = getNodes(upStreamAnchorBins);
List<Node95> downStreamNodes = getNodes(downStreamAnchorBins);
List<Node95> allNodes = getNodes(allAnchorBins);
List<Node95> upStreamNodes = getNodes(upStreamAnchorBins, resolution);
List<Node95> downStreamNodes = getNodes(downStreamAnchorBins, resolution);
List<Node95> allNodes = getNodes(allAnchorBins, resolution);

return fixedList(loops, upStreamNodes, downStreamNodes, allNodes);
}
Expand Down Expand Up @@ -123,8 +120,8 @@ private static List<Long> getAllOfFeature(List<Feature2D> loops, String attribut
}


public static List<Node95> getNodes(List<Long> genomePositions) {
List<List<Long>> clusters = SimpleClustering.cluster(genomePositions, 100);
public static List<Node95> getNodes(List<Long> genomePositions, int resolution) {
List<List<Long>> clusters = SimpleClustering.cluster(genomePositions, resolution);
List<Long> pointsToReassign = new ArrayList<>(clusters.size() / 2);
List<List<Long>> clustersToKeep = new ArrayList<>(clusters.size() / 2);
for (List<Long> cluster : clusters) {
Expand Down Expand Up @@ -173,29 +170,6 @@ private static Node95 getNearestNode(long point, List<Node95> nodes, int maxDist
return null;
}

private static List<Cluster<Point1D>> dbscan1D(List<Long> points) {
DBSCANClusterer<Point1D> dbscan = new DBSCANClusterer<>(CLUSTER_DIST, 1);
return dbscan.cluster(convert(points));
}

private static Collection<Point1D> convert(List<Long> points) {
List<Point1D> converted = new ArrayList<>(points.size());
for (Long point : points) {
converted.add(new Point1D(point));
}
return converted;
}

private static int[] getCounts(Map<Integer, List<Long>> counts, int maxBin) {
int[] countsTrack = new int[maxBin];
for (int i = 0; i < maxBin; i++) {
if (counts.containsKey(i)) {
countsTrack[i] = counts.get(i).size();
}
}
return countsTrack;
}

public static Map<Long, Node95> buildPositionToNodeMapping(List<Node95> nodes) {
Map<Long, Node95> mapping = new HashMap<>();
for (Node95 node : nodes) {
Expand Down
28 changes: 20 additions & 8 deletions src/cli/clt/loops/MotifAssignment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class MotifAssignment {

public static String usage = "assign-motifs [--window val] <genomeID> <loops.bedpe> " +
public static String usage = "assign-motifs[-permissive] [--window val] <genomeID> <loops.bedpe> " +
"<upstream.motifs.bed> <downstream.motifs.bed> <output.bedpe>";
protected final int window;
private final ChromosomeHandler handler;
Expand All @@ -25,13 +25,15 @@ public class MotifAssignment {
private final String outFile;
private Map<Integer, Map<Integer, List<int[]>>> upBed;
private Map<Integer, Map<Integer, List<int[]>>> downBed;
private final boolean isPermissive;

public MotifAssignment(String[] args, CommandLineParser parser) {
public MotifAssignment(String[] args, CommandLineParser parser, String command) {
if (args.length != 6) {
System.out.println(usage);
System.exit(6);
}

isPermissive = command.contains("permissive");
window = parser.getWindowSizeOption(250);
binSize = 3 * window;
handler = ChromosomeTools.loadChromosomes(args[1]);
Expand All @@ -49,24 +51,34 @@ public MotifAssignment(String[] args, CommandLineParser parser) {

public void run() {
Feature2DList result = new Feature2DList();
int n = 0;
for (Chromosome chromosome : handler.getChromosomeArrayWithoutAllByAll()) {
List<Feature2D> chrLoops = loopList.get(chromosome.getIndex(), chromosome.getIndex());
List<Feature2D> loopsToSave = new ArrayList<>();
Map<Integer, List<int[]>> upMotifs = upBed.get(chromosome.getIndex());
Map<Integer, List<int[]>> downMotifs = downBed.get(chromosome.getIndex());

for (Feature2D loop : chrLoops) {
int[] upMotif = IndexedBedFile.getUniqueMotif(loop.getAttribute("localX"), upMotifs, binSize, window);
int[] downMotif = IndexedBedFile.getUniqueMotif(loop.getAttribute("localY"), downMotifs, binSize, window);
if (upMotif != null && downMotif != null) {
IndexedBedFile.setMotifAttributes(loop, upMotif, true);
IndexedBedFile.setMotifAttributes(loop, downMotif, false);
loopsToSave.add(loop);
try {
long q1 = Long.parseLong(loop.getAttribute("localX"));
long q2 = Long.parseLong(loop.getAttribute("localY"));
if (q1 > 0 && q2 > 0) {
n++;
int[] upMotif = IndexedBedFile.getUniqueMotif(q1, upMotifs, binSize, window, isPermissive);
int[] downMotif = IndexedBedFile.getUniqueMotif(q2, downMotifs, binSize, window, isPermissive);
if (upMotif != null && downMotif != null) {
IndexedBedFile.setMotifAttributes(loop, upMotif, true);
IndexedBedFile.setMotifAttributes(loop, downMotif, false);
loopsToSave.add(loop);
}
}
} catch (Exception ignored) {
}
}

result.addByKey(Feature2DList.getKey(chromosome, chromosome), loopsToSave);
}
System.out.println("Number of loops with localization: " + n);
result.exportFeatureList(new File(outFile), false, Feature2DList.ListFormat.NA);
}
}
25 changes: 20 additions & 5 deletions src/cli/utils/motifs/IndexedBedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,46 @@ public static Map<Integer, Map<Integer, List<int[]>>> index(String inputBedFile,
return bedMap;
}

public static int[] getUniqueMotif(String localPos, Map<Integer, List<int[]>> binnedMotifs,
int binSize, int window) {
public static int[] getUniqueMotif(long position, Map<Integer, List<int[]>> binnedMotifs,
int binSize, int window, boolean isPermissive) {
try {
long x = Long.parseLong(localPos);
int bin = (int) (x / binSize);
int bin = (int) (position / binSize);

List<int[]> motifs = new ArrayList<>();
for (int i = bin - 1; i < bin + 2; i++) {
if (binnedMotifs.containsKey(i)) {
for (int[] motif : binnedMotifs.get(i)) {
if (Math.abs(motif[MIDPT] - x) < window) {
if (Math.abs(motif[MIDPT] - position) < window) {
motifs.add(motif);
}
}
}
}
if (motifs.size() == 1) {
return motifs.get(0);
} else if (motifs.size() > 1 && isPermissive) {
return returnClosest(position, motifs);
}
} catch (Exception e) {
return null;
}
return null;
}

private static int[] returnClosest(long position, List<int[]> motifs) {
int[] closest = motifs.get(0);
long minDist = Math.abs(closest[MIDPT] - position);

for (int[] motif : motifs) {
long dist = Math.abs(motif[MIDPT] - position);
if (dist < minDist) {
minDist = dist;
closest = motif;
}
}
return closest;
}

public static void setMotifAttributes(Feature2D loop, int[] motif, boolean isUpStream) {
String startKey, endKey, midKey;
if (isUpStream) {
Expand Down

0 comments on commit ec78606

Please sign in to comment.