我有一个txt文件,其中包含以下数据:
hr
ATGCCTTGGGCAACGGT ...(多行)
出租
AGGTTGGCCAAGGTT ...(多行)
我想先找到“ chrI”,然后遍历ATGC的多行,直到找到第x个字符。然后我要打印第x个字符,直到第y个字符。我一直在使用正则表达式,但是一旦找到了包含chrI的行,我就不知道如何继续迭代以找到xth字符。
这是我的代码:
for i, line in enumerate(sacc_gff):
for match in re.finditer(chromo_val, line):
print(line)
for match in re.finditer(r"[ATGC]{%d},{%d}\Z" % (int(amino_start), int(amino_end)), line):
print(match.group())
变量的含义是:
chromo_val
= hrI
amino_start
=(我的程序找到了一些起点)
amino_end
=(我的程序找到了一些终点)
注意:amino_start
并且amino_end
需要采用可变形式。
请让我知道是否可以为您澄清任何事情,谢谢。
看来您正在使用fasta数据,因此我会在此提供一个答案,但是如果不是这样,您仍然可以使用sub_sequence选择部分。
fasta_data = {} # creates an empty dictionary
with open( fasta_file, 'r' ) as fh:
for line in fh:
if line[0] == '>':
seq_id = line.rstrip()[1:] # strip newline character and remove leading '>' character
fasta_data[seq_id] = ''
else:
fasta_data[seq_id] += line.rstrip()
# return substring from chromosome 'chrI' with a first character at amino_start up to but not including amino_end
sequence_string1 = fasta_data['chrI'][amino_start:amino_end]
# return substring from chromosome 'chrII' with a first character at amino_start up to and including amino_end
sequence_string2 = fasta_data['chrII'][amino_start:amino_end+1]
固定格式:
>chr1
ATTTATATATAT
ATGGCGCGATCG
>chr2
AATCGCTGCTGC
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句