Search code examples
matlabmatrixcellbioinformatics

How to split a cell 1x1 in Matlab?


Let's say I have a 1x1 cell with this value : 'atcatcaaca' . My goal is :

  1. Add 1 to 5 'a' beside any 'a'.

  2. Add 1 to 10 'c' beside any 'c'.

  3. Add a random number of 'g' beside any 'g'.

  4. Add a random number of 't' beside any 't'.

    For example i have 'atcatcaaca'.My goal is to make it like:'aaaattttcccaattttccaaaaaaaaaacccccccccaa'

My thought is to take the value cell and split it somehow in a matrix: a | t | a | t | c |a |a | c | a. Is it possible?And if it is ,how?

The code is :

filename = fullfile(matlabroot,'virus_nucleotitde2.dat');

Z = readtable(filename);

S = table2cell(Z);

num_rows = size (Z,1);
num_cols = size (Z,2);
for i=1:1:num_rows
   for j=1:1:num_cols
    B = S(i,j);
    a=0;
    c=0;
    g=0;
    t=0;


B{1} = num2cell(B{1});

n = randi(6);  % Random number between 1 and 6
B{1} = strrep(B{1} , 'a' , repmat('a' , 1, n));

n = randi(11);  % Random number between 1 and 11
B{1} = strrep(B{1} , 'c' , repmat('c' , 1, n));

n = randi(11); 
B{1} = strrep(B{1} , 'g' , repmat('g' , 1, n));

n = randi(11); 
B{1} = strrep(B{1} , 't' , repmat('t' , 1, n));

    end

end

Solution

  • Inside the cell, there is a char that you can access with curly brackets:

     S = {'ataggatag'};
     B = S{1};
     disp(B)
    

    Then, strrep is your friend:

    n = randi(6);  % Random number between 1 and 6
    B = strrep(B , 'a' , repmat('a' , 1, n));
    
    n = randi(11);  % Random number between 1 and 11
    B = strrep(B , 'c' , repmat('c' , 1, n));
    
    n = randi(11); 
    B = strrep(B , 'g' , repmat('g' , 1, n));
    
    n = randi(11); 
    B = strrep(B , 't' , repmat('t' , 1, n));
    

    Then put it back into the cell

    S{1} = B;
    disp(S)
    

    Note that I used 6 as maximum number of 'a's because strrep is going to replace the original a, not adding letters beside it as you asked.

    EDIT:

    Following OP's edit, here is your solution:

    S = {'ataggatag'};
    
    num_rows = size (S,1);
    num_cols = size (S,2);
    
    for i=1:1:num_rows
       for j=1:1:num_cols
            n = randi(6);  % Random number between 1 and 6
            S{i,j} = strrep(S{i,j} , 'a' , repmat('a' , 1, n));
    
            n = randi(11);  % Random number between 1 and 11
            S{i,j} = strrep(S{i,j} , 'c' , repmat('c' , 1, n));
    
            n = randi(11); 
            S{i,j} = strrep(S{i,j} , 'g' , repmat('g' , 1, n));
    
            n = randi(11); 
            S{i,j} = strrep(S{i,j} , 't' , repmat('t' , 1, n));
        end
    
    end
    
    disp(S)