本脚本可以将从ENA下载的MD5文件(如下)
run_accession fastq_md5 fastq_ftp
SRR6039701 abd202ee5560384dc8e52e5e924c19ae;8469ccc631726de6fab0119e01732b57 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/001/SRR6039701/SRR6039701_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/001/SRR6039701/SRR6039701_2.fastq.gz
SRR6039704 6d44e41b97c339fc36895cb8e77a0949;f95ea68a0a7d4aa0c412ebf73ae7137b ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/004/SRR6039704/SRR6039704_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/004/SRR6039704/SRR6039704_2.fastq.gz
SRR6039713 1545094f325090e923132181d08ab800;6a51a3d036bab41479351531a27dd468 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/003/SRR6039713/SRR6039713_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/003/SRR6039713/SRR6039713_2.fastq.gz
SRR6039715 28ef2ac1974d6c302f7509ea61dcecc1;84155b05232383efe9bf0eb327e13d46 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/005/SRR6039715/SRR6039715_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/005/SRR6039715/SRR6039715_2.fastq.gz
SRR6039716 643ee49e5a20a415eadb063ec119f344;76a1e485c02961e3c9c7d99557551d3c ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/006/SRR6039716/SRR6039716_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/006/SRR6039716/SRR6039716_2.fastq.gz
SRR6039720 5269407a490a021ce00be6f3bd5b9ace;946b2307eb0def52cd2abe85c527c709 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/000/SRR6039720/SRR6039720_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/000/SRR6039720/SRR6039720_2.fastq.gz
SRR6039723 8657038bf0b51e5ca5011598457d394c;08c117bde33115c6f062d818237eb226 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/003/SRR6039723/SRR6039723_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/003/SRR6039723/SRR6039723_2.fastq.gz
SRR6039724 ffe27f5c5fb243b2ac12636187fa47c4;475b2ebed9ec973579d743e88960b6b3 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/004/SRR6039724/SRR6039724_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/004/SRR6039724/SRR6039724_2.fastq.gz
SRR6039726 510de07b9e579b37a58e72bcde795c42;bfdc0820f7965a1fe1025d51febea0e3 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/006/SRR6039726/SRR6039726_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/006/SRR6039726/SRR6039726_2.fastq.gz
SRR6039727 e1c2bdc5f0fb2329396613d0a807a065;8ffc58658489bb0918e8a5944cd6048f ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/007/SRR6039727/SRR6039727_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/007/SRR6039727/SRR6039727_2.fastq.gz
SRR6039728 238ae623a91cd87ab222a5cd3d41fe6b;c33fbaccea58a6ec1cd96a5efdc81894 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/008/SRR6039728/SRR6039728_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/008/SRR6039728/SRR6039728_2.fastq.gz
SRR6039732 fdd341b5ad1fafccf24479f12f11224b;1010af8747f2e017e4ad15b71a1dda2b ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/002/SRR6039732/SRR6039732_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/002/SRR6039732/SRR6039732_2.fastq.gz
SRR6039735 a161e2dad0fbcb38805b0edb2d006274;78499623c7db41560082ce28f6c190e6 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/005/SRR6039735/SRR6039735_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/005/SRR6039735/SRR6039735_2.fastq.gz
SRR6039736 2d43a8a4423a373b6ab8325c3229a832;ca8e26f8dfa930557a5dd74ab0662b15 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/006/SRR6039736/SRR6039736_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/006/SRR6039736/SRR6039736_2.fastq.gz
SRR6039739 ff2f7cad774c7352bcc02adfd2377b19;c3e983ce8f6186abf1050d26ce6bfb33 ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/009/SRR6039739/SRR6039739_1.fastq.gz;ftp.sra.ebi.ac.uk/vol1/fastq/SRR603/009/SRR6039739/SRR6039739_2.fastq.gz
转化成md5sum 可以识别的格式
abd202ee5560384dc8e52e5e924c19ae SRR6039701_1.fastq.gz
8469ccc631726de6fab0119e01732b57 SRR6039701_2.fastq.gz
6d44e41b97c339fc36895cb8e77a0949 SRR6039704_1.fastq.gz
f95ea68a0a7d4aa0c412ebf73ae7137b SRR6039704_2.fastq.gz
代码如下
# 读取原始文件内容
with open(r'F:\SduPostDoc\Maize-Genome-and-Transcriptome\Maize-transcriptome-after-pathogen-infection\玉米瘤黑粉\00Rawdata\file.md5.txt', 'r') as file:
lines = file.readlines()
output_lines = []
# 第一轮循环:处理原始文件格式,将每行拆分成样本ID、MD5哈希、URL
for line in lines:
parts = line.strip().split('\t')
sample_id = parts[0]
md5_urls = parts[1].split(';')
if len(md5_urls) == 2:
md5_hash_1 = md5_urls[0]
md5_hash_2 = md5_urls[1]
url_1 = parts[2].split(';')[0]
url_2 = parts[2].split(';')[1]
# 添加两行到输出,每行包含样本ID、MD5哈希和URL
output_lines.append(f"{sample_id}\t{md5_hash_1}\t{url_1}")
output_lines.append(f"{sample_id}\t{md5_hash_2}\t{url_2}")
elif len(md5_urls) == 1:
md5_hash = md5_urls[0]
url = parts[2]
# 添加一行到输出,包含样本ID、MD5哈希和URL
output_lines.append(f"{sample_id}\t{md5_hash}\t{url}")
# 将处理后的数据写入新文件
with open(r'F:\SduPostDoc\Maize-Genome-and-Transcriptome\Maize-transcriptome-after-pathogen-infection\玉米瘤黑粉\00Rawdata\file.md5new1.txt', 'w') as file:
file.write('\n'.join(output_lines))
# 读取处理后的文件
with open(r'F:\SduPostDoc\Maize-Genome-and-Transcriptome\Maize-transcriptome-after-pathogen-infection\玉米瘤黑粉\00Rawdata\file.md5new1.txt', 'r') as file:
lines = file.readlines()
output_lines = []
current_md5 = None
# 第二轮循环:将相同MD5哈希的文件名合并到一行
for line in lines:
parts = line.strip().split('\t')
md5_hash = parts[1]
filename = parts[2].split('/')[-1]
if current_md5 == md5_hash:
# 如果MD5哈希相同,将文件名添加到当前行
output_lines[-1] += f" {filename}"
else:
# 如果MD5哈希不同,创建新行,并添加MD5哈希和第一个文件名
current_md5 = md5_hash
output_lines.append(f"{md5_hash}\t{filename}")
# 将合并后的数据写入新文件
with open(r'F:\SduPostDoc\Maize-Genome-and-Transcriptome\Maize-transcriptome-after-pathogen-infection\玉米瘤黑粉\00Rawdata\file.md5new2.txt', 'w') as file:
file.write('\n'.join(output_lines))