~ubuntu-branches/ubuntu/precise/ncbi-tools6/precise

« back to all changes in this revision

Viewing changes to api/lsqfetch.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
* Date     Name        Description of modification
38
38
*
39
39
* $Log: lsqfetch.c,v $
40
 
* Revision 6.11  2001/09/21 20:02:04  kans
41
 
* allow U and u to be DNA in CheckDnaResidue for the BLAST guys, even though it is amino acid Selenocysteine in most places
42
 
*
43
 
* Revision 6.10  2001/03/13 16:48:58  kans
44
 
* fixes to saving path, binary search results
45
 
*
46
 
* Revision 6.9  2001/03/12 23:19:33  kans
47
 
* added IndexedFastaLib functions - currently uses genome contig naming conventions
48
 
*
49
 
* Revision 6.8  2001/01/09 00:12:39  kans
50
 
* now handles SEQID_GI
51
 
*
52
 
* Revision 6.7  1999/10/07 16:21:13  kans
53
 
* removed static AddSeqId and SeqIdDupList which were identical to public sequtil functions
54
 
*
55
 
* Revision 6.6  1999/07/20 21:18:39  sicotte
56
 
* add static AddSeqId for linker conflicts
57
 
*
58
 
* Revision 6.5  1999/07/20 21:16:49  sicotte
59
 
* add static SeqIdDupList for linker conflicts
60
 
*
61
 
* Revision 6.4  1999/04/01 22:26:13  sicotte
62
 
* Make lsqfetch Attempt to Parse the fasta defline, otherwise use the supplied SeqId
63
 
*
64
 
* Revision 6.3  1999/03/11 23:39:33  kans
65
 
* sprintf and sscanf casts
66
 
*
67
 
* Revision 6.2  1998/02/06 17:41:33  zjing
68
 
* make the function CheckDnaResidue external
69
 
*
70
 
* Revision 6.0  1997/08/25 18:06:28  madden
71
 
* Revision changed to 6.0
72
 
*
73
 
* $Log: lsqfetch.c,v $
 
40
* Revision 6.28  2004/10/05 19:11:23  kans
 
41
* separate internal CreateBinaryAsnIndex and CreateTextAsnIndex functions
 
42
*
 
43
* Revision 6.27  2004/10/05 18:56:48  kans
 
44
* AsnIndexedLibFetchEnable only works in text mode, backed out SeqEntryAsnRead change - will later implement use of catenated Seq-entry instead of se2bss processed input if text
 
45
*
 
46
* Revision 6.26  2004/10/05 17:34:16  kans
 
47
* protect all binary search functions against R out of range
 
48
*
 
49
* Revision 6.25  2004/10/05 17:25:04  kans
 
50
* AsnIndexedLibBioseqFetchFunc handles all Seq-id types, passes atp_se to SeqEntryAsnRead
 
51
*
 
52
* Revision 6.24  2004/10/05 16:21:37  kans
 
53
* LocalSeqFetchInit checks for INDEXED_TEXT_ASN and INDEXED_BIN_ASN, calls AsnIndexedLibFetchEnable
 
54
*
 
55
* Revision 6.23  2004/08/04 20:21:04  kans
 
56
* record alfp->binary at correct place in asn indexed fetch enable function
 
57
*
 
58
* Revision 6.22  2004/08/03 17:51:49  kans
 
59
* added AsnIndexedLibFetch enable and disable functions
 
60
*
 
61
* Revision 6.21  2004/08/02 19:10:14  kans
 
62
* added CreateAsnIndex for indexing Bioseq-set ftp release files
 
63
*
 
64
* Revision 6.20  2004/04/13 16:58:32  kans
 
65
* allow alt index to have gi numbers, also test .fsa if .fa fails
 
66
*
 
67
* Revision 6.19  2003/11/13 17:18:02  kans
 
68
* added SearchAltIndex, finished Alt fetch for chimp revision
 
69
*
 
70
* Revision 6.18  2003/11/12 23:49:11  kans
 
71
* SortIfpByID needed LIBCALLBACK for PC
 
72
*
 
73
* Revision 6.17  2003/11/12 23:38:48  kans
 
74
* changing AltIndexedFastaLibFetchEnable prototype, implementation not yet finished
 
75
*
 
76
* Revision 6.16  2003/08/27 21:24:05  kans
 
77
* enable alt indexed fasta looks up previously registered function, changes settings for new path
 
78
*
 
79
* Revision 6.15  2003/08/27 19:27:43  kans
 
80
* added AltIndexedFastaLibFetch functions for chimpanzee genome project
 
81
*
 
82
* Revision 6.14  2002/11/13 23:07:37  johnson
 
83
* Changed make_lib such that it looks to see if it matches the *whole* seq-id
 
84
* (defined by the next character being non-alphanumeric).
 
85
*
 
86
* Revision 6.13  2002/07/19 20:16:33  johnson
 
87
* bug fix in make_lib -- wasn't properly handling sequences >=1000 residues
 
88
*
 
89
* Revision 6.12  2002/01/22 19:50:32  kans
 
90
* IndexedFastaLibBioseqFetchFunc looks for prefix with upper case followed by lower case
 
91
*
74
92
* Revision 6.11  2001/09/21 20:02:04  kans
75
93
* allow U and u to be DNA in CheckDnaResidue for the BLAST guys, even though it is amino acid Selenocysteine in most places
76
94
*
249
267
                                i = 1;
250
268
                                while(IS_WHITESP(temp[i]))
251
269
                                        ++i;
252
 
                                if(StringNCmp(temp+i, name, (size_t)n_len) ==0) {
 
270
                                if(StringNCmp(temp+i, name, (size_t)n_len) ==0 && !isalnum(temp[i+n_len])) {
253
271
                                    is_found = TRUE;
254
272
                                    if(sip)
255
273
                                        *sip = SeqIdParse(temp+i);
283
301
                                if(temp[0] == '>')
284
302
                                        is_end = TRUE;
285
303
                                else
286
 
                                        seq_len += (StringLen(temp) -1);
 
304
                                        seq_len += StringLen(temp);
287
305
                        }
 
306
                        if (seq_len > 0) /* don't count \n */
 
307
                            --seq_len;
288
308
                }
289
309
        }
290
310
 
309
329
                *is_DNA = CheckDnaResidue(temp, StringLen(temp), NULL);
310
330
                check_DNA = TRUE;
311
331
            }
312
 
            for(p=temp; p!=NULL && *p != '\n'; ++p)
 
332
            for(p=temp; *p!='\0' && *p != '\n'; ++p)
313
333
                if(IS_ALPHA(*p) || *p == '-' || *p == '*')
314
334
                {
315
335
                        ++seq_len;
923
943
*       Reads TYPE, PATH and EXT fields from the LSQFETCH configuration file 
924
944
*       TYPE is TEXT_ASN, BIN_ASN, FASTA_FILE, or FASTA_LIB.
925
945
*
 
946
*   Now also looks for INDEXED_BIN_ASN for easy
 
947
*   access to AsnIndexedLib fetch mechanism.
 
948
*
926
949
*********************************************************************/
927
950
                
928
951
static void ReadFetchConfigFile (CharPtr sect, ValNodePtr PNTR libs, ValNodePtr PNTR path, ValNodePtr PNTR ext)
933
956
 
934
957
        if (GetAppParam ("LSQFETCH", sect, "PATH", "", str, sizeof (str) - 1)) {
935
958
                if (GetAppParam ("LSQFETCH", sect, "TYPE", "", temp, sizeof (temp) - 1)) {
 
959
 
 
960
                        /* first check for indexed asn tags */
 
961
 
 
962
                        if (StringICmp (temp, "INDEXED_BIN_ASN") == 0) {
 
963
                                AsnIndexedLibFetchEnable (str, TRUE);
 
964
                                return;
 
965
                        }
 
966
                        if (StringICmp (temp, "INDEXED_TEXT_ASN") == 0) {
 
967
                                AsnIndexedLibFetchEnable (str, FALSE);
 
968
                                return;
 
969
                        }
 
970
 
 
971
                        /* now look for regular lsqfetch files */
 
972
 
936
973
                        load_seq_info (temp, str, libs, path, ext);
937
974
                        if (GetAppParam ("LSQFETCH", sect, "EXT", "", str, sizeof (str) - 1)) {
938
975
                                load_seq_info ("EXT", str, libs, path, ext);
1076
1113
    }
1077
1114
  }
1078
1115
 
1079
 
  if (StringICmp (fip->seqids [R], seqid) == 0) {
1080
 
    if (fip->offsets [R] != NULL &&
1081
 
        sscanf (fip->offsets [R], "%ld", &val) == 1) {
1082
 
      return (Int4) val;
 
1116
  if (R >= 0 && R < fip->numlines) {
 
1117
    if (StringICmp (fip->seqids [R], seqid) == 0) {
 
1118
      if (fip->offsets [R] != NULL &&
 
1119
          sscanf (fip->offsets [R], "%ld", &val) == 1) {
 
1120
        return (Int4) val;
 
1121
      }
1083
1122
    }
1084
1123
  }
1085
1124
 
1218
1257
  return fip;
1219
1258
}
1220
1259
 
1221
 
/* object manager registerable fetch function */
 
1260
/* human genome object manager registerable fetch function */
1222
1261
 
1223
1262
static CharPtr fastalibfetchproc = "IndexedFastaLibBioseqFetch";
1224
1263
 
1225
1264
typedef struct flibftch {
1226
1265
  CharPtr        path;
 
1266
  CharPtr        fastaname;
1227
1267
  FastaIndexPtr  currentfip;
1228
1268
} FastaLibFetchData, PNTR FastaLibFetchPtr;
1229
1269
 
1255
1295
  if (sip->choice == SEQID_LOCAL) {
1256
1296
 
1257
1297
    SeqIdWrite (sip, id, PRINTID_REPORT, sizeof (id));
1258
 
    if (StringNICmp (id, "Hs", 2) == 0) {
 
1298
    if (StringLen (id) > 2 && IS_UPPER (id [0]) && IS_LOWER (id [1])) {
1259
1299
      StringCpy (tmp, id + 2);
1260
1300
      ptr = StringChr (tmp, '_');
1261
1301
      if (ptr != NULL) {
1330
1370
  flfp = (FastaLibFetchPtr) ompp->procdata;
1331
1371
  if (flfp == NULL) return;
1332
1372
  MemFree (flfp->path);
 
1373
  /* MemFree (flfp->fastaname); */
1333
1374
  FreeFastaIndex (flfp->currentfip);
1334
1375
  MemFree (flfp);
1335
1376
}
1336
1377
 
 
1378
/* chimpanzee genome object manager registerable fetch function */
 
1379
 
 
1380
static CharPtr altfastalibfetchproc = "AltIndexedFastaLibBioseqFetch";
 
1381
 
 
1382
typedef struct idfip {
 
1383
  CharPtr        seqid;
 
1384
  FastaIndexPtr  fip;
 
1385
} IdFip, PNTR IdFipPtr;
 
1386
 
 
1387
typedef struct alibftch {
 
1388
  CharPtr     path;
 
1389
  ValNodePtr  fiplist;
 
1390
  IdFipPtr    index;
 
1391
  Int4        numids;
 
1392
} AltLibFetchData, PNTR AltLibFetchPtr;
 
1393
 
 
1394
static void ChangeLocalToGenbank (BioseqPtr bsp, Pointer userdata)
 
1395
 
 
1396
{
 
1397
  Char      id [41], tmp [41];
 
1398
  SeqIdPtr  sip;
 
1399
 
 
1400
  for (sip = bsp->id; sip != NULL && sip->choice != SEQID_LOCAL; sip = sip->next) continue;
 
1401
  if (sip == NULL) return;
 
1402
  SeqIdWrite (sip, id, PRINTID_REPORT, sizeof (id));
 
1403
  sprintf (tmp, "gb|%s", id);
 
1404
  sip = SeqIdParse (tmp);
 
1405
  bsp->id = SeqIdSetFree (bsp->id);
 
1406
  bsp->id = sip;
 
1407
  SeqMgrReplaceInBioseqIndex (bsp);
 
1408
}
 
1409
 
 
1410
static FastaIndexPtr SearchAltIndex (
 
1411
  AltLibFetchPtr alfp,
 
1412
  CharPtr seqid
 
1413
)
 
1414
 
 
1415
{
 
1416
  int       compare;
 
1417
  IdFipPtr  ifp;
 
1418
  Int4      L, R, mid;
 
1419
 
 
1420
  if (alfp == NULL || alfp->index == NULL) return NULL;
 
1421
  ifp = alfp->index;
 
1422
  if (StringHasNoText (seqid)) return NULL;
 
1423
 
 
1424
  L = 0;
 
1425
  R = alfp->numids - 1;
 
1426
  while (L < R) {
 
1427
    mid = (L + R) / 2;
 
1428
    compare = StringICmp (ifp [mid].seqid, seqid);
 
1429
    if (compare < 0) {
 
1430
      L = mid + 1;
 
1431
    } else {
 
1432
      R = mid;
 
1433
    }
 
1434
  }
 
1435
 
 
1436
  if (R >= 0 && R < alfp->numids) {
 
1437
    if (StringICmp (ifp [R].seqid, seqid) == 0) {
 
1438
      return ifp [R].fip;
 
1439
    }
 
1440
  }
 
1441
 
 
1442
  return NULL;
 
1443
}
 
1444
 
 
1445
static Int2 LIBCALLBACK AltIndexedFastaLibBioseqFetchFunc (Pointer data)
 
1446
 
 
1447
{
 
1448
  AltLibFetchPtr    alfp;
 
1449
  BioseqPtr         bsp;
 
1450
  Pointer           dataptr = NULL;
 
1451
  Uint2             datatype, entityID = 0;
 
1452
  Char              file [FILENAME_MAX], path [PATH_MAX], id [41];
 
1453
  FastaIndexPtr     fip;
 
1454
  FILE              *fp;
 
1455
  Int4              offset;
 
1456
  OMProcControlPtr  ompcp;
 
1457
  ObjMgrProcPtr     ompp;
 
1458
  SeqEntryPtr       sep = NULL;
 
1459
  SeqIdPtr          sip;
 
1460
  CharPtr           tmp;
 
1461
 
 
1462
  ompcp = (OMProcControlPtr) data;
 
1463
  if (ompcp == NULL) return OM_MSG_RET_ERROR;
 
1464
  ompp = ompcp->proc;
 
1465
  if (ompp == NULL) return OM_MSG_RET_ERROR;
 
1466
  alfp = (AltLibFetchPtr) ompp->procdata;
 
1467
  if (alfp == NULL) return OM_MSG_RET_ERROR;
 
1468
  sip = (SeqIdPtr) ompcp->input_data;
 
1469
  if (sip == NULL) return OM_MSG_RET_ERROR;
 
1470
 
 
1471
  if (sip->choice == SEQID_GENBANK || sip->choice == SEQID_GI) {
 
1472
 
 
1473
    SeqIdWrite (sip, id, PRINTID_REPORT, sizeof (id));
 
1474
    fip = SearchAltIndex (alfp, id);
 
1475
    if (fip != NULL) {
 
1476
      offset = SearchFastaIndex (fip, id);
 
1477
      if (offset < 0) return OM_MSG_RET_ERROR;
 
1478
      StringCpy (file, fip->file);
 
1479
      tmp = StringStr (file, ".idx");
 
1480
      if (tmp != NULL) {
 
1481
        *tmp = '\0';
 
1482
      }
 
1483
      StringCat (file, ".fa");
 
1484
      StringNCpy_0 (path, fip->path, sizeof (path));
 
1485
      FileBuildPath (path, NULL, file);
 
1486
      fp = FileOpen (path, "r");
 
1487
      if (fp == NULL) {
 
1488
        tmp = StringStr (file, ".fa");
 
1489
        if (tmp != NULL) {
 
1490
          *tmp = '\0';
 
1491
          StringCat (file, ".fsa");
 
1492
          StringNCpy_0 (path, fip->path, sizeof (path));
 
1493
          FileBuildPath (path, NULL, file);
 
1494
          fp = FileOpen (path, "r");
 
1495
        }
 
1496
      }
 
1497
      if (fp == NULL) return OM_MSG_RET_ERROR;
 
1498
      fseek (fp, offset, SEEK_SET);
 
1499
      dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID,
 
1500
                                        FALSE, FALSE, TRUE, FALSE);
 
1501
      if (dataptr != NULL) {
 
1502
        sep = GetTopSeqEntryForEntityID (entityID);
 
1503
      }
 
1504
      FileClose (fp);
 
1505
    }
 
1506
  }
 
1507
 
 
1508
  if (sep == NULL) return OM_MSG_RET_ERROR;
 
1509
  VisitBioseqsInSep (sep, NULL, ChangeLocalToGenbank);
 
1510
  bsp = BioseqFindInSeqEntry (sip, sep);
 
1511
  ompcp->output_data = (Pointer) bsp;
 
1512
  ompcp->output_entityID = ObjMgrGetEntityIDForChoice (sep);
 
1513
  return OM_MSG_RET_DONE;
 
1514
}
 
1515
 
 
1516
static int LIBCALLBACK SortIfpByID (VoidPtr vp1, VoidPtr vp2)
 
1517
 
 
1518
{
 
1519
  IdFipPtr ifp1, ifp2;
 
1520
 
 
1521
  if (vp1 == NULL || vp2 == NULL) return 0;
 
1522
  ifp1 = (IdFipPtr) vp1;
 
1523
  ifp2 = (IdFipPtr) vp2;
 
1524
  if (ifp1 == NULL || ifp2 == NULL) return 0;
 
1525
  return StringICmp (ifp1->seqid, ifp2->seqid);
 
1526
}
 
1527
 
 
1528
NLM_EXTERN Boolean AltIndexedFastaLibFetchEnable (CharPtr path)
 
1529
 
 
1530
{
 
1531
  AltLibFetchPtr  alfp = NULL;
 
1532
  Char            file [FILENAME_MAX];
 
1533
  FastaIndexPtr   fip;
 
1534
  ValNodePtr      head;
 
1535
  Int4            i;
 
1536
  IdFipPtr        ifp;
 
1537
  Boolean         is_new = FALSE;
 
1538
  Int4            j;
 
1539
  Int4            numids = 0;
 
1540
  ObjMgrPtr       omp;
 
1541
  ObjMgrProcPtr   ompp;
 
1542
  Char            str [PATH_MAX];
 
1543
  CharPtr         tmp;
 
1544
  ValNodePtr      vnp;
 
1545
 
 
1546
  StringNCpy_0 (str, path, sizeof (str));
 
1547
  TrimSpacesAroundString (str);
 
1548
  omp = ObjMgrGet ();
 
1549
  ompp = ObjMgrProcFind (omp, 0, altfastalibfetchproc, OMPROC_FETCH);
 
1550
  if (ompp != NULL) {
 
1551
    alfp = (AltLibFetchPtr) ompp->procdata;
 
1552
    if (alfp != NULL) {
 
1553
      alfp->path = MemFree (alfp->path);
 
1554
      for (vnp = alfp->fiplist; vnp != NULL; vnp = vnp->next) {
 
1555
        fip = (FastaIndexPtr) vnp->data.ptrvalue;
 
1556
        FreeFastaIndex (fip);
 
1557
      }
 
1558
      alfp->fiplist = ValNodeFree (alfp->fiplist);
 
1559
      alfp->index = MemFree (alfp->index);
 
1560
    }
 
1561
  } else {
 
1562
    alfp = (AltLibFetchPtr) MemNew (sizeof (AltLibFetchData));
 
1563
    is_new = TRUE;
 
1564
  }
 
1565
  if (alfp != NULL) {
 
1566
    alfp->path = StringSave (str);
 
1567
    head = DirCatalog (str);
 
1568
    for (vnp = head; vnp != NULL; vnp = vnp->next) {
 
1569
      if (vnp->choice == 0) {
 
1570
        tmp = (CharPtr) vnp->data.ptrvalue;
 
1571
        if (StringStr (tmp, ".idx") != NULL) {
 
1572
          StringCpy (str, alfp->path);
 
1573
          sprintf (file, "%s", tmp);
 
1574
          FileBuildPath (str, NULL, file);
 
1575
          fip = ReadFastaIndex (str);
 
1576
          if (fip != NULL) {
 
1577
            ValNodeAddPointer (&(alfp->fiplist), 0, (Pointer) fip);
 
1578
            numids += fip->numlines;
 
1579
          }
 
1580
        }
 
1581
      }
 
1582
    }
 
1583
    ValNodeFreeData (head);
 
1584
    ifp = (IdFipPtr) MemNew (sizeof (IdFip) * (numids + 2));
 
1585
    alfp->index = ifp;
 
1586
    alfp->numids = numids;
 
1587
    if (ifp != NULL) {
 
1588
      i = 0;
 
1589
      for (vnp = alfp->fiplist; vnp != NULL; vnp = vnp->next) {
 
1590
        fip = (FastaIndexPtr) vnp->data.ptrvalue;
 
1591
        if (fip != NULL) {
 
1592
          for (j = 0; j < fip->numlines; j++, i++) {
 
1593
            ifp [i].seqid = fip->seqids [j];
 
1594
            ifp [i].fip = fip;
 
1595
          }
 
1596
        }
 
1597
      }
 
1598
      HeapSort (ifp, (size_t) numids, sizeof (IdFip), SortIfpByID);
 
1599
    }
 
1600
  }
 
1601
  if (is_new) {
 
1602
    ObjMgrProcLoad (OMPROC_FETCH, altfastalibfetchproc, altfastalibfetchproc,
 
1603
                    OBJ_SEQID, 0, OBJ_BIOSEQ, 0, (Pointer) alfp,
 
1604
                    AltIndexedFastaLibBioseqFetchFunc, PROC_PRIORITY_DEFAULT);
 
1605
  }
 
1606
  return TRUE;
 
1607
}
 
1608
 
 
1609
NLM_EXTERN void AltIndexedFastaLibFetchDisable (void)
 
1610
 
 
1611
{
 
1612
  AltLibFetchPtr  alfp;
 
1613
  FastaIndexPtr   fip;
 
1614
  ObjMgrPtr       omp;
 
1615
  ObjMgrProcPtr   ompp;
 
1616
  ValNodePtr      vnp;
 
1617
 
 
1618
  omp = ObjMgrGet ();
 
1619
  ompp = ObjMgrProcFind (omp, 0, altfastalibfetchproc, OMPROC_FETCH);
 
1620
  if (ompp == NULL) return;
 
1621
  ObjMgrFreeUserData (0, ompp->procid, OMPROC_FETCH, 0);
 
1622
  alfp = (AltLibFetchPtr) ompp->procdata;
 
1623
  if (alfp == NULL) return;
 
1624
  alfp->path = MemFree (alfp->path);
 
1625
  for (vnp = alfp->fiplist; vnp != NULL; vnp = vnp->next) {
 
1626
    fip = (FastaIndexPtr) vnp->data.ptrvalue;
 
1627
    FreeFastaIndex (fip);
 
1628
  }
 
1629
  alfp->fiplist = ValNodeFree (alfp->fiplist);
 
1630
  alfp->index = MemFree (alfp->index);
 
1631
  MemFree (alfp);
 
1632
}
 
1633
 
 
1634
/* common function for creating indexes of fasta library files */
 
1635
 
1337
1636
NLM_EXTERN void CreateFastaIndex (
1338
1637
  CharPtr file
1339
1638
)
1418
1717
  }
1419
1718
 
1420
1719
  FileClose (ifp);
1421
 
}
1422
 
 
 
1720
 
 
1721
  ValNodeFreeData (head);
 
1722
}
 
1723
 
 
1724
/* object manager registerable fetch function for local ASN.1 indexed files */
 
1725
 
 
1726
static CharPtr asnlibfetchproc = "AsnIndexedLibBioseqFetch";
 
1727
 
 
1728
typedef struct asnlibftch {
 
1729
  CharPtr     path;
 
1730
  ValNodePtr  fiplist;
 
1731
  IdFipPtr    index;
 
1732
  Int4        numids;
 
1733
  Boolean     binary;
 
1734
} AsnLibFetchData, PNTR AsnLibFetchPtr;
 
1735
 
 
1736
static FastaIndexPtr SearchAsnIndex (
 
1737
  AsnLibFetchPtr alfp,
 
1738
  CharPtr seqid
 
1739
)
 
1740
 
 
1741
{
 
1742
  int       compare;
 
1743
  IdFipPtr  ifp;
 
1744
  Int4      L, R, mid;
 
1745
 
 
1746
  if (alfp == NULL || alfp->index == NULL) return NULL;
 
1747
  ifp = alfp->index;
 
1748
  if (StringHasNoText (seqid)) return NULL;
 
1749
 
 
1750
  L = 0;
 
1751
  R = alfp->numids - 1;
 
1752
  while (L < R) {
 
1753
    mid = (L + R) / 2;
 
1754
    compare = StringICmp (ifp [mid].seqid, seqid);
 
1755
    if (compare < 0) {
 
1756
      L = mid + 1;
 
1757
    } else {
 
1758
      R = mid;
 
1759
    }
 
1760
  }
 
1761
 
 
1762
  if (R >= 0 && R < alfp->numids) {
 
1763
    if (StringICmp (ifp [R].seqid, seqid) == 0) {
 
1764
      return ifp [R].fip;
 
1765
    }
 
1766
  }
 
1767
 
 
1768
  return NULL;
 
1769
}
 
1770
 
 
1771
static Int2 LIBCALLBACK AsnIndexedLibBioseqFetchFunc (Pointer data)
 
1772
 
 
1773
{
 
1774
  AsnIoPtr          aip;
 
1775
  AsnLibFetchPtr    alfp;
 
1776
  BioseqPtr         bsp;
 
1777
  Char              file [FILENAME_MAX], path [PATH_MAX], id [41];
 
1778
  FastaIndexPtr     fip;
 
1779
  Int4              offset;
 
1780
  OMProcControlPtr  ompcp;
 
1781
  ObjMgrProcPtr     ompp;
 
1782
  SeqEntryPtr       sep = NULL;
 
1783
  SeqIdPtr          sip;
 
1784
  CharPtr           tmp;
 
1785
 
 
1786
  ompcp = (OMProcControlPtr) data;
 
1787
  if (ompcp == NULL) return OM_MSG_RET_ERROR;
 
1788
  ompp = ompcp->proc;
 
1789
  if (ompp == NULL) return OM_MSG_RET_ERROR;
 
1790
  alfp = (AsnLibFetchPtr) ompp->procdata;
 
1791
  if (alfp == NULL) return OM_MSG_RET_ERROR;
 
1792
  sip = (SeqIdPtr) ompcp->input_data;
 
1793
  if (sip == NULL) return OM_MSG_RET_ERROR;
 
1794
 
 
1795
  SeqIdWrite (sip, id, PRINTID_REPORT, sizeof (id));
 
1796
  fip = SearchAsnIndex (alfp, id);
 
1797
  if (fip != NULL) {
 
1798
    offset = SearchFastaIndex (fip, id);
 
1799
    if (offset < 0) return OM_MSG_RET_ERROR;
 
1800
    StringCpy (file, fip->file);
 
1801
    tmp = StringStr (file, ".idx");
 
1802
    if (tmp != NULL) {
 
1803
      *tmp = '\0';
 
1804
    }
 
1805
    StringCat (file, ".aso");
 
1806
    StringNCpy_0 (path, fip->path, sizeof (path));
 
1807
    FileBuildPath (path, NULL, file);
 
1808
    aip = AsnIoOpen (path, alfp->binary? "rb" : "r");
 
1809
    if (aip == NULL) {
 
1810
      tmp = StringStr (file, ".aso");
 
1811
      if (tmp != NULL) {
 
1812
        *tmp = '\0';
 
1813
        StringCat (file, ".asn");
 
1814
        StringNCpy_0 (path, fip->path, sizeof (path));
 
1815
        FileBuildPath (path, NULL, file);
 
1816
        aip = AsnIoOpen (path, alfp->binary? "rb" : "r");
 
1817
      }
 
1818
    }
 
1819
    if (aip == NULL) return OM_MSG_RET_ERROR;
 
1820
    AsnIoSeek (aip, offset);
 
1821
    sep = SeqEntryAsnRead (aip, NULL);
 
1822
    AsnIoClose (aip);
 
1823
  }
 
1824
 
 
1825
  if (sep == NULL) return OM_MSG_RET_ERROR;
 
1826
  bsp = BioseqFindInSeqEntry (sip, sep);
 
1827
  ompcp->output_data = (Pointer) bsp;
 
1828
  ompcp->output_entityID = ObjMgrGetEntityIDForChoice (sep);
 
1829
  return OM_MSG_RET_DONE;
 
1830
}
 
1831
 
 
1832
NLM_EXTERN Boolean AsnIndexedLibFetchEnable (CharPtr path, Boolean binary)
 
1833
 
 
1834
{
 
1835
  AsnLibFetchPtr  alfp = NULL;
 
1836
  Char            file [FILENAME_MAX];
 
1837
  FastaIndexPtr   fip;
 
1838
  ValNodePtr      head;
 
1839
  Int4            i;
 
1840
  IdFipPtr        ifp;
 
1841
  Boolean         is_new = FALSE;
 
1842
  Int4            j;
 
1843
  Int4            numids = 0;
 
1844
  ObjMgrPtr       omp;
 
1845
  ObjMgrProcPtr   ompp;
 
1846
  Char            str [PATH_MAX];
 
1847
  CharPtr         tmp;
 
1848
  ValNodePtr      vnp;
 
1849
 
 
1850
  StringNCpy_0 (str, path, sizeof (str));
 
1851
  TrimSpacesAroundString (str);
 
1852
  omp = ObjMgrGet ();
 
1853
  ompp = ObjMgrProcFind (omp, 0, asnlibfetchproc, OMPROC_FETCH);
 
1854
  if (ompp != NULL) {
 
1855
    alfp = (AsnLibFetchPtr) ompp->procdata;
 
1856
    if (alfp != NULL) {
 
1857
      alfp->path = MemFree (alfp->path);
 
1858
      for (vnp = alfp->fiplist; vnp != NULL; vnp = vnp->next) {
 
1859
        fip = (FastaIndexPtr) vnp->data.ptrvalue;
 
1860
        FreeFastaIndex (fip);
 
1861
      }
 
1862
      alfp->fiplist = ValNodeFree (alfp->fiplist);
 
1863
      alfp->index = MemFree (alfp->index);
 
1864
    }
 
1865
  } else {
 
1866
    alfp = (AsnLibFetchPtr) MemNew (sizeof (AsnLibFetchData));
 
1867
    is_new = TRUE;
 
1868
    if (alfp != NULL) {
 
1869
      alfp->binary = binary;
 
1870
    }
 
1871
  }
 
1872
  if (alfp != NULL) {
 
1873
    alfp->path = StringSave (str);
 
1874
    head = DirCatalog (str);
 
1875
    for (vnp = head; vnp != NULL; vnp = vnp->next) {
 
1876
      if (vnp->choice == 0) {
 
1877
        tmp = (CharPtr) vnp->data.ptrvalue;
 
1878
        if (StringStr (tmp, ".idx") != NULL) {
 
1879
          StringCpy (str, alfp->path);
 
1880
          sprintf (file, "%s", tmp);
 
1881
          FileBuildPath (str, NULL, file);
 
1882
          fip = ReadFastaIndex (str);
 
1883
          if (fip != NULL) {
 
1884
            ValNodeAddPointer (&(alfp->fiplist), 0, (Pointer) fip);
 
1885
            numids += fip->numlines;
 
1886
          }
 
1887
        }
 
1888
      }
 
1889
    }
 
1890
    ValNodeFreeData (head);
 
1891
    ifp = (IdFipPtr) MemNew (sizeof (IdFip) * (numids + 2));
 
1892
    alfp->index = ifp;
 
1893
    alfp->numids = numids;
 
1894
    if (ifp != NULL) {
 
1895
      i = 0;
 
1896
      for (vnp = alfp->fiplist; vnp != NULL; vnp = vnp->next) {
 
1897
        fip = (FastaIndexPtr) vnp->data.ptrvalue;
 
1898
        if (fip != NULL) {
 
1899
          for (j = 0; j < fip->numlines; j++, i++) {
 
1900
            ifp [i].seqid = fip->seqids [j];
 
1901
            ifp [i].fip = fip;
 
1902
          }
 
1903
        }
 
1904
      }
 
1905
      HeapSort (ifp, (size_t) numids, sizeof (IdFip), SortIfpByID);
 
1906
    }
 
1907
  }
 
1908
  if (is_new) {
 
1909
    ObjMgrProcLoad (OMPROC_FETCH, asnlibfetchproc, asnlibfetchproc,
 
1910
                    OBJ_SEQID, 0, OBJ_BIOSEQ, 0, (Pointer) alfp,
 
1911
                    AsnIndexedLibBioseqFetchFunc, PROC_PRIORITY_DEFAULT);
 
1912
  }
 
1913
  return TRUE;
 
1914
}
 
1915
 
 
1916
NLM_EXTERN void AsnIndexedLibFetchDisable (void)
 
1917
 
 
1918
{
 
1919
  AsnLibFetchPtr  alfp;
 
1920
  FastaIndexPtr   fip;
 
1921
  ObjMgrPtr       omp;
 
1922
  ObjMgrProcPtr   ompp;
 
1923
  ValNodePtr      vnp;
 
1924
 
 
1925
  omp = ObjMgrGet ();
 
1926
  ompp = ObjMgrProcFind (omp, 0, asnlibfetchproc, OMPROC_FETCH);
 
1927
  if (ompp == NULL) return;
 
1928
  ObjMgrFreeUserData (0, ompp->procid, OMPROC_FETCH, 0);
 
1929
  alfp = (AsnLibFetchPtr) ompp->procdata;
 
1930
  if (alfp == NULL) return;
 
1931
  alfp->path = MemFree (alfp->path);
 
1932
  for (vnp = alfp->fiplist; vnp != NULL; vnp = vnp->next) {
 
1933
    fip = (FastaIndexPtr) vnp->data.ptrvalue;
 
1934
    FreeFastaIndex (fip);
 
1935
  }
 
1936
  alfp->fiplist = ValNodeFree (alfp->fiplist);
 
1937
  alfp->index = MemFree (alfp->index);
 
1938
  MemFree (alfp);
 
1939
}
 
1940
 
 
1941
/* common function for creating indexes of ASN.1 Bioseq-set ftp release files */
 
1942
 
 
1943
typedef struct asnidxdata {
 
1944
  FILE        *ofp;
 
1945
  Int4        offset;
 
1946
  ValNodePtr  head;
 
1947
  ValNodePtr  last;
 
1948
} AsnIdxData, PNTR AsnIdxPtr;
 
1949
 
 
1950
static void SaveAsnIdxOffset (
 
1951
  BioseqPtr bsp,
 
1952
  Pointer userdata
 
1953
)
 
1954
 
 
1955
{
 
1956
  AsnIdxPtr  aip;
 
1957
  Char       id [41], tmp [64];
 
1958
  SeqIdPtr   sip;
 
1959
 
 
1960
  aip = (AsnIdxPtr) userdata;
 
1961
  if (bsp == NULL || aip == NULL) return;
 
1962
 
 
1963
  sip = SeqIdFindBest (bsp->id, SEQID_GI);
 
1964
  if (sip == NULL) {
 
1965
    sip = SeqIdFindBest (bsp->id, 0);
 
1966
  }
 
1967
 
 
1968
  SeqIdWrite (sip, id, PRINTID_REPORT, sizeof (id));
 
1969
  if (! StringHasNoText (id)) {
 
1970
 
 
1971
    /* save ID and offset separated by tab character */
 
1972
 
 
1973
    sprintf (tmp, "%s\t%ld", id, (long) aip->offset);
 
1974
    aip->last = ValNodeNew (aip->last);
 
1975
    if (aip->head == NULL) {
 
1976
      aip->head = aip->last;
 
1977
    }
 
1978
    if (aip->last != NULL) {
 
1979
      aip->last->data.ptrvalue = StringSave (tmp);
 
1980
    }
 
1981
  }
 
1982
}
 
1983
 
 
1984
static void CreateBinaryAsnIndex (
 
1985
  CharPtr file
 
1986
)
 
1987
 
 
1988
{
 
1989
  AsnIdxData    aid;
 
1990
  AsnIoPtr      aip;
 
1991
  AsnModulePtr  amp;
 
1992
  AsnTypePtr    atp, atp_bss, atp_se;
 
1993
  FILE          *ofp;
 
1994
  ObjMgrPtr     omp;
 
1995
  Char          path [PATH_MAX];
 
1996
  CharPtr       ptr;
 
1997
  SeqEntryPtr   sep;
 
1998
  ValNodePtr    vnp;
 
1999
 
 
2000
  if (StringHasNoText (file)) return;
 
2001
 
 
2002
  /* replace extension by .idx for index file */
 
2003
 
 
2004
  StringNCpy_0 (path, file, sizeof (path));
 
2005
  ptr = StringRChr (path, '.');
 
2006
  if (ptr != NULL) {
 
2007
    *ptr = '\0';
 
2008
  }
 
2009
  StringCat (path, ".idx");
 
2010
 
 
2011
  aip = AsnIoOpen (file, "rb");
 
2012
  if (aip == NULL) return;
 
2013
 
 
2014
  ofp = FileOpen (path, "w");
 
2015
  if (ofp != NULL) {
 
2016
 
 
2017
    MemSet ((Pointer) &aid, 0, sizeof (AsnIdxData));
 
2018
    aid.head = NULL;
 
2019
    aid.last = NULL;
 
2020
    aid.ofp = ofp;
 
2021
 
 
2022
    amp = AsnAllModPtr ();
 
2023
 
 
2024
    atp_bss = AsnFind ("Bioseq-set");
 
2025
    atp_se = AsnFind ("Bioseq-set.seq-set.E");
 
2026
 
 
2027
    atp = atp_bss;
 
2028
 
 
2029
    /* get initial file offset */
 
2030
 
 
2031
    aid.offset = AsnIoTell (aip);
 
2032
 
 
2033
    /* read next ASN.1 component */
 
2034
 
 
2035
    while ((atp = AsnReadId (aip, amp, atp)) != NULL) {
 
2036
      if (atp == atp_se) {
 
2037
 
 
2038
        sep = SeqEntryAsnRead (aip, atp);
 
2039
        VisitBioseqsInSep (sep, (Pointer) &aid, SaveAsnIdxOffset);
 
2040
 
 
2041
        SeqEntryFree (sep);
 
2042
        omp = ObjMgrGet ();
 
2043
        ObjMgrReapOne (omp);
 
2044
        ObjMgrFreeCache (0);
 
2045
 
 
2046
      } else {
 
2047
 
 
2048
        AsnReadVal (aip, atp, NULL);
 
2049
      }
 
2050
 
 
2051
      /* get file offset of next ASN.1 component */
 
2052
 
 
2053
      aid.offset = AsnIoTell (aip);
 
2054
    }
 
2055
 
 
2056
    /* sort by ID */
 
2057
 
 
2058
    aid.head = ValNodeSort (aid.head, SortVnpByString);
 
2059
    aid.head = UniqueValNode (aid.head);
 
2060
 
 
2061
    /* write ID and offset index */
 
2062
 
 
2063
    for (vnp = aid.head; vnp != NULL; vnp = vnp->next) {
 
2064
      fprintf (ofp, "%s\n", (CharPtr) vnp->data.ptrvalue);
 
2065
    }
 
2066
 
 
2067
    FileClose (ofp);
 
2068
  }
 
2069
 
 
2070
  AsnIoClose (aip);
 
2071
 
 
2072
  ValNodeFreeData (aid.head);
 
2073
}
 
2074
 
 
2075
static void CreateTextAsnIndex (
 
2076
  CharPtr file
 
2077
)
 
2078
 
 
2079
{
 
2080
  AsnIdxData   aid;
 
2081
  Pointer      dataptr = NULL;
 
2082
  Uint2        datatype, entityID = 0;
 
2083
  FILE         *ifp, *ofp;
 
2084
  Char         path [PATH_MAX];
 
2085
  CharPtr      ptr;
 
2086
  SeqEntryPtr  sep;
 
2087
  ValNodePtr   vnp;
 
2088
 
 
2089
  if (StringHasNoText (file)) return;
 
2090
 
 
2091
  /* replace extension by .idx for index file */
 
2092
 
 
2093
  StringNCpy_0 (path, file, sizeof (path));
 
2094
  ptr = StringRChr (path, '.');
 
2095
  if (ptr != NULL) {
 
2096
    *ptr = '\0';
 
2097
  }
 
2098
  StringCat (path, ".idx");
 
2099
 
 
2100
  ifp = FileOpen (file, "r");
 
2101
  if (ifp == NULL) return;
 
2102
 
 
2103
  ofp = FileOpen (path, "w");
 
2104
  if (ofp != NULL) {
 
2105
 
 
2106
    MemSet ((Pointer) &aid, 0, sizeof (AsnIdxData));
 
2107
    aid.head = NULL;
 
2108
    aid.last = NULL;
 
2109
    aid.ofp = ofp;
 
2110
 
 
2111
    /* get initial file offset */
 
2112
 
 
2113
    aid.offset = ftell (ifp);
 
2114
 
 
2115
    /* read next ASN.1 component */
 
2116
 
 
2117
    while ((dataptr = ReadAsnFastaOrFlatFile (ifp, &datatype, &entityID,
 
2118
                                              FALSE, FALSE, TRUE, FALSE)) != NULL) {
 
2119
 
 
2120
      sep = GetTopSeqEntryForEntityID (entityID);
 
2121
      VisitBioseqsInSep (sep, (Pointer) &aid, SaveAsnIdxOffset);
 
2122
 
 
2123
      ObjMgrFreeByEntityID (entityID);
 
2124
 
 
2125
      /* get file offset of next ASN.1 component */
 
2126
 
 
2127
      aid.offset = ftell (ifp);
 
2128
    }
 
2129
 
 
2130
    /* sort by ID */
 
2131
 
 
2132
    aid.head = ValNodeSort (aid.head, SortVnpByString);
 
2133
    aid.head = UniqueValNode (aid.head);
 
2134
 
 
2135
    /* write ID and offset index */
 
2136
 
 
2137
    for (vnp = aid.head; vnp != NULL; vnp = vnp->next) {
 
2138
      fprintf (ofp, "%s\n", (CharPtr) vnp->data.ptrvalue);
 
2139
    }
 
2140
 
 
2141
    FileClose (ofp);
 
2142
  }
 
2143
 
 
2144
  FileClose (ifp);
 
2145
 
 
2146
  ValNodeFreeData (aid.head);
 
2147
}
 
2148
 
 
2149
NLM_EXTERN void CreateAsnIndex (
 
2150
  CharPtr file,
 
2151
  Boolean binary
 
2152
)
 
2153
 
 
2154
{
 
2155
  if (binary) {
 
2156
    CreateBinaryAsnIndex (file);
 
2157
  } else {
 
2158
    CreateTextAsnIndex (file);
 
2159
  }
 
2160
}
1423
2161