Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: remove entries with duplicate characters

by davi54 (Sexton)
on Jul 31, 2019 at 21:54 UTC ( [id://11103674]=note: print w/replies, xml ) Need Help??


in reply to Re^2: remove entries with duplicate characters
in thread remove entries with duplicate characters

I want to set to paragraph mode because each of my entries is multiple lines long and is separated by a new line. So, I was reading about perl, it said I can use the paragraph mode to specify that. As I said, I'm new and still learning, let me know if it's wrong. Also, when I ran the code after correcting what you suggested, it gave me the following errors:

Use of uninitialized value $count_in in printf at ../remove_duplicate_genes.pl line 34.

0 total records read from 'enzymes.fasta'

Use of uninitialized value $count_out in printf at ../remove_duplicate_genes.pl line 35.

0 records written to 'duplicate_gene_entries_in_enzymes.fasta' after removing duplicate entries

  • Comment on Re^3: remove entries with duplicate characters

Replies are listed 'Best First'.
Re^4: remove entries with duplicate characters
by 1nickt (Canon) on Aug 01, 2019 at 04:36 UTC

    So like I said before, you should make an SSCCE. You can use the __DATA__ section to include sample data and read from it as a filehandle. Then after you get your data processing code working, you can add back in the file opening and reading part. (But I highly recommend using Path::Tiny for all your file handling.)

    The following script uses data stored within it for the input file, then prints the output to a file in the temp directory, then reads the file in and verifies that it is as expected.

    (Note: Because of the technique I use of printing the output to the output file with the same line separator used to break the input into "paragraphs," the string is restored to the end of each line printed, so, given that the first line never had it removed, the result is that each line begins with the same initial string as in the original file. This has the side effect of leaving the string (">sp") alone on the last line of the file.)

    use strict; use warnings; use feature 'say'; use Path::Tiny; use Test::More; # When not testing, something like this: # my $in_file = path("/path/to/file"); # my $fh = $in_file->openr; my $script = path(__FILE__); my $out_file = path( sprintf('/tmp/%s-%s.log', $script->basename('.pl' +), time) ); my %seen; my ($count_in, $count_out); { # set the line separator locally to a string that matches # the break between your "paragraphs". local $/ = "\n>sp"; while ( my $line = <DATA> ) { # <$fh> $count_in++; if ( $line =~ /GN=(\S*)/ ) { next if $seen{$1}; $count_out++; $seen{$1} = 1; $out_file->append( $line ); } } } say ' Records processed: ' . $count_in; say 'Duplicates removed: ' . ($count_in - $count_out); #----------------------------------------------------# ## Test. Always test. is( $count_in, 5, 'Input read OK'); is( $count_out, 3, 'Correct number of records kept'); my @written = $out_file->lines; is( (grep { /GN/ } @written), 3, 'File has correct count for +"GN"'); for my $value (qw/ TUFA Blorgle Blargle /) { is( (grep { /$value/ } @written), 1, sprintf('File has correct co +unt for "%s"', $value) ); } done_testing; __DATA__ >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=TUFA PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=Blorgle PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR >sp|Q43467|EFTU1_SOYBN Elongation factor Tu, chloroplastic OS=Glycine +max OX=3847 GN=TUFA PE=3 SV=1 MAVSSATASSKLILLPHASSSSSLNSTPFRSSTTNTHKLTP This is a dupe but has extra content >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=Blargle PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR >sp|O24310|EFTU_PEA Elongation factor Tu, chloroplastic OS=Pisum sativ +um OX=3888 GN=Blorgle PE=2 SV=1 MALSSTAATTSSKLKLSNPPSLSHTFTASASASVSNSTSFR
    Script output:
    $ perl 11103674.pl Records processed: 5 Duplicates removed: 2 ok 1 - Input read OK ok 2 - Correct number of records kept ok 3 - File has correct count for "GN" ok 4 - File has correct count for "TUFA" ok 5 - File has correct count for "Blorgle" ok 6 - File has correct count for "Blargle" 1..6

    Hope this helps!


    The way forward always starts with a minimal test.
Re^4: remove entries with duplicate characters
by Laurent_R (Canon) on Aug 01, 2019 at 20:46 UTC
    Use of uninitialized value $count_in in printf at ../remove_duplicate +_genes.pl line 34. 0 total records read from 'enzymes.fasta' Use of uninitialized value $count_out in printf at ../remove_duplicate +_genes.pl line 35. 0 records written to 'duplicate_gene_entries_in_enzymes.fasta' after r +emoving duplicate entries
    This is because you never increment the $count_in and $count_out variables in your code (and also did not provide a start value when you declared them).

    BTW, there is a built-in variable, $., which keeps track of the number of lines read from a file. You can just print its value after you've finished reading your file (instead of $count_in).

Re^4: remove entries with duplicate characters
by Anonymous Monk on Aug 02, 2019 at 07:37 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11103674]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-25 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found