【问题标题】:Perl: Run script on multiple files in multiple directoriesPerl:在多个目录中的多个文件上运行脚本
【发布时间】:2018-09-01 04:58:39
【问题描述】:

我有一个 perl 脚本,它读取一个 .txt 和一个 .bam 文件,并创建一个名为 output.txt 的输出。

我有很多文件都在不同的文件夹中,但文件名和目录路径略有不同。

我所有的 txt 文件都位于名为 PointMutation 的不同子文件夹中,完整路径为

/Volumes/Lab/Data/Darwin/Patient/[Plate 1/P1H10]/PointMutation

括号中的文本是更改的部分,但 Patient 子文件夹包含我所有的 txt 文件。

我的 .bam 文件位于名为 DNA 的子文件夹中,完整路径为

/Volumes/Lab/Data/Darwin/Patient/[Plate 1/P1H10]/SequencingData/DNA

目前我如何在终端上运行这个脚本

cd /Volumes/Lab/Data/Darwin/Patient/[Plate 1/P1H10]/PointMutation
perl ~/Desktop/Scripts/Perl.pl "/Volumes/Lab/Data/Darwin/Patient/[Plate 
1/P1H10]/PointMutation/txtfile.txt" "/Volumes/Lab/Data/Darwin/Patient/[Plate 
1/P1H10]/SequencingData/DNA/bamfile.bam"

只有一两个文件,这相当容易,但一旦文件变得更大,我想自动化它。同样,一旦我运行了一次,我就不想再这样做了,但是我会从同一位患者那里获得更多信息,有没有办法阻止读取文件夹?

【问题讨论】:

    标签: perl sh


    【解决方案1】:

    我会这样做:

    for my $dir (glob "/Volumes/Lab/Data/Darwin/Patient/*/"){
        # skip if not a directory
        if (! -d $dir) {
            next;
        }
        my $txt = "$dir/PointMutation/txtfile.txt";
        my $bam = "$dir/SequencingData/DNA/bamfile.bam";
    
        # ... you magical stuff here
    }
    

    这是假设/Volumes/Lab/Data/Darwin/Patient/下的所有目录都遵循约定。

    也就是说,使用大量不同文件组织分析的更长期/稳健的方法是 1)将每个分析所需的所有文件组织在一个目录下,或 2)创建元文件(我会使用 JSON/yaml),其中包含必要的文件名。

    【讨论】:

    • 有没有办法将脚本中的当前目录设置为 PointMutation 文件夹?我对 perl 还很陌生,什么是 JSON/yaml,元文件是临时文件吗?
    • 你可以做chdir "/whatever/directory/you/want". And yes, I think it's a good idea to change into the most relevant directory. JSON and YAML are common format for storing simple data structures, see their wikipedia pages, and the corresponding perl modules JSON and YAML. But, if you're just getting started, the sample I gave and the chdir`函数应该让你走得更远。
    • “创建包含必要文件名的元文件(我会使用 JSON/yaml)” 对于文件路径列表,没有理由使用其他任何东西比平面文件。而且你可以在不涉及外部模块的情况下神奇地阅读它!
    • @Borodin 如果没有 JSON/yaml,你会怎么做?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2015-09-03
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多