【问题标题】:writing an adaptor removal tool, advice on ignoring case on the sequence编写适配器删除工具,关于忽略序列大小写的建议
【发布时间】:2023-03-26 20:36:01
【问题描述】:

我正在学习如何编码。除其他事项外,我需要编写适配器删除工具。我的脚本工作正常,除非序列是小写和大写的混合。 适配器序列== TATA 序列 == TataGATTACA

这是移除适配器的功能

elif operation == "adaptor-removal":

    adaptor = args.adaptor 
    reads =  sequences(args.input, format)
    num_reads = len(reads)
    bases = "".join([read["seq"] for read in reads])
    adaptors_found = 0

    for read in reads:
        for i, j in read.items():
            if i == "seq":
                if j.startswith(adaptor.upper()) or j.startswith(adaptor.lower()):
                    adaptors_found += 1
                    j = j.replace(adaptor.upper(), "", 1) 
                    j = j.replace(adaptor.lower(), "", 1)                 
            args.output.write("%s\n" % j)
    print_summary(operation)    
    print("%s adaptors found" % adaptors_found)

我试过了:

if j.startswith(adaptor,re.I):

但不起作用,我真的不明白为什么。有经验的人可以指导我完成这个吗?

非常感谢

【问题讨论】:

    标签: python case python-re


    【解决方案1】:

    假设jTAtaGATTACAadaptorTATA

    j.startswith(adaptor.upper()) 是真的吗?不,因为j 不以TATA 开头。

    j.startswith(adaptor.lower()) 是真的吗?不,因为j 不以tata 开头。

    不区分大小写比较两个字符串的最简单方法是将它们都转换为相同的大小写,大写或小写,然后比较这两个字符串,就像您在区分大小写一样进行比较。选择大写还是小写都没有关系,只要两者都选择一样即可。

    j.lower().startswith(adaptor.lower()) 是真的吗?是的,因为j.lower()tata 开头。

    另外,请注意你的两个.replace() 调用:其中一个可能最终会删除j 中的文本,我不相信你想要这样。如果你只是想把适配器从j 前面剪掉,你最好使用字符串切片:

                    if j.lower().startswith(adaptor.lower()):
                        adaptors_found += 1
                        j = j[len(adaptor):]
    

    最后,你还要问为什么

    if j.startswith(adaptor,re.I):
    

    没有做你想做的事。答案是,如果你将第二个参数传递给.startswith(),那么第二个参数的值就是你搜索的起始位置,而不是控制匹配的标志:

    "abcd".startswith("cd")           # False
    "abcd".startswith("cd", 2)        # True
    

    恰好re.I可以转换成整数2,所以下面也是True,虽然看起来很奇怪:

    "abcd".startswith("cd", re.I)
    

    【讨论】:

    • 哇,看起来很简单知道......你无法想象我花了多少小时。非常感谢。
    猜你喜欢
    • 2018-05-31
    • 2019-10-17
    • 2014-10-30
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多