【发布时间】: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