0

I have a text file with following information:

Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
MTHFR   TCGA-BD-A2L6-01A-11D-A20W-10    4524    BCM GRCh38
SLC30A1 TCGA-BD-A2L6-01A-11D-A20W-10    7779    BCM GRCh38
USH2A   TCGA-BD-A2L6-01A-11D-A20W-10    7399    BCM GRCh38
SOS1    TCGA-BD-A2L6-01A-11D-A20W-10    6654    BCM GRCh38
TMEM51  TCGA-O8-A75V-01A-11D-A32G-10    55092   BCM GRCh38
FLG TCGA-O8-A75V-01A-11D-A32G-10    2312    BCM GRCh38
FLG TCGA-O8-A75V-01A-11D-A32G-10    2312    BCM GRCh38
PRDM16  TCGA-G3-A7M5-01A-11D-A33Q-10    63976   BCM GRCh38
DNAJC11 TCGA-G3-A7M5-01A-11D-A33Q-10    55735   BCM GRCh38
HNRNPCL2    TCGA-G3-A7M5-01A-11D-A33Q-10    440563  BCM GRCh38
C1orf94 TCGA-G3-A7M5-01A-11D-A33Q-10    84970   BCM GRCh38
NFYC    TCGA-G3-A7M5-01A-11D-A33Q-10    4802    BCM GRCh38
IPP TCGA-G3-A7M5-01A-11D-A33Q-10    3652    BCM GRCh38

As you see there are multiple samples I want to split the file into multiple files based on the column "Tumor_Sample_Barcode". The output files need to be named with samplename.txt.

First output - TCGA-BD-A2L6-01A-11D-A20W-10.txt

Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
MTHFR   TCGA-BD-A2L6-01A-11D-A20W-10    4524    BCM GRCh38
SLC30A1 TCGA-BD-A2L6-01A-11D-A20W-10    7779    BCM GRCh38
USH2A   TCGA-BD-A2L6-01A-11D-A20W-10    7399    BCM GRCh38
SOS1    TCGA-BD-A2L6-01A-11D-A20W-10    6654    BCM GRCh38

Second output - TCGA-O8-A75V-01A-11D-A32G-10.txt

Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
TMEM51  TCGA-O8-A75V-01A-11D-A32G-10    55092   BCM GRCh38
FLG TCGA-O8-A75V-01A-11D-A32G-10    2312    BCM GRCh38
FLG TCGA-O8-A75V-01A-11D-A32G-10    2312    BCM GRCh38

Third output - TCGA-G3-A7M5-01A-11D-A33Q-10.txt

Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
PRDM16  TCGA-G3-A7M5-01A-11D-A33Q-10    63976   BCM GRCh38
DNAJC11 TCGA-G3-A7M5-01A-11D-A33Q-10    55735   BCM GRCh38
HNRNPCL2    TCGA-G3-A7M5-01A-11D-A33Q-10    440563  BCM GRCh38
C1orf94 TCGA-G3-A7M5-01A-11D-A33Q-10    84970   BCM GRCh38
NFYC    TCGA-G3-A7M5-01A-11D-A33Q-10    4802    BCM GRCh38
IPP TCGA-G3-A7M5-01A-11D-A33Q-10    3652    BCM GRCh38

How to do this linux?

3
  • Thank you for the reply. But I don't see any headers in the output files. How to get the columns names also in the outputs? Commented Jan 31, 2018 at 12:51
  • 1
    @user3351523, "headers" should be the next moment. The first moment should be your posting a testable input (as a text, not as image) Commented Jan 31, 2018 at 12:53
  • Yes, sorry for that. I posted test table input as text now. How to get the headers in output files? Commented Jan 31, 2018 at 12:57

1 Answer 1

5

Awk solution:

awk 'NR==1{ h=$0 }NR>1{ print (!a[$2]++? h ORS $0 : $0) > $2".txt" }' file
  • NR==1{ h=$0 } - capture the 1st line/record as header line (NR points to a record number, $0 - contains the current line)
  • NR > 1 - for all records except the first one:
    • <cond>? <operand_1> : <operand_2> - classical ternary operator
    • !a[$2]++? - check for the 1st occurrence of barcode value $2 used as a key of associative array a
    • h ORS $0 - common header line concatenated with ORS(output record separator, defaults to \n) and current record $0
    • print ... > $2".txt" - print custom content or the current line(if nothing was specified) into file <barcode_value>.txt

Or a more self-explanatory version:

awk 'NR==1 {header = $0; next}
     !header_printed[$2]++ {print header > $2".txt"}
     {print > $2".txt"}' < file

Viewing results:

$ head TCGA*.txt
==> TCGA-BD-A2L6-01A-11D-A20W-10.txt <==
Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
MTHFR   TCGA-BD-A2L6-01A-11D-A20W-10    4524    BCM GRCh38
SLC30A1 TCGA-BD-A2L6-01A-11D-A20W-10    7779    BCM GRCh38
USH2A   TCGA-BD-A2L6-01A-11D-A20W-10    7399    BCM GRCh38
SOS1    TCGA-BD-A2L6-01A-11D-A20W-10    6654    BCM GRCh38

==> TCGA-G3-A7M5-01A-11D-A33Q-10.txt <==
Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
PRDM16  TCGA-G3-A7M5-01A-11D-A33Q-10    63976   BCM GRCh38
DNAJC11 TCGA-G3-A7M5-01A-11D-A33Q-10    55735   BCM GRCh38
HNRNPCL2    TCGA-G3-A7M5-01A-11D-A33Q-10    440563  BCM GRCh38
C1orf94 TCGA-G3-A7M5-01A-11D-A33Q-10    84970   BCM GRCh38
NFYC    TCGA-G3-A7M5-01A-11D-A33Q-10    4802    BCM GRCh38
IPP TCGA-G3-A7M5-01A-11D-A33Q-10    3652    BCM GRCh38

==> TCGA-O8-A75V-01A-11D-A32G-10.txt <==
Hugo_Symbol Tumor_Sample_Barcode    Entrez_Gene_Id  Center  NCBI_Build
TMEM51  TCGA-O8-A75V-01A-11D-A32G-10    55092   BCM GRCh38
FLG TCGA-O8-A75V-01A-11D-A32G-10    2312    BCM GRCh38
FLG TCGA-O8-A75V-01A-11D-A32G-10    2312    BCM GRCh38

To adjust a filename based on 15-char sequence of barcode value:

awk 'NR==1{ h=$0 }NR>1{ print (!a[$2]++? h ORS $0 : $0) > substr($2, 1, 15)".txt" }' file 
3
  • Thank you very much !! Could you please explain the command? Commented Jan 31, 2018 at 13:13
  • Please explain the command and could you also tell me how to get the output files with only 0-15 substring in the sample names like TCGA-BD-A2L6-01.txt, TCGA-G3-A7M5-01.txt and TCGA-O8-A75V-01.txt Commented Jan 31, 2018 at 13:31
  • 1
    @user3351523, yes, see my explanation Commented Jan 31, 2018 at 13:47

You must log in to answer this question.

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