[英]How to write specific line lengths of a file?
我有這樣的序列(超過9000):
>TsM_000224500
MTTKWPQTTVTVATLSWGMLRLSMPKVQTTYKVTQSRGPLLAPGICDSWSRCLVLRVYVDRRRPGGDGSLGRVAVTVVETGCFGSAASFSMWVFGLAFVVTIEEQLL
>TsM_000534500
MHSHIVTVFVALLLTTAVVYAHIGMHGEGCTTLQCQRHAFMMKEREKLNEMQLELMEMLMDIQTMNEQEAYYAGLHGAGMQQPLPMPIQ
>TsM_000355900
MESGEENEYPMSCNIEEEEDIKFEPENGKVAEHESGEKKESIFVKHDDAKWVGIGFAIGTAVAPAVLSGISSAAVQGIRQPIQAGRNNGETTEDLENLINSVEDDL
包含“>”的行是ID,帶有字母的行是氨基酸(aa)序列。 我需要刪除(或移至其他文件)低於40 aa和超過4000 aa的序列。 然后,生成的文件應僅包含該范圍內的序列(> = 40 aa和<= 4K aa)。
我嘗試編寫以下腳本:
def read_seq(file_name):
with open(file_name) as file:
return file.read().split('\n')[0:]
ts = read_seq("/home/tiago/t_solium/ts_phtm0less.txt")
tsf = open("/home/tiago/t_solium/ts_secp-404k", 'w')
for x in range(len(ts)):
if ([x][0:1] != '>'):
if (len([x]) > 40 or len([x]) < 4000):
tsf.write('%s\n'%(x))
tsf.close()
print "OK!"
我做了一些修改,但是我得到的只是空文件或所有+9000序列。
在您的for循環中,由於使用range()
(即0,1,2,3,4...
),所以x
是一個迭代整數。 嘗試以下方法:
for x in ts:
這將為您提供ts
每個元素x
另外,您不需要在x
周圍加上括號; Python可以自己遍歷字符串中的字符。 將括號放在字符串中時,將其放入列表中,因此,例如,如果嘗試獲取x
的第二個字符: [x][1]
,Python將嘗試獲取列表中的第二個元素你把x
放進去,就會遇到問題。
編輯:要包括ID,請嘗試以下操作:
注意:我也將if (len(x) > 40 or len(x) < 4000)
更改為if (len(x) > 40 and len(x) < 4000)
-使用and
代替or
將給您結果您正在尋找。
for i, x in enumerate(ts): #NEW: enumerate ts to get the index of every iteration (stored as i)
if (x[0] != '>'):
if (len(x) > 40 and len(x) < 4000):
tsf.write('%s\n'%(ts[i-1])) #NEW: write the ID number found on preceding line
tsf.write('%s\n'%(x))
試試這個,簡單易懂。 它不會將整個文件加載到內存中,而是逐行遍歷文件。
tsf=open('output.txt','w') # open the output file
with open("yourfile",'r') as ts: # open the input file
for line in ts: # iterate over each line of input file
line=line.strip() # removes all whitespace at the start and end, including spaces, tabs, newlines and carriage returns.
if line[0]=='>': # if line is an ID
continue # move to the next line
else: # otherwise
if (len(line)>40) or (len(line)<4000): # if line is in required length
tsf.write('%s\n'%line) # write to output file
tsf.close() # done
print "OK!"
僅供參考,如果在unix環境中工作,也可以將awk用於單行解決方案:
cat yourinputfile.txt | grep -v '>' | awk 'length($0)>=40' | awk 'length($0)<=4000' > youroutputfile.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.