我正在尝试将samtools
的使用集成到C程序中。此application以称为BAM的二进制格式读取数据,例如来自stdin
:
$ cat foo.bam | samtools view -h -
...
(我意识到这是对
cat
的无用用法,但我只是说明如何在命令行上将BAM文件的字节通过管道传输到samtools
。这些字节可能来自其他上游进程。)在C程序中,我想将
unsigned char
个字节的块写入samtools
二进制文件,同时在处理完这些字节之后从samtools
捕获标准输出。因为我不能使用
popen()
同时写入和读取进程,所以我研究了使用popen2()
的公开实现,该实现似乎是为了支持这一点。我编写了以下测试代码,该代码尝试将位于同一目录中的BAM文件的
write()
4 kB块字节发送给samtools
进程。然后,它从read()
的输出中samtools
s个字节进入行缓冲区,打印为标准错误:#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define READ 0
#define WRITE 1
pid_t popen2(const char *command, int *infp, int *outfp)
{
int p_stdin[2], p_stdout[2];
pid_t pid;
if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
return -1;
pid = fork();
if (pid < 0)
return pid;
else if (pid == 0)
{
close(p_stdin[WRITE]);
dup2(p_stdin[READ], READ);
close(p_stdout[READ]);
dup2(p_stdout[WRITE], WRITE);
execl("/bin/sh", "sh", "-c", command, NULL);
perror("execl");
exit(1);
}
if (infp == NULL)
close(p_stdin[WRITE]);
else
*infp = p_stdin[WRITE];
if (outfp == NULL)
close(p_stdout[READ]);
else
*outfp = p_stdout[READ];
return pid;
}
int main(int argc, char **argv)
{
int infp, outfp;
/* set up samtools to read from stdin */
if (popen2("samtools view -h -", &infp, &outfp) <= 0) {
printf("Unable to exec samtools\n");
exit(1);
}
const char *fn = "foo.bam";
FILE *fp = NULL;
fp = fopen(fn, "r");
if (!fp)
exit(-1);
unsigned char buf[4096];
char line_buf[65536] = {0};
while(1) {
size_t n_bytes = fread(buf, sizeof(buf[0]), sizeof(buf), fp);
fprintf(stderr, "read\t-> %08zu bytes from fp\n", n_bytes);
write(infp, buf, n_bytes);
fprintf(stderr, "wrote\t-> %08zu bytes to samtools process\n", n_bytes);
read(outfp, line_buf, sizeof(line_buf));
fprintf(stderr, "output\t-> \n%s\n", line_buf);
memset(line_buf, '\0', sizeof(line_buf));
if (feof(fp) || ferror(fp)) {
break;
}
}
return 0;
}
(对于
foo.bam
的本地副本,这是我用于测试的二进制文件的link。但是任何BAM文件都可以用于测试目的。)编译:
$ cc -Wall test_bam.c -o test_bam
问题是该过程在
write()
调用后挂起:$ ./test_bam
read -> 00004096 bytes from fp
wrote -> 00004096 bytes to samtools process
[bam_header_read] EOF marker is absent. The input is probably truncated.
如果在调用
close()
之后立即infp
变量write()
,则循环将在挂起之前再进行一次迭代:...
write(infp, buf, n_bytes);
close(infp); /* <---------- added after the write() call */
fprintf(stderr, "wrote\t-> %08zu bytes to samtools process\n", n_bytes);
...
用
close()
语句:$ ./test_bam
read -> 00004096 bytes from fp
wrote -> 00004096 bytes to samtools process
[bam_header_read] EOF marker is absent. The input is probably truncated.
[main_samview] truncated file.
output ->
@HD VN:1.0 SO:coordinate
@SQ SN:seq1 LN:5000
@SQ SN:seq2 LN:5000
@CO Example of SAM/BAM file format.
read -> 00004096 bytes from fp
wrote -> 00004096 bytes to samtools process
进行此更改后,如果在命令行上运行
samtools
,我会得到一些期望得到的输出,但是如上所述,该过程再次挂起。如何使用
popen2()
将大块数据写入和读取到内部缓冲区?如果这不可能,那么是否有popen2()
的替代方法可以更好地完成此任务? 最佳答案
作为pipe
的替代方法,为什么不通过samtools
与socket
通信?检查samtools
源,文件knetfile.c
表示samtools
具有可用的套接字通信:
#include "knetfile.h"
/* In winsock.h, the type of a socket is SOCKET, which is: "typedef
* u_int SOCKET". An invalid SOCKET is: "(SOCKET)(~0)", or signed
* integer -1. In knetfile.c, I use "int" for socket type
* throughout. This should be improved to avoid confusion.
*
* In Linux/Mac, recv() and read() do almost the same thing. You can see
* in the header file that netread() is simply an alias of read(). In
* Windows, however, they are different and using recv() is mandatory.
*/
这可能比使用
pipe2
提供更好的选择。关于c - 为什么popen2()在写和读调用之间挂起?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24316460/