001、保存为字典
(base) root@PC1:/home/test2# ls a.fastq test.py (base) root@PC1:/home/test2# cat a.fastq ## 测试fastq文件 @SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1 GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC + BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIFIIF @SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1 CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC + BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII @SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1 NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG + #0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII (base) root@PC1:/home/test2# cat test.py ## 测试脚本 #!/usr/bin/python in_file = open("a.fastq", "r") dict1 = dict() n = 0 for i in in_file: n += 1 i = i.strip() if n % 4 == 1: key = i dict1[key] = "" if n % 4 == 2: dict1[key] += i print(dict1) in_file.close() (base) root@PC1:/home/test2# python test.py ## 程序执行结果 {'@SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1': 'GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC', '@SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1': 'CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC', '@SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1': 'NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG'}
002、保存为文件
(base) root@PC1:/home/test2# ls a.fastq test.py (base) root@PC1:/home/test2# cat a.fastq ## 测试fastq文件 @SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1 GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC + BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIFIIIFIIF @SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1 CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC + BBBFFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII @SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1 NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG + #0<FFFFFFFFFFIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII (base) root@PC1:/home/test2# cat test.py ## 测试程序 #!/usr/bin/python in_file = open("a.fastq", "r") out_file = open("result.txt", "w") n = 0 for i in in_file: n += 1 if n % 4 == 1: out_file.write(i) if n % 4 == 2: out_file.write(i) in_file.close() out_file.close() (base) root@PC1:/home/test2# python test.py ## 执行程序 (base) root@PC1:/home/test2# ls a.fastq result.txt test.py (base) root@PC1:/home/test2# cat result.txt ## 程序运行结果 @SRR1596091.1 HISEQ:62:C35RDACXX:2:1101:1546:1996/1 GGAAAATAAACATCAGTTTCTTCCAATGAAATGTCTCTCATTCTATCAAC @SRR1596091.2 HISEQ:62:C35RDACXX:2:1101:1511:1997/1 CAAGCCACATCTAACAACAATATTAACTATCCAGAAAGGAGCCAGTTATC @SRR1596091.3 HISEQ:62:C35RDACXX:2:1101:2357:1925/1 NACGGAGCTGAGCTGGATTTCAGGAGACCTCCTGCCCAGCAGTCAGGAAG
参考:https://mp.weixin.qq.com/s?__biz=MzkyMTI1MTYxNA==&mid=2247493739&idx=1&sn=f690c93761307e6ec9bb77cca2eb4619&chksm=c184d21af6f35b0cda1d964ed896adee1091e1f615b7f6be0caf2508105275ca3ae66889c58e&mpshare=1&scene=23&srcid=0811LY0ghlyV0yNXki8WcW6m&sharer_sharetime=1660215059305&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd