File: ucsc_genes2gff.pl

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (724 lines) | stat: -rwxr-xr-x 19,752 bytes parent folder | download | duplicates (4)
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#!/usr/bin/env perl

# convert UCSC gene files into GFF3 data

use strict;
use File::Basename 'basename';
use Getopt::Long;
use URI::Escape;
use Text::Wrap;
$Text::Wrap::columns = 79;
use Bio::SeqIO;
use Bio::SeqFeature::Generic;
use Data::Dumper;

my $executable = basename($0);

my ($SRCDB,$ORIGIN,$ANNOTATIONS);
GetOptions('srcdb:s'       => \$SRCDB,
           'origin:i'      => \$ORIGIN,
           'annotations:s' => \$ANNOTATIONS,
          ) and $ANNOTATIONS or die <<USAGE;
Usage: $0 -annotations <dir> [options]

Convert UCSC Genome Browser-format gene files into GFF3 version files.
Only the gene IDs and their locations come through.  You have to get
the comments and aliases some other way. Currently some info is being added
as additional application-specific gff tags.

Options:

    -srcdb       <string>   Choose a source for the gene, default "UCSC"
    -origin      <integer>  Choose a relative position to number from, default is "1"
    -annotations <dir>      Directory containing UCSC annotation files
    -center      <string>   ???

USAGE

$SRCDB           ||= 'UCSC';
$ORIGIN          ||= 1;
my $KGXREF         = $ANNOTATIONS.'/kgXref.txt';
my $KNOWNGENE      = $ANNOTATIONS.'/knownGene.txt';
my $KNOWNGENEPEP   = $ANNOTATIONS.'/knownGenePep.txt';
my $KNOWNGENEMRNA  = $ANNOTATIONS.'/knownGeneMrna.txt';
my $KNOWNLOCUSLINK = $ANNOTATIONS.'/knownToLocusLink.txt';
my $KNOWNPFAM      = $ANNOTATIONS.'/knownToPfam.txt';
my $KNOWNU133      = $ANNOTATIONS.'/knownToU133.txt';
my $KNOWNU133PLUS  = $ANNOTATIONS.'/knownToU133Plus2.txt';
my $KNOWNU95       = $ANNOTATIONS.'/knownToU95.txt';
my $GENBANK        = $ANNOTATIONS.'/genbank2accessions.txt';
my $LOCACC         = $ANNOTATIONS.'/loc2acc';
my $LOCGO          = $ANNOTATIONS.'/loc2go';
my $LOCUG          = $ANNOTATIONS.'/loc2UG';
my $REFLINK        = $ANNOTATIONS.'/refLink.txt';
my $REFSEQSUMMARY  = $ANNOTATIONS.'/refSeqSummary.txt';
my $CHROMINFO      = $ANNOTATIONS.'/chromInfo.txt';

my %xref;
my %loc2mrna;
my %ref2mrna;

print STDERR "Parsing Genbank...";
parseGenbank(\%xref,$GENBANK);
print STDERR "done!\n";
print STDERR "Parsing Genbank Peptide...";
parseKnownGenePep(\%xref,$KNOWNGENEPEP);
print STDERR "done!\n";
print STDERR "Parsing Known Gene...";
parseKnownGeneMrna(\%xref,$KNOWNGENEMRNA);
print STDERR "done!\n";
print STDERR "Parsing Known Gene Xref...";
parseKgXref(\%xref,$KGXREF);
print STDERR "done!\n";
print STDERR "Parsing LocusLink...";
parseLocAcc(\%xref,$LOCACC); # the best way I've found so far
                                                  # to link Genbank mRNA accession to
                                                  # Genbank protein accession
print STDERR "done!\n";
print STDERR "Parsing LocusLink -> GO...";
parseLocGo(\%xref,$LOCGO);
print STDERR "done!\n";
print STDERR "Parsing LocusLink -> UniGene...";
parseLocUG(\%xref,$LOCUG);
print STDERR "done!\n";
print STDERR "Parsing Known Gene -> Locuslink...";
parseKnownLocusLink(\%xref,$KNOWNLOCUSLINK);
print STDERR "done!\n";
print STDERR "Parsing Known Gene -> Affy...";
parseKnownAffy(\%xref,$KNOWNU133PLUS,$KNOWNU133,$KNOWNU95);
print STDERR "done!\n";
print STDERR "Parsing Known Gene -> PFAM...";
parseKnownPfam(\%xref,$KNOWNPFAM);
print STDERR "done!\n";
print STDERR "Parsing Known Gene -> RefSeq...";
parseRefLink(\%xref,$REFLINK);
print STDERR "done!\n";
print STDERR "Parsing Known Gene -> RefSeq Summary...";
parseRefSeqSummary(\%xref,$REFSEQSUMMARY);
print STDERR "done!\n";

print "##gff-version 3\n";
#if(1){ #for debugging
open(KG,$KNOWNGENE) or die "couldn't open('$KNOWNGENE'): $!";
while (<KG>) {
  chomp;
  next if /^\#/;;
  my ($id,$chrom,$strand,$txStart,$txEnd,$cdsStart,$cdsEnd,$exons,$exonStarts,$exonEnds) = split /\t/;
  my ($utr5_start,$utr5_end,$utr3_start,$utr3_end);

  # adjust for Jim's 0-based coordinates
  $txStart++;
  $cdsStart++;

  $txStart  -= $ORIGIN;
  $txEnd    -= $ORIGIN;
  $cdsStart -= $ORIGIN;
  $cdsEnd   -= $ORIGIN;

  # print the transcript
  print join ("\t",$chrom,$SRCDB,'mRNA',$txStart,$txEnd,'.',$strand,'.',"ID=$id;");
  if(defined($xref{$id})) {
    print "Dbxref=";
    my @annotation_sets = grep { $_ =~ /^db:/ && $_ !~ /protein/ } keys %{$xref{$id}};
    my $i = 0;
    foreach my $annotation_set ( @annotation_sets ) {
      next unless $annotation_set;
      $annotation_set =~ s/^db://;

      #yeah, it's a hack.  so fix it.
      my $foo = join(",",
                     map {uri_escape("$annotation_set:$_")}
                     grep { $xref{$id}{'db:'.$annotation_set}{$_} != 0 && $_ != 0 }
                     keys %{$xref{$id}{'db:'.$annotation_set}}
                    );
      print $foo;
      $i++;
      print "," if $foo && $i != scalar(@annotation_sets);
    }
    print ";";

    if(my @aliases = keys %{ $xref{$id}{Alias} }){
      print "Alias=" . join(",", map {uri_escape($_)} @aliases) . ";";
    }

    if(my @notes = keys %{ $xref{$id}{Note} }){
      print "Note=" . join(",", map {uri_escape($_)} @notes) . ";";
    }

    print "\n";

    #and print the protein if there is one
    if( my($prot_id) = keys %{$xref{$id}{'db:RefSeq_protein'}} ){
      @annotation_sets = grep { $_ =~ /^db:/ && $_ =~ /protein/ } keys %{$xref{$id}};
      print join ("\t",'.',$SRCDB,'protein','.','.','.','.','.',"ID=$prot_id;Parent=$id;");
      my $i = 0;
      print "Dbxref=";
      foreach my $annotation_set ( @annotation_sets ) {
        my @a = keys %{ $xref{$id}{$annotation_set} };
        $annotation_set =~ s/db://;
        my $j = 0;
        foreach my $a (@a){
          print uri_escape( "$annotation_set:$a" ) if $a;
          $j++;
          print "," if $a && $j != scalar(@a);
        }
        $i++;
        print "," unless $i == scalar(@annotation_sets);
      }
      print ";\n";
    }
  }

  # now handle the CDS entries -- the tricky part is the need to keep
  # track of phase
  my $phase = 0;
  my @exon_starts = map {$_-$ORIGIN} split ',',$exonStarts;
  my @exon_ends   = map {$_-$ORIGIN} split ',',$exonEnds;

  if($strand eq '+') {
    for(my $i=0;$i<scalar(@exon_starts);$i++) {
      my $exon_start = $exon_starts[$i] + 1;
      my $exon_end   = $exon_ends[$i];
      my ($utr_start,$utr_end,$cds_start,$cds_end);

      if($exon_start < $cdsStart) { # in a 5' UTR
        $utr_start = $exon_start;
      } elsif($exon_start > $cdsEnd) {
        $utr_start = $exon_start;
      } else {
        $cds_start = $exon_start;
      }

      if($exon_end < $cdsStart) {
        $utr_end = $exon_end;
      } elsif($exon_end > $cdsEnd) {
        $utr_end = $exon_end;
      } else {
        $cds_end = $exon_end;
      }

      if($utr_start && !$utr_end) { # half in half out on 5' end
        $utr_end   = $cdsStart - 1;
        $cds_start = $cdsStart;
        $cds_end   = $exon_end;
      }

      if($utr_end && !$utr_start) { # half in half out on 3' end
        $utr_start = $cdsEnd + 1;
        $cds_end   = $cdsEnd;
        $cds_start = $exon_start;
      }

      die "programmer error, utr_start and no utr_end" unless defined $utr_start == defined $utr_end;
      die "programmer error, cds_start and no cds_end" unless defined $cds_start == defined $cds_end;

      if(defined $utr_start && $utr_start <= $utr_end && $utr_start < $cdsStart) {
        print join ("\t",$chrom,$SRCDB,"five_prime_UTR",$utr_start,$utr_end,'.',$strand,'.',"Parent=$id"),"\n"	
      }

      if(defined $cds_start && $cds_start <= $cds_end) {
        print join ("\t",$chrom,$SRCDB,'CDS',$cds_start,$cds_end,'.',$strand,$phase,"Parent=$id"),"\n";
        $phase = (($cds_end-$cds_start+1-$phase)) % 3;
      }

      if(defined $utr_start && $utr_start <= $utr_end && $utr_start > $cdsEnd) {
        print join ("\t",$chrom,$SRCDB,"three_prime_UTR",,$utr_start,$utr_end,'.',$strand,'.',"Parent=$id"),"\n"	
      }
    }
  }

  if($strand eq '-') {
    my @lines;
    for(my $i=@exon_starts-1; $i>=0; $i--) { # count backwards
      my $exon_start = $exon_starts[$i] + 1;
      my $exon_end   = $exon_ends[$i];
      my ($utr_start,$utr_end,$cds_start,$cds_end);

      if($exon_end > $cdsEnd) { # in a 5' UTR
        $utr_end = $exon_end;
      } elsif($exon_end < $cdsStart) {
        $utr_end = $exon_end;
      } else {
        $cds_end = $exon_end;
      }

      if($exon_start > $cdsEnd) {
        $utr_start = $exon_start;
      } elsif($exon_start < $cdsStart) {
        $utr_start = $exon_start;
      } else {
        $cds_start = $exon_start;
      }

      if($utr_start && !$utr_end) { # half in half out on 3' end
        $utr_end   = $cdsStart - 1;
        $cds_start = $cdsStart;
        $cds_end   = $exon_end;
      }

      if ($utr_end && !$utr_start) { # half in half out on 5' end
        $utr_start = $cdsEnd + 1;
        $cds_end   = $cdsEnd;
        $cds_start = $exon_start;
      }

      die "programmer error, utr_start and no utr_end" unless defined $utr_start == defined $utr_end;
      die "programmer error, cds_start and no cds_end" unless defined $cds_start == defined $cds_end;

      if(defined $utr_start && $utr_start <= $utr_end && $utr_start > $cdsEnd) {
        unshift @lines,join ("\t",$chrom,$SRCDB,"five_prime_UTR",,$utr_start,$utr_end,'.',$strand,'.',"Parent=$id"),"\n"	
      }
      if(defined $cds_start && $cds_start <= $cds_end) {
        unshift @lines,join ("\t",$chrom,$SRCDB,'CDS',$cds_start,$cds_end,'.',$strand,$phase,"Parent=$id"),"\n";
        $phase = (($cds_end-$cds_start+1-$phase)) % 3;
      }

      if(defined $utr_start && $utr_start <= $utr_end && $utr_end < $cdsStart) {
        unshift @lines,join ("\t",$chrom,$SRCDB,"three_prime_UTR",$utr_start,$utr_end,'.',$strand,'.',"Parent=$id"),"\n"	
      }

    }
    print @lines;	
  }
}
close(KG) or die "couldn't close('$KNOWNGENE'): $!";
#} for debugging

print "##FASTA\n";

foreach my $kg (keys %xref){
  my $seq_mrna = $xref{$kg}{'sequence:mrna'};
  my $seq_prot = $xref{$kg}{'sequence:protein'};

  if($seq_mrna){
    foreach my $k (keys %{$xref{$kg}{'db:GenBank_mRNA'}}){
      print ">$k\n". wrap('','',$seq_mrna) ."\n";
    }
  }

  if($seq_prot){
    foreach my $k (keys %{$xref{$kg}{'db:GenBank_protein'}}){
      print ">$k\n". wrap('','',$seq_prot) ."\n";
    }
  }
}

=head2 parseLocAcc

 Title   : parseLocAcc
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseLocAcc {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my @line = split /\t/;

    #note: this doesn't work b/c if the second regex doesn't match, $1 is still
    #leftover from the first regex.  a better method is given below.
	#$line[1] =~ /(.*)\.\d/;
	#my $gene = $1;
	#$line[4] =~ /(.*)\.\d/;
	#my $protein = $1;

    my $loc = $line[0];

    my $gene    = $line[1];
    my $protein = $line[4];
    $gene    =~ s/\.\d+$//;
    $protein =~ s/\.\d+$//;

    next if $gene eq 'none';

    push @{ $loc2mrna{$loc} }, $gene;

	$xref->{$gene}{'db:GenBank_protein'}{$protein} = 1 unless($gene eq 'none' || $protein eq '-' || $protein !~ /^[A-Z]{3}\d/ || $protein =~ /_/);
  }
  close ANNFILE;
}

=head2 parseLocGo

 Title   : parseLocGo
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseLocGo {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my @line = split /\t/;

    if($loc2mrna{$line[0]}){
      foreach my $mrna (@{$loc2mrna{$line[0]}}){
        $xref->{$mrna}{'cvterm:go'}{$line[1]} = 1;
      }
    }
  }
  close ANNFILE;
}

=head2 parseLocUG

 Title   : parseLocUG
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseLocUG {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my @line = split /\t/;

    if($loc2mrna{$line[0]}){
      foreach my $mrna (@{$loc2mrna{$line[0]}}){
        $xref->{$mrna}{'db:Unigene'}{$line[1]} = 1;
      }
    }
  }
  close ANNFILE;
}

=head2 parseRefLink

 Title   : parseRefLink
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseRefLink {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my($symbol,$description,$refmrna,$refprotein,undef,undef,$locus,$omim) = split /\t/;
    $description = uri_escape($description);

    if($ref2mrna{$refmrna}){
      foreach my $mrna (@{$ref2mrna{$refmrna}}){
        $xref->{$mrna}{'db:LocusLink'}{$locus} = 1;
        $xref->{$mrna}{'db:OMIM'}{$omim} = 1;
        $xref->{$mrna}{'db:RefSeq_mRNA'}{$refmrna} = 1;
        $xref->{$mrna}{'db:RefSeq_protein'}{$refprotein} = 1;
        $xref->{$mrna}{'Alias'}{$symbol} = 1;
        $xref->{$mrna}{'Note'}{$description} = 1;
      }
    }
  }
  close ANNFILE;
}

=head2 parseRefSeqSummary

 Title   : parseRefSeqSummary
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseRefSeqSummary {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my($refmrna,$completeness,$description) = split /\t/;
    $description = uri_escape($description);

    if($ref2mrna{$refmrna}){
      foreach my $mrna (@{$ref2mrna{$refmrna}}){
        $xref->{$mrna}{'completeness'}{$completeness} = 1;
        $xref->{$mrna}{'description'}{$description} = 1;
      }
    }
  }
  close ANNFILE;
}

=head2 parseKnownLocusLink

 Title   : parseKnownLocusLink
 Usage   :
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseKnownLocusLink {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my ($accession,$locuslink) = split /\t/;
	$xref->{$accession}{'db:LocusLink'}{$locuslink} = 1;
  }
  close ANNFILE;
}

=head2 parseKnownPfam

 Title   : parseKnownPfam
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub parseKnownPfam {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my ($accession,$pfam) = split /\t/;
	$xref->{$accession}{'db:PFAM'}{$pfam} = 1;
  }
  close ANNFILE;
}

=head2 parseKnownAffy

 Title   : parseKnownAffy
 Usage   :
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub parseKnownAffy {
  my $xref = shift @_;

  my $i = 0;
  my @chip = qw(U133PLUS U133 U95);
  foreach my $filename (@_){
    open  ANNFILE, $filename or die "Can't open file $filename: $!";
    while(<ANNFILE>) {
      chomp;
      next if /^#/;
      my ($accession,$probeset) = split /\t/;
      $xref->{$accession}{'db:Affymetrix_'.$chip[$i]}{$probeset} = 1;
    }
    close ANNFILE;
    $i++;
  }
}


=head2 parseKnownGeneMrna

 Title   : parseKnownGeneMrna
 Usage   : Links mRNA sequence to mRNA.
 Function:
 Example :
 Returns :
 Args    :

=cut

sub parseKnownGeneMrna {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my ($accession,$sequence) = split /\t/;
	$xref->{$accession}{'sequence:mrna'} = $sequence;
  }
  close ANNFILE;
}

=head2 parseKnownGenePep

 Title   : parseKnownGenePep
 Usage   : This method depends on parseLocAcc being run first so mRNA accessions can
           be mapped to protein accessions.
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub parseKnownGenePep {
  my($xref,$filename) = @_;
  open  ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	my ($accession,$sequence) = split /\t/;

    $xref->{$accession}{'sequence:protein'} = $sequence;
#	$xref->{$protGenbankId}{'sequence:protein'} = $sequence;
  }
  close ANNFILE;
}

=head2 mrna2protein

 Title   : mrna2protein
 Usage   : creates a hash between the mRNA genbank accession
           (used in UCSC DB to key everything) and the proper
           genbank protein accession
 Function:
 Example :
 Returns : 
 Args    :


=cut

sub parseGenbank {
  my($xref,$filename) = @_;
#  my $file = shift;
#  my $annotations = {}; # stores the mRNA genbank id as key, protein genbank id as value
  open ANNFILE, $filename or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
    my ($mrna, $prot) = split /\t/;
    $xref->{$mrna}{'db:GenBank_protein'}{$prot} = 1 if $prot =~ /^[A-Z]{3}\d/ and $prot !~ /_/;
#    $annotations->{$mrna} = $prot;
  }
  close ANNFILE;
#  return($annotations);
}



=head2 parseKgXref

 Title   : parseKgXref
 Usage   :
 Function:
 Example :
 Returns :
 Args    :


=cut

sub parseKgXref {
  my($xref,$filename) = @_;

  open(ANNFILE, $filename) or die "Can't open file $filename: $!";
  while(<ANNFILE>) {
    chomp;
    next if /^#/;
	# first two are the same (genebank) followed by swissprot etc...
    my ($kgID, $mRNA, $spID, $spDisplayID, $geneSymbol, $refseq, $protAcc, $description) = split /\t/;
    my $key = $kgID;
    # escape certain fields
    $key = uri_escape($key);
    $description = uri_escape($description);

    push @{ $ref2mrna{$refseq} }, $kgID;

    #http://www.ncbi.nlm.nih.gov/RefSeq/key.html#accessions
    #http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html#AccessionB
    #http://www.ncbi.nlm.nih.gov/Sitemap/samplerecord.html#ProteinIDB

    $xref->{$key}{'db:GenBank_protein'}{$kgID}          = 1 if $kgID and $kgID =~ /^[A-Z]{1,2}\d/ and $kgID !~ /_/;
    $xref->{$key}{'db:GenBank_mRNA'}{$mRNA}             = 1 if $mRNA and $mRNA =~ /^[A-Z]{1,2}\d/ and $mRNA !~ /_/ and $mRNA ne $kgID;
    $xref->{$key}{'db:Swiss'}{$spID}                    = 1 if $spID;
    $xref->{$key}{'db:Swiss'}{$spDisplayID}             = 1 if $spDisplayID and $spDisplayID ne $spID;
    $xref->{$key}{'Alias'}{$geneSymbol}                 = 1 if $geneSymbol;
    $xref->{$key}{'db:RefSeq_mRNA'}{$refseq}            = 1 if $refseq and $refseq =~ /^(NC|NG|NM|NR|NT|NW|XM|XR|NZ)_/;
    $xref->{$key}{'db:RefSeq_protein'}{$protAcc}        = 1 if $protAcc and $protAcc =~ /^(NP|XP|ZP)_/;
    $xref->{$key}{'Note'}{$description}          = 1 if $description;
  }
  close(ANNFILE);
}


__END__

=head1 NAME

ucsc_genes2gff.pl - Convert UCSC Genome Browser-format gene files into GFF files suitable for loading into gbrowse

=head1 SYNOPSIS

  % ucsc_genes2gff.pl [options] ucsc_file1 ucsc_file2...

Options:

    -src    <string>   Choose a source for the gene, default "UCSC"
    -origin <integer>  Choose a relative position to number from, default is "1"

=head1 DESCRIPTION

This script massages the gene files available from the "tables" link
of the UCSC genome browser (genome.ucsc.edu) into a form suitable for
loading of gbrowse.  Warning: it only works with the gene tables.
Other tables, such as EST alignments, contours and repeats, have their
own formats which will require other scripts to parse.

To use this script, get one or more UCSC tables, either from the
"Tables" link on the browser, or from the UCSC Genome Browser FTP
site.  Give the table file as the argument to this script.  You may
want to provide an alternative "source" field.  Otherwise this script
defaults to "UCSC".

  % ucsc_genes2gff.pl -src RefSeq refseq_data.ucsc > refseq.gff

The resulting GFF file can then be loaded into a Bio::DB::GFF database
using the following command:

  % bulk_load_gff.pl -d <databasename> refseq.gff

=head1 SEE ALSO

L<Bio::DB::GFF>, L<bulk_load_gff.pl>, L<load_gff.pl>

=head1 AUTHOR

Allen Day <allenday@ucla.edu>, Lincoln Stein <lstein@cshl.org>.

Copyright (c) 2003 Cold Spring Harbor Laboratory

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.  See DISCLAIMER.txt for
disclaimers of warranty.

=cut