0

I have .tsv files in more than 100 directories. I want make a single file with all the information needed from all those tsv files in the 100 directories.

For example:

Data
 |___ SOB33D
        |___ SOB33D.tsv
 |___ SOB43E
        |___ SOB43E.tsv
 |___ SOB58D
        |___ SOB58D.tsv
 |___ SOB113A
        |___ SOB113A.tsv

The data in SOB33D.tsv looks like below:

target_id         length    eff_length  est_counts
ENST00000456328.2   1657      1525.05      0
ENST00000450305.2   632       500.105      0
ENST00000488147.1   1351      1219.05    0.492522
ENST00000619216.1   68        12.9174    0.70395
ENST00000473358.1   712       580.105      0
ENST00000469289.1   535       403.105      0

SOB43E.tsv:

target_id   length  eff_length  est_counts
ENST00000456328.2   1657    1525.05 0.174591
ENST00000450305.2   632 500.105 0
ENST00000488147.1   1351    1219.05 7.70424
ENST00000619216.1   68  12.9174 0.295008
ENST00000473358.1   712 580.105 0
ENST00000469289.1   535 403.105 0

SOB58D.tsv:

target_id   length  eff_length  est_counts
ENST00000456328.2   1657    1525.05 0.282655
ENST00000450305.2   632 500.105 0
ENST00000488147.1   1351    1219.05 2.64778
ENST00000619216.1   68  12.9174 0
ENST00000473358.1   712 580.105 0
ENST00000469289.1   535 403.105 0

SOB113A.tsv:

target_id   length  eff_length  est_counts
ENST00000456328.2   1657    1525.05 0.0225974
ENST00000450305.2   632 500.105 0
ENST00000488147.1   1351    1219.05 1.35652
ENST00000619216.1   68  12.9174 0
ENST00000473358.1   712 580.105 0
ENST00000469289.1   535 403.105 0

I'm trying to use cut. I'm almost correct in doing it. I want the 1st and 2nd columns which are the same in all the files and 4th column which is different in all the files. So, I used it like below:

paste */*.tsv | cut -f 1,2,4,8,12,16 > all_samples.tsv

In the above command I took 1st and 2nd column which is same in all files and the 4th columns from all files. And the output looked like below:

Output:

target_id        length est_counts  est_counts  est_counts  est_counts
ENST00000456328.2   1657    0   0.174591    0.282655    0.0225974
ENST00000450305.2   632 0   0   0   0
ENST00000488147.1   1351    0.492522    7.70424 2.64778 1.35652
ENST00000619216.1   68  0.70395 0.295008    0   0
ENST00000473358.1   712 0   0   0   0
ENST00000469289.1   535 0   0   0   0

Expected Output:

target_id         length    SOB33D  SOB43E  SOB58D  SOB113A
ENST00000456328.2   1657    0   0.174591    0.282655    0.0225974
ENST00000450305.2   632 0   0   0   0
ENST00000488147.1   1351    0.492522    7.70424 2.64778 1.35652
ENST00000619216.1   68  0.70395 0.295008    0   0
ENST00000473358.1   712 0   0   0   0
ENST00000469289.1   535 0   0   0   0

For less number of files I can use paste, but I have 100 files in 100 directories. So, how to make a single file out of all those .tsv files in 100 directories with folder names as column names?

Any help is appreciated. thanq

2
  • You want it for 100 files, I mean variable number, not for 4, right?
    – thanasisp
    Commented Dec 4, 2020 at 14:41
  • Yes, I want to merge all 100 files. All these 100 files have 1st, 2nd and 3rd columns are same....only the 4th column is different. Commented Dec 4, 2020 at 14:51

1 Answer 1

3
$ cat tst.awk
BEGIN {
    FS=OFS="\t"
    numCols = 2
}
{
    if ( FNR == 1 ) {
        numCols++
        val = FILENAME
        sub("/[^/]+$","",val)
        sub(".*/","",val)
    }
    else {
        val = $4
    }
    vals[FNR,1] = $1
    vals[FNR,2] = $2
    vals[FNR,numCols] = val
}
END {
    for (rowNr=1; rowNr<=FNR; rowNr++) {
        for (colNr=1; colNr<=numCols; colNr++) {
            printf "%s%s", vals[rowNr,colNr], (colNr<numCols ? OFS : ORS)
        }
    }
}

$ awk -f tst.awk */estimate.tsv
target_id       length  SOB33D  SOB43E
ENST00000456328.2       1657    0       0.174591
ENST00000450305.2       632     0       0
ENST00000488147.1       1351    0.492522        7.70424
ENST00000619216.1       68      0.70395 0.295008
ENST00000473358.1       712     0       0
ENST00000469289.1       535     0       0

The above was run using this input (all spaces are tabs):

$ head */estimate.tsv
==> SOB33D/estimate.tsv <==
target_id       length  eff_length      est_counts
ENST00000456328.2       1657    1525.05 0
ENST00000450305.2       632     500.105 0
ENST00000488147.1       1351    1219.05 0.492522
ENST00000619216.1       68      12.9174 0.70395
ENST00000473358.1       712     580.105 0
ENST00000469289.1       535     403.105 0

==> SOB43E/estimate.tsv <==
target_id       length  eff_length      est_counts
ENST00000456328.2       1657    1525.05 0.174591
ENST00000450305.2       632     500.105 0
ENST00000488147.1       1351    1219.05 7.70424
ENST00000619216.1       68      12.9174 0.295008
ENST00000473358.1       712     580.105 0
ENST00000469289.1       535     403.105 0
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .