This file is indexed.

/usr/bin/gbf2xml is in ncbi-entrez-direct 7.40.20170928+ds-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl

# ===========================================================================
#
#                            PUBLIC DOMAIN NOTICE
#            National Center for Biotechnology Information (NCBI)
#
#  This software/database is a "United States Government Work" under the
#  terms of the United States Copyright Act.  It was written as part of
#  the author's official duties as a United States Government employee and
#  thus cannot be copyrighted.  This software/database is freely available
#  to the public for use. The National Library of Medicine and the U.S.
#  Government do not place any restriction on its use or reproduction.
#  We would, however, appreciate having the NCBI and the author cited in
#  any work or product based on this material.
#
#  Although all reasonable efforts have been taken to ensure the accuracy
#  and reliability of the software and data, the NLM and the U.S.
#  Government do not and cannot warrant the performance or results that
#  may be obtained by using this software or data. The NLM and the U.S.
#  Government disclaim all warranties, express or implied, including
#  warranties of performance, merchantability or fitness for any particular
#  purpose.
#
# ===========================================================================
#
# File Name:  gbf2xml
#
# Author:  Jonathan Kans
#
# Version Creation Date:   6/8/17
#
# ==========================================================================

use strict;
use warnings;


#  Script to convert GenBank flatfiles to INSDSeq XML.
#
#  Feature intervals that refer to 'far' locations, i.e., those not within
#  the cited record and which have an accession and colon, are suppressed.
#  Those rare features (e.g., trans-splicing between molecules) are lost.
#
#  Keywords and References are currently not supported.


# state variables for tracking current position in flatfile

my $in_seq;
my $in_feat;
my $in_key;
my $in_qual;
my $in_def;
my $in_tax;
my $any_feat;
my $any_qual;
my $current_key;
my $current_loc;
my $current_qual;
my $current_val;
my $moltype;
my $division;
my $update_date;
my $organism;
my $source;
my $taxonomy;
my $topology;
my $sequence;
my $length;
my $is_translation;
my $curr_seq;
my $locus;
my $defline;
my $accn;
my $accndv;
my $location_operator;
my $is_comp;

# subroutine to clear state variables for each flatfile
# start in in_feat state to gracefully handle missing FEATURES/FH line

sub clearflags {
  $in_seq = 0;
  $in_feat = 0;
  $in_key = 0;
  $in_qual = 0;
  $in_def = 0;
  $in_tax = 0;
  $any_feat = 0;
  $any_qual = 0;
  $current_key = "";
  $current_loc = "";
  $current_qual = "";
  $current_val = "";
  $moltype = "";
  $division = "";
  $update_date = "";
  $organism = "";
  $source = "";
  $taxonomy = "";
  $topology = "";
  $sequence = "";
  $length = 0;
  $is_translation = 0;
  $curr_seq = "";
  $locus = "";
  $defline = "";
  $accn = "";
  $accndv = "";
  $location_operator = "";
  $is_comp = 0;
}

# recursive subroutine for parsing flatfile representation of feature location

sub parseloc {
  my $subloc = shift (@_);
  my @working = ();

  if ( $subloc =~ /^(join|order)\((.+)\)$/ ) {
    $location_operator = $1;
    my $temploc = $2;
    my @items = split (',', $temploc);
    foreach my $thisloc (@items ) {
      if ( $thisloc !~ /^.*:.*$/ ) {
        push (@working, parseloc ($thisloc));
      }
    }

  } elsif ( $subloc =~ /^complement\((.+)\)$/ ) {
    $is_comp = 1;
    my $comploc = $1;
    my @items = parseloc ($comploc);
    my @rev = reverse (@items);
    foreach my $thisloc (@rev ) {
      if ( $thisloc =~ /^([^.]+)\.\.([^.]+)$/ ) {
        $thisloc = "$2..$1";
      }

      if ( $thisloc =~ /^>([^.]+)\.\.([^.]+)$/ ) {
        $thisloc = "<$1..$2";
      }
      if ( $thisloc =~ /^([^.]+)\.\.<([^.]+)$/ ) {
        $thisloc = "$1..>$2";
      }

      if ( $thisloc !~ /^.*:.*$/ ) {
        push (@working, parseloc ($thisloc));
      }
    }

  } elsif ( $subloc !~ /^.*:.*$/ ) {
    push (@working, $subloc);
  }

  return @working;
}

#subroutine to print next feature key / location / qualifier line

sub flushline {
  if ( $in_key == 1 ) {

    if ( $any_qual == 1 ) {
      print  "        </INSDFeature_quals>\n";
      $any_qual = 0;
    }

    if ( $any_feat == 1 ) {
      print  "      </INSDFeature>\n";
    }
    $any_feat = 1;

    print  "      <INSDFeature>\n";

    #print feature key and intervals
    print  "        <INSDFeature_key>$current_key</INSDFeature_key>\n";

    my $clean_loc = $current_loc;
    $clean_loc =~ s/</&lt;/g;
    $clean_loc =~ s/>/&gt;/g;
    print  "        <INSDFeature_location>$clean_loc</INSDFeature_location>\n";

    print  "        <INSDFeature_intervals>\n";

    # parse join() order() complement() ###..### location
    $location_operator = 0;
    $is_comp = 0;
    my @theloc = parseloc ($current_loc);

    # convert number (dot) (dot) number to number (tab) number
    my $numivals = 0;
    my $prime5 = 0;
    my $prime3 = 0;
    foreach my $thisloc (@theloc ) {
      $numivals++;
      print  "          <INSDInterval>\n";
      if ( $thisloc =~ /^([^.]+)\.\.([^.]+)$/ ) {
        my $fr = $1;
        my $to = $2;
        if ( $thisloc =~ /^</ ) {
          $prime5 = 1;
        }
        if ( $thisloc =~ /\.\.>/ ) {
          $prime3 = 1;
        }
        $fr =~ s/[<>]//;
        $to =~ s/[<>]//;
        print  "            <INSDInterval_from>$fr</INSDInterval_from>\n";
        print  "            <INSDInterval_to>$to</INSDInterval_to>\n";
        if ( $is_comp ) {
          print  "            <INSDInterval_iscomp value=\"true\"/>\n";
        }
        print  "            <INSDInterval_accession>$accndv</INSDInterval_accession>\n";
      } elsif ( $thisloc =~ /^(.+)\^(.+)$/ ) {
        my $fr = $1;
        my $to = $2;
        $fr =~ s/[<>]//;
        $to =~ s/[<>]//;
        print  "            <INSDInterval_from>$fr</INSDInterval_from>\n";
        print  "            <INSDInterval_to>$to</INSDInterval_to>\n";
        if ( $is_comp ) {
          print  "            <INSDInterval_iscomp value=\"true\"/>\n";
        }
        print  "            <INSDInterval_interbp value=\"true\"/>\n";
        print  "            <INSDInterval_accession>$accndv</INSDInterval_accession>\n";
      } elsif ( $thisloc =~ /^([^.]+)$/ ) {
        my $pt = $1;
        $pt =~ s/[<>]//;
        print  "            <INSDInterval_point>$pt</INSDInterval_point>\n";
        print  "            <INSDInterval_accession>$accndv</INSDInterval_accession>\n";
      }
      print  "          </INSDInterval>\n";
    }

    print  "        </INSDFeature_intervals>\n";

    if ( $numivals > 1 ) {
      print  "        <INSDFeature_operator>$location_operator</INSDFeature_operator>\n";
    }
    if ( $prime5 ) {
      print  "        <INSDFeature_partial5 value=\"true\"/>\n";
    }
    if ( $prime3 ) {
      print  "        <INSDFeature_partial3 value=\"true\"/>\n";
    }

  } elsif ( $in_qual == 1 ) {

    if ( $any_qual == 0 ) {
      print  "        <INSDFeature_quals>\n";
    }
    $any_qual = 1;

    if ( $current_val eq "" ) {
      print  "          <INSDQualifier>\n";
      print  "            <INSDQualifier_name>$current_qual</INSDQualifier_name>\n";
      print  "          </INSDQualifier>\n";
    } else {
      print  "          <INSDQualifier>\n";
      print  "            <INSDQualifier_name>$current_qual</INSDQualifier_name>\n";
      my $clean_val = $current_val;
      $clean_val =~ s/</&lt;/g;
      $clean_val =~ s/>/&gt;/g;
      print  "            <INSDQualifier_value>$clean_val</INSDQualifier_value>\n";
      print  "          </INSDQualifier>\n";
    }
  }
}

# initialize flags and lists at start of program

clearflags ();

print  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print  "<!DOCTYPE INSDSet PUBLIC \"-//NCBI//INSD INSDSeq/EN\" \"https://www.ncbi.nlm.nih.gov/dtd/INSD_INSDSeq.dtd\">\n";
print  "<INSDSet>\n";

# main loop reads one line at a time

while (<> ) {
  chomp;
  $_ =~ s/\r$//;

  # first check for extra definition or taxonomy lines, otherwise clear continuation flags
  if ( $in_def ) {
    if ( /^ {12}(.*)$/ ) {
      $defline = $defline . " " . $1;
    } else {
      $in_def = 0;
    }
  } elsif ( $in_tax ) {
    if ( /^ {12}(.*)$/ ) {
      if ( $taxonomy eq "" ) {
        $taxonomy = $1;
      } else {
        $taxonomy = $taxonomy . " " . $1;
      }
    } else {
      $in_tax = 0;
    }
  }

  if ( $in_def == 1 || $in_tax == 1 ) {

    # continuation lines taken care of above

  } elsif ( /^LOCUS\s+(\S*).*$/ ) {

    # record locus
    $locus = $1;
    if ( / (\d+) bp / || / (\d+) aa / ) {
      $length = $1;
    }

    if ( /^.*\s(\S+\s+\S+\s+\S+\s+\d+-\S+-\d+)$/ ) {
      my $tail = $1;
      if ( $tail =~ /^(\S*)\s+(\S*)\s+(\S*)\s+(\d*-\S*-\d*)$/ ) {
        $moltype = $1;
        $topology = $2;
        $division = $3;
        $update_date = $4;
        $moltype = uc $moltype;
      }
    }

    print  "  <INSDSeq>\n";

    print  "    <INSDSeq_locus>$locus</INSDSeq_locus>\n";
    print  "    <INSDSeq_length>$length</INSDSeq_length>\n";

    if ( $moltype ne "" ) {
      print  "    <INSDSeq_moltype>$moltype</INSDSeq_moltype>\n";
    }
    if ( $topology ne "" ) {
      print  "    <INSDSeq_topology>$topology</INSDSeq_topology>\n";
    }
    if ( $division ne "" ) {
      print  "    <INSDSeq_division>$division</INSDSeq_division>\n";
    }
    if ( $update_date ne "" ) {
      print  "    <INSDSeq_update-date>$update_date</INSDSeq_update-date>\n";
    }

  } elsif ( /^DEFINITION\s*(.*).*$/ ) {

    # record first line of definition line
    $defline = $1;
    # next line with leading spaces will be continuation of definition line
    $in_def = 1;

  } elsif ( /^ACCESSION\s*(\S*).*$/ ) {

    # record accession
    $accn = $1;

  } elsif ( /^VERSION\s*(\S*).*$/ ) {

    # record accession.version
    $accndv = $1;

  } elsif ( /^SOURCE\s*(.*)$/ ) {

    # record source
    $source = $1;

  } elsif ( /^ {1,3}ORGANISM\s+(.*)$/ ) {

    # record organism
    if ( $organism eq "" ) {
      $organism = $1;
      if ( $organism =~ /^([^(]*) \(.*\)/ ) {
        $organism = $1;
      }
    }
    # next line with leading spaces will be start of taxonomy
    $in_tax = 1;

  } elsif ( /^FEATURES\s+.*$/ ) {

    # beginning of feature table, flags already set up

    # first print saved fields
    $defline =~ s/\.$//;
    $defline =~ s/</&lt;/g;
    $defline =~ s/>/&gt;/g;
    if ( $defline ne "" ) {
      print  "    <INSDSeq_definition>$defline</INSDSeq_definition>\n";
    }
    if ( $accn ne "" ) {
      print  "    <INSDSeq_primary-accession>$accn</INSDSeq_primary-accession>\n";
    }
    if ( $accndv ne "" ) {
      print  "    <INSDSeq_accession-version>$accndv</INSDSeq_accession-version>\n";
    }

    $in_feat = 1;

    if ( $source ne "" ) {
      print  "    <INSDSeq_source>$source</INSDSeq_source>\n";
    }
    if ( $organism ne "" ) {
      print  "    <INSDSeq_organism>$organism</INSDSeq_organism>\n";
    }
    $taxonomy =~ s/\.$//;
    if ( $taxonomy ne "" ) {
      print  "    <INSDSeq_taxonomy>$taxonomy</INSDSeq_taxonomy>\n";
    }

    print  "    <INSDSeq_feature-table>\n";

  } elsif ( /^ORIGIN\s*.*$/ ) {

    # end of feature table, print final newline
    flushline ();

    if ( $any_qual == 1 ) {
      print  "        </INSDFeature_quals>\n";
      $any_qual = 0;
    }

    print  "      </INSDFeature>\n";

    print  "    </INSDSeq_feature-table>\n";

    $in_feat = 0;
    $in_key = 0;
    $in_qual = 0;
    $is_translation = 0;
    $in_seq = 1;

  } elsif ( /^\/\/\.*/ ) {

    # at end-of-record double slash
    print  "    <INSDSeq_sequence>$sequence</INSDSeq_sequence>\n";
    print  "  </INSDSeq>\n";
    # reset variables for catenated flatfiles
    clearflags ();

  } elsif ( $in_seq == 1 ) {

    if ( /^\s+\d+ (.*)$/ || /^\s+(.*)\s+\d+$/ ) {
      # record sequence
      $curr_seq = $1;
      $curr_seq =~ s/ //g;
      $curr_seq = lc $curr_seq;
      if ( $sequence eq "" ) {
        $sequence = $curr_seq;
      } else {
        $sequence = $sequence . $curr_seq;
      }
    }

  } elsif ( $in_feat == 1 ) {

    if ( /^ {1,10}(\w+)\s+(.*)$/ ) {
      # new feature key and location
      flushline ();

      $in_key = 1;
      $in_qual = 0;
      $current_key = $1;
      $current_loc = $2;

    } elsif ( /^\s+\/(\w+)=(.*)$/ ) {
      # new qualifier
      flushline ();

      $in_key = 0;
      $in_qual = 1;
      $current_qual = $1;
      # remove leading double quote
      my $val = $2;
      $val =~ s/\"//g;
      $current_val = $val;
      if ( $current_qual =~ /(?:translation|transcription|peptide)/ ) {
        $is_translation = 1;
      } else {
        $is_translation = 0;
      }

    } elsif ( /^\s+\/(\w+)$/ ) {
      # new singleton qualifier - e.g., trans-splicing, pseudo
      flushline ();

      $in_key = 0;
      $in_qual = 1;
      $current_qual = $1;
      $current_val = "";
      $is_translation = 0;

    } elsif ( /^\s+(.*)$/ ) {

      if ( $in_key == 1 ) {
        # continuation of feature location
        $current_loc = $current_loc . $1;

      } elsif ( $in_qual == 1 ) {
        # continuation of qualifier
        # remove trailing double quote
        my $val = $1;
        $val =~ s/\"//g;
        if ( $is_translation == 1 ) {
          $current_val = $current_val . $val;
        } else {
          $current_val = $current_val . " " . $val;
        }
      }
    }
  }
}

print  "</INSDSet>\n";