| 1 | #!/usr/bin/env perl | ||
| 2 | # PODNAME: abbr-ids-fas.pl | ||
| 3 | # ABSTRACT: Abbreviate (standardize) seq ids in FASTA files | ||
| 4 | |||
| 5 | use Modern::Perl '2011'; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | use File::Basename; | ||
| 9 | use Getopt::Euclid qw(:vars); | ||
| 10 | use Smart::Comments; | ||
| 11 | |||
| 12 | use Bio::MUST::Core; | ||
| 13 | use Bio::MUST::Core::Constants qw(:seqids); | ||
| 14 | use Bio::MUST::Core::Utils qw(change_suffix secure_outfile); | ||
| 15 | use aliased 'Bio::MUST::Core::Ali'; | ||
| 16 | use aliased 'Bio::MUST::Core::IdMapper'; | ||
| 17 | |||
| 18 | |||
| 19 | # regexes for capturing unique identifier component | ||
| 20 | my %regex_for = ( | ||
| 21 | ':DEF' => $DEF_ID, | ||
| 22 | ':GI' => $GI_ID, | ||
| 23 | ':GNL' => $GNL_ID, | ||
| 24 | ':JGI' => $JGI_ID, | ||
| 25 | ':PAC' => $PAC_ID, | ||
| 26 | ); | ||
| 27 | |||
| 28 | # load optional prefix mapper file | ||
| 29 | my $prefix_mapper; | ||
| 30 | if ($ARGV_id_prefix_mapper) { | ||
| 31 | ### Taking prefixes from: $ARGV_id_prefix_mapper | ||
| 32 | $prefix_mapper = IdMapper->load($ARGV_id_prefix_mapper); | ||
| 33 | } | ||
| 34 | |||
| 35 | for my $infile (@ARGV_infiles) { | ||
| 36 | |||
| 37 | ### Processing: $infile | ||
| 38 | my $ali = Ali->load($infile); | ||
| 39 | $ali->dont_guess; | ||
| 40 | |||
| 41 | # determine seq_id prefix | ||
| 42 | my $prefix = $ARGV_id_prefix // q{}; # defaults to no prefix | ||
| 43 | if ($prefix_mapper) { # infile paths are ignored | ||
| 44 | my ($filename) = fileparse($infile); | ||
| 45 | $prefix .= $prefix_mapper->abbr_id_for($filename); | ||
| 46 | } | ||
| 47 | if ($prefix) { | ||
| 48 | ### Prefixing seq ids with: $prefix | ||
| 49 | $prefix .= '|'; # add '|' separator | ||
| 50 | } | ||
| 51 | |||
| 52 | # build id_mapper | ||
| 53 | my $id_mapper; | ||
| 54 | |||
| 55 | # 1. regex mapper (first use input as hash key then as regex) | ||
| 56 | if ($ARGV_id_regex) { | ||
| 57 | my $regex = $regex_for{$ARGV_id_regex} // $ARGV_id_regex; | ||
| 58 | $id_mapper = $ali->regex_mapper($prefix, $regex); | ||
| 59 | ### Using seq id regex: $regex | ||
| 60 | } | ||
| 61 | |||
| 62 | # 2. accession mapper | ||
| 63 | elsif ($ARGV_ids_from_acc) { | ||
| 64 | $id_mapper = $ali->acc_mapper( $prefix); | ||
| 65 | ### Using accessions as seq ids | ||
| 66 | } | ||
| 67 | |||
| 68 | # 3. standard mapper | ||
| 69 | else { | ||
| 70 | $id_mapper = $ali->std_mapper( $prefix . 'seq'); | ||
| 71 | ### Using standard seq ids | ||
| 72 | } | ||
| 73 | |||
| 74 | $ali->shorten_ids($id_mapper); | ||
| 75 | |||
| 76 | my $outfile = secure_outfile($infile, $ARGV_out_suffix); | ||
| 77 | $ali->store_fasta($outfile); | ||
| 78 | |||
| 79 | # optionally store the id mapper | ||
| 80 | if ($ARGV_store_id_mapper) { | ||
| 81 | my $idmfile = change_suffix($outfile, '.idm'); | ||
| 82 | $id_mapper->store($idmfile); | ||
| 83 | } | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | __END__ |