Jalal Aghazadeh
Jalal Aghazadeh

Reputation: 65

Placing a character randomly in a bio-seq

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

Answers (3)

Divakar
Divakar

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

Luis Mendo
Luis Mendo

Reputation: 112689

According to your comments, the following assumes that:

  • The total number of dots is a random integer uniformly distributed between 0 and 0.2 times sequence length rounded to the nearest integer.
  • Dot locations are also random and uniformly distributed.

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

MeMyselfAndI
MeMyselfAndI

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

Related Questions