计算每列的比率并保留标题

尊敬的stackoverflow用户,

我有>数百列的输入文件TSV(制表符分开):

**more in.tsv:**

reg  A  B  C  D 
reg1 A1 B1 C1 D1
reg2 A2 B2 C2 D2
reg3 A3 B3 C3 D3

A1-N are numeric value a can be zero also. reg, reg1-N and A-D are string.

我想计算每个数值的比率并将其保存到新文件中:

**more A.tsv**

reg  A/B   A/C   A/D 
reg1 A1/B1 A1/C1 A1/D1
reg2 A2/B2 A2/C2 A2/D2
reg3 A3/B3 A3/C3 A3/D3

但是我还需要保留第一行-我需要知道哪一列被划分为另一列。

依此类推:

**more B.tsv**

reg  B/A   B/C   B/D 
reg1 B1/A1 B1/C1 B1/D1
reg2 B2/A2 B2/C2 B2/D2
reg3 B3/A3 B3/C3 B3/D3

I found very similar question here, but it does not keep first row (header). And it does not save new file according name of first row (A.tsv, B.tsv and so on).

我也更喜欢awk或python中的解决方案。

这不能很好地工作:

awk '
{
   gsub(/\r/,"")
}
{
  nf=NF
  close(out_file)
  for(k=2;k<=nf;k++){
    out_file=""
    for(i=2;i<=nf;i++){
      if($i!=0){
         $(NF+1)=sprintf("%.03f",$k/$i)
      }
      else{
         $(NF+1)=sprintf("%s","NaN")
      }
    }
    out_file=k"field_out_file"
    print >> (out_file)
    NF=nf
  }
}'  in.tsv