Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对每对字段执行计算_Python_Perl_Bash_Awk_Calculated Columns - Fatal编程技术网

Python 对每对字段执行计算

Python 对每对字段执行计算,python,perl,bash,awk,calculated-columns,Python,Perl,Bash,Awk,Calculated Columns,对于基因分析,我试图将2概率文件(10gb)转换为3概率文件。基本上,我必须在每2个其他实例之后插入第三列,第三列可以计算为1-(第一个实例+第二个实例)。你会怎么做 发件人: (该文件包含许多列和行) 到 在awk中: awk '{for(i=1;i<=NF;i+=2)$(i+1)=$(i+1)OFS sprintf("%.3f",1-$(i+1)-$i)}1' OFS='\t' file 0.800 0.200 0.000 0.000 0.200 0.800 0

对于基因分析,我试图将2概率文件(10gb)转换为3概率文件。基本上,我必须在每2个其他实例之后插入第三列,第三列可以计算为1-(第一个实例+第二个实例)。你会怎么做

发件人:

(该文件包含许多列和行)

awk
中:

awk '{for(i=1;i<=NF;i+=2)$(i+1)=$(i+1)OFS sprintf("%.3f",1-$(i+1)-$i)}1' OFS='\t' file
0.800   0.200   0.000   0.000   0.200   0.800   0.800   0.200   0.000
0.000   0.900   0.100   0.000   0.900   0.100   0.000   0.900   0.100
0.900   0.010   0.090   0.900   0.010   0.090   0.770   0.010   0.220
awk'{for(i=1;iIn
awk

awk '{for(i=1;i<=NF;i+=2)$(i+1)=$(i+1)OFS sprintf("%.3f",1-$(i+1)-$i)}1' OFS='\t' file
0.800   0.200   0.000   0.000   0.200   0.800   0.800   0.200   0.000
0.000   0.900   0.100   0.000   0.900   0.100   0.000   0.900   0.100
0.900   0.010   0.090   0.900   0.010   0.090   0.770   0.010   0.220
awk'{for(i=1;i
#!/usr/bin/env perl
严格使用;
使用警告;
*ARGV=*数据;#仅用于演示
而(){
咀嚼;
我的@fields=split;
我的@output;
而(@fields>=2){
my($x,$y)=拼接@字段,0,2;
推送@output,$x,$y,sprintf“%.3f”,1.0-($x+$y);
}
打印联接(““x 3,@output,@fields)”,“\n”;
}
__资料__
0.800   0.200   0.000   0.200   0.800   0.200
0.000   0.900   0.000   0.900   0.000   0.900
0.900   0.010   0.900   0.010   0.770   0.010
输出:

0.800 0.200 0.000 0.000 0.200 0.800 0.800 0.200 0.000 0.000 0.900 0.100 0.000 0.900 0.100 0.000 0.900 0.100 0.900 0.010 0.090 0.900 0.010 0.090 0.770 0.010 0.220 0.800 0.200 0.000 0.000 0.200 0.800 0.800 0.200 0.000 0.000 0.900 0.100 0.000 0.900 0.100 0.000 0.900 0.100 0.900 0.010 0.090 0.900 0.010 0.090 0.770.010 0.220
#!/usr/bin/env perl
严格使用;
使用警告;
*ARGV=*数据;#仅用于演示
而(){
咀嚼;
我的@fields=split;
我的@output;
而(@fields>=2){
my($x,$y)=拼接@字段,0,2;
推送@output,$x,$y,sprintf“%.3f”,1.0-($x+$y);
}
打印联接(““x 3,@output,@fields)”,“\n”;
}
__资料__
0.800   0.200   0.000   0.200   0.800   0.200
0.000   0.900   0.000   0.900   0.000   0.900
0.900   0.010   0.900   0.010   0.770   0.010
输出:

0.800 0.200 0.000 0.000 0.200 0.800 0.800 0.200 0.000 0.000 0.900 0.100 0.000 0.900 0.100 0.000 0.900 0.100 0.900 0.010 0.090 0.900 0.010 0.090 0.770 0.010 0.220 0.800 0.200 0.000 0.000 0.200 0.800 0.800 0.200 0.000 0.000 0.900 0.100 0.000 0.900 0.100 0.000 0.900 0.100 0.900 0.010 0.090 0.900 0.010 0.090 0.770.010 0.220
#!/usr/bin/perl
使用严格;使用警告;
my$template=加入“\t”,(“%.3f”)x3;
而(){
我的@fields=split;
@字段%2==0或“字段数不均匀”;
而(my($x,$y)=拼接@字段,0,2){
printf$template,$x,$y,1-($x+$y);
打印@fields?\t:“\n”;
}
}
用法:
perlscript.pl输出文件

#!/usr/bin/perl
使用严格;使用警告;
my$template=加入“\t”,(“%.3f”)x3;
而(){
我的@fields=split;
@字段%2==0或“字段数不均匀”;
而(my($x,$y)=拼接@字段,0,2){
printf$template,$x,$y,1-($x+$y);
打印@fields?\t:“\n”;
}
}
用法:
perlscript.pl输出文件

0.800 0.200 0.000 0.000 0.200 0.800 0.800 0.200 0.000 0.000 0.900 0.100 0.000 0.900 0.100 0.000 0.900 0.100 0.900 0.010 0.090 0.900 0.010 0.090 0.770 0.010 0.220
#!/usr/bin/perl
use strict; use warnings;

my $template = join "\t", ("%.3f")x3;

while (<>) {
  my @fields = split;
  @fields % 2 == 0 or die "Uneven number of fields";
  while (my ($x, $y) = splice @fields, 0, 2) {
    printf $template, $x, $y, 1 - ($x + $y);
    print  @fields ? "\t" : "\n";
  }
}