DNA序列操作
DNA sequence manipulations
所以我对编程非常陌生,而且我对任何编程语言都不太了解。我买了一本关于生物学家编程的书,我摸索了一些事情。我想:从文件中获取序列并从中查找并提取可变区域。我的代码如下:
**
#!/usr/bin/python
#for extracting GAA sequences
import os
import sys
import re
#opens sequence file and defines it as reps
reps = open('142sequences.txt')
#defining what to read
line = reps.readlines()
#defines what we are looking for in rep lines
for line in reps:
sear = re.search(r"C[A]{2,}G[ATCG]{17, 2700}AAT[A]{2,4}G[A]{2,}", reps)
if sear:
repeats = sear.group()
print(repeats)
else:
print('Not Recognized')
**
我在 return 中什么也得不到。请帮助
您需要搜索每一行而不是代表所有行的列表:
with open('142sequences.txt') as reps:
# iterate over each line in the file
for line in reps:
# pass each line to re.search
sear = re.search(r"C[A]{2,}G[ATCG]{17, 2700}AAT[A]{2,4}G[A]{2,}", line)
if sear:
repeats = sear.group()
print(repeats)
else:
print('Not Recognized')
调用 readlines 将所有行读入一个列表,因此您实际上永远不会在自己的代码中循环,因为如果您循环了初始 readlines 调用就会消耗迭代器它会导致错误,因为您必须传递一个字符串而不是一个列表来搜索。
所以我对编程非常陌生,而且我对任何编程语言都不太了解。我买了一本关于生物学家编程的书,我摸索了一些事情。我想:从文件中获取序列并从中查找并提取可变区域。我的代码如下:
**
#!/usr/bin/python
#for extracting GAA sequences
import os
import sys
import re
#opens sequence file and defines it as reps
reps = open('142sequences.txt')
#defining what to read
line = reps.readlines()
#defines what we are looking for in rep lines
for line in reps:
sear = re.search(r"C[A]{2,}G[ATCG]{17, 2700}AAT[A]{2,4}G[A]{2,}", reps)
if sear:
repeats = sear.group()
print(repeats)
else:
print('Not Recognized')
** 我在 return 中什么也得不到。请帮助
您需要搜索每一行而不是代表所有行的列表:
with open('142sequences.txt') as reps:
# iterate over each line in the file
for line in reps:
# pass each line to re.search
sear = re.search(r"C[A]{2,}G[ATCG]{17, 2700}AAT[A]{2,4}G[A]{2,}", line)
if sear:
repeats = sear.group()
print(repeats)
else:
print('Not Recognized')
调用 readlines 将所有行读入一个列表,因此您实际上永远不会在自己的代码中循环,因为如果您循环了初始 readlines 调用就会消耗迭代器它会导致错误,因为您必须传递一个字符串而不是一个列表来搜索。