perl python文本处理_Python和Perl处理fasta文件对比_2018-09-11

从fasta中抽取需要的序列:

Python方法一:(这种方式最慢)

由于平时工作比较忙,工作中遇到的问题都没有及时记录,现在有空时间了所以整理一下一些常见的问题及技巧供大家参考:

!/usr/bin/python

import sys

def Fasta(inputfile) :

f = open(inputfile,"r")

fastadic = {}

while True :

line = f.readline().rstrip()

if len(line) == 0 :

break

else :

pass

if line.startswith(">") :

name = line.split()[0]

fastadic[name] = ""

else :

fastadic[name]+=line

f.close()

return fastadic

def usage():

print ("1,fasta\n2,list")

if len(sys.argv) != 3 :

usage()

sys.exit()

print (sys.argv[0])

fastafile = sys.argv[1]

listfile = sys.argv[2]

dic = Fasta(fastafile)

ff=open(listfile,"r")

while 1:

line1 = ff.readline().rstrip()

if len(line1) == 0 :

break

else :

pass

array = line1.split("\t")

if ">"+array[0] in dic.keys() :

print (">"+array[0]+"\n"+dic[">"+array[0]])

else :

print ("Error:"+line1)

ff.close()

Python方法二:(次慢)

!/usr/bin/python

import sys

def Fasta(inputfile) :

f = open(inputfile,"r")

fastadic = {}

while True :

line = f.readline().rstrip()

if len(line) == 0 :

break

else :

pass

if line.startswith(">") :

name = line.split()[0]

fastadic[name] = []

else :

fastadic[name].append(line)

f.close()

return fastadic

def usage():

print ("1,fasta\n2,list")

if len(sys.argv) != 3 :

usage()

sys.exit()

print (sys.argv[0])

fastafile = sys.argv[1]

listfile = sys.argv[2]

dic = Fasta(fastafile)

ff=open(listfile,"r")

while 1:

line1 = ff.readline().rstrip()

if len(line1) == 0 :

break

else :

pass

array = line1.split("\t")

if ">"+array[0] in dic.keys() :

print (">"+array[0]+"\n"+"".join(dic[">"+array[0]]))

else :

print ("Error:"+line1)

ff.close()

Perl 方法三(最快)

!/usr/bin/perl -w

die "perl

math?formula=0%20%3Cinputfastafile%3E%20%3Clist%3E%20%5Cn%22%20if%20%40ARGV%20!%3D%202%20%3B%20use%20strict%3B%20use%20warnings%3B%20my%20%25fasta%20%3B%20my%20%40name%3B%20open%20I%2CARGV[0] ;

while () {

chomp;

if (/^>/) {

@name = split/\s+/,

math?formula=_%3Bfasta{

math?formula=name%5B0%5D%7D%20%3D%20%22%22%20%3B%20%7D%20else%20%7Bfasta{

math?formula=name%5B0%5D%7D.%3D_ ;

}

}

close I;

open T,

math?formula=ARGV%5B1%5D%20%3B%20while%20(%3CT%3E)%20%7B%20chomp%3B%20my%20%40tax%20%3D%20split%2F%5Ct%2F%20%2C_ ;

if (exists

math?formula=fasta%7B%22%3E%22.tax[0]}) {

print ">".

math?formula=tax%5B0%5D%2C%22%5Cn%22%2Cfasta{">".$tax[0]}, "\n" ;

}

}

close T;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值