Skip to content

Commit

Permalink
Cell groups
Browse files Browse the repository at this point in the history
  • Loading branch information
harmbrugge committed Nov 11, 2016
1 parent 35573ab commit 35094f7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.harmbrugge.bamtools'
version '1.0-SNAPSHOT'
version '1.0'

apply plugin: 'java'

Expand Down
32 changes: 24 additions & 8 deletions src/main/java/com.harmbrugge.bamtools/BamFileSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* BamFileSplitter splits a BAM files generated in cell-ranger pipeline from 10xGenomics into a file per singe cell.
Expand All @@ -28,12 +27,14 @@ public class BamFileSplitter {

private static final String FILE_NAME_PREFIX = "cell_";
private static final String EXTENSION = ".bam";
private static final int GROUP_SIZE = 50;

private final Log logger = LogFactory.getLog(this.getClass());
private final Path pathToBarcodeFile;
private final String outputDir;

private int fileCount;
private int groupCount;
private int recordCount;
private int absentBarcodeCount;
private int invalidBarcodeCount;
Expand All @@ -52,7 +53,7 @@ public BamFileSplitter(Path pathToBamFile, Path pathToBarcodeFile, Path outputPa
if (outputPath == null) outputDir = pathToBamFile.getParent() + "/output/";
else outputDir = outputPath.toString();

File file = new File(outputDir);
File file = new File(outputDir + "/0/");
if (!file.exists()) file.mkdirs();

this.pathToBarcodeFile = pathToBarcodeFile;
Expand Down Expand Up @@ -136,24 +137,39 @@ private boolean isMultimapped(SAMRecord samRecord) {
private void addRecordToBam(String barcode, SAMRecord samRecord) {
SAMFileWriter outputBam = outputBams.get(barcode);

List<SAMReadGroupRecord> readGroup = outputBam.getFileHeader().getReadGroups();
if (readGroup.size() > 0) {
String readGroupId = readGroup.get(0).getId();
samRecord.setAttribute("RG", readGroupId);
}

outputBam.addAlignment(samRecord);
}

private File createBamFile(String barcode) {
fileCount++;

String filePath = outputDir + "/" + FILE_NAME_PREFIX + fileCount + "_" + barcode + EXTENSION;
if (fileCount % GROUP_SIZE == 0) {
groupCount++;
File file = new File(outputDir + "/" + groupCount);
if (!file.exists()) file.mkdirs();
}

String filePath = outputDir + "/" + groupCount + "/" + FILE_NAME_PREFIX + fileCount + "_" + barcode + EXTENSION;
return new File(filePath);
}

private SAMFileHeader createBamHeader(String barcode) {
SAMFileHeader samHeader = samReader.getFileHeader();

SAMReadGroupRecord readGroup = new SAMReadGroupRecord(barcode);
readGroup.setSample("cell_" + fileCount);
readGroup.setPlatform("Chromium");
SAMReadGroupRecord readGroup = new SAMReadGroupRecord(String.valueOf(fileCount));
readGroup.setSample("cell_" + fileCount + "_" + barcode);
readGroup.setPlatform("ILLUMINA");

List<SAMReadGroupRecord> readGroups = new ArrayList<>();
readGroups.add(readGroup);

samHeader.addReadGroup(readGroup);
samHeader.setReadGroups(readGroups);

return samHeader;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com.harmbrugge.bamtools/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private void start(String[] args) {

if (outputPath == null) outputPath = Paths.get(System.getProperty("user.dir"));

sampleSheetCreator.createSamplesheet(outputPath);
sampleSheetCreator.create(outputPath);
}
}
catch (ParseException exp) {
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com.harmbrugge.bamtools/SampleSheetCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ public class SampleSheetCreator {

private static final String SAMPLE_SHEET_FILENAME = "samplesheet.csv";

private static final int GROUP_SIZE = 50;

private int fileCount;
private int groupCount;

private final Path pathToBarcodeFile;

public SampleSheetCreator(Path pathToBarcodeFile) {
this.pathToBarcodeFile = pathToBarcodeFile;
}

public void createSamplesheet(Path outputDir) throws IOException {
public void create(Path outputDir) throws IOException {

outputDir.toFile().mkdirs();
File outputFile = new File(outputDir.toString(), SAMPLE_SHEET_FILENAME);
Expand All @@ -36,16 +39,24 @@ public void createSamplesheet(Path outputDir) throws IOException {
// project,cellId,bamFile
FileWriter fileWriter = new FileWriter(outputFile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write("project,cellId,bamFile\n");
bw.write("project,cellId,bamFile,cellGroup\n");

try (BufferedReader br = new BufferedReader(new FileReader(pathToBarcodeFile.toFile()))) {
String barcode = br.readLine();

while (barcode != null) {
fileCount++;
String filePath = "${splitBamDir}/" + FILENAME_PREFIX + fileCount + "_" + barcode + EXTENSION;
bw.write(PROJECT_NAME + "," + FILENAME_PREFIX + fileCount + "_" + barcode + "," + filePath);
bw.write("\n");

if (fileCount % GROUP_SIZE == 0) {
groupCount++;
}

String cellId = FILENAME_PREFIX + fileCount + "_" + barcode;
String filePath = groupCount + "/" + cellId + EXTENSION;
bw.write(PROJECT_NAME + ",");
bw.write(cellId + ",");
bw.write(filePath + ",");
bw.write(groupCount + "\n");

barcode = br.readLine();
}
Expand Down

0 comments on commit 35094f7

Please sign in to comment.