计算序列中的碱基 - Nonetype

发布于 2025-01-16 01:39:59 字数 1332 浏览 2 评论 0原文

我正在尝试创建一个函数,用户可以输入文件名(包含 DNA 序列),并对所选文件中存在的相应碱基数量进行计数并按顺序输出到屏幕上:#A,# G、#C、#T。然后,我想将该输出保存为用户输入名称下扩展名为 .count 的新文件。在 bash 中,我尝试将这些文件(总共 100 个)连接到一个 .csv 文档中,格式如下:

File#A,#G,#C,#T
file.count 123,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 123,43,32,41
file.count 2etc...
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2025-01-23 01:39:59

如果没有 MWE 很难说,但一般来说,“NoneType 不可迭代”意味着(也许并不奇怪)您正在尝试迭代值 None

在您发布的代码中,只有一个地方可以进行迭代:

for i in dnatext:

这里 dnatext 预计是可迭代的,而您的错误表明它实际上是 None

原因可能是该函数中的错误:

def openseq(filename):
    filename=input("enter file to open: ")
    openfile=open(filename,"r")
    dnatext=print(openfile.read())  # <-- this line
    return dnatext

print 函数不返回任何内容(或返回 None,具体取决于您如何看待它)。所以这个函数总是返回None

相反,您可能(但同样,很难说)想要:

def openseq(filename):
    filename = input("enter file to open: ")
    with open(filename,"r") as openfile:
        dnatext = openfile.read()
    print(dnatext)
    return dnatext

  • 使用上下文管理器来为您关闭文件句柄,并
  • 打印并返回数据已读取(而不仅仅是打印)

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:

for i in dnatext:

Here dnatext is expected to be iterable and your error suggests it's in fact None.

The cause is probably the bug in this function:

def openseq(filename):
    filename=input("enter file to open: ")
    openfile=open(filename,"r")
    dnatext=print(openfile.read())  # <-- this line
    return dnatext

The print function doesn't return anything (or returns None, depending on how you look at it). So this function will always return None.

Instead, you probably (but again, it's hard to say) want:

def openseq(filename):
    filename = input("enter file to open: ")
    with open(filename,"r") as openfile:
        dnatext = openfile.read()
    print(dnatext)
    return dnatext

which

  • uses a context manager to also close the file handle for you, and
  • prints and returns the data that was read (instead of just printing it)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文