| 1 | package GO::Utils::File; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | GO::Utils::File - simply utility module for dealing with file parsing | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | GO::Utils::File provides a single exported function for retrieving the | ||
| 10 | lines out of a file, that can be easily reused. | ||
| 11 | |||
| 12 | It will simply expect one gene name to exist per line. It should deal | ||
| 13 | correctly with Mac and DOS line-endings, and will remove whitespace | ||
| 14 | from the beginning and end of the names, then return the gene names as | ||
| 15 | an array. | ||
| 16 | |||
| 17 | use GO::Utils::File qw(GenesFromFile); | ||
| 18 | |||
| 19 | my @genes = GenesFromFile($filename); | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | use diagnostics; | ||
| 26 | |||
| 27 | use IO::File; | ||
| 28 | |||
| 29 | use vars qw (@ISA @EXPORT_OK $VERSION); | ||
| 30 | use Exporter; | ||
| 31 | @ISA = ('Exporter'); | ||
| 32 | @EXPORT_OK = qw(GenesFromFile); | ||
| 33 | |||
| 34 | $VERSION = 0.12; | ||
| 35 | |||
| 36 | ########################################################################## | ||
| 37 | sub GenesFromFile{ | ||
| 38 | ########################################################################## | ||
| 39 | |||
| 40 | =head2 GenesFromFile | ||
| 41 | |||
| 42 | GenesFromFile returns an array of gene names that were read from the | ||
| 43 | supplied file. It assumes one name per line. | ||
| 44 | |||
| 45 | Usage: | ||
| 46 | |||
| 47 | my @genes = GenesFromFile($filename); | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | my $filename = shift; | ||
| 52 | |||
| 53 | my $fh = IO::File->new($filename, q{<} )|| die "Cannot open $filename : $!"; | ||
| 54 | |||
| 55 | my @genes; | ||
| 56 | my @lines; | ||
| 57 | |||
| 58 | my $var = chr(13); # to deal with Mac end of line | ||
| 59 | |||
| 60 | while (<$fh>){ | ||
| 61 | |||
| 62 | if (/$var/o){ | ||
| 63 | |||
| 64 | # if it's a Mac file multiple lines get read at once, so | ||
| 65 | # we have to split on the end-of line character | ||
| 66 | |||
| 67 | @lines = split($var, $_); | ||
| 68 | |||
| 69 | }else{ | ||
| 70 | |||
| 71 | @lines = ($_); | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | foreach my $gene (@lines){ | ||
| 76 | |||
| 77 | $gene =~ s/\cM//g; # remove Control-M characters | ||
| 78 | |||
| 79 | $gene =~ s/\s+$//; # remove any trailing or leading whitespace | ||
| 80 | $gene =~ s/^\s+//; | ||
| 81 | |||
| 82 | next unless $gene; | ||
| 83 | |||
| 84 | push (@genes, $gene); | ||
| 85 | |||
| 86 | } | ||
| 87 | |||
| 88 | } | ||
| 89 | |||
| 90 | $fh->close; | ||
| 91 | |||
| 92 | return @genes; | ||
| 93 | |||
| 94 | } | ||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | =pod | ||
| 99 | |||
| 100 | =head1 AUTHOR | ||
| 101 | |||
| 102 | Gavin Sherlock; sherlock@genome.stanford.edu | ||
| 103 | |||
| 104 | =cut |