【问题标题】:Assist writing awk script version of python code to generate count matrix协助编写awk脚本版python代码生成计数矩阵
【发布时间】:2021-08-09 15:12:25
【问题描述】:

我没有发现任何类似的问题...

我有这个 python 脚本可以从只包含序列的文件中生成一个计数矩阵,但是它需要永远运行,但我知道 awk 会做得更快。我不太擅长 awk,但希望有人能提供帮助。 python脚本如下:

    numFiles = int(sys.argv[1])
    allParams = int(numFiles + 4)
    key_file = sys.argv[2]
    out_file = sys.argv[3]
    #open the output file
    outHandle = open(out_file,'w')
    #Open key file and read one line at a time
    with open(key_file) as kf:
            for eachline in kf:
                    temp_list = [0] * numFiles
                    kSeq = eachline.strip(' \t\n\r')
                    upRange = int(numFiles + 4)

                    for i in range(4,upRange):
                            with open(sys.argv[i]) as f:
                                    for eachline in f:
                                            seq = eachline.strip(' \t\n\r')
                                            if (kSeq == seq):
                                                    curr = int(temp_list[i-4])
                                                    nw = int(curr + 1)
                                                    temp_list[i-4] = nw
                                            else:
                                                    continue

                    outHandle.write(str(kSeq) + "\t")
                    for ind,item in enumerate(temp_list):
                            lastItemIndex = numFiles - 1
                            if(ind == lastItemIndex):
                                    outHandle.write(str(item) + "\n")
                            else:
                                    outHandle.write(str(item) + "\t")

尝试创建示例:

输入:一个keyFile,X个其他文件(所有输入文件基本上只是单列中的单词) 输出:一个矩阵,其中包含 X 个文件中 keyFile 中的单词出现的次数。

密钥文件:

word
one
two
three
four
five

文件1:

word
three
five
three
one
two
one
four
four
three

文件2:

word
four
one
three
three
one
two
three
two
one

输出:

word file1 file2
one 2 3
two 1 2
three 3 3
four 2 1
five 1 0

文件数最多可达 4 个

我希望这个插图更清楚。

谢谢

【问题讨论】:

  • 您假设 a) 是 awk 专家的人也会了解 python 的详细信息,并且 b) python 脚本的编写方式是做任何您想做的事情的最佳方式在python中做。如果您在编写 awk 脚本时需要帮助,请发布 minimal reproducible example,其中包含简洁、可测试的示例输入和预期输出以及您的要求声明,并告诉我们您在哪里遇到问题,以便我们可以帮助您它。见How to Ask
  • @EdMorton,感谢您的指导回复。我会按照建议修改这个
  • 需要永恒的时间才能运行,但我知道 awk 会做得更快 在丢弃您已经工作的解决方案之前,我建议进行分析以检测消耗最多时间的内容并检查是否有改进这是可能的。
  • @Daweo:根据我使用 python 运行脚本并使用 awk 执行相同操作的经验,我知道,尤其是在文件中进行一对一比较时。在这种情况下,AWK 只是比 python 快。
  • @user2960998 AWK is just faster than python in such cases.。取决于您对 awk 的使用程度与对 python 的使用程度。或相反亦然。很难一概而论。

标签: python matrix awk count sequence


【解决方案1】:

所以,经过大量阅读和尝试,我得到了我想要使用代码实现的目标

awk 'fname != FILENAME { fname = FILENAME; idx++ } idx == 1 {key[$0] = $0 } idx == 2 {if($1 == key[$1]){ f1[$1] += 1 }} idx == 3 {if($1 == key[$1]){ f2[$1] += 1 }} END {for(seq in key) print seq "\t" f1[seq] "\t" f2[seq] }' keyFile file1 file2

感谢大家的意见。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多