~ubuntu-branches/ubuntu/natty/ncbi-tools6/natty

« back to all changes in this revision

Viewing changes to demo/tbl2asn.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2002-04-04 22:13:09 UTC
  • Revision ID: james.westby@ubuntu.com-20020404221309-vfze028rfnlrldct
Tags: upstream-6.1.20011220a
ImportĀ upstreamĀ versionĀ 6.1.20011220a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*   tbl2asn.c
 
2
* ===========================================================================
 
3
*
 
4
*                            PUBLIC DOMAIN NOTICE
 
5
*            National Center for Biotechnology Information (NCBI)
 
6
*
 
7
*  This software/database is a "United States Government Work" under the
 
8
*  terms of the United States Copyright Act.  It was written as part of
 
9
*  the author's official duties as a United States Government employee and
 
10
*  thus cannot be copyrighted.  This software/database is freely available
 
11
*  to the public for use. The National Library of Medicine and the U.S.
 
12
*  Government do not place any restriction on its use or reproduction.
 
13
*  We would, however, appreciate having the NCBI and the author cited in
 
14
*  any work or product based on this material
 
15
*
 
16
*  Although all reasonable efforts have been taken to ensure the accuracy
 
17
*  and reliability of the software and data, the NLM and the U.S.
 
18
*  Government do not and cannot warrant the performance or results that
 
19
*  may be obtained by using this software or data. The NLM and the U.S.
 
20
*  Government disclaim all warranties, express or implied, including
 
21
*  warranties of performance, merchantability or fitness for any particular
 
22
*  purpose.
 
23
*
 
24
* ===========================================================================
 
25
*
 
26
* File Name:  tbl2asn.c
 
27
*
 
28
* Author:  Jonathan Kans
 
29
*
 
30
* Version Creation Date:   5/5/00
 
31
*
 
32
* $Revision: 6.25 $
 
33
*
 
34
* File Description: 
 
35
*
 
36
* Modifications:  
 
37
* --------------------------------------------------------------------------
 
38
* Date     Name        Description of modification
 
39
* -------  ----------  -----------------------------------------------------
 
40
*
 
41
*
 
42
* ==========================================================================
 
43
*/
 
44
 
 
45
#include <ncbi.h>
 
46
#include <objall.h>
 
47
#include <objsset.h>
 
48
#include <objsub.h>
 
49
#include <objfdef.h>
 
50
#include <sequtil.h>
 
51
#include <edutil.h>
 
52
#include <seqport.h>
 
53
#include <gather.h>
 
54
#include <sqnutils.h>
 
55
#include <subutil.h>
 
56
#include <toasn3.h>
 
57
#include <valid.h>
 
58
#include <asn2gnbk.h>
 
59
#include <explore.h>
 
60
#include <simple.h>
 
61
 
 
62
static FILE* OpenOneFile (
 
63
  CharPtr directory,
 
64
  CharPtr base,
 
65
  CharPtr suffix
 
66
)
 
67
 
 
68
{
 
69
  Char  file [FILENAME_MAX], path [PATH_MAX];
 
70
 
 
71
  if (base == NULL) {
 
72
    base = "";
 
73
  }
 
74
  if (suffix == NULL) {
 
75
    suffix = "";
 
76
  }
 
77
  StringNCpy_0 (path, directory, sizeof (path));
 
78
  sprintf (file, "%s%s", base, suffix);
 
79
  FileBuildPath (path, NULL, file);
 
80
 
 
81
  return FileOpen (path, "r");
 
82
}
 
83
 
 
84
static void WriteOneFile (
 
85
  CharPtr results,
 
86
  CharPtr base,
 
87
  CharPtr suffix,
 
88
  SeqEntryPtr sep,
 
89
  SubmitBlockPtr sbp
 
90
)
 
91
 
 
92
{
 
93
  AsnIoPtr   aip;
 
94
  Char       file [FILENAME_MAX], path [PATH_MAX];
 
95
  SeqSubmit  ssb;
 
96
 
 
97
  MemSet ((Pointer) &ssb, 0, sizeof (SeqSubmit));
 
98
  ssb.sub = sbp;
 
99
  ssb.datatype = 1;
 
100
  ssb.data = (Pointer) sep;
 
101
 
 
102
  StringNCpy_0 (path, results, sizeof (path));
 
103
  sprintf (file, "%s%s", base, suffix);
 
104
  FileBuildPath (path, NULL, file);
 
105
 
 
106
  aip = AsnIoOpen (path, "w");
 
107
  if (aip == NULL) return;
 
108
 
 
109
  if (sbp != NULL) {
 
110
    SeqSubmitAsnWrite (&ssb, aip, NULL);
 
111
  } else {
 
112
    SeqEntryAsnWrite (sep, aip, NULL);
 
113
  }
 
114
 
 
115
  AsnIoFlush (aip);
 
116
  AsnIoClose (aip);
 
117
}
 
118
 
 
119
static void ValidateOneFile (
 
120
  CharPtr results,
 
121
  CharPtr base,
 
122
  CharPtr suffix,
 
123
  SeqEntryPtr sep
 
124
)
 
125
 
 
126
{
 
127
  Char            file [FILENAME_MAX], path [PATH_MAX];
 
128
  ErrSev          oldErrSev;
 
129
  ValidStructPtr  vsp;
 
130
 
 
131
  StringNCpy_0 (path, results, sizeof (path));
 
132
  sprintf (file, "%s%s", base, suffix);
 
133
  FileBuildPath (path, NULL, file);
 
134
 
 
135
  ErrSetOptFlags (EO_LOGTO_USRFILE);
 
136
  ErrSetLogfile (path, ELOG_APPEND | ELOG_NOCREATE);
 
137
 
 
138
  vsp = ValidStructNew ();
 
139
  if (vsp != NULL) {
 
140
    vsp->useSeqMgrIndexes = TRUE;
 
141
    vsp->suppressContext = TRUE;
 
142
    oldErrSev = ErrSetMessageLevel (SEV_NONE);
 
143
    ValidateSeqEntry (sep, vsp);
 
144
    ValidStructFree (vsp);
 
145
    ErrSetMessageLevel (oldErrSev);
 
146
  }
 
147
 
 
148
  ErrSetLogfile (NULL, ELOG_APPEND | ELOG_NOCREATE);
 
149
  ErrClearOptFlags  (EO_LOGTO_USRFILE);
 
150
}
 
151
 
 
152
static void FlatfileOneFile (
 
153
  CharPtr results,
 
154
  CharPtr base,
 
155
  CharPtr suffix,
 
156
  SeqEntryPtr sep
 
157
)
 
158
 
 
159
{
 
160
  Char    file [FILENAME_MAX], path [PATH_MAX];
 
161
  FILE    *fp;
 
162
  ErrSev  oldErrSev;
 
163
 
 
164
  StringNCpy_0 (path, results, sizeof (path));
 
165
  sprintf (file, "%s%s", base, suffix);
 
166
  FileBuildPath (path, NULL, file);
 
167
 
 
168
  fp = FileOpen (path, "w");
 
169
  if (fp == NULL) return;
 
170
 
 
171
  oldErrSev = ErrSetMessageLevel (SEV_MAX);
 
172
  SeqEntryToGnbk (sep, NULL, GENBANK_FMT, ENTREZ_MODE, NORMAL_STYLE, 0, fp);
 
173
  ErrSetMessageLevel (oldErrSev);
 
174
 
 
175
  FileClose (fp);
 
176
}
 
177
 
 
178
/* for full-length cDNAs, allow automatic annotation of largest internal ORF */
 
179
 
 
180
typedef struct orfdata {
 
181
  Int4     curlen [6], bestlen [6], currstart [6], beststart [6], sublen [6];
 
182
  Boolean  inorf [6], altstart;
 
183
} OrfData, PNTR OrfDataPtr;
 
184
 
 
185
static void LIBCALLBACK LookForOrfs (
 
186
  Int4 position,
 
187
  Char residue,
 
188
  Boolean atgStart,
 
189
  Boolean altStart,
 
190
  Boolean orfStop,
 
191
  Int2 frame,
 
192
  Uint1 strand,
 
193
  Pointer userdata
 
194
)
 
195
 
 
196
{
 
197
  Int2        idx;
 
198
  OrfDataPtr  odp;
 
199
 
 
200
  odp = (OrfDataPtr) userdata;
 
201
  if (strand == Seq_strand_plus) {
 
202
 
 
203
    /* top strand */
 
204
 
 
205
    idx = frame;
 
206
    if (odp->inorf [idx]) {
 
207
      if (orfStop) {
 
208
        odp->inorf [idx] = FALSE;
 
209
        if (odp->curlen [idx] > odp->bestlen [idx]) {
 
210
          odp->bestlen [idx] = odp->curlen [idx];
 
211
          odp->beststart [idx] = odp->currstart [idx];
 
212
        }
 
213
      } else {
 
214
        (odp->curlen [idx])++;
 
215
      }
 
216
    } else if (atgStart || (altStart && odp->altstart)) {
 
217
      odp->inorf [idx] = TRUE;
 
218
      odp->curlen [idx] = 1;
 
219
      odp->currstart [idx] = position - frame;
 
220
    }
 
221
  } else {
 
222
 
 
223
    /* bottom strand */
 
224
 
 
225
    idx = frame + 3;
 
226
    if (orfStop) {
 
227
      odp->curlen [idx] = 0;
 
228
      odp->sublen [idx] = 0;
 
229
      odp->currstart [idx] = position - frame;
 
230
    } else if (atgStart || (altStart && odp->altstart)) {
 
231
      (odp->sublen [idx])++;
 
232
      odp->curlen [idx] = odp->sublen [idx];
 
233
      if (odp->curlen [idx] > odp->bestlen [idx]) {
 
234
        odp->bestlen [idx] = odp->curlen [idx];
 
235
        odp->beststart [idx] = odp->currstart [idx];
 
236
      }
 
237
    } else {
 
238
      (odp->sublen [idx])++;
 
239
    }
 
240
  }
 
241
}
 
242
 
 
243
static SeqFeatPtr AnnotateBestOrf (
 
244
  BioseqPtr bsp,
 
245
  Int2 genCode,
 
246
  Boolean altstart,
 
247
  SqnTagPtr stp
 
248
  
 
249
)
 
250
 
 
251
{
 
252
  SeqFeatPtr      cds = NULL;
 
253
  CdRegionPtr     crp;
 
254
  GeneRefPtr      grp;
 
255
  Int2            i, best, idx;
 
256
  OrfData         od;
 
257
  ProtRefPtr      prp;
 
258
  SeqFeatPtr      sfp;
 
259
  SeqInt          sint;
 
260
  CharPtr         str;
 
261
  TransTablePtr   tbl;
 
262
  ValNode         vn;
 
263
  SeqFeatXrefPtr  xref;
 
264
 
 
265
  if (bsp == NULL) return NULL;
 
266
  for (i = 0; i < 6; i++) {
 
267
    od.curlen [i] = INT4_MIN;
 
268
    od.bestlen [i] = 0;
 
269
    od.currstart [i] = 0;
 
270
    od.beststart [i] = 0;
 
271
    od.sublen [i] = INT4_MIN;
 
272
    od.inorf [i] = FALSE;
 
273
  }
 
274
  od.altstart = altstart;
 
275
 
 
276
  /* use simultaneous 6-frame translation finite state machine */
 
277
 
 
278
  tbl = PersistentTransTableByGenCode (genCode);
 
279
  if (tbl != NULL) {
 
280
    TransTableProcessBioseq (tbl, LookForOrfs, (Pointer) &od, bsp);
 
281
  }
 
282
  /* TransTableFree (tbl); - now using persistent tables, free at end */
 
283
  best = -1;
 
284
  idx = -1;
 
285
  for (i = 0; i < 6; i++) {
 
286
    if (od.bestlen [i] > best) {
 
287
      best = od.bestlen [i];
 
288
      idx = i;
 
289
    }
 
290
  }
 
291
  if (idx == -1) return NULL;
 
292
 
 
293
  /* make feature location on largest ORF */
 
294
 
 
295
  if (idx < 3) {
 
296
    MemSet ((Pointer) &sint, 0, sizeof (SeqInt));
 
297
    sint.from = od.beststart [idx] + idx;
 
298
    sint.to = sint.from + (od.bestlen [idx]) * 3 + 2;
 
299
    sint.id = SeqIdFindBest (bsp->id, 0);
 
300
    sint.strand = Seq_strand_plus;
 
301
    vn.choice = SEQLOC_INT;
 
302
    vn.extended = 0;
 
303
    vn.data.ptrvalue = (Pointer) &sint;
 
304
    vn.next = NULL;
 
305
  } else {
 
306
    MemSet ((Pointer) &sint, 0, sizeof (SeqInt));
 
307
    sint.from = od.beststart [idx] + idx - 3;
 
308
    sint.to = sint.from + (od.bestlen [idx]) * 3 + 2;
 
309
    sint.id = SeqIdFindBest (bsp->id, 0);
 
310
    sint.strand = Seq_strand_minus;
 
311
    vn.choice = SEQLOC_INT;
 
312
    vn.extended = 0;
 
313
    vn.data.ptrvalue = (Pointer) &sint;
 
314
    vn.next = NULL;
 
315
  }
 
316
 
 
317
  /* make CDS feature with unknown product - now check [protein=...] */
 
318
 
 
319
  cds = CreateNewFeatureOnBioseq (bsp, SEQFEAT_CDREGION, &vn);
 
320
  if (cds == NULL) return NULL;
 
321
  crp = CreateNewCdRgn (1, FALSE, genCode);
 
322
  if (crp == NULL) return NULL;
 
323
  crp->frame = 1;
 
324
  cds->data.value.ptrvalue = (Pointer) crp;
 
325
 
 
326
  prp = ProtRefNew ();
 
327
  if (prp == NULL) return cds;
 
328
  xref = SeqFeatXrefNew ();
 
329
  if (xref == NULL) return cds;
 
330
  xref->data.choice = SEQFEAT_PROT;
 
331
  xref->data.value.ptrvalue = (Pointer) prp;
 
332
  xref->next = cds->xref;
 
333
  cds->xref = xref;
 
334
  prp = ParseTitleIntoProtRef (stp, prp);
 
335
  if (prp->name == NULL) {
 
336
    prp->name = ValNodeCopyStr (NULL, 0, "unknown");
 
337
  }
 
338
 
 
339
  /* parse CDS comment ("note" goes to biosource) and experimental evidence */
 
340
 
 
341
  str = SqnTagFind (stp, "comment");
 
342
  if (! StringHasNoText (str)) {
 
343
    cds->comment = StringSave (str);
 
344
  }
 
345
 
 
346
  str = SqnTagFind (stp, "evidence");
 
347
  if (StringICmp (str, "experimental") == 0) {
 
348
    cds->exp_ev = 1;
 
349
  }
 
350
 
 
351
  /* now check [gene=...], make gene feature if locus or synonym present */
 
352
 
 
353
  grp = GeneRefNew ();
 
354
  if (grp == NULL) return cds;
 
355
  grp = ParseTitleIntoGeneRef (stp, grp);
 
356
  if (grp->locus == NULL && grp->syn == NULL) {
 
357
    GeneRefFree (grp);
 
358
    return cds;
 
359
  }
 
360
  sfp = CreateNewFeatureOnBioseq (bsp, SEQFEAT_GENE, NULL);
 
361
  if (sfp == NULL) return cds;
 
362
  sfp->data.value.ptrvalue = (Pointer) grp;
 
363
 
 
364
  return cds;
 
365
}
 
366
 
 
367
/* change all feature IDs to entered accession */
 
368
 
 
369
static void PromoteSeqId (SeqIdPtr sip, Pointer userdata)
 
370
 
 
371
{
 
372
  SeqIdPtr  bestid, newid, oldid;
 
373
 
 
374
  bestid = (SeqIdPtr) userdata;
 
375
 
 
376
  newid = SeqIdDup (bestid);
 
377
  if (newid == NULL) return;
 
378
 
 
379
  oldid = ValNodeNew (NULL);
 
380
  if (oldid == NULL) return;
 
381
 
 
382
  MemCopy (oldid, sip, sizeof (ValNode));
 
383
  oldid->next = NULL;
 
384
 
 
385
  sip->choice = newid->choice;
 
386
  sip->data.ptrvalue = newid->data.ptrvalue;
 
387
 
 
388
  SeqIdFree (oldid);
 
389
  ValNodeFree (newid);
 
390
 
 
391
  SeqIdStripLocus (sip);
 
392
}
 
393
 
 
394
static void CorrectFeatureSeqIds (
 
395
  SeqFeatPtr sfp,
 
396
  Pointer userdata
 
397
)
 
398
 
 
399
{
 
400
  VisitSeqIdsInSeqLoc (sfp->location, userdata, PromoteSeqId);
 
401
}
 
402
 
 
403
/* source information for several common organisms sequenced by genome centers */
 
404
 
 
405
typedef struct orgstuff {
 
406
  CharPtr  taxname;
 
407
  CharPtr  common;
 
408
  CharPtr  lineage;
 
409
  CharPtr  division;
 
410
  Uint1    gcode;
 
411
  Uint1    mgcode;
 
412
  Int4     taxID;
 
413
} OrgStuff, PNTR OrfStuffPtr;
 
414
 
 
415
static OrgStuff commonOrgStuff [] = {
 
416
  {
 
417
    "Saccharomyces cerevisiae", "baker's yeast",
 
418
    "Eukaryota; Fungi; Ascomycota; Saccharomycetes; Saccharomycetales; Saccharomycetaceae; Saccharomyces",
 
419
    "PLN", 1, 3, 4932
 
420
  },
 
421
  {
 
422
    "Drosophila melanogaster", "fruit fly",
 
423
    "Eukaryota; Metazoa; Arthropoda; Tracheata; Hexapoda; Insecta; Pterygota; Neoptera; Endopterygota; Diptera; Brachycera; Muscomorpha; Ephydroidea; Drosophilidae; Drosophila",
 
424
    "INV", 1, 5, 7227
 
425
  },
 
426
  {
 
427
    "Homo sapiens", "human",
 
428
    "Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Mammalia; Eutheria; Primates; Catarrhini; Hominidae; Homo",
 
429
    "PRI", 1, 2, 9606
 
430
  },
 
431
  {
 
432
    "Escherichia coli", "",
 
433
    "Bacteria; Proteobacteria; gamma subdivision; Enterobacteriaceae; Escherichia",
 
434
    "BCT", 11, 0, 562
 
435
  },
 
436
  {
 
437
    "Helicobacter pylori", "",
 
438
    "Bacteria; Proteobacteria; epsilon subdivision; Helicobacter group; Helicobacter",
 
439
    "BCT", 11, 0, 210
 
440
  },
 
441
  {
 
442
    "Arabidopsis thaliana", "thale cress",
 
443
    "Eukaryota; Viridiplantae; Embryophyta; Tracheophyta; Spermatophyta; Magnoliophyta; eudicotyledons; core eudicots; Rosidae; eurosids II; Brassicales; Brassicaceae; Arabidopsis",
 
444
    "PLN", 1, 1, 3702
 
445
  },
 
446
  {
 
447
    "Mus musculus", "house mouse",
 
448
    "Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Mammalia; Eutheria; Rodentia; Sciurognathi; Muridae; Murinae; Mus",
 
449
    "ROD", 1, 2, 10090
 
450
  },
 
451
  {
 
452
    "Rattus norvegicus", "Norway rat",
 
453
    "Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Mammalia; Eutheria; Rodentia; Sciurognathi; Muridae; Murinae; Rattus",
 
454
    "ROD", 1, 2, 10116
 
455
  },
 
456
  {
 
457
    "Danio rerio", "zebrafish",
 
458
    "Eukaryota; Metazoa; Chordata; Craniata; Vertebrata; Euteleostomi; Actinopterygii; Neopterygii; Teleostei; Euteleostei; Ostariophysi; Cypriniformes; Cyprinidae; Rasborinae; Danio",
 
459
    "VRT", 1, 2, 7955
 
460
  },
 
461
  {
 
462
    "Zea mays", "",
 
463
    "Eukaryota; Viridiplantae; Embryophyta; Tracheophyta; Spermatophyta; Magnoliophyta; Liliopsida; Poales; Poaceae; Zea",
 
464
    "PLN", 1, 1, 4577
 
465
  },
 
466
  {
 
467
    "Caenorhabditis elegans", "",
 
468
    "Eukaryota; Metazoa; Nematoda; Chromadorea; Rhabditida; Rhabditoidea; Rhabditidae; Peloderinae; Caenorhabditis",
 
469
    "INV", 1, 5, 6239
 
470
  },
 
471
  {
 
472
    "Caenorhabditis briggsae", "",
 
473
    "Eukaryota; Metazoa; Nematoda; Chromadorea; Rhabditida; Rhabditoidea; Rhabditidae; Peloderinae; Caenorhabditis",
 
474
    "INV", 1, 5, 6238
 
475
  },
 
476
  {
 
477
    "Anopheles gambiae", "African malaria mosquito",
 
478
    "Eukaryota; Metazoa; Arthropoda; Tracheata; Hexapoda; Insecta; Pterygota; Neoptera; Endopterygota; Diptera; Nematocera; Culicoidea; Anopheles",
 
479
    "INV", 1, 5, 7165
 
480
  },
 
481
  {
 
482
    "Anopheles gambiae str. PEST", "African malaria mosquito",
 
483
    "Eukaryota; Metazoa; Arthropoda; Tracheata; Hexapoda; Insecta; Pterygota; Neoptera; Endopterygota; Diptera; Nematocera; Culicoidea; Anopheles",
 
484
    "INV", 1, 5, 180454
 
485
  },
 
486
  {
 
487
    "Tetrahymena thermophila", "",
 
488
    "Eukaryota; Alveolata; Ciliophora; Oligohymenophorea; Hymenostomatida; Tetrahymenina; Tetrahymena",
 
489
    "INV", 6, 4, 5911
 
490
  },
 
491
  {
 
492
    NULL, NULL, NULL, 0, 0, 0
 
493
  }
 
494
};
 
495
 
 
496
static Boolean HasTaxon (OrgRefPtr orp)
 
497
 
 
498
{
 
499
  ValNodePtr  db;
 
500
  DbtagPtr    dbt;
 
501
 
 
502
  if (orp == FALSE) return FALSE;
 
503
  for (db = orp->db; db != NULL; db = db->next) {
 
504
    dbt = (DbtagPtr) db->data.ptrvalue;
 
505
    if (dbt != NULL && dbt->db != NULL &&
 
506
        StringICmp (dbt->db, "taxon") == 0) return TRUE;
 
507
  }
 
508
  return FALSE;
 
509
}
 
510
 
 
511
static void AddMissingSourceInfo (BioSourcePtr biop)
 
512
 
 
513
{
 
514
  ValNodePtr   db;
 
515
  DbtagPtr     dbt;
 
516
  Int2         idx;
 
517
  ObjectIdPtr  oip;
 
518
  OrgNamePtr   onp;
 
519
  OrgRefPtr    orp;
 
520
  OrfStuffPtr  osp;
 
521
 
 
522
  if (biop == NULL) return;
 
523
  orp = biop->org;
 
524
  if (orp == NULL) return;
 
525
  onp = orp->orgname;
 
526
  if (onp == NULL) return;
 
527
 
 
528
  /* look for entry of organisms in commonOrgStuff table */
 
529
 
 
530
  for (idx = 0; commonOrgStuff [idx].taxname != NULL; idx++) {
 
531
    osp = &(commonOrgStuff [idx]);
 
532
    if (StringICmp (orp->taxname, osp->taxname) == 0) {
 
533
      if (StringHasNoText (orp->common) && (! StringHasNoText (osp->common))) {
 
534
        orp->common = StringSave (osp->common);
 
535
      }
 
536
      if (onp->gcode == 0) {
 
537
        onp->gcode = osp->gcode;
 
538
      }
 
539
      if (onp->mgcode == 0) {
 
540
        onp->mgcode = osp->mgcode;
 
541
      }
 
542
      if (StringHasNoText (onp->div)) {
 
543
        onp->div = StringSave (osp->division);
 
544
      }
 
545
      if (StringHasNoText (onp->lineage)) {
 
546
        onp->lineage = StringSave (osp->lineage);
 
547
      }
 
548
      if (! HasTaxon (orp)) {
 
549
        db = ValNodeNew (NULL);
 
550
        if (db != NULL) {
 
551
          dbt = DbtagNew ();
 
552
          if (dbt != NULL) {
 
553
            oip = ObjectIdNew ();
 
554
            if (oip != NULL) {
 
555
              oip->id = osp->taxID;
 
556
              dbt->db = StringSave ("taxon");
 
557
              dbt->tag = oip;
 
558
              db->data.ptrvalue = (Pointer) dbt;
 
559
              orp->db = db;
 
560
            }
 
561
          }
 
562
        }
 
563
      }
 
564
    }
 
565
  }
 
566
}
 
567
 
 
568
static BioseqPtr SqnGetBioseqGivenSeqLoc (SeqLocPtr slp, Uint2 entityID)
 
569
 
 
570
{
 
571
  BioseqPtr    bsp;
 
572
  SeqEntryPtr  sep;
 
573
  SeqIdPtr     sip;
 
574
  SeqLocPtr    tmp;
 
575
 
 
576
  if (slp == NULL) return NULL;
 
577
  bsp = NULL;
 
578
  sip = SeqLocId (slp);
 
579
  if (sip != NULL) {
 
580
    bsp = BioseqFind (sip);
 
581
  } else {
 
582
    tmp = SeqLocFindNext (slp, NULL);
 
583
    if (tmp != NULL) {
 
584
      sip = SeqLocId (tmp);
 
585
      if (sip != NULL) {
 
586
        bsp = BioseqFind (sip);
 
587
        if (bsp != NULL) {
 
588
          sep = SeqMgrGetSeqEntryForData (bsp);
 
589
          entityID = ObjMgrGetEntityIDForChoice (sep);
 
590
          bsp = GetBioseqGivenSeqLoc (slp, entityID);
 
591
        }
 
592
      }
 
593
    }
 
594
  }
 
595
  return bsp;
 
596
}
 
597
 
 
598
static BioseqPtr GetBioseqReferencedByAnnot (SeqAnnotPtr sap, Uint2 entityID)
 
599
 
 
600
{
 
601
  SeqAlignPtr   align;
 
602
  BioseqPtr     bsp;
 
603
  DenseDiagPtr  ddp;
 
604
  DenseSegPtr   dsp;
 
605
  SeqFeatPtr    feat;
 
606
  SeqGraphPtr   graph;
 
607
  SeqIdPtr      sip;
 
608
  SeqLocPtr     slp;
 
609
  StdSegPtr     ssp;
 
610
  SeqLocPtr     tloc;
 
611
 
 
612
  if (sap == NULL) return NULL;
 
613
  switch (sap->type) {
 
614
    case 1 :
 
615
      feat = (SeqFeatPtr) sap->data;
 
616
      while (feat != NULL) {
 
617
        slp = feat->location;
 
618
        if (slp != NULL) {
 
619
          bsp = SqnGetBioseqGivenSeqLoc (slp, entityID);
 
620
          if (bsp != NULL) return bsp;
 
621
        }
 
622
        feat = feat->next;
 
623
      }
 
624
      break;
 
625
    case 2 :
 
626
      align = (SeqAlignPtr) sap->data;
 
627
      while (align != NULL) {
 
628
        if (align->segtype == 1) {
 
629
          ddp = (DenseDiagPtr) align->segs;
 
630
          if (ddp != NULL) {
 
631
            for (sip = ddp->id; sip != NULL; sip = sip->next) {
 
632
              bsp = BioseqFind (sip);
 
633
              if (bsp != NULL) return bsp;
 
634
            }
 
635
          }
 
636
        } else if (align->segtype == 2) {
 
637
          dsp = (DenseSegPtr) align->segs;
 
638
          if (dsp != NULL) {
 
639
            for (sip = dsp->ids; sip != NULL; sip = sip->next) {
 
640
              bsp = BioseqFind (sip);
 
641
              if (bsp != NULL) return bsp;
 
642
            }
 
643
          }
 
644
        } else if (align->segtype == 3) {
 
645
          ssp = (StdSegPtr) align->segs;
 
646
          if (ssp != NULL && ssp->loc != NULL) {
 
647
            for (tloc = ssp->loc; tloc != NULL; tloc = tloc->next) {
 
648
              bsp = BioseqFind (SeqLocId (tloc));
 
649
              if (bsp != NULL) return bsp;
 
650
            }
 
651
          }
 
652
        }
 
653
        align = align->next;
 
654
      }
 
655
      break;
 
656
    case 3 :
 
657
      graph = (SeqGraphPtr) sap->data;
 
658
      while (graph != NULL) {
 
659
        slp = graph->loc;
 
660
        if (slp != NULL) {
 
661
          bsp = SqnGetBioseqGivenSeqLoc (slp, entityID);
 
662
          if (bsp != NULL) return bsp;
 
663
        }
 
664
        graph = graph->next;
 
665
      }
 
666
      break;
 
667
    default :
 
668
      break;
 
669
  }
 
670
  return NULL;
 
671
}
 
672
 
 
673
static BioseqPtr AttachSeqAnnotEntity (Uint2 entityID, SeqAnnotPtr sap)
 
674
 
 
675
{
 
676
  BioseqPtr      bsp;
 
677
  Int2           genCode;
 
678
  SeqEntryPtr    oldscope;
 
679
  OMProcControl  ompc;
 
680
  SeqEntryPtr    sep;
 
681
  SeqFeatPtr     sfp = NULL;
 
682
 
 
683
  if (sap == NULL) return NULL;
 
684
  bsp = GetBioseqReferencedByAnnot (sap, entityID);
 
685
  if (bsp == NULL) {
 
686
    oldscope = SeqEntrySetScope (NULL);
 
687
    if (oldscope != NULL) {
 
688
      bsp = GetBioseqReferencedByAnnot (sap, entityID);
 
689
      SeqEntrySetScope (oldscope);
 
690
    }
 
691
  }
 
692
  if (bsp != NULL) {
 
693
    sep = SeqMgrGetSeqEntryForData (bsp);
 
694
    entityID = ObjMgrGetEntityIDForChoice (sep);
 
695
    if (sap->type == 1) {
 
696
      sfp = (SeqFeatPtr) sap->data;
 
697
      sep = GetBestTopParentForData (entityID, bsp);
 
698
      genCode = SeqEntryToGeneticCode (sep, NULL, NULL, 0);
 
699
      SetEmptyGeneticCodes (sap, genCode);
 
700
    }
 
701
    MemSet ((Pointer) &ompc, 0, sizeof (OMProcControl));
 
702
    ompc.input_entityID = entityID;
 
703
    ompc.input_itemID = GetItemIDGivenPointer (entityID, OBJ_BIOSEQ, (Pointer) bsp);
 
704
    ompc.input_itemtype = OBJ_BIOSEQ;
 
705
    ompc.output_itemtype = OBJ_SEQANNOT;
 
706
    ompc.output_data = (Pointer) sap;
 
707
    if (! AttachDataForProc (&ompc, FALSE)) {
 
708
      Message (MSG_POSTERR, "AttachSeqAnnotEntity failed");
 
709
    } else if (sfp != NULL) {
 
710
      PromoteXrefs (sfp, bsp, entityID);
 
711
    }
 
712
  } else {
 
713
    Message (MSG_POSTERR, "Feature table identifiers do not match record");
 
714
  }
 
715
  return bsp;
 
716
}
 
717
 
 
718
static CharPtr TrimBracketsFromString (CharPtr str)
 
719
 
 
720
{
 
721
  Uchar    ch;  /* to use 8bit characters in multibyte languages */
 
722
  CharPtr  dst;
 
723
  CharPtr  ptr;
 
724
 
 
725
  if (StringHasNoText (str)) return str;
 
726
 
 
727
  /* remove bracketed fields */
 
728
 
 
729
  dst = str;
 
730
  ptr = str;
 
731
  ch = *ptr;
 
732
  while (ch != '\0') {
 
733
    if (ch == '[') {
 
734
      ptr++;
 
735
      ch = *ptr;
 
736
      while (ch != '\0' && ch != ']') {
 
737
        ptr++;
 
738
        ch = *ptr;
 
739
      }
 
740
      ptr++;
 
741
      ch = *ptr;
 
742
    } else {
 
743
      *dst = ch;
 
744
      dst++;
 
745
      ptr++;
 
746
      ch = *ptr;
 
747
    }
 
748
  }
 
749
  *dst = '\0';
 
750
 
 
751
  /* remove runs of whitespace characters */
 
752
 
 
753
  dst = str;
 
754
  ptr = str;
 
755
  ch = *ptr;
 
756
  while (ch != '\0') {
 
757
    if (IS_WHITESP (ch)) {
 
758
      *dst = ch;
 
759
      dst++;
 
760
      ptr++;
 
761
      ch = *ptr;
 
762
      while (IS_WHITESP (ch)) {
 
763
        ptr++;
 
764
        ch = *ptr;
 
765
      }
 
766
    } else {
 
767
      *dst = ch;
 
768
      dst++;
 
769
      ptr++;
 
770
      ch = *ptr;
 
771
    }
 
772
  }
 
773
  *dst = '\0';
 
774
 
 
775
  return str;
 
776
}
 
777
 
 
778
static void ProcessOneNuc (
 
779
  Uint2 entityID,
 
780
  BioseqPtr bsp,
 
781
  BioSourcePtr src,
 
782
  CharPtr organism,
 
783
  Boolean findorf,
 
784
  Boolean altstart
 
785
)
 
786
 
 
787
{
 
788
  BioSourcePtr  biop = NULL;
 
789
  SeqFeatPtr    cds;
 
790
  GBBlockPtr    gbp;
 
791
  Int2          genCode;
 
792
  MolInfoPtr    mip;
 
793
  Boolean       mito;
 
794
  OrgNamePtr    onp;
 
795
  OrgRefPtr     orp;
 
796
  SeqEntryPtr   sep;
 
797
  SeqHistPtr    shp;
 
798
  SqnTagPtr     stp = NULL;
 
799
  CharPtr       str;
 
800
  CharPtr       ttl = NULL;
 
801
  ValNodePtr    vnp;
 
802
 
 
803
  if (bsp == NULL) return;
 
804
 
 
805
  sep = GetBestTopParentForData (entityID, bsp);
 
806
  genCode = SeqEntryToGeneticCode (sep, NULL, NULL, 0);
 
807
 
 
808
  if (bsp->mol == Seq_mol_na) {
 
809
    bsp->mol = Seq_mol_dna;
 
810
  }
 
811
 
 
812
  if (src != NULL) {
 
813
    src = AsnIoMemCopy ((Pointer) src,
 
814
                        (AsnReadFunc) BioSourceAsnRead,
 
815
                        (AsnWriteFunc) BioSourceAsnWrite);
 
816
  }
 
817
 
 
818
  vnp = ValNodeExtract (&(bsp->descr), Seq_descr_title);
 
819
  if (vnp != NULL) {
 
820
    ttl = (CharPtr) vnp->data.ptrvalue;
 
821
    if (ttl != NULL) {
 
822
      stp = SqnTagParse (ttl);
 
823
    }
 
824
  }
 
825
 
 
826
  if (stp != NULL) {
 
827
    biop = ParseTitleIntoBioSource (stp, organism, src);
 
828
    ParseTitleIntoBioseq (stp, bsp);
 
829
  }
 
830
  if (biop == NULL) {
 
831
    biop = ParseTitleIntoBioSource (NULL, organism, src);
 
832
  }
 
833
  if (biop != NULL) {
 
834
    SeqDescrAddPointer (&(bsp->descr), Seq_descr_source, (Pointer) biop);
 
835
    AddMissingSourceInfo (biop);
 
836
  }
 
837
 
 
838
  if (BioseqGetSeqDescr (bsp, Seq_descr_molinfo, NULL) == NULL) {
 
839
    mip = MolInfoNew ();
 
840
    if (mip != NULL) {
 
841
      if (stp != NULL) {
 
842
        mip = ParseTitleIntoMolInfo (stp, mip);
 
843
      }
 
844
      if (mip->biomol == 0) {
 
845
        mip->biomol = MOLECULE_TYPE_GENOMIC;
 
846
      }
 
847
      SeqDescrAddPointer (&(bsp->descr), Seq_descr_molinfo, (Pointer) mip);
 
848
    }
 
849
  }
 
850
 
 
851
  if (genCode == 0 && biop != NULL) {
 
852
    orp = biop->org;
 
853
    if (orp != NULL) {
 
854
      onp = orp->orgname;
 
855
      if (onp != NULL) {
 
856
        mito = (Boolean) (biop->genome == 4 || biop->genome == 5);
 
857
        if (mito) {
 
858
          genCode = onp->mgcode;
 
859
        } else {
 
860
          genCode = onp->gcode;
 
861
        }
 
862
      }
 
863
    }
 
864
  }
 
865
 
 
866
  if (stp != NULL) {
 
867
    gbp = ParseTitleIntoGenBank (stp, NULL);
 
868
    if (gbp != NULL && gbp->extra_accessions != NULL) {
 
869
      SeqDescrAddPointer (&(bsp->descr), Seq_descr_genbank, (Pointer) gbp);
 
870
    } else {
 
871
      gbp = GBBlockFree (gbp);
 
872
    }
 
873
 
 
874
    shp = ParseTitleIntoSeqHist (stp, NULL);
 
875
    if (shp != NULL && shp->replace_ids != NULL) {
 
876
      bsp->hist = SeqHistFree (bsp->hist);
 
877
      bsp->hist = shp;
 
878
    } else {
 
879
      shp = SeqHistFree (shp);
 
880
    }
 
881
  }
 
882
 
 
883
  if (findorf) {
 
884
    cds = AnnotateBestOrf (bsp, genCode, altstart, stp);
 
885
    if (cds != NULL) {
 
886
      PromoteXrefs (cds, bsp, entityID);
 
887
    }
 
888
  }
 
889
 
 
890
  if (stp != NULL) {
 
891
    SqnTagFree (stp);
 
892
  }
 
893
 
 
894
  TrimBracketsFromString (ttl);
 
895
  if (! StringHasNoText (ttl)) {
 
896
    str = StringSave (ttl);
 
897
    SeqDescrAddPointer (&(bsp->descr), Seq_descr_title, (Pointer) str);
 
898
  }
 
899
 
 
900
  ValNodeFreeData (vnp);
 
901
}
 
902
 
 
903
static void ProcessOneAnnot (
 
904
  SeqAnnotPtr sap,
 
905
  Uint2 entityID,
 
906
  CharPtr accn
 
907
)
 
908
 
 
909
{
 
910
  BioseqPtr    bsp;
 
911
  Int2         genCode;
 
912
  SeqEntryPtr  sep;
 
913
  SeqFeatPtr   sfp;
 
914
  SeqIdPtr     sip;
 
915
 
 
916
  if (sap == NULL) return;
 
917
 
 
918
  bsp = AttachSeqAnnotEntity (entityID, sap);
 
919
  if (bsp == NULL) return;
 
920
 
 
921
  sep = GetBestTopParentForData (entityID, bsp);
 
922
  genCode = SeqEntryToGeneticCode (sep, NULL, NULL, 0);
 
923
 
 
924
  /* if existing accession, coerce all SeqIds */
 
925
 
 
926
  if (! StringHasNoText (accn)) {
 
927
    sip = SeqIdFromAccession (accn, 0, NULL);
 
928
    if (sip != NULL) {
 
929
      bsp->id = SeqIdSetFree (bsp->id);
 
930
      bsp->id = sip;
 
931
      SeqMgrReplaceInBioseqIndex (bsp);
 
932
      VisitFeaturesOnBsp (bsp, (Pointer) bsp->id, CorrectFeatureSeqIds);
 
933
    }
 
934
  }
 
935
 
 
936
  /* for parsed in features or best ORF, promote CDS products to protein bioseq */
 
937
 
 
938
  for (sap = bsp->annot; sap != NULL; sap = sap->next) {
 
939
    if (sap->type == 1) {
 
940
      SetEmptyGeneticCodes (sap, genCode);
 
941
      sfp = (SeqFeatPtr) sap->data;
 
942
      PromoteXrefs (sfp, bsp, entityID);
 
943
    }
 
944
  }
 
945
}
 
946
 
 
947
static void ReplaceOnePeptide (
 
948
  SimpleSeqPtr ssp,
 
949
  Boolean conflict
 
950
)
 
951
 
 
952
{
 
953
  Uint1         aa;
 
954
  ByteStorePtr  bs;
 
955
  BioseqPtr     bsp;
 
956
  SeqFeatPtr    cds;
 
957
  CdRegionPtr   crp;
 
958
  SeqFeatPtr    prt;
 
959
  SeqIntPtr     sintp;
 
960
  SeqIdPtr      sip;
 
961
  SeqLocPtr     slp;
 
962
  CharPtr       str1, str2;
 
963
 
 
964
  if (ssp == NULL || ssp->numid < 1) return;
 
965
 
 
966
  sip = MakeSeqID (ssp->id [0]);
 
967
  bsp = BioseqFind (sip);
 
968
  SeqIdFree (sip);
 
969
  if (bsp == NULL || bsp->repr != Seq_repr_raw) return;
 
970
 
 
971
  /* remove trailing X and * */
 
972
 
 
973
  bs = ssp->seq;
 
974
  BSSeek (bs, -1, SEEK_END);
 
975
  aa = (Uint1) BSGetByte (bs);
 
976
  while ((aa == 'X' || aa == '*') && ssp->seqlen > 0) {
 
977
    BSSeek (bs, -1, SEEK_END);
 
978
    BSDelete (bs, 1);
 
979
    BSSeek (bs, -1, SEEK_END);
 
980
    aa = (Uint1) BSGetByte (bs);
 
981
  }
 
982
  ssp->seqlen = BSLen (bs);
 
983
 
 
984
  str1 = BSMerge (ssp->seq, NULL);
 
985
  str2 = BSMerge (bsp->seq_data, NULL);
 
986
 
 
987
  if (StringCmp (str1, str2) != 0) {
 
988
 
 
989
    /* swap sequence byte stores */
 
990
 
 
991
    bs = bsp->seq_data;
 
992
    bsp->seq_data = ssp->seq;
 
993
    ssp->seq = bs;
 
994
    bsp->length = BSLen (bsp->seq_data);
 
995
    bsp->seq_data_type = Seq_code_ncbieaa;
 
996
 
 
997
    cds = SeqMgrGetCDSgivenProduct (bsp, NULL);
 
998
    if (cds != NULL) {
 
999
      crp = (CdRegionPtr) cds->data.value.ptrvalue;
 
1000
 
 
1001
      /* conditionally set CDS conflict flag, suppress validator complaint */
 
1002
 
 
1003
      if (crp != NULL && conflict) {
 
1004
        crp->conflict = TRUE;
 
1005
      }
 
1006
    }
 
1007
 
 
1008
    prt = SeqMgrGetBestProteinFeature (bsp, NULL);
 
1009
    if (prt != NULL) {
 
1010
      slp = prt->location;
 
1011
      if (slp != NULL && slp->choice == SEQLOC_INT) {
 
1012
        sintp = (SeqIntPtr) slp->data.ptrvalue;
 
1013
        if (sintp != NULL) {
 
1014
          sintp->to = bsp->length - 1;
 
1015
        }
 
1016
      }
 
1017
    }
 
1018
  }
 
1019
 
 
1020
  MemFree (str1);
 
1021
  MemFree (str2);
 
1022
}
 
1023
 
 
1024
static Uint2 ProcessOneAsn (
 
1025
  FILE* fp,
 
1026
  BioSourcePtr src,
 
1027
  CharPtr organism,
 
1028
  Boolean findorf,
 
1029
  Boolean altstart
 
1030
)
 
1031
 
 
1032
{
 
1033
  BioseqPtr    bsp = NULL;
 
1034
  Pointer      dataptr;
 
1035
  Uint2        datatype, entityID;
 
1036
  SeqEntryPtr  sep;
 
1037
 
 
1038
  if (fp == NULL) return 0;
 
1039
 
 
1040
  dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID, FALSE, FALSE, TRUE, FALSE);
 
1041
  if (dataptr == NULL) return 0;
 
1042
 
 
1043
  sep = GetTopSeqEntryForEntityID (entityID);
 
1044
  bsp = FindNucBioseq (sep);
 
1045
  if (bsp == NULL) {
 
1046
    ObjMgrFreeByEntityID (entityID);
 
1047
    return 0;
 
1048
  }
 
1049
 
 
1050
  ProcessOneNuc (entityID, bsp, src, organism, findorf, altstart);
 
1051
 
 
1052
  return entityID;
 
1053
}
 
1054
 
 
1055
static Uint2 ProcessAsnSet (
 
1056
  FILE* fp,
 
1057
  BioSourcePtr src,
 
1058
  CharPtr organism,
 
1059
  Boolean findorf,
 
1060
  Boolean altstart
 
1061
)
 
1062
 
 
1063
{
 
1064
  BioseqPtr     bsp;
 
1065
  BioseqSetPtr  bssp;
 
1066
  Pointer       dataptr;
 
1067
  Uint2         datatype, entityID;
 
1068
  SeqEntryPtr   sep, topsep;
 
1069
 
 
1070
  bssp = BioseqSetNew ();
 
1071
  if (bssp == NULL) return 0;
 
1072
  bssp->_class = BioseqseqSet_class_genbank;
 
1073
 
 
1074
  topsep = SeqEntryNew ();
 
1075
  if (topsep == NULL) return 0;
 
1076
  topsep->choice = 2;
 
1077
  topsep->data.ptrvalue = (Pointer) bssp;
 
1078
 
 
1079
  entityID = ObjMgrRegister (OBJ_BIOSEQSET, (Pointer) bssp);
 
1080
 
 
1081
  while ((dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE)) != NULL) {
 
1082
    if (datatype == OBJ_BIOSEQ) {
 
1083
 
 
1084
      sep = SeqMgrGetSeqEntryForData (dataptr);
 
1085
      AddSeqEntryToSeqEntry (topsep, sep, FALSE);
 
1086
 
 
1087
      bsp = (BioseqPtr) dataptr;
 
1088
      ProcessOneNuc (entityID, bsp, src, organism, findorf, altstart);
 
1089
 
 
1090
    } else {
 
1091
      ObjMgrFree (datatype, dataptr);
 
1092
    }
 
1093
  }
 
1094
 
 
1095
  SeqMgrLinkSeqEntry (topsep, 0, NULL);
 
1096
 
 
1097
  return entityID;
 
1098
}
 
1099
 
 
1100
static void ProcessOneRecord (
 
1101
  SubmitBlockPtr sbp,
 
1102
  PubdescPtr pdp,
 
1103
  BioSourcePtr src,
 
1104
  CharPtr directory,
 
1105
  CharPtr results,
 
1106
  CharPtr base,
 
1107
  CharPtr suffix,
 
1108
  Boolean fastaset,
 
1109
  CharPtr accn,
 
1110
  CharPtr organism,
 
1111
  SeqDescrPtr sdphead,
 
1112
  Boolean findorf,
 
1113
  Boolean altstart,
 
1114
  Boolean conflict,
 
1115
  Boolean validate,
 
1116
  Boolean flatfile
 
1117
)
 
1118
 
 
1119
{
 
1120
  BioseqPtr     bsp;
 
1121
  BioseqSetPtr  bssp;
 
1122
  Pointer       dataptr;
 
1123
  Uint2         datatype, entityID;
 
1124
  DatePtr       dp;
 
1125
  FILE          *fp;
 
1126
  SeqAnnotPtr   sap;
 
1127
  SeqDescrPtr   sdp;
 
1128
  SeqEntryPtr   sep;
 
1129
  SimpleSeqPtr  ssp;
 
1130
 
 
1131
  fp = OpenOneFile (directory, base, suffix);
 
1132
  if (fp == NULL) return;
 
1133
 
 
1134
  /* read one or more ASN.1 or FASTA sequence files */
 
1135
 
 
1136
  if (fastaset) {
 
1137
    entityID = ProcessAsnSet (fp, src, organism, findorf, altstart);
 
1138
  } else {
 
1139
    entityID = ProcessOneAsn (fp, src, organism, findorf, altstart);
 
1140
  }
 
1141
  FileClose (fp);
 
1142
 
 
1143
  if (entityID == 0) return;
 
1144
 
 
1145
  /* read one or more feature tables from .tbl file */
 
1146
 
 
1147
  fp = OpenOneFile (directory, base, ".tbl");
 
1148
  if (fp != NULL) {
 
1149
 
 
1150
    while ((dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE)) != NULL) {
 
1151
      if (datatype == OBJ_SEQANNOT) {
 
1152
 
 
1153
        sap = (SeqAnnotPtr) dataptr;
 
1154
        ProcessOneAnnot (sap, entityID, accn);
 
1155
 
 
1156
      } else {
 
1157
        ObjMgrFree (datatype, dataptr);
 
1158
      }
 
1159
    }
 
1160
    FileClose (fp);
 
1161
  }
 
1162
 
 
1163
  /* read one or more feature tables from .pep file */
 
1164
 
 
1165
  fp = OpenOneFile (directory, base, ".pep");
 
1166
  if (fp != NULL) {
 
1167
 
 
1168
    /* indexing needed to find CDS from protein product to set conflict flag */
 
1169
 
 
1170
    SeqMgrIndexFeatures (entityID, NULL);
 
1171
 
 
1172
    while ((dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, TRUE)) != NULL) {
 
1173
      if (datatype == OBJ_FASTA) {
 
1174
 
 
1175
        ssp = (SimpleSeqPtr) dataptr;
 
1176
        ReplaceOnePeptide (ssp, conflict);
 
1177
        SimpleSeqFree (ssp);
 
1178
 
 
1179
      } else {
 
1180
        ObjMgrFree (datatype, dataptr);
 
1181
      }
 
1182
    }
 
1183
    FileClose (fp);
 
1184
  }
 
1185
 
 
1186
  sep = GetTopSeqEntryForEntityID (entityID);
 
1187
  if (sep != NULL) {
 
1188
    if (sdphead != NULL) {
 
1189
      if (IS_Bioseq (sep)) {
 
1190
        bsp = (BioseqPtr) sep->data.ptrvalue;
 
1191
        ValNodeLink (&(bsp->descr),
 
1192
                     AsnIoMemCopy ((Pointer) sdphead,
 
1193
                                   (AsnReadFunc) SeqDescrAsnRead,
 
1194
                                   (AsnWriteFunc) SeqDescrAsnWrite));
 
1195
      } else if (IS_Bioseq_set (sep)) {
 
1196
        bssp = (BioseqSetPtr) sep->data.ptrvalue;
 
1197
        ValNodeLink (&(bssp->descr),
 
1198
                     AsnIoMemCopy ((Pointer) sdphead,
 
1199
                                   (AsnReadFunc) SeqDescrAsnRead,
 
1200
                                   (AsnWriteFunc) SeqDescrAsnWrite));
 
1201
      }
 
1202
    }
 
1203
    dp = DateCurr ();
 
1204
    if (dp != NULL) {
 
1205
      sdp = CreateNewDescriptor (sep, Seq_descr_create_date);
 
1206
      if (sdp != NULL) {
 
1207
        sdp->data.ptrvalue = (Pointer) dp;
 
1208
      }
 
1209
    }
 
1210
    SeriousSeqEntryCleanup (sep, NULL, NULL);
 
1211
    WriteOneFile (results, base, ".sqn", sep, sbp);
 
1212
 
 
1213
    if (validate || flatfile) {
 
1214
      if (pdp != NULL) {
 
1215
 
 
1216
        /* copy in citsub as publication for validator and flatfile */
 
1217
 
 
1218
        sdp = CreateNewDescriptor (sep, Seq_descr_pub);
 
1219
        if (sdp != NULL) {
 
1220
          sdp->data.ptrvalue = AsnIoMemCopy ((Pointer) pdp,
 
1221
                                             (AsnReadFunc) PubdescAsnRead,
 
1222
                                             (AsnWriteFunc) PubdescAsnWrite);
 
1223
        }
 
1224
      }
 
1225
      SeqMgrIndexFeatures (entityID, 0);
 
1226
    }
 
1227
    if (validate) {
 
1228
      Message (MSG_POST, "Validating %s\n", base);
 
1229
      ValidateOneFile (results, base, ".val", sep);
 
1230
    }
 
1231
    if (flatfile) {
 
1232
      Message (MSG_POST, "Flatfile %s\n", base);
 
1233
      sep = FindNucSeqEntry (sep);
 
1234
      FlatfileOneFile (results, base, ".gbf", sep);
 
1235
    }
 
1236
  }
 
1237
 
 
1238
  ObjMgrFreeByEntityID (entityID);
 
1239
}
 
1240
 
 
1241
static void GetFirstBiop (
 
1242
  BioSourcePtr biop,
 
1243
  Pointer userdata
 
1244
)
 
1245
 
 
1246
{
 
1247
  BioSourcePtr PNTR biopp;
 
1248
 
 
1249
  biopp = (BioSourcePtr PNTR) userdata;
 
1250
  if (biop == NULL || biopp == NULL) return;
 
1251
  if (*biopp != NULL) return;
 
1252
  *biopp = biop;
 
1253
}
 
1254
 
 
1255
static CharPtr overwriteMsg = "Your template with a .sqn suffix will be overwritten.  Do you wish to continue?";
 
1256
 
 
1257
static Boolean TemplateOverwriteRisk (
 
1258
  CharPtr tmplate,
 
1259
  CharPtr single,
 
1260
  CharPtr directory,
 
1261
  CharPtr suffix
 
1262
)
 
1263
 
 
1264
{
 
1265
  Char     file [FILENAME_MAX], path [PATH_MAX];
 
1266
  CharPtr  ptr;
 
1267
 
 
1268
 
 
1269
  if (StringStr (tmplate, ".sqn") == NULL) return FALSE;
 
1270
  if (! StringHasNoText (single)) {
 
1271
    StringNCpy_0 (file, tmplate, sizeof (file));
 
1272
    ptr = StringStr (file, ".");
 
1273
    if (ptr != NULL) {
 
1274
      *ptr = '\0';
 
1275
    }
 
1276
    ptr = StringStr (single, ".");
 
1277
    if (ptr != NULL) {
 
1278
      StringCat (file, ptr);
 
1279
    }
 
1280
    if (StringCmp (file, single) == 0) return TRUE;
 
1281
  } else if (! StringHasNoText (directory)) {
 
1282
    StringNCpy_0 (path, directory, sizeof (path));
 
1283
    StringNCpy_0 (file, tmplate, sizeof (file));
 
1284
    ptr = StringStr (file, ".");
 
1285
    if (ptr != NULL) {
 
1286
      *ptr = '\0';
 
1287
    }
 
1288
    StringCat (file, suffix);
 
1289
    FileBuildPath (path, NULL, file);
 
1290
    if (FileLength (path) > 0) return TRUE;
 
1291
  }
 
1292
  return FALSE;
 
1293
}
 
1294
 
 
1295
/* Args structure contains command-line arguments */
 
1296
 
 
1297
#define p_argInputPath  0
 
1298
#define r_argOutputPath 1
 
1299
#define f_argSingleFile 2
 
1300
#define x_argSuffix     3
 
1301
#define t_argTemplate   4
 
1302
#define s_argFastaSet   5
 
1303
#define a_argAccession  6
 
1304
#define n_argOrgName    7
 
1305
#define c_argFindOrf    8
 
1306
#define m_argAltStart   9
 
1307
#define k_argConflict  10
 
1308
#define v_argValidate  11
 
1309
#define b_argGenBank   12
 
1310
 
 
1311
Args myargs [] = {
 
1312
  {"Path to files", NULL, NULL, NULL,
 
1313
    TRUE, 'p', ARG_STRING, 0.0, 0, NULL},
 
1314
  {"Path for results", NULL, NULL, NULL,
 
1315
    TRUE, 'r', ARG_STRING, 0.0, 0, NULL},
 
1316
  {"Only this file", NULL, NULL, NULL,
 
1317
    TRUE, 'f', ARG_FILE_IN, 0.0, 0, NULL},
 
1318
  {"Suffix", ".fsa", NULL, NULL,
 
1319
    TRUE, 'x', ARG_STRING, 0.0, 0, NULL},
 
1320
  {"Template file", NULL, NULL, NULL,
 
1321
    TRUE, 't', ARG_FILE_IN, 0.0, 0, NULL},
 
1322
  {"Read Set of FASTAs", "F", NULL, NULL,
 
1323
    TRUE, 's', ARG_BOOLEAN, 0.0, 0, NULL},
 
1324
  {"Accession", NULL, NULL, NULL,
 
1325
    TRUE, 'a', ARG_STRING, 0.0, 0, NULL},
 
1326
  {"Organism name", NULL, NULL, NULL,
 
1327
    TRUE, 'n', ARG_STRING, 0.0, 0, NULL},
 
1328
  {"Annotate longest ORF", "F", NULL, NULL,
 
1329
    TRUE, 'c', ARG_BOOLEAN, 0.0, 0, NULL},
 
1330
  {"Allow alternative starts", "F", NULL, NULL,
 
1331
    TRUE, 'm', ARG_BOOLEAN, 0.0, 0, NULL},
 
1332
  {"Set conflict on mismatch", "F", NULL, NULL,
 
1333
    TRUE, 'k', ARG_BOOLEAN, 0.0, 0, NULL},
 
1334
  {"Validate", "F", NULL, NULL,
 
1335
    TRUE, 'v', ARG_BOOLEAN, 0.0, 0, NULL},
 
1336
  {"Generate GenBank file", "F", NULL, NULL,
 
1337
    TRUE, 'b', ARG_BOOLEAN, 0.0, 0, NULL},
 
1338
};
 
1339
 
 
1340
Int2 Main (void)
 
1341
 
 
1342
{
 
1343
  Boolean         altstart, conflict, fastaset, findorf, flatfile, validate;
 
1344
  CharPtr         base, directory, results, suffix, accn, organism, ptr, tmplate;
 
1345
  Pointer         dataptr;
 
1346
  Uint2           datatype;
 
1347
  CitSubPtr       csp;
 
1348
  FILE            *fp;
 
1349
  ValNodePtr      head, vnp;
 
1350
  Pubdesc         pd;
 
1351
  PubdescPtr      pdp = NULL;
 
1352
  ValNode         pb;
 
1353
  SubmitBlockPtr  sbp = NULL;
 
1354
  SeqDescrPtr     sdphead = NULL;
 
1355
  SeqEntryPtr     sep;
 
1356
  Char            sfx [32];
 
1357
  BioSourcePtr    src = NULL;
 
1358
  SeqSubmitPtr    ssp = NULL;
 
1359
 
 
1360
  /* standard setup */
 
1361
 
 
1362
  ErrSetFatalLevel (SEV_MAX);
 
1363
  ErrClearOptFlags (EO_SHOW_USERSTR);
 
1364
  UseLocalAsnloadDataAndErrMsg ();
 
1365
  ErrPathReset ();
 
1366
 
 
1367
  /* finish resolving internal connections in ASN.1 parse tables */
 
1368
 
 
1369
  if (! AllObjLoad ()) {
 
1370
    Message (MSG_FATAL, "AllObjLoad failed");
 
1371
    return 1;
 
1372
  }
 
1373
  if (! SubmitAsnLoad ()) {
 
1374
    Message (MSG_FATAL, "SubmitAsnLoad failed");
 
1375
    return 1;
 
1376
  }
 
1377
  if (! FeatDefSetLoad ()) {
 
1378
    Message (MSG_FATAL, "FeatDefSetLoad failed");
 
1379
    return 1;
 
1380
  }
 
1381
  if (! SeqCodeSetLoad ()) {
 
1382
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
 
1383
    return 1;
 
1384
  }
 
1385
  if (! GeneticCodeTableLoad ()) {
 
1386
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
 
1387
    return 1;
 
1388
  }
 
1389
 
 
1390
  /* process command line arguments */
 
1391
 
 
1392
  if (! GetArgs ("tbl2asn", sizeof (myargs) / sizeof (Args), myargs)) {
 
1393
    return 0;
 
1394
  }
 
1395
 
 
1396
  directory = (CharPtr) myargs [p_argInputPath].strvalue;
 
1397
  results = (CharPtr) myargs [r_argOutputPath].strvalue;
 
1398
  if (StringHasNoText (results)) {
 
1399
    results = directory;
 
1400
  }
 
1401
  suffix = (CharPtr) myargs [x_argSuffix].strvalue;
 
1402
  base = (CharPtr) myargs [f_argSingleFile].strvalue;
 
1403
  tmplate = (CharPtr) myargs [t_argTemplate].strvalue;
 
1404
  fastaset = (Boolean) myargs [s_argFastaSet].intvalue;
 
1405
  accn = (CharPtr) myargs [a_argAccession].strvalue;
 
1406
  organism = (CharPtr) myargs [n_argOrgName].strvalue;
 
1407
  findorf = (Boolean) myargs [c_argFindOrf].intvalue;
 
1408
  altstart = (Boolean) myargs [m_argAltStart].intvalue;
 
1409
  conflict = (Boolean) myargs [k_argConflict].intvalue;
 
1410
  validate = (Boolean) myargs [v_argValidate].intvalue;
 
1411
  flatfile = (Boolean) myargs [b_argGenBank].intvalue;
 
1412
 
 
1413
  if (StringHasNoText (base) && (! StringHasNoText (accn))) {
 
1414
    Message (MSG_FATAL, "Accession can be entered only for a single record");
 
1415
    return 1;
 
1416
  }
 
1417
 
 
1418
  /* Seq-submit or Submit-block template is optional */
 
1419
 
 
1420
  if (! StringHasNoText (tmplate)) {
 
1421
    if (TemplateOverwriteRisk (tmplate, base, directory, suffix)) {
 
1422
      if (Message (MSG_YN, overwriteMsg) == ANS_NO) return 0;
 
1423
    }
 
1424
    fp = FileOpen (tmplate, "r");
 
1425
    if (fp != NULL) {
 
1426
      while ((dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE)) != NULL) {
 
1427
        if (datatype == OBJ_SEQSUB) {
 
1428
          ssp = (SeqSubmitPtr) dataptr;
 
1429
        } else if (datatype == OBJ_SUBMIT_BLOCK) {
 
1430
          sbp = (SubmitBlockPtr) dataptr;
 
1431
        } else if (datatype == OBJ_SEQDESC) {
 
1432
          ValNodeLink (&sdphead, (SeqDescrPtr) dataptr);
 
1433
        } else {
 
1434
          ObjMgrFree (datatype, dataptr);
 
1435
        }
 
1436
      }
 
1437
      FileClose (fp);
 
1438
    }
 
1439
 
 
1440
    if (ssp != NULL && sbp == NULL) {
 
1441
      sbp = ssp->sub;
 
1442
    }
 
1443
    if (sbp == NULL) {
 
1444
      Message (MSG_FATAL, "Unable to read required template file");
 
1445
      return 1;
 
1446
    }
 
1447
 
 
1448
    if (sbp != NULL) {
 
1449
      if (ssp != NULL) {
 
1450
 
 
1451
        /* copy submit block, will free SeqSubmit before processing */
 
1452
 
 
1453
        sbp = AsnIoMemCopy ((Pointer) sbp,
 
1454
                            (AsnReadFunc) SubmitBlockAsnRead,
 
1455
                            (AsnWriteFunc) SubmitBlockAsnWrite);
 
1456
      }
 
1457
      sbp->tool = MemFree (sbp->tool);
 
1458
      sbp->tool = StringSave ("tbl2asn");
 
1459
      sbp->hup = FALSE;
 
1460
      sbp->reldate = DateFree (sbp->reldate);
 
1461
      csp = sbp->cit;
 
1462
      if (csp != NULL) {
 
1463
        csp->date = DateFree (csp->date);
 
1464
        csp->date = DateCurr ();
 
1465
        MemSet ((Pointer) &pd, 0, sizeof (Pubdesc));
 
1466
        MemSet ((Pointer) &pb, 0, sizeof (ValNode));
 
1467
        pb.choice = PUB_Sub;
 
1468
        pb.data.ptrvalue = (Pointer) csp;
 
1469
        pd.pub = &pb;
 
1470
        pdp = &pd;
 
1471
      }
 
1472
    }
 
1473
    if (ssp != NULL && ssp->datatype == 1) {
 
1474
      sep = (SeqEntryPtr) ssp->data;
 
1475
      if (sep != NULL) {
 
1476
        VisitBioSourcesInSep (sep, (Pointer) &src, GetFirstBiop);
 
1477
        if (src != NULL) {
 
1478
 
 
1479
          /* copy top biosource */
 
1480
 
 
1481
          src = AsnIoMemCopy ((Pointer) src,
 
1482
                              (AsnReadFunc) BioSourceAsnRead,
 
1483
                              (AsnWriteFunc) BioSourceAsnWrite);
 
1484
        }
 
1485
      }
 
1486
 
 
1487
      /* in case template has colliding ID, free it now */
 
1488
 
 
1489
      SeqSubmitFree (ssp);
 
1490
    }
 
1491
  }
 
1492
 
 
1493
  /* process one or more records */
 
1494
 
 
1495
  if (! StringHasNoText (base)) {
 
1496
    ptr = StringStr (base, ".");
 
1497
    if (ptr != NULL) {
 
1498
      StringNCpy_0 (sfx, ptr, sizeof (sfx));
 
1499
      *ptr = '\0';
 
1500
    }
 
1501
    ProcessOneRecord (sbp, pdp, src, directory, results, base, sfx, fastaset, accn,
 
1502
                      organism, sdphead, findorf, altstart, conflict, validate, flatfile);
 
1503
  } else {
 
1504
 
 
1505
    /* get list of all files in source directory */
 
1506
 
 
1507
    head = DirCatalog (directory);
 
1508
 
 
1509
    for (vnp = head; vnp != NULL; vnp = vnp->next) {
 
1510
      if (vnp->choice == 0) {
 
1511
        base = (CharPtr) vnp->data.ptrvalue;
 
1512
        if (! StringHasNoText (base)) {
 
1513
          ptr = StringStr (base, suffix);
 
1514
          if (ptr != NULL) {
 
1515
            *ptr = '\0';
 
1516
            Message (MSG_POST, "Processing %s\n", base);
 
1517
            ProcessOneRecord (sbp, pdp, src, directory, results, base, suffix, fastaset, NULL,
 
1518
                              organism, sdphead, findorf, altstart, conflict, validate, flatfile);
 
1519
          }
 
1520
        }
 
1521
      }
 
1522
    }
 
1523
 
 
1524
    /* clean up file list */
 
1525
 
 
1526
    ValNodeFreeData (head);
 
1527
  }
 
1528
 
 
1529
  if (sbp != NULL) {
 
1530
    SubmitBlockFree (sbp);
 
1531
  }
 
1532
  if (src != NULL) {
 
1533
    BioSourceFree (src);
 
1534
  }
 
1535
 
 
1536
  SeqDescrFree (sdphead);
 
1537
 
 
1538
  TransTableFreeAll ();
 
1539
 
 
1540
  return 0;
 
1541
}
 
1542