JPRED-2 Move Jpred 3.0.1 to public Git
[jpred.git] / jpred / lib / Concise / File.pm
1 package Concise::File;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 use base qw(Root Read Write Sequence::File);
8 use Concise;
9
10 # Pass a filehandle
11 sub read {
12         my ($self, $fh) = @_;
13
14         while (<$fh>) {
15                 chomp;
16
17                 next if /^\s*$/;        # Skip blank lines
18                 next if /^#/;           # skip comment lines
19 #               s/#.*//g;                       # Clear comments
20                 
21                 my ($head, $field) = split /:/, $_, 2;
22
23                 unless (defined $field and defined $head) {
24                         carp "Line $. doesn't match concise file format, skipping";
25                         next;
26                 }
27
28                 $field =~ s/,$//g;
29                 my @fields = split /,/, $field;
30
31                 my $new = Concise->new(id => $head);
32                 $new->seq(@fields);
33                 $self->add_entries($new);
34         }
35 }
36
37 sub write {
38         my ($self, $fh) = @_;
39
40         for ($self->get_entries) {
41                 my $id = $_->id;
42                 my @seq = $_->seq;
43
44                 my $seq = join ',', @seq;
45                 $seq =~ s/,$//;
46
47                 print $fh "$id:$seq\n";
48         }
49 }
50
51 1;
52
53 __END__
54
55 =head1 NAME
56
57 Concise::File - Module to read Concise file
58
59 =head1 SYNOPSYS
60
61   # Read a concise file and print the id's in the file
62   $concise = Concise::File->new(read_file => "path_to_file");
63   for ($concise->get_entries) {
64     print $_->id, "\n";
65   }
66
67 =head1 DESCRIPTION
68
69 This module allows you to read concise files and then get all the information held in the file or to select those entries you want by their IDs.
70
71 =head1 METHODS
72
73 This module inherits from the Root, Read and Write modules. See these for default methods.
74
75 =over
76
77 =item $concise->add_entries(@Concise)
78
79 Add a list of Concise objects to the Concise::File object.
80
81 =item $concise->get_entries
82
83 Returns a list of the entries held in the object. These are held as Concise objects.
84
85 =item $concise->get_entry_by_id( qr/foo/ )
86
87 Get entries dependant upon their IDs and a regex. The argument can either be a regex object or a string to be interpreted as a regex. A list of Concise objects is returned.
88
89 =back
90
91 =head1 AUTHOR
92
93 Jonathan Barber (jon@compbio.dundee.ac.uk)