使用python

时间:2019-03-27 06:13:05

标签: python regex bioinformatics dna-sequence

输入文件的示例:

1  AAcgGGGGGGtacctgt    yes
2  TTcccccctgtAAcgta   no
3  tcgAAAAaatacgacc     no
4  AAcgtataatacctgt   no
...

我想编写一个程序来扫描每个序列并检查单体核苷酸重复(mnr)

示例输出:

1,AAcgGGGGGGtacctgt,yes
2,TTcccccctgtAAcgta,no

定义:单体核苷酸是:A,T,C,G的重复(不区分大小写)

我连续要查找的内容如下: AAAAaaAAgtc 要么 gtAAAAAAAAAAc 要么 aaaaaaAAA 要么 aaaaaaaaaa 要么 ccccccccccc 要么 CCCCCcccCCC 或...

我尝试过此正则表达式,但不起作用:

import csv
import re
list=[]
with open('sequences.txt', 'r') as f:
    reader = csv.reader(f,delimiter="\t")
    seq=re.findall(r'[Aa]{6, }','sequences.txt')
    for line in reader:
        if line.__contains__(seq):
            print(list.append(line))

任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

更新:此后,提出了使用正则表达式的部分解决方案。请注意,以下解决方案不适用于正则表达式,而是查找长度为6或更大的任何字符的任何序列。

测试数据:

number,sequence,status
1,kjhfklashfkldflkhasdfl,0
2,aaaaaljgkldfkjgldkfjgfldj,0
3,bbbbbbjigdfsjgjg,0
4,ccCccCCcjjfijsdfjsdf,0
5,klsjdflsjdfhdddddjnjlkhngjk,0
6,kjkljfhnlasjkdfheeeeeeejjjeeeeeeeeeekjdkljfleeef,0
7,jhfshffFffFFFFffkljjjj908u89,0

找到长度为6或更大的MNR的代码:

import csv

def contains_mnr(sequence):
    start_char = "$" # choose a character that is sure not to be in the sequence
    count = 0
    seq_lower = sequence.lower()

    for pos in range(0, len(seq_lower)):
        if seq_lower[pos] == start_char:
            count += 1
        else:
            start_char = seq_lower[pos]
            count = 1
        if count >= 6:
            return True

    return False

with open("input.csv", "r") as input_file:
    with open("output.csv", "w") as output_file:
        reader = csv.DictReader(input_file, dialect=csv.unix_dialect())
        writer = csv.writer(output_file, dialect=csv.unix_dialect())
        writer.writerow(reader.fieldnames)

        for row in reader:
            if contains_mnr(row["sequence"]):
                writer.writerow([
                    row["number"],
                    row["sequence"],
                    row["status"]
                ])

请注意,可能必须将CSV方言调整为运行代码和生成数据文件的系统。

输出上面给定的测试数据:

"number","sequence","status"
"3","bbbbbbjigdfsjgjg","0"
"4","ccCccCCcjjfijsdfjsdf","0"
"6","kjkljfhnlasjkdfheeeeeeejjjeeeeeeeeeekjdkljfleeef","0"
"7","jhfshffFffFFFFffkljjjj908u89","0"

答案 1 :(得分:0)

这是您想要的紧凑解决方案:

import csv
with open('sequences.txt', 'r') as f:
    reader = csv.reader(f, delimiter=",")
    for line in reader:
        seq_lower = line[1].lower()
        if 'aaaaaa' in seq_lower or 'cccccc' in seq_lower or 'tttttt' in seq_lower or 'gggggg' in seq_lower:
            print(line)

在这里,我假设您只是在使用a,c,g,t的mnrs,因为您正在使用DNA序列。