Skip to content

Commit

Permalink
bigbed / bigwig simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobinso committed Oct 4, 2023
1 parent bc6b418 commit 0e7341f
Show file tree
Hide file tree
Showing 56 changed files with 1,329 additions and 7,316 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/broad/igv/bbfile/BBBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.broad.igv.bbfile;

class BBBlock {
long offset;
long size;

public BBBlock(long offset, long size) {
this.offset = offset;
this.size = size;
}
}
76 changes: 76 additions & 0 deletions src/main/java/org/broad/igv/bbfile/BBChromTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.broad.igv.bbfile;

import org.broad.igv.util.ByteBufferUnsigned;

import java.util.HashMap;
import java.util.Map;


/**
* Parser for BB chromosome tree - a B+ tree of chromosomes, their sizes, and a unique ID for each.
*/
public class BBChromTree {

private final int keySize;
private final int valSize;
private final long startOffset;

Map<String, Integer> nameToIdMap;
Map<Integer, String> idToNameMap;
Map<String, Long> nameToSizeMap;

BBChromTree(ByteBufferUnsigned bb, long startOffset) {

int magic = bb.getInt();
boolean legit = magic == 0x78CA8C91;
int blockSize = bb.getInt();
this.keySize = bb.getInt();
this.valSize = bb.getInt();
long itemCount = bb.getLong();
long reserved = bb.getLong();

this.nameToIdMap = new HashMap<>();
this.idToNameMap = new HashMap<>();
this.nameToSizeMap = new HashMap<>();

// Kick off the recursive walk of the tree
this.startOffset = startOffset;
this.readTreeNode(-1, bb);
}

void readTreeNode(int offset, ByteBufferUnsigned bb) {

if (offset >= 0) bb.position(offset);
byte type = bb.get();
byte reserved = bb.get();
int count = bb.getUShort();

if (type == 1) {
// Leaf node -- supplemental table 10
for (int i = 0; i < count; i++) {
String name = bb.getFixedLengthString(keySize);
if (valSize == 8) {
int id = bb.getInt();
long chromSize = bb.getUInt();
//if (genome) key = genome.getChromosomeName(key) // Translate to canonical chr name
this.nameToIdMap.put(name, id);
this.idToNameMap.put(id, name);
this.nameToSizeMap.put(name, chromSize);

} else {
throw new RuntimeException("Unexpected 'valSize' in chromosome tree. Expected 8, actual value is" + valSize);
}
}
} else {
// non leave node - supplemental table 12
for (int i = 0; i < count; i++) {
String key = bb.getFixedLengthString(keySize);
long childOffset = bb.getLong();
int bufferOffset = (int) (childOffset - startOffset);
int currOffset = bb.position();
readTreeNode(bufferOffset, bb);
bb.position(currOffset);
}
}
}
}
Loading

0 comments on commit 0e7341f

Please sign in to comment.