Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: remove entries with duplicate characters

by 1nickt (Canon)
on Aug 01, 2019 at 04:36 UTC ( [id://11103692]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-28 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found