Reputation: 65
suppose that we have a bio_sequence like :
VYDDGYHNGN
and we're gonna place random number of '.' in random positions along the sequence, sth like this:
..VY.DD...GY..HN.GN..
is there any function or optimum solution for this task in MATLAB?
Upvotes: 0
Views: 59
Reputation: 221574
cumsum
based approach -
seq = 'VYDDGYHNGN'; %// Input sequence
N = numel(seq); %// number of elements in input sequence
grplen = ceil(0.2*N); %// group length
idx = cumsum(randi(grplen,1,N)) %// random indices for elements in output sequence
outseq = repmat('.',1, idx(end)+randi(grplen,1) ) %// placeholder for output
outseq(idx)=seq; %// Put elements from seq into outseq at random places indexed by idx
Sample output -
outseq =
V.YDDG.Y.H.NG.N.
Upvotes: 3
Reputation: 112689
According to your comments, the following assumes that:
Code:
%// Data
seq = 'VYDDGYHNGN';
%// Let's go
m = numel(seq); %// sequence length
n = randi([0 round(.2*m)]); %// number of dots
p = m+n;
result = repmat('.', 1, p); %// initiallize result to all dots
result(sort(randsample(p,m))) = seq; %// place sequence in uniformly random positions
Upvotes: 2
Reputation: 1320
This could work:
sequence = 'VYDDGYHNGN';
factor = .2;
n = numel(sequence);
len = randi(n,n+1,1)-(1-factor)*n;
sequence = [sequence repmat('.',1,len(n+1))];
for idx = n:-1:1
sequence= [sequence(1:idx-1) repmat('.',1,len(idx)) sequence(idx:end)];
end
sequence
Avoiding the loop can be done as follows:
sequence = 'VYDDGYHNGN';
factor = .2;
n = numel(sequence);
len = randi(n,n+1,1)-(1-factor)*n;
seqdot = repmat('.',1,n+sum(max(0,len)));
seqdot((1:n) + cumsum(max(0,len(1:end-1)))') = sequence;
Upvotes: 0