# Print specific columns in a matrix on the basis of sample id's in the header

I have a matrix file (expression.txt) in which the first column is a gene_id and from the second column the sample id's start. This matrix has 20,000 columns with sample id's corresponding to different tissue. I want to extract the samples of one tissue. Example: SM-5GZZ7 SM-5GIEN SM-5EGGH are samples from brain tissue. So I want to separate the list of samples [(file=brain.txt;n=5000] corresponding to brain tissue from this matrix (with bash or R)

Name    SM-5GZZ7    SM-5EGHI    SM-5EGHJ    SM-5N9CS    SM-5GIEN    SM-5EGHH    SM-5EGGH
ENSG00000223972.5   0   0   0   0   0   0   0
ENSG00000227232.5   187 109 143 251 113 139 199
ENSG00000278267.1   0   0   1   0   0   0   0
ENSG00000243485.5   1   0   0   1   0   0   0
ENSG00000237613.2   0   0   0   0   0   0   0
ENSG00000268020.3   0   1   0   0   0   1   1


The file brain.txt looks like (Number of lines in this files = 5000)

SM-5GZZ7
SM-5GIEN
SM-5EGGH
SM-5GZXC
SM-5GZXO
SM-5GZY1
SM-5EGHK
SM-5EGHL

• This should answer the question stackoverflow.com/questions/11098189/… Mar 6 '21 at 18:13
• Thank you soo much @user438383 Mar 6 '21 at 18:44
• Hi @user438383. I tried the script by dogbane, it didn't work Mar 7 '21 at 18:06

Example in R. If you have a file of column names, names.txt, such as:

mpg
cyl
disp


and a data.frame like mtcars

> mtcars
mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360        14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
[ reached 'max' / getOption("max.print") -- omitted 23 rows ]


Then you can select the columns by:

library(dplyr)
> mtcars %>% dplyr::select(list_names\$V1)
mpg cyl  disp
Mazda RX4           21.0   6 160.0
Mazda RX4 Wag       21.0   6 160.0
Datsun 710          22.8   4 108.0
Hornet 4 Drive      21.4   6 258.0
Valiant             18.1   6 225.0
Duster 360          14.3   8 360.0
Merc 240D           24.4   4 146.7

• Thank you @user438383 for the help Mar 8 '21 at 14:23

There's a million different ways to do simple taks like this. Pick one and learn it:) Python is my go to as it can do pretty much everything you'd ever want. I saved your example as csv with no spaces called expression.txt.

import pandas as pd
df[['SM-5GZZ7','SM-5GIEN','SM-5EGGH']]

SM-5GZZ7    SM-5GIEN    SM-5EGGH
0   0   0.0 0.0
1   187 109 143 251 113 139 199 NaN NaN
2   0   0.0 0.0
3   1   0.0 0.0
4   0   0.0 0.0
5   0   0.0 1.0

• I too prefer Python :) Mar 6 '21 at 7:05
• Thank you for your answer. But I prefer to use bash. I have used awk in past to do this task (but only too get 10-12 columns). In this case, I have to extract 5000 columns based on a list in another file named (brain.txt). Mar 6 '21 at 8:54
• @Liam McIntyre: Can you something in bash or R? and I have to extract nearly 5000 columns from the mastermatrix.txt file. All the columns to be extracted are in a file brain.txt. Mar 7 '21 at 18:10
• I don't know how to off the top of my head sorry. I see that you've been given solutions in both bash and r. Now it's up to you to do some work to make them fit Ur problem. Mar 7 '21 at 22:20