FastaFile.pm 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package FastaFile;
  2. use PirFile;
  3. sub new {
  4. my ($caller, $filename) = @_;
  5. my $caller_is_obj = ref($caller);
  6. my $class = $caller_is_obj || $caller;
  7. no strict "refs";
  8. my $self = bless [], $class;
  9. if (defined($filename)) {
  10. $self->read_from_file("$filename");
  11. }
  12. return $self;
  13. }
  14. sub read_from_file {
  15. my ($self, $filename) = @_;
  16. my @lines;
  17. open(FH, "< $filename") or die ("Cant open $filename: $!\n");
  18. @lines = <FH>;
  19. close(FH);
  20. my $readSeq = 0;
  21. my $seq = "";
  22. my $comment = "";
  23. my $id;
  24. my $idxEntry = 0;
  25. for (my $i=0; $i<@lines; $i++) {
  26. my $curLine = $lines[$i];
  27. if ($curLine =~ /^\s*>(\S+)/) {
  28. if ($readSeq == 1) {
  29. $self->[$idxEntry] = {id=>$id, seq=>$seq, comment=>$comment};
  30. $idxEntry++;
  31. }
  32. $id = $1;
  33. $comment = "";
  34. if ($curLine =~ /^\s*>(\S+)\s+(.+)/) {
  35. $comment = $2;
  36. }
  37. $readSeq = 1;
  38. $seq = "";
  39. next;
  40. }
  41. if ($readSeq == 1) {
  42. chomp($curLine);
  43. $seq .= $curLine;
  44. }
  45. }
  46. $self->[$idxEntry] = {id=>$id, seq=>$seq, comment=>$comment};
  47. }
  48. sub size {
  49. my $self = shift;
  50. scalar(@{$self});
  51. }
  52. sub to_string {
  53. my ($self) = @_;
  54. my $res = "";
  55. for (my $i=0; $i<$self->size(); $i++) {
  56. $res .= ">" . $self->[$i]->{id} . " " . $self->[$i]->{comment} . "\n";
  57. $res .= $self->[$i]->{seq} . "\n";
  58. }
  59. $res;
  60. }
  61. sub print {
  62. my $self = shift;
  63. print $self->to_string();
  64. }
  65. sub write_to_file {
  66. my ($self, $filename) = @_;
  67. open(OH, "> $filename") or die ("Cant write to $filename: $!\n");
  68. print OH $self->to_string();
  69. close(OH);
  70. }
  71. sub add_entry {
  72. my ($self, $id, $comment, $seq) = @_;
  73. my $idx = $self->size();
  74. $self->[$idx]->{id} = $id;
  75. $self->[$idx]->{seq} = $seq;
  76. $self->[$idx]->{comment} = $comment;
  77. }
  78. sub get_seq {
  79. my ($self, $idx) = @_;
  80. $self->[$idx]->{seq};
  81. }
  82. sub get_comment {
  83. my ($self, $idx) = @_;
  84. $self->[$idx]->{comment};
  85. }
  86. sub to_pir {
  87. my ($self) = @_;
  88. my $pir = PirFile->new();
  89. for (my $i=0; $i<$self->size(); $i++) {
  90. my $seq = $self->[$i]->{seq};
  91. $seq .= "*";
  92. $pir->add_entry($self->[$i]->{id}, $self->[$i]->{comment}, $seq);
  93. }
  94. return $pir;
  95. }
  96. 1;