0

基本的に、短時間で問題を解決できるスクリプトが必要です。私は2つのファイルを持っています:

$ ヘッド -n 6 fcu.tsv

NM576455     0.324009324     0.578896174     2577
NM539570     0.204545455     0.607877092     2247
NM337132     0.288973384     0.673636364     792
NM374379     0.308300395     0.42            762
NM373443     0.263043478     0.547132867     1383
NM371839     0.298210736     0.492857143     1512

$ ヘッド -n 6 mart.tsv

NM539570 ILMN_2199362    15      58.52   protein_coding
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_2
NM576455 ILMN_2195138    1       65.74   protein_coding  protein binding molecular_function      SAM_2
NM576455 ILMN_1709067    1       65.74   protein_coding  nucleus cellular_component      SAM_2
NM576455 ILMN_1709067    1       65.74   protein_coding  protein binding molecular_function      SAM_2
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_type1

非常に短い時間で、各 NM ID の mart.tsv に fcu.tsv の 2 番目、3 番目、および 4 番目のフィールドを追加する必要があります。

$ 頭出し.tsv

NM539570 ILMN_2199362    15      58.52   protein_coding  0.204545455     0.607877092     2247
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_2 0.324009324   0.578896174     2577
    NM576455 ILMN_2195138    1       65.74   protein_coding  protein binding molecular_function      SAM_2 0.324009324   0.578896174     2577
    NM576455 ILMN_1709067    1       65.74   protein_coding  nucleus cellular_component      SAM_2 0.324009324   0.578896174     2577
    NM576455 ILMN_1709067    1       65.74   protein_coding  protein binding molecular_function      SAM_2 0.324009324   0.578896174     2577
    NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_type1 0.324009324   0.578896174     2577

これは私がmatlabで行ったことです(新しいコードを書くよりも、ここで悪いコードを修正して高速化することをお勧めします)

fr1 = fopen('fcu.tsv', 'r');
fr2 = fopen('mart.tsv', 'r');

fw = fopen('out.tsv', 'w');

while feof(fr1) == 0
   line = fgetl(fr1);
   scan = textscan(line, '%s%f%f%d');

   frewind(fr2);

    while feof(fr2) == 0
        line2 = fgetl(fr2);
        scan2 = textscan(line2, '%s%s%s%f%s%s%s%s');

            if scan{1}{1} == scan2{1}{1} 

                fprintf(fw, '%s\t%f\t%f\t%d\n', line2, scan{2}, scan{3}, scan{4});

            end

    end

end

助けていただければ幸いです

4

2 に答える 2

2

を使用した片道awk。たとえばFNR == NR、引数の最初の入力ファイル ( fcu.tsv) を読み取り、最初のフィールドをキーとしてハッシュに保存し、残りのフィールド\tを値として結合します。FNR < NR読み取りの場合、最初のフィールドがハッシュのキーと一致する場合はmart.tsv、その値を行末で結合し、そうでない場合は元の行を出力します。

の内容script.awk:

BEGIN {
    OFS = "\t"
}

FNR == NR {
    for ( i = 2; i <= NF; i++ ) { 
        line = (line ? line OFS : "") $i
    }   
    fcu[ $1 ] = line 
    line = ""
    next
}

FNR < NR {
    if ( $1 in fcu ) { 
        print $0 OFS fcu[ $1 ]
    }   
    else {
        print $0
    }   
}

次のように実行します。

awk -f script.awk fcu.tsv mart.tsv

次の出力で:

NM539570 ILMN_2199362    15      58.52   protein_coding 0.204545455     0.607877092     2247
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_2  0.324009324     0.578896174     2577
NM576455 ILMN_2195138    1       65.74   protein_coding  protein binding molecular_function      SAM_2  0.324009324     0.578896174     2577
NM576455 ILMN_1709067    1       65.74   protein_coding  nucleus cellular_component      SAM_2  0.324009324     0.578896174     2577
NM576455 ILMN_1709067    1       65.74   protein_coding  protein binding molecular_function      SAM_2  0.324009324     0.578896174     2577
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component      SAM_type1      0.324009324     0.578896174     2577
于 2012-07-15T10:33:22.070 に答える
0

これはコマンドライン中心のソリューションであり、サポートしているすべてのシステムで機能coreutilsします。あなたのケースに適用できない場合はお詫びします。

mart.tsv次のように、適切にパディングされた場合:

NM539570 ILMN_2199362    15      58.52   protein_coding  NA      NA                 NA                      NA
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component NA                      SAM_2
NM576455 ILMN_2195138    1       65.74   protein_coding  protein binding            molecular_function      SAM_2
NM576455 ILMN_1709067    1       65.74   protein_coding  nucleus cellular_component NA                      SAM_2
NM576455 ILMN_1709067    1       65.74   protein_coding  protein binding            molecular_function      SAM_2
NM576455 ILMN_2195138    1       65.74   protein_coding  nucleus cellular_component NA                      SAM_type1

解決策は簡単ですjoin( を参照info join)。

$ join <(sort mart.tsv) <(sort fcu.tsv) | column -t
NM539570  ILMN_2199362  15  58.52  protein_coding  NA       NA                  NA                  NA         0.204545455  0.607877092  2247
NM576455  ILMN_1709067  1   65.74  protein_coding  nucleus  cellular_component  NA                  SAM_2      0.324009324  0.578896174  2577
NM576455  ILMN_1709067  1   65.74  protein_coding  protein  binding             molecular_function  SAM_2      0.324009324  0.578896174  2577
NM576455  ILMN_2195138  1   65.74  protein_coding  nucleus  cellular_component  NA                  SAM_2      0.324009324  0.578896174  2577
NM576455  ILMN_2195138  1   65.74  protein_coding  nucleus  cellular_component  NA                  SAM_type1  0.324009324  0.578896174  2577
NM576455  ILMN_2195138  1   65.74  protein_coding  protein  binding             molecular_function  SAM_2      0.324009324  0.578896174  2577

columnbsdmainutilsパッケージから来ています。

于 2012-07-15T10:12:08.803 に答える