File idxFile, int tileWidth) throws
IOException {
FileInputStream is = new FileInputStream(samFile);
InputStream bis = new BufferedInputStream(is);
AsciiLineReader reader = new AsciiLineReader(bis);
long fileLength = samFile.length();
long progressIncrement = fileLength / 100;
long lastFilePosition = 0;
String lastChr = null;
int lastAlignmentStart = 0;
FeatureIndex featureIndex = new FeatureIndex(tileWidth);
int recordCount = 0;
long filePosition = 0;
int currentTile = 0;
int longestFeature = 0;
long startTime = System.currentTimeMillis();
int progressCounter = 1; // progress in %
String nextLine = "";
int lineNumber = 0;
while ((nextLine = reader.readLine()) != null) {
lineNumber++;
if (worker != null && worker.isCancelled()) {
return null;
}
//int nBytes = nextLine.length();
nextLine = nextLine.trim();
String[] fields = Globals.tabPattern.split(nextLine, -1);
int nFields = fields.length;
if (!nextLine.startsWith("@") && nFields > 3 && isMapped(fields)) {
String chr = getChromosome(fields);
int alignmentStart = getAlignmentStart(fields);
int tileNumber = alignmentStart / tileWidth;
if (lastChr == null) {
// First record
currentTile = tileNumber;
for (int i = 0; i < currentTile; i++) {
featureIndex.add(chr, lastFilePosition, 0, longestFeature);
}
lastChr = chr;
} else if (!chr.equals(lastChr)) { // New chromosome
featureIndex.add(lastChr, filePosition, recordCount, longestFeature);
filePosition = lastFilePosition;
currentTile = 0;
recordCount = 0;
lastAlignmentStart = 0;
longestFeature = 0;
lastChr = chr;
} else {
longestFeature = Math.max(longestFeature, getAlignmentLength(fields));
if (alignmentStart < 0) {
System.out.println("Warning: negative start position at line: " + lineNumber + " : " + nextLine);
continue;
}
if (alignmentStart < lastAlignmentStart) {
throw new UnsortedFileException(" File must be sorted by start position. " +
"Sort test failed at: " + nextLine);
}
lastAlignmentStart = alignmentStart;
if (tileNumber > currentTile) {
// We have crossed a tile boundary. Record index and counts for previous tile
featureIndex.add(lastChr, filePosition, recordCount, longestFeature);
// If tiles were skipped record zero counts for these.
for (int cnt = 0; cnt < (tileNumber - currentTile - 1); cnt++) {
featureIndex.add(lastChr, filePosition, 0, longestFeature);
}
filePosition = lastFilePosition;
currentTile = tileNumber;
recordCount = 0;
}
recordCount++;
}
}
lastFilePosition = reader.getPosition();
if (lastFilePosition > (progressCounter * progressIncrement)) {
updateProgress(progressCounter, startTime);
progressCounter++;