root/lib/FastaFile.pm

Revision 127, 4.6 kB (checked in by cholt, 10 months ago)

major maker update, gff passthrough, evaluator integration, tblastx, ncbi, error control

Line 
1 #-------------------------------------------------------------------------------
2 #------                            FastaFile                           ---------
3 #-------------------------------------------------------------------------------
4 package FastaFile;
5 use strict;
6 use vars qw(@ISA @EXPORT $VERSION);
7 use Exporter;
8 use PostData;
9 use FileHandle;
10
11 @ISA = qw(
12           );
13
14 #-------------------------------------------------------------------------------
15 #------------------------------- SUBS ------------------------------------------
16 #-------------------------------------------------------------------------------
17 sub  getWantedFromMulti {
18         my $multiFasta = shift;
19         my $wanted     = shift;
20
21         $/ = "\n>";
22
23         my $fh = new FileHandle;
24            $fh->open("$multiFasta") || die "couldn't open $multiFasta";
25
26         my @fastas;
27         while(my $line = <$fh>){
28                 $line =~ s/>//;
29                 $line = ">".$line;
30                 if (!defined($wanted) || $line =~ /$wanted/){
31                         push(@fastas, $line);
32                 }
33         }
34         $/ = "\n";
35         $fh->close || die "couldn't close $multiFasta";
36
37         return \@fastas;
38 }
39 #-----------------------------------------------------------------------------
40 sub getFasta {
41         my $fastaFile = shift;
42
43         my $seq = getSeq($fastaFile);
44         my $def = getDef($fastaFile);   
45
46         return toFasta($def, $seq);
47 }
48 #-----------------------------------------------------------------------------
49 sub writeFile {
50         my $f     = shift;
51         my $loc   = shift;
52
53         my $fasta = (ref($f) eq '') ? \$f : $f;
54
55         my $fh = new FileHandle();
56            $fh->open(">$loc") || die "couldn't open $loc";
57
58         print $fh $$fasta;
59
60         $fh->close() || die "couldn't close $loc";
61
62         return $loc;
63 }
64 #-----------------------------------------------------------------------------
65 sub getDef {
66         my $fastaFile = shift;
67
68         my $fh = new FileHandle();
69            $fh->open($fastaFile) || die "couldn't open $fastaFile";
70
71         my $seq = '';
72         while(my $l = <$fh>){
73                 chomp($l);
74                 return $l if $l =~ /^>/;
75             }
76         $fh->close() || die "couldn't close $fastaFile";
77
78 }
79 #-------------------------------------------------------------------------------
80 sub getName {
81         my $fastaFile = shift;
82
83         my $fh = new FileHandle();
84            $fh->open($fastaFile) || die "couldn't open $fastaFile";
85
86         my $seq = '';
87         while(my $l = <$fh>){
88                 chomp($l);
89                 return $1 if $l =~ /^>(\S+)/;
90         }
91         $fh->close() || die "couldn't close $fastaFile";;
92
93 }
94 #-------------------------------------------------------------------------------
95 sub getSeq {
96         my $fastaFile = shift;
97
98         my $fh = new FileHandle();
99            $fh->open($fastaFile) || die "couldn't open $fastaFile";
100
101         my $seq = '';
102         while(my $l = <$fh>){
103                 chomp($l);
104                 next if $l =~ /^>/;
105                 $seq .= $l;
106         }
107         $fh->close() || die "couldn't close $fastaFile";
108
109         return \$seq;
110        
111 }
112 #-------------------------------------------------------------------------------
113 sub revComp {
114         my $seq = shift;
115
116         $seq =~ tr/ACGTYRKMB/TGCARYMKV/;
117
118         die "fix this! dead in FastaFile::revComp\n";
119         return reverse($seq);
120 }
121 #-------------------------------------------------------------------------------
122 sub toFasta {
123         my $def = shift;
124         my $seq = shift;
125
126         my $fasta = $def."\n";
127
128         for (my $i=0; $i< length($$seq);$i+=60){
129                 $fasta .= substr($$seq, $i, 60)."\n";
130         }
131         return \$fasta;
132
133 }
134 #-------------------------------------------------------------------------------
135 sub toBpos {
136         my $def = shift;
137         my $seq = shift;
138
139         my $fasta = $def."\n";
140
141         my $bpos = "0 " x length($$seq);
142
143         for (my $i=0; $i< length($bpos);$i+=60){
144                 $fasta .= substr($bpos, $i, 60)."\n";
145         }
146         return \$fasta;
147
148 }
149
150 #-------------------------------------------------------------------------------
151 sub toQual {
152         my $def = shift;
153         my $seq = shift;
154
155         $$seq  =~ s/^\s+//;
156
157         my @values = split(/\s+/, $$seq);
158         my $fasta  = $def."\n";
159
160         my $j = 0;
161         for (my $i=0; $i< @values ;$i++){
162                 if ($j < 20){
163                         my $v = $values[$i];
164
165                         $fasta .= length($values[$i]) == 1 ? $values[$i]."  "
166                                                            : $values[$i]." ";
167                         $j++;
168                 }
169                 elsif ($j == 20){
170                         $fasta .= $values[$i]."\n";
171                         $j = 0;
172                 }
173                 else {
174                         die "dead in toQual\n";
175                 }
176         }
177         $fasta .= "\n" unless $fasta =~ /\n$/;
178
179         return \$fasta;
180
181 }
182 #-------------------------------------------------------------------------------
183 sub shift2 (\@) {
184         return splice(@{$_[0]}, 0, 2);
185 }
186 #-------------------------------------------------------------------------------
187 1;
188
189
190
191
192
193
194
195
196
Note: See TracBrowser for help on using the browser.