| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: inst-abbr-ids.pl | ||
| 3 | # ABSTRACT: Abbreviate seq ids in FASTA files (optimized) | ||
| 4 | # CONTRIBUTOR: Valerian LUPO <valerian.lupo@uliege.be> | ||
| 5 | |||
| 6 | use Modern::Perl '2011'; | ||
| 7 | use autodie; | ||
| 8 | |||
| 9 | use File::Basename; | ||
| 10 | use Getopt::Euclid qw(:vars); | ||
| 11 | use Path::Class qw(dir file); | ||
| 12 | use Smart::Comments; | ||
| 13 | |||
| 14 | use Bio::MUST::Core; | ||
| 15 | use Bio::MUST::Core::Constants qw(:seqids); | ||
| 16 | use Bio::MUST::Core::Utils qw(change_suffix secure_outfile); | ||
| 17 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 18 | use aliased 'Bio::MUST::Core::IdMapper'; | ||
| 19 | |||
| 20 | # TODO add optional %seen hash, either global or filewise and either .1, .2 etc or .2, .3 etc | ||
| 21 | # TODO: make things more souple | ||
| 22 | # perl -nle 'if ( ($prot,$gca) = m/^>(\S+).*:(GC[AF]_[^:]+)/ ) { print q{>} . $gca . q{|} . $prot } else { print }' hexa-900-p-a_prot_cplt.fa > hexa-900-p-a_prot_cplt_abbr2.fa | ||
| 23 | |||
| 24 | # regexes for capturing unique identifier component | ||
| 25 | my %regex_for = ( | ||
| 26 | ':DEF' => $DEF_ID, | ||
| 27 | ':GI' => $GI_ID, | ||
| 28 | ':GNL' => $GNL_ID, | ||
| 29 | ':JGI' => $JGI_ID, | ||
| 30 | ':PAC' => $PAC_ID, | ||
| 31 | ); | ||
| 32 | |||
| 33 | my $regex = $regex_for{$ARGV_id_regex} // $ARGV_id_regex; | ||
| 34 | ### Using seq id regex: $regex | ||
| 35 | |||
| 36 | # build optional output dir | ||
| 37 | my $dir = q{.}; | ||
| 38 | if ($ARGV_outdir) { | ||
| 39 | $dir = dir($ARGV_outdir)->relative; | ||
| 40 | $dir->mkpath(); | ||
| 41 | } | ||
| 42 | |||
| 43 | # load optional prefix mapper file | ||
| 44 | my $prefix_mapper; | ||
| 45 | if ($ARGV_id_prefix_mapper) { | ||
| 46 | ### Taking prefixes from: $ARGV_id_prefix_mapper | ||
| 47 | $prefix_mapper = IdMapper->load($ARGV_id_prefix_mapper); | ||
| 48 | } | ||
| 49 | |||
| 50 | # global variables that will be updated for each infile | ||
| 51 | my $prefix; | ||
| 52 | my @long_ids; | ||
| 53 | my @abbr_ids; | ||
| 54 | |||
| 55 | my $abbrid_filter = sub { | ||
| 56 | my $seq = shift; | ||
| 57 | |||
| 58 | # get long id | ||
| 59 | my $long_id = $seq->full_id; | ||
| 60 | push @long_ids, $long_id; | ||
| 61 | |||
| 62 | # abbreviate seq_id | ||
| 63 | my $abbr_id = $seq->seq_id->abbr_with_regex($prefix, $regex); | ||
| 64 | push @abbr_ids, $abbr_id; | ||
| 65 | |||
| 66 | # store allowed seqs | ||
| 67 | my $str = '>' . $abbr_id . "\n"; # use abbreviated seq_id | ||
| 68 | $str .= $seq->wrapped_str; | ||
| 69 | |||
| 70 | return $str; | ||
| 71 | }; | ||
| 72 | |||
| 73 | for my $infile (@ARGV_infiles) { | ||
| 74 | |||
| 75 | ### Processing: $infile | ||
| 76 | |||
| 77 | # determine seq_id prefix | ||
| 78 | $prefix = $ARGV_id_prefix // q{}; # defaults to no prefix | ||
| 79 | if ($prefix_mapper) { # infile paths are ignored | ||
| 80 | my ($filename) = fileparse($infile); | ||
| 81 | $prefix .= $prefix_mapper->abbr_id_for($filename); | ||
| 82 | } | ||
| 83 | ### Prefixing seq ids with: $prefix | ||
| 84 | $prefix .= '|'; | ||
| 85 | |||
| 86 | my $outfile = secure_outfile( file($dir, $infile), '-abbr'); | ||
| 87 | Ali->instant_store( | ||
| 88 | $outfile, { infile => $infile, coderef => $abbrid_filter } | ||
| 89 | ); | ||
| 90 | |||
| 91 | # optionally store the id mapper | ||
| 92 | if ($ARGV_store_id_mapper) { | ||
| 93 | my $idmfile = change_suffix($outfile, '.idm'); | ||
| 94 | my $id_mapper = IdMapper->new( | ||
| 95 | long_ids => \@long_ids, abbr_ids => \@abbr_ids | ||
| 96 | ); | ||
| 97 | $id_mapper->store($idmfile); | ||
| 98 | } | ||
| 99 | |||
| 100 | # reset arrays for next infile | ||
| 101 | @long_ids = (); | ||
| 102 | @abbr_ids = (); | ||
| 103 | } | ||
| 104 | |||
| 105 | __END__ |