Skip to content

Commit e6261e5

Browse files
committed
rewrite GetNextReadBuffer
1 parent 1a1638f commit e6261e5

1 file changed

Lines changed: 50 additions & 165 deletions

File tree

PretextMap.cpp

Lines changed: 50 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ SOFTWARE.
2828
#include <math.h>
2929
#include <sys/stat.h>
3030
#include <errno.h>
31-
#include <zlib.h>
3231

3332
global_variable
3433
u08
@@ -515,13 +514,10 @@ ProcessBodyLine(void *in)
515514
{
516515
u64 nLinesTotal = __atomic_add_fetch(&Total_Reads_Processed, 1, 0);
517516

518-
// Store start of current line for error reporting
519-
u08 *lineStart = line;
520-
521517
// Skip readID
522518
while (*line && *line != '\t') line++;
523519
if (!*line) {
524-
PrintError("Malformed line - missing fields after readID at line %lu", nLinesTotal);
520+
PrintError("Malformed line - missing fields after readID");
525521
Global_Error_Flag = 1;
526522
return;
527523
}
@@ -531,7 +527,7 @@ ProcessBodyLine(void *in)
531527
u32 contigName1[16] = {0};
532528
line = PushStringIntoIntArray(contigName1, ArrayCount(contigName1), line, '\t');
533529
if (!*line) {
534-
PrintError("Malformed line - missing pos1 at line %lu", nLinesTotal);
530+
PrintError("Malformed line - missing pos1");
535531
Global_Error_Flag = 1;
536532
return;
537533
}
@@ -545,7 +541,7 @@ ProcessBodyLine(void *in)
545541
pos1Len++;
546542
}
547543
if (!pos1Len || line[pos1Len] != '\t') {
548-
PrintError("Malformed line - invalid pos1 at line %lu", nLinesTotal);
544+
PrintError("Malformed line - invalid pos1");
549545
Global_Error_Flag = 1;
550546
return;
551547
}
@@ -554,17 +550,18 @@ ProcessBodyLine(void *in)
554550
char *endPtr;
555551
errno = 0;
556552
u64 relPos1 = strtoull(pos1Str, &endPtr, 10);
557-
if (errno || (*endPtr != '\0' && *endPtr != '\t' && *endPtr != '\n' && *endPtr != '\r')) {
558-
PrintError("Invalid pos1 value: %s at line %lu", pos1Str, nLinesTotal);
559-
continue; // Skip this line instead of exiting
553+
if (errno || *endPtr != '\0') {
554+
PrintError("Invalid pos1 value: %s", pos1Str);
555+
Global_Error_Flag = 1;
556+
return;
560557
}
561558
line += pos1Len + 1; // Skip number and tab
562559

563560
// Parse chr2
564561
u32 contigName2[16] = {0};
565562
line = PushStringIntoIntArray(contigName2, ArrayCount(contigName2), line, '\t');
566563
if (!*line) {
567-
PrintError("Malformed line - missing pos2 at line %lu", nLinesTotal);
564+
PrintError("Malformed line - missing pos2");
568565
Global_Error_Flag = 1;
569566
return;
570567
}
@@ -578,42 +575,46 @@ ProcessBodyLine(void *in)
578575
pos2Len++;
579576
}
580577
if (!pos2Len) {
581-
PrintError("Malformed line - invalid pos2 at line %lu", nLinesTotal);
582-
continue; // Skip this line instead of exiting
578+
PrintError("Malformed line - invalid pos2");
579+
Global_Error_Flag = 1;
580+
return;
583581
}
584582
pos2Str[pos2Len] = '\0';
585583

586584
errno = 0;
587585
u64 relPos2 = strtoull(pos2Str, &endPtr, 10);
588-
if (errno || (*endPtr != '\0' && *endPtr != '\t' && *endPtr != '\n' && *endPtr != '\r')) {
589-
PrintError("Invalid pos2 value: %s at line %lu", pos2Str, nLinesTotal);
590-
continue; // Skip this line instead of exiting
586+
if (errno || (*endPtr != '\0' && *endPtr != '\t' && *endPtr != '\n')) {
587+
PrintError("Invalid pos2 value: %s", pos2Str);
588+
Global_Error_Flag = 1;
589+
return;
591590
}
592591

593-
// Skip remaining fields (strand1, strand2, and any additional fields) until end of line
592+
// Skip remaining fields (strand1, strand2)
594593
while (*line && *line != '\n') line++;
595594
if (*line == '\n') line++;
596595

597596
// Look up contigs and add to image
598597
contig *cont1 = 0;
599598
if (!ContigHashTableLookup(contigName1, ArrayCount(contigName1), &cont1)) {
600-
PrintError("Unknown contig: %.64s at line %lu", (char*)contigName1, nLinesTotal);
599+
if (*(char*)contigName1 != '!') // suppress error for '!' contig
600+
PrintError("Unknown contig: %.64s", (char*)contigName1);
601601
continue;
602602
}
603603

604604
contig *cont2 = 0;
605605
if (!ContigHashTableLookup(contigName2, ArrayCount(contigName2), &cont2)) {
606-
PrintError("Unknown contig: %.64s at line %lu", (char*)contigName2, nLinesTotal);
606+
if (*(char*)contigName2 != '!') // suppress error for '!' contig
607+
PrintError("Unknown contig: %.64s", (char*)contigName2);
607608
continue;
608609
}
609610

610611
// Validate positions
611-
if (relPos1 >= cont1->length) {
612-
PrintError("Position %lu exceeds contig length %lu for contig %.64s at line %lu", relPos1, cont1->length, (char*)contigName1, nLinesTotal);
612+
if (relPos1 > cont1->length) {
613+
PrintError("Position %lu exceeds contig length %lu", relPos1, cont1->length);
613614
continue;
614615
}
615-
if (relPos2 >= cont2->length) {
616-
PrintError("Position %lu exceeds contig length %lu for contig %.64s at line %lu", relPos2, cont2->length, (char*)contigName2, nLinesTotal);
616+
if (relPos2 > cont2->length) {
617+
PrintError("Position %lu exceeds contig length %lu", relPos2, cont2->length);
617618
continue;
618619
}
619620

@@ -1247,8 +1248,6 @@ read_pool
12471248
s32 handle;
12481249
u32 bufferPtr;
12491250
read_buffer *buffers[2];
1250-
gzFile gzHandle; // For gzip files
1251-
u08 isGzip; // Flag to indicate if file is gzip-compressed
12521251
};
12531252

12541253
global_function
@@ -1266,117 +1265,43 @@ CreateReadPool(memory_arena *arena)
12661265
pool->buffers[1] = PushStructP(arena, read_buffer);
12671266
pool->buffers[1]->buffer = PushArrayP(arena, u08, ReadBufferSize);
12681267
pool->buffers[1]->size = 0;
1269-
pool->gzHandle = 0;
1270-
pool->isGzip = 0;
12711268

12721269
return(pool);
12731270
}
12741271

12751272
global_function
1276-
read_buffer *
1277-
GetNextReadBuffer(read_pool *readPool)
1273+
void
1274+
FillReadBuffer(void *in)
12781275
{
1279-
FenceIn(ThreadPoolWait(readPool->pool));
1280-
read_buffer *buffer = readPool->buffers[readPool->bufferPtr];
1281-
1282-
ssize_t bytesRead = 0;
1283-
1284-
if (readPool->isGzip) {
1285-
// Handle gzip file
1286-
if (!readPool->gzHandle) {
1287-
PrintError("Gzip handle is null");
1288-
buffer->size = 0;
1289-
return buffer;
1290-
}
1291-
1292-
bytesRead = gzread(readPool->gzHandle, buffer->buffer, ReadBufferSize);
1293-
if (bytesRead < 0) {
1294-
PrintError("Failed to read from gzip file: %s", gzerror(readPool->gzHandle, 0));
1295-
buffer->size = 0;
1296-
} else {
1297-
buffer->size = (u64)bytesRead;
1298-
}
1299-
} else {
1300-
// Handle regular file
1301-
if (readPool->handle < 0) {
1302-
PrintError("Invalid file descriptor: %d", readPool->handle);
1303-
buffer->size = 0;
1304-
return buffer;
1305-
}
1306-
1307-
bytesRead = read(readPool->handle, buffer->buffer, ReadBufferSize);
1308-
if (bytesRead < 0) {
1309-
PrintError("Failed to read from input: %s (fd=%d)", strerror(errno), readPool->handle);
1310-
buffer->size = 0;
1311-
} else {
1312-
buffer->size = (u64)bytesRead;
1313-
}
1314-
}
1276+
read_pool *pool = (read_pool *)in;
1277+
read_buffer *buffer = pool->buffers[pool->bufferPtr];
13151278

1316-
// Debug first read
1317-
if (readPool->bufferPtr == 0) {
1318-
PrintStatus("First read: %ld bytes from %s", bytesRead, readPool->isGzip ? "gzip file" : "regular file");
1319-
if (bytesRead > 0) {
1320-
PrintStatus("First byte: 0x%02X (%c)",
1321-
buffer->buffer[0],
1322-
(buffer->buffer[0] >= 32 && buffer->buffer[0] <= 126) ? buffer->buffer[0] : '?');
1323-
}
1279+
// Check if file descriptor is valid
1280+
if (pool->handle < 0) {
1281+
PrintError("Invalid file descriptor: %d", pool->handle);
1282+
buffer->size = 0;
1283+
return;
13241284
}
1325-
1326-
// Switch buffers and read into the next one
1327-
readPool->bufferPtr = (readPool->bufferPtr + 1) & 1;
1328-
read_buffer *nextBuffer = readPool->buffers[readPool->bufferPtr];
1329-
1330-
// Read into the next buffer
1331-
if (readPool->isGzip) {
1332-
bytesRead = gzread(readPool->gzHandle, nextBuffer->buffer, ReadBufferSize);
1333-
if (bytesRead < 0) {
1334-
PrintError("Failed to read from gzip file: %s", gzerror(readPool->gzHandle, 0));
1335-
nextBuffer->size = 0;
1336-
} else {
1337-
nextBuffer->size = (u64)bytesRead;
1338-
}
1285+
1286+
// Try to read from the file descriptor
1287+
ssize_t bytesRead = read(pool->handle, buffer->buffer, ReadBufferSize);
1288+
if (bytesRead < 0) {
1289+
PrintError("Failed to read from input: %s", strerror(errno));
1290+
buffer->size = 0;
13391291
} else {
1340-
bytesRead = read(readPool->handle, nextBuffer->buffer, ReadBufferSize);
1341-
if (bytesRead < 0) {
1342-
PrintError("Failed to read from input: %s (fd=%d)", strerror(errno), readPool->handle);
1343-
nextBuffer->size = 0;
1344-
} else {
1345-
nextBuffer->size = (u64)bytesRead;
1346-
}
1292+
buffer->size = (u64)bytesRead;
13471293
}
1348-
1349-
return buffer;
13501294
}
13511295

13521296
global_function
1353-
void
1354-
FillReadBuffer(void *in)
1297+
read_buffer *
1298+
GetNextReadBuffer(read_pool *readPool)
13551299
{
1356-
read_pool *pool = (read_pool *)in;
1357-
read_buffer *buffer = pool->buffers[pool->bufferPtr];
1358-
1359-
ssize_t bytesRead = 0;
1360-
1361-
if (pool->isGzip) {
1362-
// Handle gzip file
1363-
bytesRead = gzread(pool->gzHandle, buffer->buffer, ReadBufferSize);
1364-
if (bytesRead < 0) {
1365-
PrintError("Failed to read from gzip file: %s", gzerror(pool->gzHandle, 0));
1366-
buffer->size = 0;
1367-
} else {
1368-
buffer->size = (u64)bytesRead;
1369-
}
1370-
} else {
1371-
// Handle regular file
1372-
bytesRead = read(pool->handle, buffer->buffer, ReadBufferSize);
1373-
if (bytesRead < 0) {
1374-
PrintError("Failed to read from input: %s", strerror(errno));
1375-
buffer->size = 0;
1376-
} else {
1377-
buffer->size = (u64)bytesRead;
1378-
}
1379-
}
1300+
FenceIn(ThreadPoolWait(readPool->pool));
1301+
read_buffer *buffer = readPool->buffers[readPool->bufferPtr];
1302+
readPool->bufferPtr = (readPool->bufferPtr + 1) & 1;
1303+
ThreadPoolAddTask(readPool->pool, FillReadBuffer, readPool);
1304+
return buffer;
13801305
}
13811306

13821307
#define SAM_LINE_BUFFER_SIZE KiloByte(16)
@@ -1413,32 +1338,6 @@ GrabStdIn()
14131338
readPool->handle = STDIN_FILENO;
14141339
#endif
14151340

1416-
// Check if input is gzip-compressed by reading first few bytes
1417-
u08 magic[2];
1418-
ssize_t bytesRead = read(readPool->handle, magic, 2);
1419-
if (bytesRead == 2 && magic[0] == 0x1F && magic[1] == 0x8B) {
1420-
// This is a gzip file
1421-
PrintStatus("Detected gzip-compressed input");
1422-
readPool->isGzip = 1;
1423-
1424-
// Reset file position to beginning for gzip
1425-
lseek(readPool->handle, 0, SEEK_SET);
1426-
1427-
// Create gzip handle from the file descriptor
1428-
readPool->gzHandle = gzdopen(readPool->handle, "rb");
1429-
if (!readPool->gzHandle) {
1430-
PrintError("Failed to open gzip file: %s", strerror(errno));
1431-
Global_Error_Flag = 1;
1432-
return;
1433-
}
1434-
} else {
1435-
// Regular file, reset position
1436-
if (bytesRead > 0) {
1437-
lseek(readPool->handle, 0, SEEK_SET);
1438-
}
1439-
readPool->isGzip = 0;
1440-
}
1441-
14421341
// Debug stdin status
14431342
struct stat st;
14441343
if (fstat(readPool->handle, &st) == 0) {
@@ -1468,14 +1367,15 @@ GrabStdIn()
14681367

14691368
// Try to read first buffer
14701369
readBuffer = GetNextReadBuffer(readPool);
1370+
/***
14711371
if (!readBuffer || readBuffer->size == 0) {
14721372
PrintError("No input data received");
14731373
Global_Error_Flag = 1;
14741374
return;
14751375
}
14761376
14771377
PrintStatus("Initial buffer size: %lu bytes", readBuffer->size);
1478-
1378+
**/
14791379
do
14801380
{
14811381
if (!readBuffer || readBuffer->size == 0) {
@@ -1580,12 +1480,6 @@ GrabStdIn()
15801480
File_Type = pairs;
15811481
PrintStatus("Detected pairs format input v1.0");
15821482
}
1583-
else if (strncmp((char*)samLine, "#columns:", 9) == 0)
1584-
{
1585-
// If we see a columns line, assume it's pairs format
1586-
File_Type = pairs;
1587-
PrintStatus("Detected pairs format input (via columns line)");
1588-
}
15891483
}
15901484
else
15911485
{
@@ -1680,15 +1574,6 @@ GrabStdIn()
16801574
}
16811575

16821576
readBuffer = GetNextReadBuffer(readPool);
1683-
1684-
// For gzip files, check if we've reached end of file
1685-
if (readPool->isGzip && readBuffer && readBuffer->size == 0) {
1686-
// Check if gzip file is at end
1687-
if (gzeof(readPool->gzHandle)) {
1688-
PrintStatus("Reached end of gzip file");
1689-
break;
1690-
}
1691-
}
16921577
} while (readBuffer && readBuffer->size);
16931578

16941579
// Process any remaining lines
@@ -2646,4 +2531,4 @@ MainArgs
26462531
fclose(Output_File);
26472532

26482533
EndMain;
2649-
}
2534+
}

0 commit comments

Comments
 (0)