in reply to How do i extract only contigs of interest

Searching for information taken from one file in another file is quite a common task. The usual solution is to hash the identifiers, then iterate over the second file and check the hash for the existence of the identifier.

In your case, you have to change the identifiers, as the underscore in contig names is not present in the genbank:

#!/usr/bin/perl use warnings; use strict; my %contig; open my $CNTG, '<', 'contig_names' or die $!; while (<$CNTG>) { chomp; s/_//; # Remove the underscore, as it doesn't appear in the genba +nk. $contig{$_} = 1; } local $/ = '//'; # Read the file one record a time. open my $GB, '<', 'genbank' or die $!; while (<$GB>) { if (my ($id) = /LOCUS \s+ \S+ _ (contig [0-9]+)/x) { print if $contig{$id}; } }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: How do i extract only contigs of interest
by faozhi (Acolyte) on Sep 09, 2015 at 12:18 UTC
    It is working now: this is my final code
    #!/usr/bin/perl use warnings; use strict; my %contig; open (my $CNTG, '<', $ARGV[0]) or die $!; while (<$CNTG>) { chomp; # s///; # Remove the underscore, as it doesn't appear in the genba +nk. $contig{$_} = 1; # print $_; } local $/ = '//'; # Read the file one record a time. open (my $GB, '<', $ARGV[1]) or die $!; while (<$GB>) { if (my ($id) = /LOCUS \s+ \S+ _ (contig [0-9]+)/x) { print if $contig{$id}; } } ~
    THANKS!