【问题标题】:How to separate parts of the string with a specific percent of the needed characters?如何用特定百分比的所需字符分隔字符串的各个部分?
【发布时间】:2021-06-02 20:13:54
【问题描述】:

我正在尝试编写用于查找 siRNA 的代码。现在我已经编写了一个代码来查找所有可能的 siRNAs 组合。我还需要找到最好的,其中必须包含 30% 到 50% 的 GC 或 CG 核苷酸。

此程序正在搜索 21 个核苷酸长度的序列,其中 CG 或 GC 含量从 30% 到 50%。目前,我的程序只在一个字符串中生成长度为 21 的所有可能的 siRNA,但我需要将所需数量的 GC 或 CG 分开。

我的程序如何使用 K = 2 的示例,这意味着来自 mRNA 的 iRNA 序列的长度:

  1. 输入 DNA:ATGC
  2. 它通过将 T 替换为 U 来转换为 mRNA,因此我们得到:AUGC
  3. 根据 Chargaff 规则,A 到 U,U 到 A,g 到 C,C 到 G 形成互补链,我们得到:UACG
  4. 现在我们有了一个大 iRNA,现在我们以所有可能的方式将其拆分以获得 siRNA,因此: 所有 iRNA 组合 ['UA'、'AC'、'CG']

最后我想从中选择 C+G 核苷酸含量在 30-50% 范围内的那些。

好吧,我们只有 100 的 CG,但我们将 K 更改为 4,并使用 ATGCCGTA 作为输入。

ATCGCGTA 所有 iRNA 组合 ['UAGC', 'AGCG', 'GCGC', 'CGCA', 'GCAU']

所以,在这里,正确的是 - UAGC 和 GCAU

    import re

def converttostr(input_seq, seperator):
   # Join all the strings in list
   final_str = seperator.join(input_seq)
   return final_str

DNA_seq = input("")

RNA_seq  = DNA_seq.replace("T", "U")

N = RNA_seq
iRNA = (N.translate(str.maketrans({"A": "U", "G": "C", "U": "A", "C": "G"})))
iRNA_str = iRNA 

K = 21
iRNA_comb = [iRNA[i: j] for i in range(len(iRNA_str)) for j in range(i + 1, len(iRNA_str) + 1) if len(iRNA_str[i:j]) == K]
print("All iRNA combinations", iRNA_comb)

seperator = ', '
LtS = converttostr(iRNA_comb, seperator)
print("List converted to string: ", LtS)

CG = re.split(" CG |[^a-zA-Z ]+",LtS)
print("siRNA with CG founded",CG)

【问题讨论】:

  • 你能解释一下逻辑吗,即最后一个有30%-50%的GC核苷酸?因为 6/11 是 54.5%
  • 最后一个只是一个例子,它不应该在 30-50% 的范围内。
  • @OrkBiotechnologist 你应该提供工作输入和输出值,无论如何我在下面提供了一个答案希望它有所帮助。
  • @OrkBiotechnologist 请提供输入和预期输出,以便我们推导出所需的逻辑

标签: python python-3.x string bioinformatics


【解决方案1】:

此代码查找字符串中有多少 GCCG 组合,并将值在 30-50% 之间的组合过滤到输出数组。

我还打印了针对不同测试用例计算的百分比供您参考。

代码

import regex as re

siRNAs=['GUUUCCCTTTG', 'GCTTTUGCTUT', 'GCTUGCUTGCU', 'CGTUCGUTCGU', 'GCTUCGUTCGU', 'CGCGTUUTCGU', 'GCGCTUUTGCU',
        'GCGCGCGCTUUTGCU', 'GCGCGCGCCGCGCGTUUTGCU'  ]

def get_count(mstring, sub1, sub2):
    idxs1 = [(m.start(), m.end()) for m in re.finditer(sub1, mstring)]
    idxs2 = [(m.start(), m.end()) for m in re.finditer(sub2, mstring)]
    count = len(idxs1)
    for i2 in idxs2:
        if any([i1[0] <= i2[0] < i1[1] for i1 in idxs1]):
            continue
        count+=1
    return count


for x in siRNAs:
    print('siRNA: ', x, ' percentage: ',((get_count(x, "GC", "CG")) * 2) / len(x) * 100, '%')


output = [x for x in siRNAs if 30 <= ((get_count(x, "GC", "CG")) * 2) / len(x) * 100 <=50]
print('output: ', output)

输入

['GUUUCCCTTTG', 'GCTTTUGCTUT', 'GCTUGCUTGCU', 'CGTUCGUTCGU', 'GCTUCGUTCGU', 'CGCGTUUTCGU', 'GCGCTUUTGCU', 'GCGCGCGCTUUTGCU', 'GCGCGCGCCGCGCGTUUTGCU']

输出

siRNA:  GUUUCCCTTTG  percentage:  0.0 %
siRNA:  GCTTTUGCTUT  percentage:  36.36363636363637 %
siRNA:  GCTUGCUTGCU  percentage:  54.54545454545454 %
siRNA:  CGTUCGUTCGU  percentage:  54.54545454545454 %
siRNA:  GCTUCGUTCGU  percentage:  54.54545454545454 %
siRNA:  CGCGTUUTCGU  percentage:  54.54545454545454 %
siRNA:  GCGCTUUTGCU  percentage:  54.54545454545454 %
siRNA:  GCGCGCGCTUUTGCU  percentage:  66.66666666666666 %
siRNA:  GCGCGCGCCGCGCGTUUTGCU  percentage:  76.19047619047619 %

output:  ['GCTTTUGCTUT']

【讨论】:

  • @OrkBiotechnologist 能否提供Lts 变量
  • @OrkBiotechnologist 传递 iRNA_comb,因为此代码需要字符串列表而不是字符串
  • @OrkBiotechnologist 确切地说,它不是一个列表,而是一个字符串。 iRNA_comb 中的每个序列不是您要处理的值吗?所以通过 iRNA_comb 而不是 LtS
  • @OrkBiotechnologist 是的,我也只是认为有一种情况会打破这种逻辑。让我试着纠正这个
  • @OrkBiotechnologist 等等,这就是你想要的?我认为您需要 GCGCCG 一样一起出现才能使其计数。例如,对于 GUUUCCCTTTG,您的代码将显示 45%,其中没有 GCCG
【解决方案2】:

我试图弄清楚这段代码的作用,但我无法))你以前用另一种语言写过吗?只需指定要接收的输入数据和输出即可。

如果符合条件 (30%-50%),则返回 true。然后您可以将其添加到列表或其他任何内容中。

def foo(seq: str) -> bool:
    """searching for the 21 nucleotides length sequences with content of CG or GC from 30 to 50%
    """
    return 30 < (seq.count("GC") * 2) / len(h) * 100 < 50

【讨论】:

    【解决方案3】:
    DNA_seq = input("")
    
    RNA_seq  = DNA_seq.replace("T", "U")
    
    N = RNA_seq
    iRNA = (N.translate(str.maketrans({"A": "U", "G": "C", "U": "A", "C": "G"})))
    iRNA_str = iRNA 
    
    K = 4
    iRNA_comb = [iRNA[i: j] for i in range(len(iRNA_str)) for j in range(i + 1, len(iRNA_str) + 1) if len(iRNA_str[i:j]) == K]
    print("All iRNA combinations", iRNA_comb)
    
    siRNAs = iRNA_comb
    
    for x in siRNAs:
        print('siRNA: ', x, ' percentage: ',((x.count("C") + x.count("G"))) / len(x) * 100, '%')
    
    output = [x for x in siRNAs if 30 <= ((x.count("C") + x.count("G"))) / len(x) * 100 <=50]
    
    print('output: ', output)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 2011-04-11
      • 2023-02-25
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多