Wednesday, August 26, 2015

Using zgrep and regular expression

If you have a file like the one below and want to select all AP1 entries and AP1 control entries, use regular expressions within grep or zgrep (if the file is gzipped).


IRF4_1_C2 chr1 11450 11464 +
MEF2_disc3_C1 chr1 11451 11459 +
REL_2_C7 chr1 11452 11461 -
DDIT3::CEBPA_1_C7 chr1 11454 11466 -
ESRRG_1_C5 chr1 11459 11476 +
HOXB7_1_C7 chr1 11462 11477 +
HOXB3_2_C8 chr1 11464 11473 +
NR2E1_2_C5 chr1 11464 11477 +
GATA_known7_C3 chr1 11465 11475 +
HOXA2_3_C3 chr1 11465 11474 -
PITX1_2_C7 chr1 11465 11473 -
PITX3_2_C6 chr1 11465 11473 -
NR4A_known2_C7 chr1 11465 11481 +
RUNX2_5_C9 chr1 11472 11489 +
RUNX1_9_C5 chr1 11473 11483 -
MYB_1_C4 chr1 11477 11494 +
EN1_4_C6 chr1 11480 11489 +
NFY_known2_C4 chr1 11482 11495 +
BHLHE22_1 chr1 11482 11493 +
ATF3_disc1_C10 chr1 11482 11491 +
BHLHE23_1 chr1 11483 11492 +
BHLHE23_1 chr1 11483 11492 -
OLIG1_1 chr1 11483 11492 +
OLIG1_1 chr1 11483 11492 -
TFE_1_C8 chr1 11486 11493 +
HOXD13_2_C8 chr1 11487 11496 -
GLI2_2_C1 chr1 11489 11500 -
IRF_known9_C5 chr1 11492 11503 -
IRF_disc6_C9 chr1 11782 11789 +
SOX8_8_C1 chr1 11783 11795 -
AP1_known8_C2 chr1 11784 11792 +
...

To select AP1 both known and discovered use (disc|known) and to select to one digit or two digit numbers use [0=9]{1,2}

zgrep "^AP1_\(disc\|known\)[0-9]\{1,2\}_C[0-9]\{1,2\}" matches-with-controls.txt.gz > ~/AP1_cont.txt

head AP1_cont.txt
AP1_known8_C2 chr1 11784 11792 +
AP1_disc4_C9 chr1 11850 11862 -
AP1_disc4_C7 chr1 11850 11862 -
AP1_known1_C10 chr1 12262 12272 -
AP1_disc6_C6 chr1 12409 12423 -
AP1_known5_C1 chr1 12466 12474 -
AP1_disc6_C7 chr1 12779 12793 +
AP1_disc6_C1 chr1 12779 12793 +
AP1_disc5_C9 chr1 13115 13123 -
AP1_disc6_C8 chr1 13153 13167 -

zgrep "^AP1_\(disc\|known\)[0-9]\{1,2\}" matches-with-controls.txt.gz > ~/AP1.txt

head AP1.txt
AP1_known8_C2 chr1 11784 11792 +
AP1_disc4_C9 chr1 11850 11862 -
AP1_disc4_C7 chr1 11850 11862 -
AP1_known1_C10 chr1 12262 12272 -
AP1_disc6_C6 chr1 12409 12423 -
AP1_known5_C1 chr1 12466 12474 -
AP1_disc6_C7 chr1 12779 12793 +
AP1_disc6_C1 chr1 12779 12793 +
AP1_disc1 chr1 12839 12848 -
AP1_disc5_C9 chr1 13115 13123 -

No comments:

Post a Comment