rollback Florent's changes that defaulted Bio::PrimarySeq instead of Bio::Seq creatio...
[bioperl-live.git] / Bio / Seq / SeqFastaSpeedFactory.pm
blob5c12b7019abd8e0ec068b6dbc6d7832ae040f016
1 # $Id$
3 # BioPerl module for Bio::Seq::SeqFastaSpeedFactory
5 # Please direct questions and support issues to <bioperl-l@bioperl.org>
7 # Cared for by Jason Stajich <jason@bioperl.org>
9 # Copyright Jason Stajich
11 # You may distribute this module under the same terms as perl itself
13 # POD documentation - main docs before the code
15 =head1 NAME
17 Bio::Seq::SeqFastaSpeedFactory - Rapid instantiation of new Bio::SeqI objects through a factory using FASTA files.
19 =head1 SYNOPSIS
21 use Bio::Seq::SeqFastaSpeedFactory;
22 my $factory = Bio::Seq::SeqFastaSpeedFactory->new();
23 my $seq = $factory->create(-seq => 'WYRAVLC',
24 -id => 'name');
26 # If you want the factory to create Bio::PrimarySeq objects instead
27 # of the default Bio::Seq objects, use the -type parameter:
28 my $factory = Bio::Seq::SeqFactory->new(-type => 'Bio::PrimarySeq');
30 =head1 DESCRIPTION
32 This factory is quick at building simple L<Bio::PrimarySeqI> and L<Bio::SeqI>
33 objects generically derived from FASTA files (no annotations).
35 =head1 FEEDBACK
37 =head2 Mailing Lists
39 User feedback is an integral part of the evolution of this and other
40 Bioperl modules. Send your comments and suggestions preferably to
41 the Bioperl mailing list. Your participation is much appreciated.
43 bioperl-l@bioperl.org - General discussion
44 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
46 =head2 Support
48 Please direct usage questions or support issues to the mailing list:
50 I<bioperl-l@bioperl.org>
52 rather than to the module maintainer directly. Many experienced and
53 reponsive experts will be able look at the problem and quickly
54 address it. Please include a thorough description of the problem
55 with code and data examples if at all possible.
57 =head2 Reporting Bugs
59 Report bugs to the Bioperl bug tracking system to help us keep track
60 of the bugs and their resolution. Bug reports can be submitted via the
61 web:
63 http://bugzilla.open-bio.org/
65 =head1 AUTHOR - Jason Stajich
67 Email jason@bioperl.org
69 =head1 APPENDIX
71 The rest of the documentation details each of the object methods.
72 Internal methods are usually preceded with a _
74 =cut
77 # Let the code begin...
80 package Bio::Seq::SeqFastaSpeedFactory;
81 use strict;
83 use Bio::Seq;
84 use Bio::PrimarySeq;
86 use base qw(Bio::Root::Root Bio::Seq::SeqFactory);
87 # a Bio::Seq::SeqFactory is also a Bio::Factory::SequenceFactoryI
90 =head2 new
92 Title : new
93 Usage : my $obj = Bio::Seq::SeqFastaSpeedFactory->new();
94 Function: Builds a new Bio::Seq::SeqFastaSpeedFactory object
95 Returns : Bio::Seq::SeqFastaSpeedFactory
96 Args : -type => string, name of a PrimarySeqI derived class
97 This is optional. Default=Bio::Seq.
99 =cut
101 sub new {
102 my($class,@args) = @_;
103 my $self = $class->SUPER::new(@args);
104 my ($type) = $self->_rearrange([qw(TYPE)], @args);
105 $self->type($type);
106 return $self;
110 =head2 create
112 Title : create
113 Usage : my $seq = $seqbuilder->create(-seq => 'CAGT', -id => 'name');
114 Function: Instantiates new Bio::SeqI (or one of its child classes)
115 This object allows us to genericize the instantiation of sequence
116 objects.
117 Returns : Bio::Seq object (default)
118 The return type is configurable using new(-type =>"...").
119 Args : initialization parameters specific to the type of sequence
120 object we want. Typically
121 -seq => $str,
122 -id => $name
124 =cut
126 # Overloading the 'create' method of Bio::Seq::SeqFactory
127 sub create {
128 my ($self,@args) = @_;
130 my %param = @args;
131 @param{ map { lc $_ } keys %param } = values %param; # lowercase keys
133 my $sequence = $param{'-seq'};
134 my $fulldesc = $param{'-desc'};
135 my $id = defined $param{'-id'} ? $param{'-id'} : $param{'-primary_id'};
136 my $alphabet = $param{'-alphabet'};
138 # Constructing Bio::PrimarySeq object
139 my $t_pseq = bless {}, 'Bio::PrimarySeq';
140 $t_pseq->{'seq'} = $sequence;
141 $t_pseq->{'desc'} = $fulldesc;
142 $t_pseq->{'display_id'} = $id;
143 if( $sequence and !$alphabet ) {
144 $t_pseq->_guess_alphabet();
145 } elsif ( $sequence and $alphabet ) {
146 $t_pseq->{'alphabet'} = $alphabet;
149 my $seq;
150 my $type = $self->type;
151 if ($type eq 'Bio::Seq') {
152 # Constructing Bio::Seq object
153 $seq = bless {}, 'Bio::Seq';
154 $seq->{'primary_seq'} = $t_pseq;
155 } elsif ($type eq 'Bio::PrimarySeq') {
156 # Nothing more to do for a Bio::PrimarySeq
157 $seq = $t_pseq;
158 } else {
159 # Should not have any other sequence type
160 $self->warn("Expected sequence type Bio::Seq or Bio::PrimarySeq. Got ".
161 "$type. Defaulting to Bio::PrimarySeq\n");
162 $self->type('Bio::PrimarySeq');
163 $seq = $t_pseq;
166 return $seq;
170 =head2 type
172 Title : type
173 Usage : $obj->type($newval)
174 Function:
175 Returns : value of type
176 Args : newvalue (optional)
178 =cut
180 # Using the 'type' method from Bio::Seq::SeqFactory