3
$\begingroup$

Hi there I have generated a counts table of samples I need to compare by differential expression analysis.

The layout of the counts table is as follows:

Gene_id Sample_A_r1 Sample_A_r2 Sample_B_r1 Sample_B_r2 Sample_AB_r1 Sample_AB_r2
KLTH001 0 0 2 5 8 3
KLTH002 0 0 5 3 9 7
YL99812 6 4 0 0 12 14
YL99813 9 7 0 0 15 19

So here, for the differential expression analysis, I only want to compare for example the genes starting with "YL-". So in a new file I want the rows including the first row which is "Gene_id" and the sample names e.g. "Sample_A_r1" and then only the rows with genes starting with "YL-" in their names. This will allow me to only compare the changes between Sample_A alone versus when it was combined with Sample_B, or in the table Sample_AB.

If anyone can please give me an indication of how to do this in linux I would really appreciate it. I have seen other similar posts, involving grep and awk I am just not sure of exactly how to do this. I am still quite new to linux. Any help will be appreciated.

$\endgroup$
2
  • $\begingroup$ Would something like this work? awk ' $1 YL* ' counts_table.tsv > edited_counts_table.tsv $\endgroup$
    – Justin1609
    Sep 25, 2021 at 17:00
  • $\begingroup$ Would this preserve the header/first line? $\endgroup$
    – haci
    Sep 25, 2021 at 17:28

3 Answers 3

3
$\begingroup$

Something like this should do job:

# creating a new file with the first line of the original    
head -1 foo.txt > bar.txt

# ">>" appends to the file on the right hand side
# "^" dictates that the search term should be at the beginning of a line
grep "^YL" foo.txt >> bar.txt 
$\endgroup$
1
  • $\begingroup$ Thanks very much, will give it a try now $\endgroup$
    – Justin1609
    Sep 26, 2021 at 9:26
1
$\begingroup$

One way using :

awk 'NR==1 || /^YL/ { print $0 }' file.tsv

The default action is to print the line/record, so all you need is the condition:

awk 'NR==1 || /^YL/' file.tsv
$\endgroup$
3
  • $\begingroup$ Thanks I will try this out, it seems the previous command didn't copy all of the occurrences of "YL-" across. Could you maybe suggest how I could just remove any gene names starting with "KLTH-" from my original counts file as well? I would just like to be able to check that I got all records from the original file in the separated files I create. $\endgroup$
    – Justin1609
    Sep 26, 2021 at 10:49
  • $\begingroup$ @Justin1609 You can just negate the pattern match expression: awk 'NR==1 || !/^KLTH/' file. This will just print the first line, or lines that don't start with KLTH. $\endgroup$
    – Steve
    Sep 26, 2021 at 11:02
  • 1
    $\begingroup$ okay awesome thanks @Steve I appreciate it, will give it a try $\endgroup$
    – Justin1609
    Sep 26, 2021 at 14:37
1
$\begingroup$

Alternatively, you can use grep like so to remove all lines starting with KLTH:

grep -v '^KLTH' in_file > out_file

For example:

echo "Gene_id\nKLTH001\nYL99812" | grep -v '^KLTH'

Prints:

Gene_id
YL99812

Here, GNU grep uses option:
-v : Print lines that do not match.

In the regular expression (regex):
^ : Match the beginning of the line.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.