Linux合并巨大的gzip文件,只保留十字路口

Linux合并巨大的gzip文件,只保留十字路口,第1张

概述Linux合并巨大的gzip文件,只保留十字路口

我有一个名为“main.txt”的制表符分隔的文件,我试图从许多多个gz文件,称为“chr1.info.gz”,“chr2.info.gz”,“chr3.info”添加信息到这个文件。 gz“等等,其中包含比主文件更多的行。 注意这些文件是使用Gzip压缩的,我不能先解压缩并保存,因为它们是巨大的文件(我没有空间来做这个)。

我想将主文件中名为“name_ID”(第6个字段)的列与多个不同文件(第3个字段)中名为“rs_ID”的匹配列进行匹配,并添加来自这些文件的附加信息,同时只保留行在主文件中:

main.txt文件如下所示:

number maf effect se pval name_ID 34 0.7844 0.2197 0.0848 0.009585 snp1 78 0.6655 -0.1577 0.0796 0.04772 snp2

chr1.info.gz是这样的:

我怎样才能在汞同时工作默认和分支?

将rhapsody DiffMerge设置为svn merge-tool

计时器在windows 7之前合并

如何将多个pdf页面连接到一个页面

如何从所有子目录合并具有相同名称的文本文件?

use pos rs_ID a1 a2 a3 a4 f 10584 snp34 0 0 0 0 g 10687 snp35 0 0 0 0 t 13303 snp1 0 0 0 0

chr2.info.gz是这样的:

use pos rs_ID a1 a2 a3 a4 s 13328 snp67 0 0 0 0 g 10612 snp2 0 0 0 0 t 13303 snp10 0 0 0 0

…等等

我想从其他文件添加信息的文件main.all.gz:

number maf effect se pval name_ID use pos rs_ID a1 a2 a3 a4 34 0.7844 0.2197 0.0848 0.009585 snp1 t 13303 snp1 0 0 0 0 78 0.6655 -0.1577 0.0796 0.04772 snp2 g 10612 snp2 0 0 0 0

我已经尝试过“join”,但它看起来像它需要解压缩文件,sorting和保存,我得到的消息,我没有足够的空间在这个设备(我不认为我有正确的代码无论如何):

join -1 6 -2 3 <(zcat main.txt | sort -k6,6) <(zcat chr1.info.gz | sort -k3,3 ) > try.txt

我已经尝试过使用awk,但是我肯定做了几件事情,因为它给了我一个空文件,而且在使用多个文件的时候遇到困难。

我已经花了一天的时间,并找不到一个好的解决scheme,你能帮我解决这个问题吗?

非常感谢你! -F

在Python中的Unix猫function(cat *> merged.txt)?

Perforce:基于linux的3路合并/parsing工具?

合并文件(猫)在每个文件夹的Unix

bash:合并由数字文件名sorting的文本文件

SVN:将本地更改合并到其他工作副本

我会用Python来做。

将主文件读入内存中,并使用一个字典(使用name_ID作为键)。 然后将每个info.gzip文件进行流式处理,并根据所找到的内容在dict中扩展信息。 (考虑如果您不止一次查找某一行的信息,该怎么办。)

然后用你需要的格式写出字典。

这种方法有帮助吗?

#!/usr/bin/env python import gzip from collections import OrderedDict mainData = OrderedDict() # or just {} if order is not important with open('main.txt') as mainfile: pos = None for line in mainfile: elements = line.split() if pos is None: pos = elements.index('name_ID') mainheaders = elements else: mainData[elements[pos]] = elements infoheaders = None for infofilename in [ 'chr1.info.gz','chr2.info.gz' ]: with gzip.open(infofilename) as infofile: pos = None for line in infofile: elements = line.split() if pos is None: pos = elements.index('rs_ID') if infoheaders is None: infoheaders = elements else: if infoheaders != elements: print "headers in",infofilename,"mismatch" # maybe abort? else: key = elements[pos] try: mainData[key] += elements except KeyError: pass # this key does not exist in main with gzip.open('main.all.gz','w') as outfile: outfile.write(' '.join(mainheaders + infoheaders) + 'n') for key,value in mainData.iteritems(): outfile.write(' '.join(value) + 'n')

我的结果是这样的:

number maf effect se pval name_ID use pos rs_ID a1 a2 a3 a4 34 0.7844 0.2197 0.0848 0.009585 snp1 t 13303 snp1 0 0 0 0 78 0.6655 -0.1577 0.0796 0.04772 snp2 g 10612 snp2 0 0 0 0

它不会期望或生成制表符分隔的文件(但对所有内容都使用空格)。 您的示例数据看起来不像是制表符分隔;-)但是,您可以将输出代码(最后一行)中的' ' (空格) ' '更改为't'以将制表符用作分隔符。

总结

以上是内存溢出为你收集整理的Linux合并巨大的gzip文件,只保留十字路口全部内容,希望文章能够帮你解决Linux合并巨大的gzip文件,只保留十字路口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1247272.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存