Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法用Python替换序列列表中的前三个字符和后三个字符?_Python_Python 3.x_Bioinformatics - Fatal编程技术网

有没有办法用Python替换序列列表中的前三个字符和后三个字符?

有没有办法用Python替换序列列表中的前三个字符和后三个字符?,python,python-3.x,bioinformatics,Python,Python 3.x,Bioinformatics,我试图用Python替换序列列表中的某些字符,这些字符将被发送到合成中。所讨论的字符是每个序列的前三个和后三个。我还试图在每个字符之间添加一个* 棘手的是,第一个和最后一个角色需要与其他两个角色不同 例如:DNA序列TGTACGTGCTCCGAC需要更改为/52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*/i2MOErG/*/i2MOErA/*/32MOErC/ 第一个字符必须是/52MOEr_u/,最后一个字符必须是/32MOEr_u/,其中u是该

我试图用Python替换序列列表中的某些字符,这些字符将被发送到合成中。所讨论的字符是每个序列的前三个和后三个。我还试图在每个字符之间添加一个*

棘手的是,第一个和最后一个角色需要与其他两个角色不同

例如:DNA序列TGTACGTGCTCCGAC需要更改为/52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*/i2MOErG/*/i2MOErA/*/32MOErC/

第一个字符必须是/52MOEr_u/,最后一个字符必须是/32MOEr_u/,其中u是该索引处的字符。对于上面的例子,第一个是T,最后一个是C。另外两个,GT和GA需要进行/i2MOEr_uU2/修改

到目前为止,我已经使用.split函数将序列转换为列表。最终结果是['AAGTCTGGTTAACCAT','aatactaggtaaccac','TGTACGTTGCTCCGTC','TGTAGTTAGCTCCGTC']。我已经玩了一段时间了,但我觉得我需要一些指导


这难道不像我想象的那么容易吗?

你可以使用分治算法。这是我实现你目标的解决方案

dna = "TGTACGTTGCTCCGAC"
dnaFirst3Chars = '/52MOEr' + dna[0] + '/*/i2MOEr' + dna[1] + '/*/i2MOEr' + dna[2] + '/*'
dnaMiddle = '*'.join(dna[3:-3])
dnaLast3Chars = '*/i2MOEr' + dna[-3] + '/*i2MOEr' + dna[-2] + '/*/32MOEr' + dna[-1] + '/'

dnaTransformed = dnaFirst3Chars + dnaMiddle + dnaLast3Chars

print(dnaTransformed)
输出:

更新:

为简单起见,您可以在如下函数中转换上述代码:

def dna_transformation(dna):
    """ Takes a DNA string and returns the transformed DNA """

    dnaFirst3Chars = '/52MOEr' + dna[0] + '/*/i2MOEr' + dna[1] + '/*/i2MOEr' + dna[2] + '/*'
    dnaMiddle = '*'.join(dna[3:-3])
    dnaLast3Chars = '*/i2MOEr' + dna[-3] + '/*i2MOEr' + dna[-2] + '/*/32MOEr' + dna[-1] + '/'

    return dnaFirst3Chars + dnaMiddle + dnaLast3Chars

print(dna_transformation("TGTACGTTGCTCCGAC")) # call the function
输出:/52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*i2MOErA/*/32MOErC/


假设您的预期结果中有一个输入错误,并且它实际上应该是 /52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*/i2MOErG/*/i2MOErA/*/32MOErC/以下代码将起作用:

蟒蛇3 def encode_SEQUENCEQ: 序号前=序号[:3] seq_back=seq[-3:] 序号中间=序号[3:-3] front_ix=[/52MOEr{}/,/i2MOEr{}/,/i2MOEr{}/] back_ix=[/i2MOEr{}/,/i2MOEr{}/,/32MOEr{}/] 编码=[] 对于基础,在zipseq_-front和front_-ix中索引: encoded.appendindex.formatbase encoded.extendseq_middle 对于基准,在zipseq_back、back_ix中索引: encoded.appendindex.formatbase 返回*.joinencoded 通读代码并确保您理解它。本质上,我们只是将原始字符串切片,并将基插入到所需的格式中。最终输出的每个元素都被添加到一个列表中,并由末尾的*字符连接

如果需要动态指定从序列的前后提取的基的数量和名称,可以使用此版本。注意,{}大括号告诉string.format函数在哪里插入基

def编码序列2seq,前ix,后ix: seq_front=seq[:lenfront_ix] seq_back=seq[-lenback_ix:] seq_middle=seq[lenfront_ix:-lenback_ix] 编码=[] 对于基础,在zipseq_-front和front_-ix中索引: encoded.appendindex.formatbase encoded.extendseq_middle 对于基准,在zipseq_back、back_ix中索引: encoded.appendindex.formatbase 返回*.joinencoded 以下是输出:

> seq = "TGTACGTTGCTCCGAC"
> encode_sequence(seq)
/52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*/i2MOErA/*/32MOErC/
如果您有一个要编码的序列列表,您可以遍历该列表并对每个序列进行编码:

编码的_列表=[] 对于dna_列表中的seq: encoded_list.appendencode_sequenceseq 或使用列表:

encoded_list=[encode_sequenceseq for seq for dna_list]
谢谢你,埃文。你是对的,我已经纠正了错误,很多道歉。我想我能够理解代码是如何工作的。有没有一种方法可以对序列列表采用这种方法?比如说,我有一堆DNA序列,想应用算法。>>>dna='AAGTCTGGTTAACCAT aatactaggtataactac tgtagttgctccgtc TGTAGTTAGCTCCGTC>>>dna_list=dna.split>>编码测序dna_list/52MOEr'aagtggttaaccat/*/I2Moeraatactagtaactac/*/I2Moertgtacgtgtctccgtc/*/I2Moerattagtaggtactac/*/I2Moertgtacgtgttgctccgtc/*/32moertagttagttagtcac/这是输出iI get。要为其单独编码的字符串吗每个序列,对吗?试试我在答案末尾添加的示例。这将对列表中的每个序列进行编码,并创建一个新的编码序列列表。这就是你想要的吗?
> seq = "TGTACGTTGCTCCGAC"
> encode_sequence(seq)
/52MOErT/*/i2MOErG/*/i2MOErT/*A*C*G*T*T*G*C*T*C*C*/i2MOErG/*/i2MOErA/*/32MOErC/