计算序列中的碱基 - Nonetype
我正在尝试创建一个函数,用户可以输入文件名(包含 DNA 序列),并对所选文件中存在的相应碱基数量进行计数并按顺序输出到屏幕上:#A,# G、#C、#T。然后,我想将该输出保存为用户输入名称下扩展名为 .count 的新文件。在 bash 中,我尝试将这些文件(总共 100 个)连接到一个 .csv 文档中,格式如下:
File | #A,#G,#C,#T |
---|---|
file.count 1 | 23,43,32, 41 |
file.count 2 | 等等... |
To open the file, I have:
def openseq(filename):
filename=input("enter file to open: ")
openfile=open(filename,"r")
dnatext=print(openfile.read())
return dnatext
然后最初我尝试在(在 dnatext 下)内执行以下 for 循环:
for i in dnatext:
comma = ","
numberofbases=str(dnatext.count('A')) + comma + str(dnatext.count('G')) + comma + str(dnatext.count('C')) + comma + str(dnatext.count('T'))
return numberofbases
然后将文件保存在用户输入的新名称下:
directory="<desired directory>" #removed directory for privacy
newname= input("Enter output file name: ")
filepath = directory + newname + ".count"
filepath.close()
但无论我如何移动周围的事物我收到错误消息 TypeError: 'NoneType' object is not iterable 或某些变量未定义。我已经尝试了几种方法来尝试解决这个问题,但我只是没有任何运气,并且由于我对编码相对较新(特别是Python和bash的组合),我非常感谢一些帮助,甚至解释为什么我无法计算输入序列中的碱基数量。
理想情况下,我试图将所有这些都放入 1 或 2 个函数中,这样我就可以在 bash 中轻松调用它们,但我不确定这是否可能。
I am trying to create a function where the user is able to input a file name (containing a DNA sequence), and the respective number of bases present in the selected file are counted and output onto the screen in the order: #A, #G, #C, #T. I then want to save that output as a new file under a user input name with extension .count. In bash I am then trying to concatenate those file (will be 100 in total) into a single .csv document with the following format:
File | #A,#G,#C,#T |
---|---|
file.count 1 | 23,43,32,41 |
file.count 2 | etc... |
To open the file, I have:
def openseq(filename):
filename=input("enter file to open: ")
openfile=open(filename,"r")
dnatext=print(openfile.read())
return dnatext
and then originally I was trying to next a for loop within (under dnatext) with the following:
for i in dnatext:
comma = ","
numberofbases=str(dnatext.count('A')) + comma + str(dnatext.count('G')) + comma + str(dnatext.count('C')) + comma + str(dnatext.count('T'))
return numberofbases
And then to save the file under a new name input by the user:
directory="<desired directory>" #removed directory for privacy
newname= input("Enter output file name: ")
filepath = directory + newname + ".count"
filepath.close()
But no matter how i move things around i either get the error message TypeError: 'NoneType' object is not iterable or that some variable is not defined. I've tried a few ways to try and resolve this but am just not having any luck and seeing as i am relatively new to coding (especially combination of python and bash) i would very much appreciate some help or even an explanation as to why I am unable to count the number of bases in the inputed sequence.
Ideally I am trying to get this all into 1 or 2 functions so I can call them easily in bash but I am not sure whether that is even possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果没有 MWE 很难说,但一般来说,“NoneType 不可迭代”意味着(也许并不奇怪)您正在尝试迭代值
None
。在您发布的代码中,只有一个地方可以进行迭代:
这里
dnatext
预计是可迭代的,而您的错误表明它实际上是None
。原因可能是该函数中的错误:
print
函数不返回任何内容(或返回None
,具体取决于您如何看待它)。所以这个函数总是返回None
。相反,您可能(但同样,很难说)想要:
它
It's hard to tell without a MWE, but in general, "NoneType is not iterable" means (perhaps unsurprisingly) that you're trying to iterate over the value
None
.In your code you posted there's only one place where you iterate:
Here
dnatext
is expected to be iterable and your error suggests it's in factNone
.The cause is probably the bug in this function:
The
print
function doesn't return anything (or returnsNone
, depending on how you look at it). So this function will always returnNone
.Instead, you probably (but again, it's hard to say) want:
which