python 转dll_单行数据转fa

bcff9de5d98d95616a3c699622284863.png

Kim

读完需要

4分钟

速读仅需 2 分钟

/ 单行数据转 fa /

虽然已经慢慢淡出生信领域,慢慢渗透到 IT, 看到洲更和阿秭发这种让人手痒的问题, 就忍不住在写完一堆自己都想吐的逻辑实现后抓住这个小问题练习一下,避免手生, 也算是给摸了这么长时间的鱼一点安慰吧。

1

   

可能是最简单的方式

这是非常适合萌新上手的练习,如果你还没有 linux 也没关系, git for windows 提供了 常用的 gnu 工具包括sed awk grep 三剑客

cat ./temp|awk -F '\t' '{print ">"$1"|"$2"|"$3"\n"$4}' >> ./a.txt

python 转dll_单行数据转fa_第1张图片

2

   

Python 的方式

with open("./temp","r") as fi, open("./b.txt","w") as fo:    for line in fi:        tmp = line.split("\t")        print(">"+'|'.join(tmp[:3])+"\n"+tmp[3],file=fo)

3

   

Python 进阶训练

除了常见单行序列的 fa, 折行序列的 fa 有时也会出现,那我们如何生成折行的 fa 呢?以 100nt 为例:

with open("./temp","r") as fi, open("./c.txt","w") as fo:    for line in fi:        tmp = line.split("\t")        print(">"+'|'.join(tmp[:3]),file=fo)        for i in range(len(tmp[3])//100):            print(tmp[3][i*100:(i+1)*100],file=fo)        print(tmp[3][(len(tmp[3])//100)*100:],file=fo)

4

   

在 Python 中引用C

c 代码

#include #include #include typedef struct {    char name[256];    char nick1[256];    char nick2[256];    char seq[2048];} fasta;char fmtfa(char *iname,char *oname){    FILE *fi;    FILE *fo;    char line[4096];     char *arr;    fi = fopen(iname,"r");    fo = fopen(oname,"a");    if (fi==NULL | fo== NULL){        return 1;    }    fasta *fa = (fasta  *)malloc(sizeof(fasta));    while(fgets(line, sizeof(line), fi)!= NULL)	{        sscanf(line,"%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t",fa->name,fa->nick1,fa->nick2,fa->seq);		fprintf(fo,">%s|%s|%s\n%s\n",fa->name,fa->nick1,fa->nick2,fa->seq);	}	fclose(fi);    fclose(fo);};int main(){    fmtfa("./temp","./d.txt");}

将 c 编译成动态库

gcc ./mylib.c -shared -o mylib.dll

在 python 中调用生成库

from ctypes import *mylib = cdll.LoadLibrary("./mylib.dll")mylib.fmtfa(create_string_buffer(b"./temp"),create_string_buffer(b"./d.txt"))

5

   

敲黑板划重点

好啦,今天就分享到这里啦,代码已经上传到生信媛的 Gitub https://github.com/biosxy/share  , 欢迎大家 watch,star,fork,contrib.

你可能感兴趣的:(python,转dll)