【问题标题】:occurance of a blank row in text file after the insertion of a header row插入标题行后文本文件中出现空白行
【发布时间】:2016-06-25 04:35:34
【问题描述】:

我想在每个 .tsv 文件中插入自定义标头,解析每个文件的内容并附加到最​​终的变体文件。我试图在一个循环中完成此操作,但它不起作用,所以我尝试了两个单独的循环。第一个循环将标题插入到每个 .tsv 文件中,但是,第二个循环将插入的标题替换为空白行。有人可以解释为什么会发生这种情况以及如何解决这个问题吗?谢谢

#!perl
use strict;
use warnings;

my $home="/data/";                                                     
my $tsv_directory = $home."test_all_runs/".$ARGV[0];
my $tsvfiles = $home."test_all_runs/".$ARGV[0]."/tsv_files.txt";

my @run_directory = (); @run_directory = split /\//, $tsv_directory; print "The run directory is #############".$run_directory[3]."\n";

my $cmd = `ls $tsv_directory/FOCUS*\.tsv > $tsvfiles`; #print "$cmd";
my $cmda = "ls $tsv_directory/FOCUS*\.tsv > $tsvfiles"; #print "$cmda";

my @tsvfiles =();
#this code opens the vcf_files.txt file and passes each line into an array for indidivudal manipulation
open(TXT2, "$tsvfiles");
        while (<TXT2>){  
                push (@tsvfiles, $_);   
                }
close(TXT2);

foreach (@tsvfiles){
        chop($_);
}
#this loop works fine
for my $tsv_file (@tsvfiles){

    $tsv_file =~ m|([^/]+)-oncomine.tsv$| or die "Can't extract Sample ID";
    my $sample_id = $1;
    print "The sample ID is ############## $sample_id\n";
    my $headerline = $run_directory[3]."/".$sample_id;
   my $cmd9 = `sed -i '1i$headerline' $tsv_file`; print $cmd9;#local @ARGV = ($tsv_file);  
}

my $final_variants = $home."test_all_runs/".$ARGV[0]."/final_variant_file.txt";
open my $out_fh, '>', $final_variants or die qq{Unable to open "$final_variants" for output: $!};

my @tsv_files_new = glob $tsv_directory."/FOCUS*.tsv";

##this loop unintentionally replaces the newly inserted header with a blank line.
for my $tsv_file_new ( @tsv_files_new ) {

    print "The current VCF is ############# $tsv_file_new\n";

    $tsv_file_new =~ m|([^/]+)-oncomine.tsv$| or die "Can't extract Sample ID";
    my $sample_id = $1;
    print "The sample ID is ############## $sample_id\n";

    open my $in_fh, '<', $tsv_file_new
            or die qq{Unable to open "$tsv_file_new" for input: $!};

    while ( <$in_fh> ) {

        next if /^#/;
        next if /\b(?:CNV|intronic|synonymous|utr_3|utr_5)\b/;
        next if /\b(?:FORMAT.1.FSRF)\b/;# remove the original headers from Ion Reporter.

        my @fields = split;
        next if ($fields[70] =~ m|([0.])/\1|);
        my $chr = $fields[9]."check";

        my @wanted = ( 10, 21, 67, 68, 70, 77, 78, 81, 83, 84, 88, 92, 98, 100 );
        my $current_line = join "\t", @fields[@wanted];
        my $current_final_line = $sample_id."\t".$chr."\t".$current_line;
        print $out_fh $current_final_line, "\n";
    }
}
exit;

【问题讨论】:

  • 你好。这看起来像我的代码!你试过什么?您应该发布您正在使用的整个程序并寻求帮助,但您的想法是错误的。您来自对一个 Perl 程序使用多个文件和 shell 命令,您只需要在打印之前更改构建$current_line 的方式。写完后再修改是错误的想法
  • 我看到您将程序拆分为涉及一行的问题,并为每个问题提出新问题。所有这些问题都可以通过学习语言来解决,因为这些问题并不难编码。对您而言,最好描述您的整个问题,而不是一步一步地询问。
  • @Borodin 您的代码没有问题。我只是想学习如何一次完成一件事,而不会让我的问题变得太长和混乱。我有可以工作的代码,但是我有很多中间文件,但是现在我正在学习有一种更好的方法,我可以从头开始弄清楚整个过程。我已经使用 my $cmd9 = sed -i '1i$SampleID[4]' $Controldata; print $cmd9; 将第一行添加到每个行并且它工作但现在我需要能够以某种方式在 while 循环中插入自定义标头。

标签: perl


【解决方案1】:

试试这个:

use strict;
use warnings;
use Tie::File;

my $tsv_file = 'test.txt';

tie my @textFile, 'Tie::File', $tsv_file, recsep => "\n"  or die $!;
for my $line (@textFile) {
    $line = "New Sample\n" if($line =~ /FORMAT\.1\.FSRF/);
}
untie @textFile;

【讨论】:

  • 它创建了一个没有文本“新样本”的空行。我正在尝试操作每个样本的第一行,以便显示样本相关信息。您的代码几乎可以工作,但我没有看到“新示例”只是一个空行。谢谢安德烈。
  • 是的,我认为那是因为文件中的数据尚未写入。这遵循Duplicated output with for each loop
【解决方案2】:

在 Perl 中,您可以将 AAA 替换为 BBB

$string =~ s/AAA/BBB/;

所以如果你想:

#replace lines containing "FORMAT.1.FSRF" with "New Sample"

然后尝试:

$string =~ s/FORMAT\.1\.FSRF/New Sample/g;

如果您只想对每一行进行一次替换,请删除最后一个 g

【讨论】:

  • 我正在尝试删除标题行并将它们替换为特定于每个文件的自定义标题。只有标题行包含“FORMAT.1.FSRF”,因此我将其用作搜索字符串。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 2021-01-31
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 2016-02-04
  • 1970-01-01
相关资源
最近更新 更多