How can i extract data from data from table using a string of words.

I am trying to extract data from a file but using a nested loop and if statement has not worked. The main file has column 1 as genes ranging form (1:60000) its a very large file. These genes have names like DUOX1 and so on. Now each column has the patient name a total of 45. Each gene has an expression number for each patient. What i want to do is to extract a certain amount of genes by using their names to extract all the expression for all patients.
the loop that i have been trying has been:
%16 TDS Genes
data_table=readtable("Normal and Tumor TPM.xlsx","Sheet","Normal Expression","VariableNamingRule","preserve");
tds_genes_table=readtable("16 TDS.xlsx","Sheet","Normal");
genes_16 = string(tds_genes_table.Properties.VariableNames);
n = max(size(data_table)) %gives total amount of genes in the main file "data". Each has a name like DUOX1
m = max(size(genes_16)) %contains gene names that i want to extract from data
%loop to match 16 tds genes with patient and expression
for i = 1:n
for j = 1:m
if i=j
expression(j,:) = data_table(i,:)
end
end
end

6 Comments

Could you show 2 or 3 example lines from the file?
Can you please please help me. I have been stuck on how to do this for days. I cant seem to find a video where they do something similar.
Gene_ID TCGA-E8-A2JQ-11A-11R-A180-07 TCGA-EL-A3ZK-11A-11R-A23N-07 TCGA-BJ-A28X-11A-11R-A22L-07 TCGA-BJ-A28R-11A-11R-A16R-07 TCGA-EL-A3T8-11A-11R-A22L-07 TCGA-EL-A3TA-11A-12R-A22L-07 TCGA-FY-A3TY-11A-12R-A22U-07 TCGA-EL-A3MY-11A-12R-A21D-07 TCGA-EL-A3T6-11A-11R-A220-07 TCGA-EL-A3T3-11A-11R-A22L-07 TCGA-EL-A3ZR-11A-11R-A23N-07 TCGA-EL-A3MW-11A-11R-A20F-07 TCGA-ET-A3DW-11A-11R-A19O-07 TCGA-BJ-A2NA-11A-11R-A19O-07 TCGA-EL-A3GZ-11A-11R-A20F-07 TCGA-EL-A3ZT-11A-13R-A23N-07 TCGA-EL-A3TB-11A-11R-A22L-07 TCGA-BJ-A2N8-11A-11R-A18C-07
TSPAN6 30.9273 37.8304 29.9709 45.7266 49.4516 61.7303 60.7903 60.4433 41.7224 53.1328 56.1531 69.1416 55.8076 53.2203 73.4547 52.5637 60.1408 76.1689
TNMD 0 0.0838 0.0948 0.0769 0.434 0.2774 0.451 0.0422 0.1076 0.4358 0.3598 0.1382 0.163 0.2915 0.414 0.0849 0.147 0.2917
DPM1 87.4576 90.3226 73.453 89.4248 84.4784 101.8336 104.2385 109.321 124.3312 88.2581 108.3735 128.2581 101.027 92.3699 113.2799 93.7593 123.7256 110.0408
SCYL3 8.9487 8.6067 8.9914 8.1128 8.8326 9.4938 11.6166 10.7698 4.816 9.4601 9.9522 12.021 8.338 7.9479 13.0286 11.6256 9.4572 11.3216
C1orf112 3.229 1.9059 2.3983 1.9962 1.3461 1.8086 1.9823 1.6055 2.0223 1.3261 2.1347 2.1751 1.31 1.4171 1.7529 1.9103 2.108 1.7741
FGR 28.9093 28.9257 32.1856 10.2355 6.0092 11.015 4.5053 2.7421 13.5278 3.1893 18.2832 5.6288 3.4152 10.0699 4.6527 3.9835 7.0573 2.5333
CFH 58.737 27.9973 76.9869 80.2089 64.6325 18.5704 68.0059 59.9789 63.4502 58.2025 101.8999 101.5552 37.3535 47.628 66.9276 35.1158 63.0888 22.023
FUCA2 40.7753 51.9767 30.6895 44.2405 51.6707 55.3599 49.7212 69.9161 44.246 55.7906 50.764 58.3266 72.2659 56.581 56.1382 46.7411 47.402 56.4635
GCLC 16.2476 21.7845 12.3872 16.291 24.3931 23.1927 21.637 22.5404 25.1553 23.3499 23.1543 20.5903 27.517 18.8355 16.3973 19.8936 27.5128 30.5839
NFYA 34.7495 37.3848 40.1026 33.5339 38.7505 50.5133 42.6987 42.5445 46.7278 36.6773 56.0446 57.4263 35.8168 26.4893 41.1087 44.0327 47.2563 41.4378
STPG1 4.8789 5.6672 4.0002 4.5807 6.8899 8.0866 6.4658 7.079 5.8733 5.7554 7.2582 7.2453 5.9585 4.9366 6.2555 6.1697 6.4181 6.3636
NIPAL3 39.2969 38.7241 32.129 40.6468 45.3107 54.0195 44.5086 58.3458 28.5164 40.595 39.6359 47.749 45.7777 37.047 55.5895 57.7469 48.9915 65.9087
My computer does allow me to upload an image but essential the tcgas are the columns of the table. Genes are the all the rows for each patient starting at column 1
GENE_ID TCGA1 TCGA2
GENE 1 262 666
GENE 2 555 676
thats a good representation of how the file looks on excel
what i want to do is extract these 16 genes form the large file with all the expression information
this are the 16 genes:
DIO1 DIO2 DUOX1 DUOX2 FOXE1 GLIS3 NKX2-1 PAX8 SLC26A4 SLC5A5 SLC5A8 TG THRA THRB TPO TSHR

Sign in to comment.

Answers (2)

I think the Access Data in a Table page is what you want. You might also be interested in the height and width functions for tables. Also, I think this is a scenario where it is best to set the TextType to string.
I think the code might look something like this.
data_table=readtable("Normal and Tumor TPM.xlsx","Sheet","Normal Expression","VariableNamingRule","preserve","TextType","string")
tds_genes_table=readtable("16 TDS.xlsx","Sheet","Normal","TextType","string")
genes_16 = string(tds_genes_table.Properties.VariableNames)
idx = ismember(data_table.Gene_ID,genes_16)
expression = data_table(idx,:)
data_table=readtable("Normal and Tumor TPM.xlsx","Sheet","Normal Expression","VariableNamingRule","preserve");
geneslist=strtrim({'DIO1';'DIO2';'DUOX1';'DUOX2';'FOXE1';'GLIS3';'NKX2-1';'PAX8';'SLC26A4 ';'SLC5A5 ';'SLC5A8 ';'TG ';'THRA ';'THRB ';'TPO ';'TSHR'});
data_table.GENEVARIABLENAME=categorical(data_table.GENEVARIABLENAME); % turn to categorical if isn't; you don't tell us what the variable names are...
tWantedByGene=data_table(matches,data_table.GENEVARIABLENAME,geneslist),:); % select rows with matching genes
should be all you're looking for...
As always, attaching a representative section of the data file would facilitate writing actual code to address any problems.

Categories

Asked:

on 9 Sep 2022

Edited:

dpb
on 9 Sep 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!