inputfile.pngcafe输出结果是很难整理的,虽然有脚本输出了各个物种的基因家族扩张收缩结果,每个物种是用数字代替的,而且结果都整合到一起了,现在需要单独提取某一段关于特定物种的情况,如图所示:物种是由######n来代替,第二列是扩张收缩(+/-)以及stable(*)情况,这里我们只要某个物种的扩张收缩信息。
平时只要在Excel里截取就可以,但如果行数太多不好截取,脚本如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# conda activate python3
"""
作者:徐诗芬
内容:截取CAFE某个物种的结果
日期:2021.12.14
"""
import sys
def usage():
print('Usage: python extract.py [size] [infile] [outfile]')
def main():
size = eval(sys.argv[1])
infile = open(sys.argv[2], 'rt')
outfile = open(sys.argv[3], 'wt')
END = size + 1
list = infile.readlines() # 读完全部行放入一个list里
infile.close()
for (num, line) in enumerate(list): # 生成一个带有行数的generator
# print(num,line)
line = line.strip("\n")
if line == '######' + str(size):
start = num #读取起始位置
if line == '######' + str(END):
stop = num #读取终止位置
break
newList = list[start:stop]
# print(newList)
for i in newList:
name = i.split('\t')
if len(name) > 1 and name[1] == '*':
continue
outfile.write(i)
outfile.close()
try:
main()
except IndexError:
usage()
下面这个脚本也可以,利用readline()逐行读取的作用,以及两个elif的非负即正的选择作用
import sys
def usage():
print('Usage: python extract.py [size] [infile] [outfile]')
def main():
size = eval(sys.argv[1])
infile = open(sys.argv[2], 'rt')
outfile = open(sys.argv[3], 'wt')
infile.seek(0, 0) #文件指针的初始位置设定
#size = 1
#END = size + 1
line = infile.readline()
while line:
#print(line)
if line != '######' + str(size) + "\n":
line = infile.readline()
elif line == '######' + str(size) + "\n":
outfile.write(line)
line = infile.readline()
while line.startswith('OG'):
if line.split('\t')[1] != "*":
outfile.write(line)
line = infile.readline()
#elif line == '######' + str(END) + "\n":
#break
outfile.close()
infile.close()
try:
main()
except IndexError:
usage()
size=1,则运行后输出的测试文件如下:
outputfile.png
网友评论