File: extract_UG_EST

package info (click to toggle)
estscan 3.0.3-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 388 kB
  • sloc: ansic: 2,272; perl: 1,879; fortran: 130; makefile: 58; sh: 11
file content (527 lines) | stat: -rwxr-xr-x 16,564 bytes parent folder | download | duplicates (3)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env perl
# $Id: extract_UG_EST,v 1.2 2006/12/19 12:52:07 c4chris Exp $

################################################################################
#
# extract_UG_EST
# --------------
# 
# Claudio Lottaz, SIB-ISREC, Claudio.Lottaz@isb-sib.ch
# Christian Iseli, LICR ITO, Christian.Iseli@licr.org
#
# Copyright (c) 2006 Swiss Institute of Bioinformatics. All rights reserved.
#
################################################################################

use strict;
use FASTAFile;
use Symbol;

# global variables
my $verbose = 1;
my $norna = 0;

my $datadir = '.';
my $filestem = '';

require "build_model_utils.pl";

################################################################################
#
#   Check command-line for switches
#

my $usage = "Usage: extract_UG_EST [options] <conf-files>\n" .
  "       where options are:\n" .
  "        -q        don't log on terminal\n" .
  "More information  is obtained using 'perldoc extract_UG_EST'\n";

while ($ARGV[0] =~ m/^-/) {
  if ($ARGV[0] eq '-q') { shift; $verbose = 0; next; }
  die "Unrecognized switch: $ARGV[0]\n$usage";
}
if ($#ARGV < 0) { die "No configuration file specified\n$usage"; }

################################################################################
#
#   Main-loop through all specified config-files
#

my $parFile;
while($parFile = shift) { 
  my($organism, $hightaxo, $dbfiles, $ugdata, $estdata, $datadir2, $filestem2, 
     $rnafile, $estfile, $estcdsfile, $estutrfile, $trainingfile, $testfile, 
     $utrfile, $cdsfile, $tuplesize, $minmask, $pseudocounts, $minscore, 
     $startlength, $startpreroll, $stoplength, $stoppreroll, $smatfile,
     $estscanparams, $nb_isochores, $isochore_borders) =
       readConfig($parFile, undef, undef, undef, 
		  undef, undef, undef,
		  undef, undef, $verbose);
  log_open("readconfig.log");
  $datadir = $datadir2; $filestem = $filestem2;
  showConfig($parFile, $organism, $hightaxo, $dbfiles, $ugdata, $estdata, $datadir, 
	     $rnafile, $estfile, $estcdsfile, $estutrfile, $trainingfile,
	     $testfile, $utrfile, $cdsfile, $tuplesize, $minmask,
	     $pseudocounts, $minscore, $startlength, $startpreroll,
	     $stoplength, $stoppreroll, $smatfile, $estscanparams,
	     $nb_isochores, $isochore_borders);
  log_close();

  # evaluate on mRNAs
  log_open("extract_UG_EST.log");

  # extract ESTs from UG clusters
  die "No UG clusters data file provided" unless defined $ugdata;
  log_print("\nGenerating evaluation EST data....");
  analyzeClusters($testfile, $ugdata);
  collect_testsets($estcdsfile, $estutrfile);
  log_close();
  print "$parFile done.\n";
}

exit 0;

################################################################################
#
#   Estimate false positive/negative rate on EST-data extracted through UniGene
#

sub analyzeClusters {
  # For each UniGene cluster with reference to refseq mRNA or mRNA
  # entry in embl containing complete CDS annotated, find all ESTs
  # and use megablast to align the ESTs with the mRNA. Store the
  # match positions.

  my($mrna, $refseqmrna, @emblmrnas, @ests, $eststring);
  my $fh = gensym;
  my $ugdata = "$datadir/Evaluate/ug.data";
  my($testfile, $ugcf) = @_;

  if (-s $ugdata) {
    log_print("   $ugdata already exists, skip.");
  } else {
    my %ID;
    my $ua = BTFile->new("$ugcf.ac.idx" . "[18,10,10]", "", $ugcf);
    log_print("   find UniGene clusters for test mRNAs...");
    my $fh = gensym;
    open $fh, $testfile;
    while ( <$fh> ) {
      if (m/^>tem.(\S+)/) {
	my $ac = $1;
	my $e = $ua->fetch($ac);
	unless (defined $e) {
	  print STDERR "Couldn't find AC $ac in $ugcf\n";
	  next;
	}
	($ac) = $e =~ /^ID\s+(\S+)/s;
	$ID{$ac} = 1;
      }
    }
    close $fh;
    undef $ua;
    open $fh, ">$ugdata";
    $ua = BTFile->new("$ugcf.idx" . "[18,10,10]", "", $ugcf);
    foreach my $ac (sort(keys %ID)) {
      my $e = $ua->fetch($ac);
      print $fh $e;
    }
    close $fh;
  }

  log_print("   reading UniGene clusters...");
  my @clusters;
  my $clusterfile = "$datadir/Evaluate/clusters.lst";
  if (-s $clusterfile) { 
    log_print("   $clusterfile already exists, read existing");
    open $fh, $clusterfile;
    @clusters = <$fh>;
    close $fh;
  } else {
    if (system("which fetch") != 0) {
      log_print("   fatal: fetch not found");
      die;
    }
    $mrna = undef;
    $refseqmrna = undef; 
    undef @ests;
    undef @emblmrnas;
    open $fh, ">$clusterfile";
    my $ugfh = gensym;
    open $ugfh, $ugdata;
    while ( <$ugfh> ) {
      if (m/^SEQUENCE    ACC=([NX]M_[^.;]+)/) {
	$refseqmrna = "rs:$1";
      } elsif (m/PID=/ && m/^SEQUENCE    ACC=([^.;]+)/) {
	push @emblmrnas, "embl:$1";
      }
      if (m/LID=/ && m/^SEQUENCE    ACC=([^.;]+)/) {
	push @ests, "embl:$1";
      }
      if (m+^//+) { # found the end of the current entry
	if ($#ests >= 0) {
	  if (defined $refseqmrna) {
	    $mrna = "$refseqmrna";
	  } else {
	    foreach my $id (@emblmrnas) {
	      my $e = `fetch $id`;
	      if (($e =~ m/^ID\s+\S+; SV \d+; linear; mRNA;/s)
		  && ($e =~ m/^FT   CDS\s+\d+\.\.\d+$/m)) { 
		$mrna = "$id";
		last; 
	      }
	    }
	  }
	}
	if (defined($mrna)) {
	  my $line = "$mrna : " . join(' ', @ests) . "\n";
	  print $fh $line; 
	  push @clusters, $line; 
	}
	$mrna = undef;
	$refseqmrna = undef; 
	undef @ests;
	undef @emblmrnas;
      }
    }
    close $ugfh;
    close $fh;
  }
  my $nclusters = $#clusters + 1;
  log_print("   found $nclusters clusters");

  log_print(" - matching ESTs against mRNAs...");
  my $matchfile = "$datadir/Evaluate/matches.lst";
  if (-s $matchfile) {
    log_print("   $matchfile already exists, skipped");
    return;
  }
  if (system("which formatdb") != 0) {
    log_print("   fatal: formatdb not found");
    die;
  }
  if (system("which megablast") != 0) {
    log_print("   fatal: megablast not found");
    die;
  }
  my $i = 0;
  my $tmprnafile = "$datadir/Evaluate/tmpmrna.seq";
  my $tmpestdb = "$datadir/Evaluate/tmpestdb";
  my $matchfh = gensym;
  system("touch $matchfile");
  foreach (@clusters) {
    my $cdsBegin;
    my $cdsEnd;
    ($mrna, $eststring) = split / : /, $_, 2;
    chop $eststring;
    @ests = split / /, $eststring;
    $i++;
    my $nests = $#ests + 1;
    log_print("   analyzing cluster $i of $nclusters:"
	      . " $mrna (has $nests ESTs)...");

    # find full RNA and the position of its coding sequence
    my $e = `fetch $mrna`;
    if ($e =~ m/CDS\s+(\d+)\.\.(\d+)/) {
      $cdsBegin = $1;
      $cdsEnd = $2;
    } else {
      next;
    }
    system "fetch -f $mrna > $tmprnafile";

    # collect ESTs into BLAST database
    open $matchfh, "|xargs fetch -f >$tmpestdb.seq";
    print $matchfh join("\n", @ests), "\n";
    close $matchfh;
    system "formatdb -p F -n $tmpestdb -i $tmpestdb.seq";

    # match ESTs using megablast
    my $minLen = 300;      # minimal length an EST matches the RNA nicely
    my $maxMissed = 0.05;  # maximal number of unmatched nucleotides
    open $matchfh, ">>$matchfile";
    open $fh, "megablast -D 0 -e 1e-20 -d $tmpestdb -i $tmprnafile |";
    $_ = <$fh>;
    my($estid, $ori, $eStart, $rStart, $eStop, $rStop, $mismatch) =
      m/^\'emb\|([^\|]+)[^\']+\'==\'([+-])\S+ \((\d+) (\d+) (\d+) (\d+)\) (\d+)/;
    my $oldestid = $estid;
    my $oldori = $ori;
    my $offset = $eStart;
    my $matchBegin = $rStart;
    my $matchEnd = $rStop;
    my $matchLen = $eStop;
    my $matchMissed = $offset + $mismatch;
    while ( <$fh> ) {
      ($estid, $ori, $eStart, $rStart, $eStop, $rStop, $mismatch) =
	m/^\'emb\|([^\|]+)[^\']+\'==\'([+-])\S+ \((\d+) (\d+) (\d+) (\d+)\) (\d+)/;	    
      next if $ori eq '-';
      if ($oldestid eq $estid) {
	my $tmpa = abs($eStart - $matchLen);
	my $tmpb = abs($rStart - $matchEnd);
	$matchMissed += (($tmpa < $tmpb) ? $tmpb : $tmpa)  + $mismatch;
      } else {
	my $tmp = $matchEnd - $matchBegin + $offset;
	if ($matchLen < $tmp) {
	  $matchLen = $tmp;
	}
	if (($oldori eq '+')
	    && ($matchLen > $minLen)
	    && ($matchMissed / $matchLen < $maxMissed)) { 
	  print $matchfh "$mrna $cdsBegin $cdsEnd embl:$oldestid ",
		"$matchBegin $matchEnd $offset $matchMissed\n";
	}
	$oldestid = $estid;
	$oldori = $ori;
	$offset = $eStart;
	$matchBegin = $rStart;
	$matchMissed = (($eStart < $rStart) ? $eStart : $rStart) + $mismatch;
      }
      $matchEnd = $rStop;
      $matchLen = $eStop;
    }
    my $tmp = $matchEnd - $matchBegin + $offset;
    if ($matchLen < $tmp) {
      $matchLen = $tmp;
    }
    if (($oldori eq '+')
	&& ($matchLen > $minLen)
	&& ($matchMissed / $matchLen < $maxMissed)) { 
      print $matchfh "$mrna $cdsBegin $cdsEnd embl:$oldestid ",
	    "$matchBegin $matchEnd $offset $matchMissed\n";
    }
    close $matchfh;
    close $fh;
    unlink <$datadir/Evaluate/tmp*>;
  }
}

sub collect_testsets {
  # reads the list generated by analyzeClusters and for each
  # full-length mRNA determines at most one piece of EST entirely
  # contained in coding sequence and one contained in untranslated
  # region.

  my $fh = gensym;
  my($estcdsfile, $estutrfile) = @_;
  log_print(" - collecting ESTs from coding sequences and"
	    . " untranslated regions...");

  if ((-s $estutrfile) && (-s $estcdsfile)) {
    log_print("   $estcdsfile already exists, skipped");
    log_print("   $estutrfile already exists, skipped");
    return;
  }

  my $i = 0;
  my($oldmrna, $utrdone, $cdsdone);
  my $minUTRLen = 100;   # minimum length of a valid UTR match
  my $matchfile = "$datadir/Evaluate/matches.lst";
  my $nbMatches = `wc -l $matchfile`;
  $nbMatches =~ m/^\s*(\S+)/;
  $nbMatches = $1;
  my $matchfh = gensym;
  open $matchfh, $matchfile;
  while ( <$matchfh> ) {
    my ($mrna, $cdsBegin, $cdsEnd, $est,
	$matchBegin, $matchEnd, $offset, $mismatches) = split;
    if ($mrna ne $oldmrna) {
      $utrdone = undef;
      $cdsdone = undef;
      $oldmrna = $mrna;
    }
    my $matchLen = $matchEnd - $matchBegin + $offset;
    next if $matchLen < $minUTRLen;
    if ($matchEnd < $cdsBegin) { # match is entirely in 5'UTR
      unless (defined $utrdone) { 
	addESTBegin($est, $estutrfile, $matchLen);
	$utrdone = 1;
      }
    } else {
      if ($matchEnd < $cdsEnd) { # match end is in CDS
	if ($matchBegin < $cdsBegin) { # match overlaps CDS begin
	  if (!defined($utrdone) && (($cdsBegin-$matchBegin) > $minUTRLen)) { 
	    addESTBegin($est, $estutrfile,
			$cdsBegin - $matchBegin + $offset - 1);
	    $utrdone = 1;
	  }
	  unless (defined $cdsdone) { 
	    addCdsEST($est, $estcdsfile, $matchLen, 
		      $cdsBegin - $matchBegin + $offset, $matchLen);
	    $cdsdone = 1; 
	  }
	} else { # match is included in CDS
	  unless (defined $cdsdone) { 
	    addCdsEST($est, $estcdsfile, $matchLen, 1, $matchLen); 
	    $cdsdone = 1; 
	  }
	}
      } else { # match end is in 3'UTR
	if ($matchBegin < $cdsBegin) { # match includes CDS
	  if (!defined($utrdone) && (($cdsBegin-$matchBegin) > $minUTRLen)) { 
	    addESTBegin($est, $estutrfile,
			$cdsBegin - $matchBegin + $offset - 1);
	    $utrdone = 1;
	  }
	  if (!defined($utrdone) && (($matchEnd - $cdsEnd) > $minUTRLen)) { 
	    addESTEnd($est, $estutrfile, $matchLen, $matchEnd - $cdsEnd);
	    $utrdone = 1;
	  }
	  unless (defined $cdsdone) { 
	    addCdsEST($est, $estcdsfile, $matchLen, 
		      $cdsBegin - $matchBegin + $offset, 
		      $cdsEnd - $matchBegin + $offset); 
	    $cdsdone = 1; 
	  }
	} else { 
	  if ($matchBegin < $cdsEnd) { # match overlaps CDS end
	    if (!defined($utrdone) && (($matchEnd-$cdsEnd) > $minUTRLen)) { 
	      addESTEnd($est, $estutrfile, $matchLen, $matchEnd - $cdsEnd);
	      $utrdone = 1;
	    }
	    unless (defined $cdsdone) { 
	      addCdsEST($est, $estcdsfile, $matchLen, 
			1, $cdsEnd - $matchBegin + $offset);
	      $cdsdone = 1; 
	    }
	  } else { # match is entirely in 3'UTR
	    if (!defined($utrdone) && ($matchEnd - $matchBegin > $minUTRLen)) { 
	      addESTBegin($est, $estutrfile, $matchLen);
	      $utrdone = 1;
	    }
	  }
	}
      }
    }
    $i += 1;
    if (($i % 100) == 0) {
      log_print("   $i of $nbMatches matches evaluated");
    }
  }
  close $matchfh;
}

sub addCdsEST {
  my($est, $estfile, $len, $cdsFrom, $cdsTo) = @_;
  my $src = FASTAFile->new("fetch -f $est |");
  $src->openStream;
  my $e = $src->getNext;
  close $src->{_BTFfile};
  $e->{_seq} = substr $e->{_seq}, 0, $len;
  $e->{_seqHead} =~ s/(>\S+)/$1 CDS: $cdsFrom $cdsTo /;
  $e->{_seqHead} .= " (first $len nucleotides)";
  my $fh = gensym;
  open $fh, ">> $estfile";
  $e->printFASTA($fh);
  close $fh;
}

sub addESTBegin {
  my($est, $estfile, $len) = @_;
  my $src = FASTAFile->new("fetch -f $est |");
  $src->openStream;
  my $e = $src->getNext;
  close $src->{_BTFfile};
  $e->{_seq} = substr $e->{_seq}, 0, $len;
  $e->{_seqHead} .= " (first $len nucleotides)";
  my $fh = gensym;
  open $fh, ">> $estfile";
  $e->printFASTA($fh);
  close $fh;
}

sub addESTEnd {
  my($est, $estfile, $matchLen, $len) = @_;
  my $src = FASTAFile->new("fetch -f $est |");
  $src->openStream;
  my $e = $src->getNext;
  close $src->{_BTFfile};
  $e->{_seq} = substr $e->{_seq}, 0, $matchLen;
  $e->{_seq} = substr $e->{_seq}, -$len;
  $e->{_seqHead} .= " (last $len nucleotides)";
  my $fh = gensym;
  open $fh, ">> $estfile";
  $e->printFASTA($fh);
  close $fh;
}

################################################################################
#
#   Documentation
#

=head1 NAME

extract_UG_EST - extract ESTs from UG clusters as test sets for ESTScan

=head1 SYNOPSIS

extract_UG_EST [options] <config-files...>

=head1 DESCRIPTION

The data needed for evaluating ESTScan using ESTs is extracted from UniGene
clusters.  UniGene clusters are used to determine ESTs from untranslated
regions and coding sequence respectively.  This is done by matching the ESTs
of a given cluster against its full-length mRNA with megablast and then
determining where the match occurs relative to the annotated coding sequence.
For each category, coding and non-coding, a single EST is chosen per cluster,
in order to avoid redundancy.  The matching location also allows to determine
where coding sequences start and end in partially coding ESTs.  The same
annotation in FASTA headers is used as for mRNAs.  The sets of coding and
non-coding ESTs can later be used to perform the same computational
experiments as those done with mRNA data.

Files which already exist are reused. If an existing file is to be recomputed,
it must deleted before running the script again. For instance, if a particular
collection of EST sequences should be used instead of data extracted by
extract_UG_EST, providing these in FASTA format under the name of the EST file
(where extract_UG_EST would store the extracted data) is enough. The same
procedure can be applied to provide hand picked test data.  However, in mRNA
and EST data used for test and training, evaluate_model expects annotations of
coding sequence start and stop in the header as two integer values following
the tag 'CDS:'.  The first integer points to the first nucleotide of the CDS,
the second to the last.  Thus the length of the CDS is <stop> - <start> + 1.
The position counting starts with 1.

=head1 DIRECTORY STRUCTURE

extract_UG_EST uses the same directory structure as build_model, the root of
which is given in the configuration file.  From this root it adds the
subdirectory 'Evaluate', which contains all result files.  EST data as well as
test and training data files are deposited in the data-root directory if not
otherwise specified in the configuration file.

=head1 OPTIONS AND CONFIGURATION FILE

 -q
    Be quiet.

Additional parameters defined in the configuration files for extract_UG_EST
are listed here:

 * $ugdata
   Name of the file(s) containing data about unigene clusters.
   If this is not defined, no evaluation is currently implemented.

$filestem is used to generate many filenames. It is generated automatically
according to the tuplesize, the minmask and the pseudocounts applied to
generate them.

=head1 REQUIREMENTS

During analysis of UniGene clusters and evaluation of the generated tables
some external packages are used to collect and compare sequences.
extract_UG_EST relies on 'megablast' to determine where ESTs match on
full-length mRNA sequences.  The 'fetch' utility is used to find the EST and
mRNA entries.  This tool needs a properly indexed version of EMBL and RefSeq
flatfiles.  Use 'indexer' for this.  Both tools are part of the BTLib toolset.

=head1 AUTHOR

Claudio Lottaz, SIB-ISREC, Claudio.Lottaz@isb-sib.ch

=cut

#
#   End of file
#
################################################################################