|
| 1 | +package cli.clt.bedpe; |
| 2 | + |
| 3 | +import cli.Main; |
| 4 | +import cli.clt.CommandLineParser; |
| 5 | +import cli.utils.clique.Node95; |
| 6 | +import cli.utils.clique.SimpleClustering; |
| 7 | +import cli.utils.peaks.Point1D; |
| 8 | +import javastraw.feature2D.Feature2D; |
| 9 | +import javastraw.feature2D.Feature2DList; |
| 10 | +import javastraw.feature2D.Feature2DParser; |
| 11 | +import javastraw.reader.basics.Chromosome; |
| 12 | +import javastraw.reader.basics.ChromosomeHandler; |
| 13 | +import javastraw.reader.basics.ChromosomeTools; |
| 14 | +import org.apache.commons.math3.ml.clustering.Cluster; |
| 15 | +import org.apache.commons.math3.ml.clustering.DBSCANClusterer; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.util.*; |
| 19 | + |
| 20 | +public class AnchorFix { |
| 21 | + |
| 22 | + private static final int widthToConnect = 2; |
| 23 | + public static String usage = "anchor-fix[-clean] [-r resolution] <genomeID> <input.bedpe> <output.stem>\n" + |
| 24 | + "\t\tdefault behavior will fix the hi-res shared anchors for loops\n" + |
| 25 | + "\t\tclean avoids saving old attributes"; |
| 26 | + private static int resolution = 100; |
| 27 | + public static int MAX_DIST = 250; |
| 28 | + public static int CLUSTER_DIST = 100; |
| 29 | + |
| 30 | + public static void run(String[] args, CommandLineParser parser, String command) { |
| 31 | + if (args.length != 4) { |
| 32 | + Main.printGeneralUsageAndExit(57, usage); |
| 33 | + } |
| 34 | + resolution = parser.getResolutionOption(resolution); |
| 35 | + String genomeID = args[1]; |
| 36 | + String inFile = args[2]; |
| 37 | + String outStem = args[3]; |
| 38 | + fixAnchors(inFile, genomeID, outStem, command.contains("clean")); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + private static void fixAnchors(String inputBedpe, String genomeID, String outStem, boolean noAttributes) { |
| 43 | + Feature2DList output = new Feature2DList(); |
| 44 | + ChromosomeHandler handler = ChromosomeTools.loadChromosomes(genomeID); |
| 45 | + Feature2DList loopList = Feature2DParser.loadFeatures(inputBedpe, handler, !noAttributes, |
| 46 | + null, false); |
| 47 | + for (Chromosome chrom : handler.getChromosomeArrayWithoutAllByAll()) { |
| 48 | + if (Main.printVerboseComments) System.out.println("Processing " + chrom.getName()); |
| 49 | + List<Feature2D> loops = loopList.get(chrom.getIndex(), chrom.getIndex()); |
| 50 | + if (loops.size() > 0) { |
| 51 | + if (true) System.out.println("Processing " + chrom.getName()); |
| 52 | + List<Feature2D> newLoops = recoverLoops(loops); |
| 53 | + output.addByKey(Feature2DList.getKey(chrom, chrom), newLoops); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + output.exportFeatureList(new File(outStem + ".fixed.anchors.bedpe"), false, Feature2DList.ListFormat.NA); |
| 58 | + } |
| 59 | + |
| 60 | + private static List<Feature2D> recoverLoops(List<Feature2D> loops) { |
| 61 | + List<Long> upStreamAnchorBins = getAllOfFeature(loops, "localX"); |
| 62 | + List<Long> downStreamAnchorBins = getAllOfFeature(loops, "localY"); |
| 63 | + List<Long> allAnchorBins = new ArrayList<>(2 * loops.size()); |
| 64 | + allAnchorBins.addAll(upStreamAnchorBins); |
| 65 | + allAnchorBins.addAll(downStreamAnchorBins); |
| 66 | + |
| 67 | + List<Node95> upStreamNodes = getNodes(upStreamAnchorBins); |
| 68 | + List<Node95> downStreamNodes = getNodes(downStreamAnchorBins); |
| 69 | + List<Node95> allNodes = getNodes(allAnchorBins); |
| 70 | + |
| 71 | + return fixedList(loops, upStreamNodes, downStreamNodes, allNodes); |
| 72 | + } |
| 73 | + |
| 74 | + private static List<Feature2D> fixedList(List<Feature2D> loops, List<Node95> upStreamNodes, |
| 75 | + List<Node95> downStreamNodes, List<Node95> allNodes) { |
| 76 | + Map<Long, Node95> upStreamBinToNode = buildPositionToNodeMapping(upStreamNodes); |
| 77 | + Map<Long, Node95> downStreamBinToNode = buildPositionToNodeMapping(downStreamNodes); |
| 78 | + Map<Long, Node95> anyBinToNode = buildPositionToNodeMapping(allNodes); |
| 79 | + |
| 80 | + for (Feature2D loop : loops) { |
| 81 | + long x = Long.parseLong(loop.getAttribute("localX")); |
| 82 | + long y = Long.parseLong(loop.getAttribute("localY")); |
| 83 | + Node95 xNodeUp = upStreamBinToNode.get(x); |
| 84 | + Node95 yNodeDown = downStreamBinToNode.get(y); |
| 85 | + Node95 xNodeAny = anyBinToNode.get(x); |
| 86 | + Node95 yNodeAny = anyBinToNode.get(y); |
| 87 | + if (xNodeUp == null || yNodeDown == null || xNodeAny == null || yNodeAny == null) { |
| 88 | + System.err.println("Error: null node : " + x + " " + y + "\n " + xNodeUp + "\n" + yNodeDown + "\n" + xNodeAny + "\n" + yNodeAny); |
| 89 | + } |
| 90 | + setAttributes(loop, xNodeUp, yNodeDown, xNodeAny, yNodeAny); |
| 91 | + } |
| 92 | + return loops; |
| 93 | + } |
| 94 | + |
| 95 | + private static void setAttributes(Feature2D loop, Node95 xNodeUp, Node95 yNodeDown, |
| 96 | + Node95 xNodeAny, Node95 yNodeAny) { |
| 97 | + if (xNodeUp == null || yNodeDown == null || xNodeAny == null || yNodeAny == null) { |
| 98 | + //System.err.println("Error: null node\n "+xNodeUp+"\n"+yNodeDown+"\n"+xNodeAny+"\n"+yNodeAny); |
| 99 | + } else { |
| 100 | + setAttributes(loop, xNodeUp, "upstream_start_1", "upstream_end_1"); |
| 101 | + setAttributes(loop, yNodeDown, "downstream_start_2", "downstream_end_2"); |
| 102 | + setAttributes(loop, xNodeAny, "highRes_start_1", "highRes_end_1"); |
| 103 | + setAttributes(loop, yNodeAny, "highRes_start_2", "highRes_end_2"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static void setAttributes(Feature2D loop, Node95 node, String startName, String endName) { |
| 108 | + if (node == null) { |
| 109 | + System.err.println("Node is null _ " + startName + " - " + endName); |
| 110 | + } |
| 111 | + long[] bounds = node.getBounds95(); |
| 112 | + loop.setAttribute(startName, String.valueOf(bounds[0])); |
| 113 | + loop.setAttribute(endName, String.valueOf(bounds[1])); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + private static List<Long> getAllOfFeature(List<Feature2D> loops, String attribute) { |
| 118 | + List<Long> positions = new ArrayList<>(loops.size()); |
| 119 | + for (Feature2D feature : loops) { |
| 120 | + positions.add(Long.parseLong(feature.getAttribute(attribute))); |
| 121 | + } |
| 122 | + return positions; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + public static List<Node95> getNodes(List<Long> genomePositions) { |
| 127 | + List<List<Long>> clusters = SimpleClustering.cluster(genomePositions, 100); |
| 128 | + List<Long> pointsToReassign = new ArrayList<>(clusters.size() / 2); |
| 129 | + List<List<Long>> clustersToKeep = new ArrayList<>(clusters.size() / 2); |
| 130 | + for (List<Long> cluster : clusters) { |
| 131 | + if (cluster.size() > 1) { |
| 132 | + clustersToKeep.add(cluster); |
| 133 | + } else { |
| 134 | + pointsToReassign.add(cluster.get(0)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + List<Node95> cleanedUpClusters = assignToNearestClusterOrMakeSingleton(clustersToKeep, pointsToReassign); |
| 139 | + clustersToKeep.clear(); |
| 140 | + pointsToReassign.clear(); |
| 141 | + return cleanedUpClusters; |
| 142 | + } |
| 143 | + |
| 144 | + private static List<Node95> assignToNearestClusterOrMakeSingleton(List<List<Long>> clustersToKeep, List<Long> pointsToReassign) { |
| 145 | + List<Node95> nodes = Node95.convert(clustersToKeep); |
| 146 | + for (Node95 node : nodes) { |
| 147 | + node.calculate95(); |
| 148 | + } |
| 149 | + for (long point : pointsToReassign) { |
| 150 | + Node95 nearest = getNearestNode(point, nodes, MAX_DIST); |
| 151 | + if (nearest == null) { |
| 152 | + nodes.add(new Node95(point, true)); |
| 153 | + } else { |
| 154 | + nearest.addWeak(point); |
| 155 | + } |
| 156 | + } |
| 157 | + return nodes; |
| 158 | + } |
| 159 | + |
| 160 | + private static Node95 getNearestNode(long point, List<Node95> nodes, int maxDist) { |
| 161 | + double currDist = Double.MAX_VALUE; |
| 162 | + Node95 nearest = null; |
| 163 | + for (Node95 node : nodes) { |
| 164 | + double dist = Math.abs(node.getMu() - point); |
| 165 | + if (dist < currDist) { |
| 166 | + currDist = dist; |
| 167 | + nearest = node; |
| 168 | + } |
| 169 | + } |
| 170 | + if (currDist < maxDist) { |
| 171 | + return nearest; |
| 172 | + } |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + private static List<Cluster<Point1D>> dbscan1D(List<Long> points) { |
| 177 | + DBSCANClusterer<Point1D> dbscan = new DBSCANClusterer<>(CLUSTER_DIST, 1); |
| 178 | + return dbscan.cluster(convert(points)); |
| 179 | + } |
| 180 | + |
| 181 | + private static Collection<Point1D> convert(List<Long> points) { |
| 182 | + List<Point1D> converted = new ArrayList<>(points.size()); |
| 183 | + for (Long point : points) { |
| 184 | + converted.add(new Point1D(point)); |
| 185 | + } |
| 186 | + return converted; |
| 187 | + } |
| 188 | + |
| 189 | + private static int[] getCounts(Map<Integer, List<Long>> counts, int maxBin) { |
| 190 | + int[] countsTrack = new int[maxBin]; |
| 191 | + for (int i = 0; i < maxBin; i++) { |
| 192 | + if (counts.containsKey(i)) { |
| 193 | + countsTrack[i] = counts.get(i).size(); |
| 194 | + } |
| 195 | + } |
| 196 | + return countsTrack; |
| 197 | + } |
| 198 | + |
| 199 | + public static Map<Long, Node95> buildPositionToNodeMapping(List<Node95> nodes) { |
| 200 | + Map<Long, Node95> mapping = new HashMap<>(); |
| 201 | + for (Node95 node : nodes) { |
| 202 | + for (long val : node.getPositions()) { |
| 203 | + mapping.put(val, node); |
| 204 | + } |
| 205 | + for (long val : node.getWeakPositions()) { |
| 206 | + mapping.put(val, node); |
| 207 | + } |
| 208 | + } |
| 209 | + return mapping; |
| 210 | + } |
| 211 | +} |
0 commit comments