• Publicidad

Sustituir letras en Perl

Perl aplicado a la bioinformática

Sustituir letras en Perl

Notapor wampaier » 2010-07-29 13:12 @592

Hola,
¿Podrían orientarme? Necesito sustituir la letra U por T con Perl, en mi archivo, ignorando la línea que contiene el mayor que, puesto que representa el nombre de mi secuencia. Gracias.

Sintáxis: [ Descargar ] [ Ocultar ]
Using text Syntax Highlighting
>A97104.7.1522UU :Streptococcaceae;Streptococcus
AGAGUUCGAU CCUGGCUCAG GACGAACGCU GGCGGCGUGC CUAAUACAUG CAAGUAGAAC GCUGAAGACU UUAGCUUGCU
AGAGUUGGAA GAGUUGCGAA CGGGUGAGUA ACGCGUAGGU AACCUGCCUA UUAGUGGGGG AUAACUAUUG GAAACGAUAG
Coloreado en 0.000 segundos, usando GeSHi 1.0.8.4
wampaier
Perlero nuevo
Perlero nuevo
 
Mensajes: 66
Registrado: 2008-08-12 12:50 @576

Publicidad

Re: Sustituir letras en Perl

Notapor explorer » 2010-07-29 13:17 @595

Esta es una de las múltiples formas de hacerlo...

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2. use 5.010;
  3. use strict;
  4. use warnings;
  5. use diagnostics;
  6.  
  7. my $fichero = qq(
  8. >A97104.7.1522UU :Streptococcaceae;Streptococcus
  9. AGAGUUCGAU CCUGGCUCAG GACGAACGCU GGCGGCGUGC CUAAUACAUG CAAGUAGAAC GCUGAAGACU UUAGCUUGCU
  10. AGAGUUGGAA GAGUUGCGAA CGGGUGAGUA ACGCGUAGGU AACCUGCCUA UUAGUGGGGG AUAACUAUUG GAAACGAUAG
  11. );
  12.  
  13. for my $linea ( split /\n/, $fichero ) {    # dividimos el fichero en líneas
  14.  
  15.     next if $linea ~~ /^>/;                 # saltamos a la siguiente si la línea comienza por '>'
  16.  
  17.     $linea =~ tr/U/T/;                      # traducción
  18. }
  19. continue {
  20.     say $linea;                             # impresión
  21. }
  22. __END__
  23.  
  24. >A97104.7.1522UU :Streptococcaceae;Streptococcus
  25. AGAGTTCGAT CCTGGCTCAG GACGAACGCT GGCGGCGTGC CTAATACATG CAAGTAGAAC GCTGAAGACT TTAGCTTGCT
  26. AGAGTTGGAA GAGTTGCGAA CGGGTGAGTA ACGCGTAGGT AACCTGCCTA TTAGTGGGGG ATAACTATTG GAAACGATAG
Coloreado en 0.005 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: 14475
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Sustituir letras en Perl

Notapor wampaier » 2010-07-29 14:35 @649

haaa ok gracias... solo que me marca un error no sera por la version que tengo??? mi version es la 5.8.8 le borre la linea que dice perl 5.10 pero me marca un error... es el siguiente:

syntax error at sustituir_U_T.pl line 30, near "$linea ~"
Execution of sustituir_U_T.pl aborted due to compilation errors (#1)
(F) Probably means you had a syntax error. Common reasons include:

A keyword is misspelled.
A semicolon is missing.
A comma is missing.
An opening or closing parenthesis is missing.
An opening or closing brace is missing.
A closing quote is missing.

Often there will be another error message associated with the syntax
error giving more information. (Sometimes it helps to turn on -w.)
The error message itself often tells you where it was in the line when
it decided to give up. Sometimes the actual error is several tokens
before this, because Perl is good at understanding random input.
Occasionally the line number may be misleading, and once in a blue moon
the only way to figure out what's triggering the error is to call
perl -c repeatedly, chopping away half the program each time to see
if the error went away. Sort of the cybernetic version of S<20
questions>.

Uncaught exception from user code:
syntax error at sustituir_U_T.pl line 30, near "$linea ~"
Execution of sustituir_U_T.pl aborted due to compilation errors.
at sustituir_U_T.pl line 42
wampaier
Perlero nuevo
Perlero nuevo
 
Mensajes: 66
Registrado: 2008-08-12 12:50 @576

Re: Sustituir letras en Perl

Notapor explorer » 2010-07-29 14:40 @653

Efectivamente, tienes un Perl de hace un montón de años...

Además de quitar el 'use 5.010;' debes cambiar el '~~' por '=~', y cambiar el say() por un print() con un "\n" al final.

Hay que modernizarse :)
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14475
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Sustituir letras en Perl

Notapor wampaier » 2010-07-29 14:49 @659

jajaja :lol: la verdad es que sí, ¿eh?... pero es que es la versión que se va actualizando automáticamente con Centos5 entonces... qué le puedo hacer :D oye, otra pregunta... ¿si es un archivo de muchas secuencias puedo poner esta línea?

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. open (my $fh, $ARGV[0]) or die "No se puede abrir el archivo\n";
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
y cambiar la variable
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. my $fichero
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
por
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
my $fh
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4
wampaier
Perlero nuevo
Perlero nuevo
 
Mensajes: 66
Registrado: 2008-08-12 12:50 @576

Re: Sustituir letras en Perl

Notapor explorer » 2010-07-29 15:24 @683

No... no...

$fichero contiene el fichero. Tu $fh es el controlador de fichero con el que te refieres al fichero abierto.

Lo que sí puedes hacer, una vez abierto el fichero, es leerlo todo de golpe a $fichero:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
my $fichero = join '', <$fh>;    # unimos todas las líneas leídas desde <$fh>
Coloreado en 0.001 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: 14475
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Sustituir letras en Perl

Notapor wampaier » 2010-07-30 08:27 @394

¡Aaaahhhh! Ok, ok, gracias... y creo que ya actualizaré mi Perl... porque aún es de la edad primitiva, creo yo... :lol: ¡¡¡¡gracias por la ayuda!!!!

¡¡Saludos!!
wampaier
Perlero nuevo
Perlero nuevo
 
Mensajes: 66
Registrado: 2008-08-12 12:50 @576


Volver a Bioinformática

¿Quién está conectado?

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

cron