• Publicidad

Generación de secuencias aleatorias

Perl aplicado a la bioinformática

Generación de secuencias aleatorias

Notapor Marne » 2013-11-12 06:57 @331

Buenas, después de generar un ADN random, que pego a continuación con explicaciones, compuesto por valores 'A', 'T' 'G' y 'C', no sé cómo hacer para que en una cadena, el número de caracteres de un tipo no exceda en más de uno el número de caracteres de otro tipo.

Muchas gracias de antemano, estoy bastante agobiado porque solo consigo que el ordenador me escupa a la cara por mi ignorancia. :oops:

# Generate random DNA
# using a random number generator to randomly select bases

use strict;
use warnings;

# Declare and initialize the variables
my $size_of_set = 12;
my $maximum_length = 30;
my $minimum_length = 15;

# An array, initialized to the empty list, to store the DNA in
my @random_DNA = ( );

# Seed the random number generator.
# time|$$ combines the current time with the current process id
srand(time|$$);

# And here's the subroutine call to do the real work
@random_DNA = make_random_DNA_set( $minimum_length, $maximum_length, $size_of_set );

# Print the results, one per line
print "Here is an array of $size_of_set randomly generated DNA sequences\n";
print " with lengths between $minimum_length and $maximum_length:\n\n";

foreach my $dna (@random_DNA) {

print "$dna\n";
}

print "\n";

exit;

################################################################################
# Subroutines
################################################################################

# make_random_DNA_set
#
# Make a set of random DNA
#
# Accept parameters setting the maximum and minimum length of
# each string of DNA, and the number of DNA strings to make
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.

sub make_random_DNA_set {

# Collect arguments, declare variables
my($minimum_length, $maximum_length, $size_of_set) = @_;

# length of each DNA fragment
my $length;

# DNA fragment
my $dna;
my $resto=0;
# set of DNA fragments
my @set;

# Create set of random DNA
for (my $i = 0; $i < $size_of_set ; ++$i) {

do {
# find a random length between min and max
$length = randomlength ($minimum_length, $maximum_length);
$resto= $length%2;
}
until ($resto==0);
# make a random DNA fragment
$dna = make_random_DNA ( $length );

# add $dna fragment to @set
push( @set, $dna );
}

return @set;
}

# Notice that we've just discovered a new subroutine that's
# needed: randomlength, which will return a random
# number between (or including) the min and max values.
# Let's write that first, then do make_random_DNA

# randomlength
#
# A subroutine that will pick a random number from
# $minlength to $maxlength, inclusive.
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.

sub randomlength {

# Collect arguments, declare variables
my($minlength, $maxlength) = @_;

# Calculate and return a random number within the
# desired interval.
# Notice how we need to add one to make the endpoints inclusive,
# and how we first subtract, then add back, $minlength to
# get the random number in the correct interval.
return ( int(rand($maxlength - $minlength + 1)) + $minlength );
}

# make_random_DNA
#
# Make a string of random DNA of specified length.
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.

sub make_random_DNA {

# Collect arguments, declare variables
my($length) = @_;

my $dna;

for (my $i=0 ; $i < $length ; ++$i) {

$dna .= randomnucleotide( );
}

return $dna;
}

# We also need to include the previous subroutine
# randomnucleotide.
# Here it is again for completeness.

# randomnucleotide
#
# Select at random one of the four nucleotides
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.

sub randomnucleotide {

my(@nucleotides) = ('A', 'C', 'G', 'T');


# scalar returns the size of an array.
# The elements of the array are numbered 0 to size-1
return randomelement(@nucleotides);
}

# randomelement
#
# randomly select an element from an array
#
# WARNING: make sure you call srand to seed the
# random number generator before you call this function.

sub randomelement {

my(@array) = @_;

return $array[rand @array];
}
Marne
Perlero nuevo
Perlero nuevo
 
Mensajes: 10
Registrado: 2013-11-12 06:46 @324

Publicidad

Re: Generación de secuencias aleatorias

Notapor explorer » 2013-11-12 09:09 @423

Bienvenido a los foros de Perl en Español, Marne.

Una cosa que puedes hacer es: dado que sabemos la longitud de la cadena a generar, la dividimos entre 4, y así sabemos cuántas bases de cada tipo hay que generar.

Luego, se puede hacer de varias maneras el generar esas secuencias aleatorias. Una de ellas sería generar un array con tantas letras por base que necesitamos, y luego un bucle que vaya sacando las letras de forma aleatoria, con splice(). Así, el array se va reduciendo, creando otro.

Otra forma más sencilla: hacer los cambios en el mismo array:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2. use v5.10;
  3.  
  4. my $largo = 12;
  5. my @seq = (("A") x $largo, ("T") x $largo, ("C") x $largo, ("G") x $largo);
  6.  
  7. say @seq;
  8.  
  9. for (1..1000) {
  10.  
  11.     my $x = rand @seq;  # elegimos dos posiciones al azar
  12.     my $y = rand @seq;
  13.  
  14.     @seq[$x, $y] = @seq[$y, $x];
  15. }
  16.  
  17. say @seq;
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Ejemplo de salida:
Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
AAAAAAAAAAAATTTTTTTTTTTTCCCCCCCCCCCCGGGGGGGGGGGG
ACGTACATTGTGGACGCTTGACACCTAGTGTACATGCGCCGTATGAAC
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14476
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Generación de secuencias aleatorias

Notapor Marne » 2013-11-19 14:03 @627

Muchas gracias, me has quitado un dolor de cabeza, a ver ahora cómo hago las alineaciones multicadena, que por ahora estoy más pez que el carajo.

Gracias de nuevo y un saludo.
Marne
Perlero nuevo
Perlero nuevo
 
Mensajes: 10
Registrado: 2013-11-12 06:46 @324


Volver a Bioinformática

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados