【问题标题】:how to make two or more combinations of specific letter?如何制作两个或多个特定字母的组合?
【发布时间】:2014-07-17 14:52:11
【问题描述】:

我是 python 新手,上周我一直在努力做到这一点,有人可以帮我解决这个问题,这对完成我的项目很有帮助。

我尝试根据给定序列的用户输入进行单突变及其 2,3 组合:

输入序列:>PEACCEL

用户变异输入文件:
E2R
C4W
E6G

#!/usr/bin/python
import getopt
import sys
import itertools as it 
from itertools import groupby

def main(argv):
try:
    opts,operands = getopt.getopt(sys.argv[1:],'i:m:o:'["INPUT_FILE:=","MUTATIONFILE:=","OUTPUT_FILE:=","help"])
    if len(opts) == 0:
        print "Please use the correct arguments, for usage type --help "
    else:
        for option,value in opts:
            if option == "-i" or option == "--INPUT_FILE:":
                seq=inputFile(value)
            if option == "-m" or option == "--MUTATION_FILE:":
                conA=MutationFile(value)
            if option == "-o" or option == "--OUTPUT_FILE:":
                out=outputName(value)
        return seq,conA
except getopt.GetoptError,err:
       print str(err)
       print "Please use the correct arguments, for usage type --help"

def inputFile(value):
try:
    fh = open(value,'r')
except IOError:
    print "The file %s does not exist \n" % value
else:
    ToSeperate= (x[1] for x in groupby(fh, lambda line: line[0] == ">"))
    for header in ToSeperate:
        header = header.next()[1:].strip()
        Sequence = "".join(s.strip() for s in ToSeperate.next())
        return Sequence

 def MutationFile(value):
 try:
    fh=open(value,'r')
    content=fh.read()
    Rmcontent=str(content.rstrip())
 except IOError:
    print "The file %s does not exist \n" % MutFile
 else:
    con=list(Rmcontent)
    return con

def Mutation(SEQUENCES,conA):
 R=len(conA)
 if R>1:
    out=[]  
    SecondNum=1
    ThirdChar=2
    for index in range(len(conA)):
        MR=conA[index]
        if index==SecondNum:
            SN=MR
            SecondNum=SecondNum+4
        if index==ThirdChar:
            TC=MR
            ThirdChar=ThirdChar+4

            SecNum=int(SN.rstrip())
            MutateResidue=str(TC.rstrip())
            for index in range(len(SEQUENCES)):
                if index==SecNum-1:
                    NonMutate=SEQUENCES[index]
                    AfterMutate=NonMutate.replace(NonMutate,MutateResidue)
                    new=SEQUENCES[ :index]+AfterMutate+SEQUENCES[index+1: ]
                    MutatedInformation= ['>',NonMutate,index+1,MutateResidue,'\n',new]
                    values2 = ''.join(str(i)for i in MutatedInformation)

if __name__ == "__main__":          
seq,conA=main(sys.argv[1:])
Mutation(seq,conA)

这是我的程序部分,我将 (2,4,6) 的 R、W、G 替换为 E、C、E,然后将这些替换的字母存储到名为 R 的变量中,其中包含如下三行:-

PrACCEL
PEAwCEL
PEACCgL

现在,我想将这三个单一突变组合成 2 和 3。 这就像一行两个突变和一行三个突变的梳子。

样本和预期输出将是这样的:

2C
   PrAwCEL 
   PrACCgL 
   PEAwCgL 
3C
   PrAwCgL 

算法

这是我的代码的一部分,所以我将解释我的算法

1.我读取了具有三个字符的突变文件,例如(E2R),其中(E)是氨基酸字母,这是(2)输入序列 PEACCEL 的位置,第三个字母(R)是 E2 将是 R。

2. 所以首先我从用户变异文件中提取位置和第三个变量,并将它们存储到变量 SecNum 和 MutateResidue(thirdchar) 中。

3.then,我使用 for 循环按索引读取序列(PEACCEL),然后与 SecNUm(E2,4,6)匹配的任何索引都将这些序列替换为具有突变残基的序列,这是突变文件中的第三个字符( 2R,4W,6G)

4.最后我通过这一行将突变的残基索引与其他残基加入:(new=SEQUENCES[:index]+AfterMutate+SEQUENCES[index+1:]

提前致谢

【问题讨论】:

  • 感谢您的快速回复:
  • 这是我的代码的一部分,所以我将解释我的算法 1. 我阅读了包含三个字符的突变文件,例如 (E2R) 其中 (E) 是氨基酸字母,即 (2) 位置输入序列 PEACCEL 和第三个字母 (R) 是 E2 将是 R。所以首先我从用户突变文件中提取位置和第三个变量并将它们存储到变量 SecNum 和 MutateResidue(thirdchar) 中。然后,我使用 for 循环按索引读取序列(PEACCEL),然后无论哪个索引匹配到 SecNUm(E2,4,6)我用突变残基替换那些序列,这是突变文件中的第三个字符(2R,4W,6G )
  • 最后我通过这一行将突变残基索引与其他残基加入:(new=SEQUENCES[ :index]+AfterMutate+SEQUENCES[index+1: ]
  • 对不起亲爱的朋友 sundar nataraj Сундар 因为我正在编辑,所以我上次无法看到你的回复。这是为了通知您,我的主要目标是从具有三行的单个突变文件中进行 2,3 组合
  • 请接受问题,我的联系方式是 iamsundar04@gmail.com

标签: python list iterator combinations itertools


【解决方案1】:
from itertools import combinations,chain
from collections import Counter


def Mutation(SEQUENCES,conA):
    
    #mutations=map(lambda x:x.strip(),open('a.txt','r').readlines())
    
    mutation_combinations= chain.from_iterable([list(combinations(conA,i))for i in range(1,4)])
    #[('E2R',), ('C4W',), ('E6G',), ('E2R', 'C4W'), ('E2R', 'E6G'), ('C4W', 'E6G'), ('E2R', 'C4W', 'E6G')]
    
    for i in mutation_combinations:
        print "combination :"+'_'.join(i)
        c=Counter({})
        temp_string=SEQUENCES
        for j in i:
            c[j[1]]=j[2].lower()
        for index,letter in c.items():
            temp_string=temp_string[:int(index)-1]+letter+temp_string[int(index):]
        print temp_string

输出

combination :E2R
PrACCEL
combination :C4W
PEAwCEL
combination :E6G
PEACCgL
combination :E2R_C4W
PrAwCEL
combination :E2R_E6G
PrACCgL
combination :C4W_E6G
PEAwCgL
combination :E2R_C4W_E6G
PrAwCgL

我遵循的算法:

  1. 使用mutations=map(lambda x:x.strip(),open('a.txt','r').readlines())从文件中读取突变序列,如 E2R....

  2. 如果您有 4 突变,您希望所有四个都更改 range value to 5 的突变组合 range value to 5

  3. 所以对于每个组合,我用指定的字符替换它们

    for j in i:
        c[j[1]]=j[2].lower()
    

我使用上面的计数器来跟踪在突变组合过程中要替换的字符

【讨论】:

  • 我得到这样的组合:组合:E组合:2组合:R组合:组合:C组合:4组合:W组合:组合:E组合:6我认为这是因为我们需要将此 conA 列表更改为字符串吗?
  • @user3805057 是的 conA 应该类似于 ['E2R', 'C4W', 'E6G']
  • 我改变了一些 ot 这些修改 str = ''.join(conA)---> mut= str.split("\n")---> mutation_combinations=chain.from_iterable([list (combinations(mut,i))-->for i in range(1,4)])--> for i in mutation_combinations:--> print ">"+'_'.join(i) 并得到结果像这样 >E2R >C4W >E6G >E2R_C4W >E2R_E6G >C4W_E6G >E2R_C4W_E6G
  • @user3805057 你能在你的方法中打印出你得到的conA值和序列值吗
  • 最终输出上我如下:> e2r praccel> c4w peacel> e6g peaccl> e2r_c4w praccel praccel prawcel> e2r_e6g praccel praccel praccgl> c4w_e6g peacel pepwel peaccgl> e2r_c4w_e6g praccel praccel prawcel prawcel prawcel prawcel prawcel prawcel prawcel prawcggl但我想要像这样 >E2R PrACCEL >C4W PEAwCEL >E6G PEACCgL >E2R_C4W PrAwCEL >E2R_E6G PrACCgL >C4W_E6G PEAwCgL >E2R_C4W_E6G PrAwCgL
猜你喜欢
  • 2021-09-16
  • 2011-02-16
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 1970-01-01
相关资源
最近更新 更多