1

I have a file that looks like this:

Chr Start   End Ref Alt Func.refGene    Gene.refGene    ExonicFunc.refGene  AAChange.refGene    Func.knownGene  Gene.knownGene                                                      
1   53387379    53387379    G   C   UTR5    ECHDC2  NA  NA  UTR5    ECHDC2(FFF)
1   53387380    53387380    G   C   UTR5    C2(hhh) NA  NA  UTR5    C2(FFF)
1   1647814 1647814 T   C   exonic  CDK11A,CDK11B   synonymous SNV  NA  exonic  CDK11A,CDK11B
1   1647814 1647814 T   C   exonic  CDK11A23,CDK11B23   synonymous SNV  NA  exonic  CDK11A23,CDK11B23
1   1670958 1670958 C   G   exonic  SLC35E2A    synonymous SNV  NA  exonic  SLC35E2
1   1684347 1684347 -   CCT exonic  NADK    nonframeshift insertion NA  exonic  NADK
1   7069620 7069620 T   C   intronic    PTPN6(ggg),IL3  NA  NA  intronic    PTPN6(ggg),IL3

I would like to output all lines that contain the genes "C2", "CDK11A" and "IL3". Obviously, I have a much bigger file and a longer set of genes, but this is just a small example for convenience.

I have been using the following script:

tail -n+1 Book3.txt | awk -F'\t' 'BEGIN{OFS=FS}{if(NR==1 || $7=="C2" || $7~/C2[(]/ || $7~/C2/  || $11=="C2" || $11~/C2[(]/ || $11~/C2/ || 
$7=="CDK11A" || $7~/CDK11A[(]/ || $7~/CDK11A/ || $11=="CDK11A" || $11~/CDK11A[(]/ || $11~/CDK11A/ || 
$7=="IL3" || $7~/IL3[(]/ || $7~/IL3/ || $11=="IL3" || $11~/IL3[(]/ || $11~/IL3/) {print($0)}}' > Book3.genes.txt

This script outputs unnecessary lines, as you can see below:

Chr     Start   End     Ref     Alt     Func.refGene    Gene.refGene    ExonicFunc.refGene      AAChange.refGene        Func.knownGene  Gene.knownGene
1       53387379        53387379        G       C       UTR5    ECHDC2  NA      NA      UTR5    ECHDC2(FFF)
1       53387380        53387380        G       C       UTR5    C2(hhh) NA      NA      UTR5    C2(FFF)
1       1647814 1647814 T       C       exonic  CDK11A,CDK11B   synonymous SNV  NA      exonic  CDK11A,CDK11B
1       1647814 1647814 T       C       exonic  CDK11A23,CDK11B23       synonymous SNV  NA      exonic  CDK11A23,CDK11B23
1       7069620 7069620 T       C       intronic        PTPN6(ggg),IL3  NA      NA      intronic        PTPN6(ggg),IL3

I don't want lines 2 and 5. How can I modify the script to have only the given list of genes in the output?

1 Answer 1

0

Put the genes you want to match into a file, one per line. Then it's just a grep call:

grep -Fwf genes.txt Book3.txt 

To preserve the header:

{ head -n1 Book3.txt; grep -Fwf genes.txt Book3.txt; }

grep options:

  • -F "fixed strings" -- disable regular expressions, look only for substring matches
  • -w "word matches" -- look only for matches that are whole words
  • -f file -- specify file containing patterns (one per line)

With your sample data

$ cat genes.txt 
C2
CDK11A
IL3
$ { head -n1 Book3.txt; grep -Fwf genes.txt Book3.txt; }
Chr Start   End Ref Alt Func.refGene    Gene.refGene    ExonicFunc.refGene  AAChange.refGene    Func.knownGene  Gene.knownGene
1   53387380    53387380    G   C   UTR5    C2(hhh) NA  NA  UTR5    C2(FFF)
1   1647814 1647814 T   C   exonic  CDK11A,CDK11B   synonymous SNV  NA  exonic  CDK11A,CDK11B
1   7069620 7069620 T   C   intronic    PTPN6(ggg),IL3  NA  NA  intronic    PTPN6(ggg),IL3

You must log in to answer this question.

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