我的目标是通过非常长(> 115,000)的DNA序列发现隐藏在AscII 8bits中的文本。
我已编写代码打开带有DNA的文件,将所有 C和A 转换为 0 以及所有 T和G 到 1 。然后我将这个字符串转换为AscII字符。以下是我的代码。
with open("DNAseq.txt") as mydnaseq:
sequence = mydnaseq.read().replace('\n','')
DNAa = sequence.replace('A','0').replace('C','0').replace('G','1').replace('T','1')
DNAb = ''.join(DNAa)
DNAc = [DNAb[i:i+8] for i in range(0, len(DNAb), 8)]
DNAd = []
for i in DNAc:
j = int(i,2)
DNAd.append(j)
DNA1 = []
for i in DNAd:
if i >= 32 and i <=127:
DNA1.append(i)
text = []
for i in DNAd:
j = chr(i)
text.append(j)
Answer = open("textanswer.txt", 'w')
Answer.writelines(text)
Answer.close()
但是我收到了错误;
UnicodeEncodeError: 'charmap' codec can't encode character '\x9e' in position 0: character maps to <undefined>
我不知道这可能是什么。我的DNA序列显然有一个混合的随机字符,但是一个剧本/诗的片段。
我使用包含以下内容的 testDNA.txt 测试了我的代码;
ATAGCCTTAGTGCTACATTAATCGCGGACAAGAGGAGCT
TAAGCCCACCGACCCGAAGGAAACTCGGAGATTCGGAAGCG
返回(按预期);
Steak Bake
任何人都可以解释为什么我的DNA序列会出现这个错误?
答案 0 :(得分:3)
正如我在评论中提到的,DNAd
包含有效ASCII范围之外的数字。但是,您在创建DNA1
时已经过滤了这些内容,因此您应该循环DNA1
以构建text
。
但是,在Python 3中,无需在每个ASCII代码编号上调用chr
函数。您只需将一个列表(或任何其他可迭代的)传递给bytes
构造函数,它就会构建一个bytes
字符串,然后您可以将其解码为Unicode文本。
此外,我们可以使用str.replace
而不是使用str.translate
方法将DNA字母转换为'0'和'1'字符,这在您需要映射单个字符时效率更高其他单个字符; str.translate
还可以删除不需要的字符。在下面的代码中,我用它来删除空格和换行符。我还删除了你的'DNAseq.txt'文件开头的Unicode Byte Order Mark。
首先,这是一个使用问题中给出的短DNA序列的演示。
# Translation table to convert DNA letters to bit characters
# Deletes newlines, spaces, and the Unicode Byte Order Mark
tbl = str.maketrans('ACGT', '0011', '\n \ufeff')
def dna_to_bytes(dna, offset=0):
# Convert DNA letters to zero and one characters
bits = dna.translate(tbl)
# Convert groups of 8 zeros and ones to bytes, starting from `offset`
return bytes(int(bits[i:i+8], 2) for i in range(offset, len(bits), 8))
dna = '''\
ATAGCCTTAGTGCTACATTAATCGCGGACAAGAGGAGCT
TAAGCCCACCGACCCGAAGGAAACTCGGAGATTCGGAAGCG
'''
print(dna_to_bytes(dna).decode('ascii'))
<强>输出强>
Steak Bake
要查找隐藏在DNAseq.txt文件中的消息,我们需要忽略有效ASCII范围之外的字节,就像您的代码一样。但是,在开始将8位块转换为字节之前,我们还需要跳过几位。只有8种可能的偏移量,并且由于数据量不是很大,因此很容易通过反复试验发现2的正确偏移量。 OTOH,考虑尝试偏移确实需要我一点时间。 ;)如果我们使用数百万字节,那么我们可能需要采用统计分析来查找可能是有效英语的字节块。
以下程序并不打算尝试隔离隐藏的消息,它很容易在垃圾文本的中间找到。请注意,消息的第一行隐藏在前一长行垃圾的末尾。
# ASCII codes, excluding control chars apart from newline
asciibytes = frozenset(b'\n' + bytes(range(32, 127)))
# Translation table to convert DNA letters to bit characters
# Deletes newlines, spaces, and the Unicode Byte Order Mark
tbl = str.maketrans('ACGT', '0011', '\n \ufeff')
def dna_to_bytes(dna, offset=0):
# Convert DNA letters to zero and one characters
bits = dna.translate(tbl)
# Convert groups of 8 zeros and ones to bytes, starting from `offset`
return bytes(int(bits[i:i+8], 2) for i in range(offset, len(bits), 8))
fname = 'DNAseq.txt'
with open(fname) as f:
dna = f.read()
b = dna_to_bytes(dna, offset=2)
a = bytes(u for u in b if u in asciibytes)
print(a.decode('ascii'))
<强>输出强>
;J\Zza%_&jHs F0kM:!ZsfCq1)^7!Bg%=8:2eMz(|tl KRS@@9$`!2wAD5@>K~_CA"u_R9<
p?+D*WRCH`=LY/v0&Sl[l|"x1h-_GT!P'36'PS&&<eY5yakZd?$R!I@^5uAs4d{q5P7^%Rr]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\"LJ]oll7_2b!*]}5}{^O1o@)UE%dA6ea~O!~ (S7(q>2xu}i8Vf9N)}^n]e} >($6_/K,Kmiv)'`2*~z-S3zg^@$eTTn^Y1*jH_N"5M~EtQ4]V&N'1:HP4/e`Y|h.^xLPM:[F`s!E9]m*J'3Zni24}UNQ&'Xg4`P.tS#Lku86o PJTM+:(J&k;]a2<6E=bAgN?_q6*j3_hTRAk7%zH$M)e(#("oIAkH{LH,+"x1RZ hkxF<.9#.r^R<AA%FUS}"ODLL*;r)VS!$3(N1[y^ZXV6cLL`kBIW]Dd,(&DEi}8f/40pTEDLr7KtNV!piBIgoH].|c#$6~]Ex$-9P`H Ob%;H|7,kS1>[]6TBR}D1;
x %Y#w.Hh8NzOL,[zOugJ60"R#m@`E YKo>YPc&C]O
O1z7O;R8~
DYw`6kBxdha_l..%]G4Z/j:Ic1BHe$5W^0.;Hqxq'D 1 RLa1CKR)LVA[lk2,z@D"jl%~N-w)y)=Gc?(y>pE9|QA[?
4,2@$)8kMJ^XmNeBuuN5Y)4ZdV"#6?x7^$)C|a[77H;i5)3xq.Af=n7#8j.>'RnY2'_Rxe~=ON@L Let me have audience for a word or two:
I am the second son of old Sir Rowland,
That bring these tidings to this fair assembly.
Duke Frederick, hearing how that every day
Men of great worth resorted to this forest,
Address'd a mighty power; which were on foot,
In his own conduct, purposely to take
His brother here and put him to the sword:
And to the skirts of this wild wood he came;
Where meeting with an old religious man,
After some question with him, was converted
Both from his enterprise and from the world,
His crown bequeathing to his banish'd brother,
And all their lands restored to them again
That were with him exiled. This to be true,
I do engage my life.
[b$gdj~S~ma 7&x$aDa2w/N@&}Dx'+- p;^9J]9?!"HKTY&X
!dF5 ab%|=(Z--!<*)T$I<L!$fT`."ZhD~2FP?8M-4{u@1_qJ
nN+m:FvEI>bA
(VVJyAc2U|ixggPwTEXBsW',S>z3=u[C|J)Zbv^&4A;QAE(9%O\ #.z8T=+
L.!ycBr/WBTAWTT Jf|fEt|@&8^E/8DnV~:7S#i<Bs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b-@?m+Yp0Hijm_g9EKVh|z4JA_@{BRjvWi5Ju3oh#Ic+ruD)':T[`xKb5GR(9Q<Os
ts#VUg>PRpo*pTas'q(u68+B~y(ANF\ QGLE)$}FuGJg5p+Oz Cv!<dQJ> 4BsiR~8F:}t;Dy%yYIGq9c~QF?R.2_!,Z
Bg
'PV1CZ]Pk];[Y8Y-fCDvLnxBmE+I)J,)zgX(:{UmU}yPeU$!}Ld:ac*F8buf6Ane
FWIW,秘密信息是莎士比亚的 As You Like It ,第5幕,场景4的一段。
答案 1 :(得分:1)
我认为您希望使用内置chr()函数。
以下是使用str.translate将字符转换为数字字符的简短示例。然后将子串转换为它们的ascii等价物。
>>> s = "ATAGCCTTAGTGCTACATTAATCGCGGACAAGAGGAGCTTAAGCCCACCGACCCGAAGGAAACTCGGAGATTCGGAAGCG"
>>> trans_dict = {"A":"0", "C":"0", "G":"1", "T":"1"}
>>> trans_table = str.maketrans(trans_dict)
>>> s.translate(trans_table)
'01010011011101000110010101100001011010110010000001000010011000010110101101100101'
>>> t = s.translate(trans_table)
>>> [t[i:i+8] for i in range(0, len(t), 8)]
['01010011', '01110100', '01100101', '01100001', '01101011', '00100000', '01000010', '01100001', '01101011', '01100101']
>>> [chr(int(t[i:i+8],2)) for i in range(0, len(t), 8)]
['S', 't', 'e', 'a', 'k', ' ', 'B', 'a', 'k', 'e']