拼接两个文件中名字相同的序列
作者:互联网
目的:有两个序列文件,我们要把名字相同的序列首尾拼接起来
# ly-2021-08-29 version-01 # Usage: python infile1 infile2 > outfile # 每条序列必须处于同一行 a = [] b = [] with open("1.txt", "r+") as file1: for line1 in file1.readlines(): line1 = line.strip() a.append(line1) with open("2.txt", "r+") as file2: for line2 in file2.readlines(): line2 = line2.strip() b.append(line2) for k in a: for v in b: if k==v: #要求序列名完全相同 print(k) print(b[b.index(k)]+1)] + a[a.index(v)+1])
# ly-2021-08-30 version-02
# Reviewed: 不要求序列只能处在一行
# Usage: py infile1 infile2 > outfile #!/usr/bin/python import sys a=[] b=[] flag=0 with open(sys.argv[1], "r+") as file1: for line in file1.readlines(): if flag==0: a.append(line) flag+=1 else: if ">" not in line: line=line.strip() b.append(line) item1="".join(b) else: item1="" item="".join(b) a.append(item) b=[] a.append(line) a.append(item1) #print(a) c=[] d=[] flag=0 with open(sys.argv[2], "r+") as file2: for line in file2.readlines(): if flag==0: c.append(line) flag+=1 else: if ">" not in line: line=line.strip() d.append(line) item1="".join(d) #用于处理最后一个序列 else: item1="" item="".join(d) #将前一个if里的列表d里的元素拼接起来。如果没有item1,可以发现最后一条序列是处理不到的 c.append(item) d=[] #清空,用于下一次if循环 c.append(line) c.append(item1) #print(c) for k in a: for v in c: if k==v: #要求序列名完全相同 print(k, end="") print(a[a.index(k)+1]+c[c.index(v)+1])
# ly-2021-08-30 version-03 # Reversed: function # Usage: py infile1 infile2 > outfile
#!/usr/bin/python import sys def INFILE(x): a=[] b=[] flag=0
with open(x, "r+") as file1:
for line in file1.readlines():
if flag==0:
a.append(line)
flag+=1
else:
if ">" not in line:
line=line.strip()
b.append(line)
item1="".join(b) #用于处理最后一个序列
else:
item1=""
item="".join(b) #将前一个if里的列表d里的元素拼接起来。如果没有item1,可以发现最后一条序列是处理不到的
a.append(item)
b=[] #清空,用于下一次if循环
a.append(line)
a.append(item1)
#print(a)
return a
c=INFILE(x=sys.argv[1])
d=INFILE(x=sys.argv[2]) for k in c: for v in d: if k==v: #要求序列名完全相同 print(k, end="")
print(c[c.index(k)+1]+d[d.index(v)+1])
标签:item1,flag,print,名字,拼接,序列,line,append 来源: https://www.cnblogs.com/ly-zy/p/15203769.html