-
Notifications
You must be signed in to change notification settings - Fork 50
/
DFAM.pm
executable file
·358 lines (292 loc) · 8.83 KB
/
DFAM.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/perl
##---------------------------------------------------------------------------##
## File:
## @(#) DFAM.pm
## Author:
## Robert M. Hubley [email protected]
## Description:
## This is a simple datastructure for encapsulating the DFAM
## formatted profile data and RepeatMasker meta-data.
##
#******************************************************************************
#* Copyright (C) Institute for Systems Biology 2003-2004 Developed by
#* Robert Hubley.
#*
#* This work is licensed under the Open Source License v2.1. To view a copy
#* of this license, visit http://www.opensource.org/licenses/osl-2.1.php or
#* see the license.txt file contained in this distribution.
#*
#******************************************************************************
# Implementation Details:
#
#
#******************************************************************************
#
=head1 NAME
DFAM
=head1 SYNOPSIS
use DFAM
Usage:
$DB = DFAM->new( fileName => "dfam-hmmbuild-20111027.hmm" );
=head1 DESCRIPTION
=head1 INSTANCE METHODS
=cut
package DFAM;
use strict;
use Carp;
use Data::Dumper;
use DFAMRecord;
use Text::Wrap;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
require Exporter;
@ISA = qw();
@EXPORT = qw();
@EXPORT_OK = qw();
%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
#
# Version
#
my $VERSION = "0.1";
my $CLASS = "DFAM";
##-------------------------------------------------------------------------##
## Constructor
##-------------------------------------------------------------------------##
sub new {
my $class = shift;
my %nameValuePairs = @_;
# Create ourself as a hash
my $this = {};
$this->{'records'} = [];
# Bless this hash in the name of the father, the son...
bless $this, $class;
if ( %nameValuePairs ) {
my %validKeys = ( fileName => 1 );
while ( my ( $name, $value ) = each( %nameValuePairs ) ) {
unless ( exists $validKeys{$name} ) {
croak "$CLASS::new: Error $name is not a valid "
. "parameter to the constructor!\n\n"
. "Usage: \$DB = $CLASS->new();\n"
. " \$DB = $CLASS->new( fileName=>\"dfam.hmm\" );\n";
}
$this->{$name} = $value;
}
}
if ( exists $this->{'fileName'} ) {
_parseFromFile( $this, $this->{'fileName'} );
}
return $this;
}
##-------------------------------------------------------------------------##
## Get and Set Methods
##-------------------------------------------------------------------------##
##-------------------------------------------------------------------------##
## General Object Methods
##-------------------------------------------------------------------------##
##-------------------------------------------------------------------------##
=head2 Use: $DB->writeEMBLFile( $fileName );
Write out this object as an EMBL file;
=cut
##-------------------------------------------------------------------------##
sub writeEMBLFile {
my $this = shift;
my $fileName = shift;
open OUT, ">$fileName"
or die "$CLASS::writeEMBLFile() Unable to open "
. "file $fileName for writing: $!\n";
foreach my $record ( @{ $this->{'records'} } ) {
print OUT "" . &toEMBLString( $record );
}
close OUT;
}
sub getRecordCount {
my $this = shift;
return ( $#{ $this->{'records'} } + 1 );
}
sub getRecord {
my $this = shift;
my $index = shift;
return if ( $index < 0 || $index > $#{ $this->{'records'} } );
return $this->{'records'}->[ $index ];
}
sub getRelease {
my $this = shift;
return $this->{'release'};
}
##-------------------------------------------------------------------------##
## Private Object Methods
##-------------------------------------------------------------------------##
##-------------------------------------------------------------------------##
## Use: my _parseFromFile( $obj, $filename );
##
## Parse a FASTA output file into this datastructure.
##
##-------------------------------------------------------------------------##
sub _parseFromFile {
my $this = shift;
my $filename = shift;
open IN, "<$filename"
|| die "$CLASS::_parseFromFile() Unable to open "
. "Repbase/EMBL file $filename: $!\n";
my $inRepeatMaskerAnnotBlock = 0;
my $data = "";
my $recordRef = DFAMRecord->new();
my $recordLines = "";
my $id = "";
my $acc = "";
while ( <IN> ) {
## Get file release if it exists
if ( /^#\s+Release:\s+(\S+)/ ) {
$this->{'release'} = $1;
}
# IDs are just plain id's - no type/subtype.
# Need to add back when we create our own libraries.
if ( /^NAME\s+(\S+)/ ) {
$id = $1;
}
# Accession
if ( /^ACC\s+(\S+)/ ) {
$acc = $1;
}
#
if ( /^HMMER(\d+).*/ ) {
if ( $1 != 3 ) {
warn "DFAM.pm: Warning HMM format is not recognized: $_\n";
}
$recordLines = $_;
$id = "";
}
else {
# Create substitution TAG
if ( /^NAME\s+\S+/ ) {
$recordLines .= "NAME #NEWID#\n";
}
else {
$recordLines .= $_;
}
}
# New TH tag
#TH TaxId:10090; TaxName:Mus musculus; GA:13.90; TC:33.14; NC:13.85; fdr:0.002;
if (
/^TH\s+TaxId:\s*(\d+);\s*TaxName:\s*([^;]+);\s*GA:\s*([\d\.]+);\s*TC:\s*([\d\.]+);\s*NC:\s*([\d\.]+);\s*fdr:\s*([\d\.]+);\s*$/
)
{
$recordRef->pushThresh(
{
'taxid' => $1,
'taxname' => $2,
'hit_ga' => $3,
'hit_tc' => $4,
'hit_nc' => $5,
'fdr' => $6
}
);
}
## Process the end record tag
if ( /^\/\/\s*$/ || eof( IN ) ) {
# Create new ID
my $newId = $id;
if ( $recordRef->getRMType() ) {
$newId .= "#" . $recordRef->getRMType();
}
if ( $recordRef->getRMSubType() ) {
$newId .= "/" . $recordRef->getRMSubType();
}
$recordLines =~ s/NAME\s+#NEWID#/NAME $newId/g;
# Save record!
$recordRef->setRecordLines( $recordLines );
$recordRef->setId( $id );
$recordRef->setAcc( $acc );
push @{ $this->{'records'} }, $recordRef;
$recordRef = DFAMRecord->new();
next;
}
## Capture the tag!
if ( /^CC(?:\s+(\S.*))?/ ) {
# New tag so overwrite old data
if ( defined $1 ) {
$data .= $1 . "\n";
}
else {
$data .= "\n";
}
$inRepeatMaskerAnnotBlock = 1;
}
else {
$inRepeatMaskerAnnotBlock = 0;
if ( $data ne "" ) {
# Parse last tag
_parseTagData( $data, $recordRef );
}
$data = "";
}
}
close IN;
}
##-------------------------------------------------------------------------##
## Use: my _parseTagData( $tag, $data, $recRef );
##
## Parse a RepeatMasker annotation block. This is a helper function
## for the general parser. This function expects data for
## multiline tags to already be compressed into a single
## blob. Ie.
##
## ## line1
## ## line2
##
## Should be passed to this function as:
##
## $data = "line1\nline2"
##
##-------------------------------------------------------------------------##
sub _parseTagData {
my $data = shift;
my $recRef = shift;
if ( $data =~ /RepeatMasker\s+Annotations/i ) {
if ( $data =~ /Type:[ \t]*(\S+)[ \t]*[\n\r]/i ) {
$recRef->setRMType( $1 );
}
if ( $data =~ /SubType:[ \t]*(\S+)[ \t]*[\n\r]/i ) {
$recRef->setRMSubType( $1 );
}
if ( $data =~ /Species:[ \t]*([\S \t]+)[ \t]*[\n\r]/i ) {
my $speciesStr = $1;
$speciesStr =~ s/\s//g;
$speciesStr =~ s/(.*),$/$1/g;
my @species = split( /\,/, $speciesStr );
foreach my $species ( @species ) {
if ( $species =~ /Search/ ) { die "Hmmm $speciesStr\n"; }
$species =~ s/\s//g;
$recRef->pushRMSpecies( $species );
}
}
if ( $data =~ /Refineable/ ) {
$recRef->setRMRefineable( 1 );
}
if ( $data =~ /SearchStages:\s*([\d\s*\,\s*]+)[\n\r]/i ) {
my $stageStr = $1;
$stageStr =~ s/\s//g;
$stageStr =~ s/(.*),$/$1/g;
my @stages = split( /\,/, $stageStr );
foreach my $stage ( @stages ) {
$recRef->pushRMSearchStages( $stage );
}
}
if ( $data =~ /BufferStages:([^\n\r]*)[\n\r]+/i ) {
my $bufData = $1;
while ( $bufData =~ s/\s*(\d+\s*(?:\[\s*\d+\s*-\s*\d+\s*\]\s*)*),?//m ) {
my $bufStr = $1;
$bufStr =~ s/\s//g;
if ( $bufStr ne "" ) {
$recRef->pushRMBufferStages( $bufStr );
}
}
}
if ( $data =~ /Description:\s+(\S.*)/i ) {
$recRef->setRMDescription( $1 );
}
}
# Coment line
# Multi-record/multi-line comment allowed
$recRef->pushComments( $data );
}
1;