python分割_Python分割fasta文件

import sys, getopt

# get parameter

opts, args = getopt.getopt(sys.argv[1:], "hi:o:x:")

X = 1

input_file = ""

prefix = "split_"

for op, value in opts:

if op == "-i":

input_file = value

elif op == "-o":

prefix = value

elif op == "-x":

X = int(value)

elif op == "-h":

print("Usage: python3 split_fasta.py -i input.fasta -o prefix -x split_number")

print("default prefix = split_")

print("default split_number = 1")

sys.exit()

FA_in_file = open(input_file, "r")

# read fasta file to a list

fa_Info = []

fa_Seq = []

fa_Num = -1

for Y in FA_in_file.readlines():

Y = Y.rstrip()

if Y[0] == ">":

fa_Info.append(Y)

fa_Num = fa_Num + 1

fa_Seq.append("")

else:

fa_Seq[fa_Num] = fa_Seq[fa_Num] + Y

# split the fasta list to multipe files

file_Num = (fa_Num + 1)//X + 1

for i in range(file_Num):

exec(prefix + str(i + 1) + ' = open("' + prefix + str(i + 1) + '.fasta"' + ', "w")')

start = i * X

end = (i + 1) * X

if end > fa_Num + 1:

end = fa_Num + 1

for j in range(start, end, 1):

exec(prefix + str(i + 1) + '.write(fa_Info[j] + "\\n")')

while len(fa_Seq[j]) > 60:

exec(prefix + str(i + 1) + '.write(fa_Seq[j][:60] + "\\n")')

fa_Seq[j] = fa_Seq[j][60:]

else:

exec(prefix + str(i + 1) + '.write(fa_Seq[j] + "\\n")')

exec(prefix + str(i + 1) + '.close()')

FA_in_file.close()

import sys, getopt

# get parameter

opts, args = getopt.getopt(sys.argv[1:], "hi:o:x:")

X = 1

input_file = ""

prefix = "split_"

for op, value in opts:

if op == "-i":

input_file = value

elif op == "-o":

prefix = value

elif op == "-x":

X = int(value)

elif op == "-h":

print("Usage: python3 split_fasta.py -i input.fasta -o prefix -x split_number")

print("default prefix = split_")

print("default split_number = 1")

sys.exit()

FA_in_file = open(input_file, "r")

# read fasta file to a list

fa_Info = []

fa_Seq = []

fa_Num = -1

for Y in FA_in_file.readlines():

Y = Y.rstrip()

if Y[0] == ">":

fa_Info.append(Y)

fa_Num = fa_Num + 1

fa_Seq.append("")

else:

fa_Seq[fa_Num] = fa_Seq[fa_Num] + Y

# split the fasta list to multipe files

file_Num = (fa_Num + 1)//X + 1

for i in range(file_Num):

exec(prefix + str(i + 1) + ' = open("' + prefix + str(i + 1) + '.fasta"' + ', "w")')

start = i * X

end = (i + 1) * X

if end > fa_Num + 1:

end = fa_Num + 1

for j in range(start, end, 1):

exec(prefix + str(i + 1) + '.write(fa_Info[j] + "\\n")')

while len(fa_Seq[j]) > 60:

exec(prefix + str(i + 1) + '.write(fa_Seq[j][:60] + "\\n")')

fa_Seq[j] = fa_Seq[j][60:]

else:

exec(prefix + str(i + 1) + '.write(fa_Seq[j] + "\\n")')

exec(prefix + str(i + 1) + '.close()')

FA_in_file.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值