1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918
|
/* nps2gps.c
* ===========================================================================
*
* 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: nps2gps.c
*
* Author: Jonathan Kans
*
* Version Creation Date: 5/12/05
*
* $Revision: 1.30 $
*
* File Description:
*
* Modifications:
* --------------------------------------------------------------------------
* ==========================================================================
*/
#include <ncbi.h>
#include <objall.h>
#include <objsset.h>
#include <objsub.h>
#include <objfdef.h>
#include <sqnutils.h>
#include <seqport.h>
#include <explore.h>
#include <subutil.h>
#include <toasn3.h>
#include <pmfapi.h>
#define NPS2GPSAPP_VER "2.9"
CharPtr NPS2GPSAPPLICATION = NPS2GPSAPP_VER;
typedef struct n2gdata {
CharPtr results;
CharPtr outfile;
Boolean failure;
Boolean lock;
Boolean byTscriptID;
Boolean byFeatID;
Boolean useProtID;
Boolean refSeqTitles;
Boolean smarttitle;
Boolean noncoding;
Boolean removeunnecxref;
CharPtr genDb;
CharPtr genQual;
} N2GData, PNTR N2GPtr;
typedef struct npsseqs {
BioseqPtr nuc;
BioseqPtr prot;
} NpsSeqs, PNTR NpsSeqsPtr;
static void FindNucProtSeqs (
BioseqPtr bsp,
Pointer userdata
)
{
NpsSeqsPtr nsp;
if (bsp == NULL) return;
nsp = (NpsSeqsPtr) userdata;
if (nsp == NULL) return;
if (ISA_na (bsp->mol)) {
nsp->nuc = bsp;
} else if (ISA_aa (bsp->mol)) {
nsp->prot = bsp;
}
}
static void LclMakeNucProtCDS (
BioseqSetPtr bssp,
Pointer userdata
)
{
CodeBreakPtr cbp;
SeqFeatPtr cds;
CdRegionPtr crp;
SeqFeatPtr mrna;
NpsSeqs ns;
Boolean partial5, partial3;
SeqFeatPtr sfp;
SeqIdPtr sip;
SeqLocPtr slp;
Int4 start, stop;
SeqFeatPtr temp;
ns.nuc = NULL;
ns.prot = NULL;
if (VisitBioseqsInSet (bssp, (Pointer) &ns, FindNucProtSeqs) != 2) return;
if (ns.nuc == NULL || ns.prot == NULL) return;
cds = SeqMgrGetCDSgivenProduct (ns.prot, NULL);
mrna = SeqMgrGetRNAgivenProduct (ns.nuc, NULL);
if (cds == NULL || mrna == NULL) return;
CheckSeqLocForPartial (cds->location, &partial5, &partial3);
start = GetOffsetInLoc (cds->location, mrna->location, SEQLOC_START);
stop = GetOffsetInLoc (cds->location, mrna->location, SEQLOC_STOP);
if (start < 0 || start >= ns.nuc->length ||
stop < 0 || stop >= ns.nuc->length) return;
sip = SeqIdFindBest (ns.nuc->id, 0);
if (sip == NULL) return;
/* copy cds feature fields to paste into new cds feature */
temp = AsnIoMemCopy (cds,
(AsnReadFunc) SeqFeatAsnRead,
(AsnWriteFunc) SeqFeatAsnWrite);
if (temp == NULL) return;
sfp = CreateNewFeatureOnBioseq (ns.nuc, SEQFEAT_CDREGION, NULL);
if (sfp == NULL) return;
sfp->location = SeqLocFree (sfp->location);
if (StringISearch (cds->except_text, "ribosomal slippage") == NULL &&
StringISearch (cds->except_text, "ribosome slippage") == NULL &&
StringISearch (cds->except_text, "trans splicing") == NULL &&
StringISearch (cds->except_text, "trans-splicing") == NULL &&
StringISearch (cds->except_text, "artificial frameshift") == NULL) {
sfp->location = AddIntervalToLocation (NULL, sip, start, stop, partial5, partial3);
} else {
slp = SeqLocFindNext (cds->location, NULL);
while (slp != NULL) {
start = GetOffsetInLoc (slp, mrna->location, SEQLOC_START);
stop = GetOffsetInLoc (slp, mrna->location, SEQLOC_STOP);
sfp->location = AddIntervalToLocation (sfp->location, sip, start,
stop, partial5, partial3);
slp = SeqLocFindNext (cds->location, slp);
}
sfp->location = SeqLocMergeEx (ns.nuc, sfp->location, NULL, FALSE, TRUE, FALSE, FALSE);
}
SetSeqFeatProduct (sfp, ns.prot);
/* paste fields from temp copy of original cds - but not feature ID */
crp = (CdRegionPtr) temp->data.value.ptrvalue;
sfp->data.value.ptrvalue = (Pointer) crp;
sfp->partial = temp->partial;
sfp->excpt = temp->excpt;
sfp->comment = temp->comment;
sfp->qual = temp->qual;
sfp->title = temp->title;
sfp->ext = temp->ext;
sfp->cit = temp->cit;
sfp->exp_ev = temp->exp_ev;
sfp->xref = temp->xref;
sfp->dbxref = temp->dbxref;
sfp->pseudo = temp->pseudo;
sfp->except_text = temp->except_text;
MemFree (temp); /* do not SeqFeatFree */
/* update code break locations */
for (cbp = crp->code_break; cbp != NULL; cbp = cbp->next) {
CheckSeqLocForPartial (cbp->loc, &partial5, &partial3);
start = GetOffsetInLoc (cbp->loc, mrna->location, SEQLOC_START);
stop = GetOffsetInLoc (cbp->loc, mrna->location, SEQLOC_STOP);
if (start < 0 || start >= ns.nuc->length ||
stop < 0 || stop >= ns.nuc->length) continue;
cbp->loc = SeqLocFree (cbp->loc);
cbp->loc = AddIntervalToLocation (NULL, sip, start, stop, partial5, partial3);;
}
/* remove feature ID cross-references of original cds */
ClearFeatIDXrefs (sfp);
}
/* copy gene from contig onto nuc-prot, single interval on cdna bioseq */
static GeneRefPtr LclCopyGeneWork (
SeqFeatPtr sfp,
BioseqPtr bsp
)
{
SeqFeatPtr gene, copy, temp;
GeneRefPtr grp = NULL, xref;
Boolean partial5, partial3;
if (sfp == NULL || bsp == NULL) return NULL;
if (sfp->data.choice != SEQFEAT_RNA) return NULL;
gene = GetGeneForFeature (sfp);
if (gene == NULL) {
xref = SeqMgrGetGeneXref (sfp);
if (xref == NULL) return NULL;
if (SeqMgrGeneIsSuppressed (xref)) return NULL;
/* copy gene xref for new gene feature */
grp = AsnIoMemCopy (xref,
(AsnReadFunc) GeneRefAsnRead,
(AsnWriteFunc) GeneRefAsnWrite);
if (grp == NULL) return NULL;
/* make new gene feature on full-length of cdna */
copy = CreateNewFeatureOnBioseq (bsp, SEQFEAT_GENE, NULL);
if (copy == NULL) return NULL;
copy->data.value.ptrvalue = grp;
return grp;
}
CheckSeqLocForPartial (gene->location, &partial5, &partial3);
/* copy gene feature fields to paste into new gene feature */
temp = AsnIoMemCopy (gene,
(AsnReadFunc) SeqFeatAsnRead,
(AsnWriteFunc) SeqFeatAsnWrite);
if (temp == NULL) return NULL;
/* make new gene feature on full-length of cdna */
copy = CreateNewFeatureOnBioseq (bsp, SEQFEAT_GENE, NULL);
if (copy == NULL) {
SeqFeatFree (temp);
return NULL;
}
/* paste fields from temp copy of original gene */
copy->data.value.ptrvalue = temp->data.value.ptrvalue;
copy->partial = temp->partial;
copy->excpt = temp->excpt;
copy->comment = temp->comment;
copy->qual = temp->qual;
copy->title = temp->title;
copy->ext = temp->ext;
copy->cit = temp->cit;
copy->exp_ev = temp->exp_ev;
copy->xref = temp->xref;
copy->dbxref = temp->dbxref;
copy->pseudo = temp->pseudo;
copy->except_text = temp->except_text;
SetSeqLocPartial (copy->location, partial5, partial3);
SeqLocFree (temp->location);
MemFree (temp); /* do not SeqFeatFree */
grp = (GeneRefPtr) copy->data.value.ptrvalue;
return grp;
}
static void LclCopyGene (
SeqFeatPtr sfp,
Pointer userdata
)
{
BioseqPtr bsp;
/* input mrna features are multi-interval on contig */
if (sfp->data.choice != SEQFEAT_RNA) return;
/* find cdna product of mrna */
bsp = BioseqFindFromSeqLoc (sfp->product);
if (bsp == NULL) return;
LclCopyGeneWork (sfp, bsp);
}
static void LclAddMrnaTitles (
SeqLocPtr slp,
CharPtr organism,
Boolean refSeqTitles
)
{
BioseqPtr bsp;
SeqMgrFeatContext ccontext;
CharPtr cdslabel = NULL;
SeqMgrFeatContext gcontext;
CharPtr genelabel = NULL;
size_t len;
SeqFeatPtr sfp;
CharPtr str;
if (slp == NULL) return;
bsp = BioseqFindFromSeqLoc (slp);
if (bsp == NULL) return;
if (! ISA_na (bsp->mol)) return;
if (BioseqGetTitle (bsp) != NULL) return;
sfp = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_GENE, 0, &gcontext);
if (sfp != NULL) {
genelabel = gcontext.label;
if (StringHasNoText (genelabel)) {
genelabel = NULL;
}
}
sfp = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_CDREGION, 0, &ccontext);
if (sfp != NULL) {
cdslabel = ccontext.label;
if (StringHasNoText (cdslabel)) {
cdslabel = NULL;
}
}
len = StringLen (organism) + StringLen (genelabel) + StringLen (cdslabel) +
StringLen (" mRNA, complete cds.") + 10;
str = (CharPtr) MemNew (len * sizeof (Char));
if (str == NULL) return;
str [0] = '\0';
if (! StringHasNoText (organism)) {
StringCat (str, organism);
}
if (cdslabel != NULL) {
StringCat (str, " ");
StringCat (str, cdslabel);
}
if (genelabel != NULL) {
StringCat (str, " (");
StringCat (str, genelabel);
StringCat (str, ")");
}
if (cdslabel != NULL && genelabel != NULL) {
if (ccontext.partialL || ccontext.partialR) {
if (refSeqTitles) {
StringCat (str, " partial mRNA.");
} else {
StringCat (str, " mRNA, partial cds.");
}
} else {
if (refSeqTitles) {
/* requested to make all mRNAs partial in defline */
StringCat (str, " partial mRNA.");
} else {
StringCat (str, " mRNA, complete cds.");
}
}
} else if (genelabel != NULL) {
StringCat (str, " mRNA.");
}
SeqDescrAddPointer (&(bsp->descr), Seq_descr_title, (Pointer) str);
}
static CharPtr RnaTypeLabel (
SeqFeatPtr rna
)
{
if (rna == NULL) return "RNA";
switch (rna->idx.subtype) {
case FEATDEF_preRNA :
return "preRNA";
case FEATDEF_mRNA :
return "mRNA";
case FEATDEF_tRNA :
return "tRNA";
case FEATDEF_rRNA :
return "rRNA";
case FEATDEF_snRNA :
return "snRNA";
case FEATDEF_scRNA :
return "scRNA";
case FEATDEF_otherRNA :
return "otherRNA";
case FEATDEF_snoRNA :
return "snoRNA";
case FEATDEF_ncRNA :
return "ncRNA";
case FEATDEF_tmRNA :
return "tmRNA";
default :
break;
}
return "RNA";
}
static void LclMakeOneRnaTitle (
SeqFeatPtr rna,
SeqFeatPtr gene,
CharPtr label,
CharPtr organism,
Boolean alt_splice
)
{
BioseqPtr bsp;
SeqMgrFeatContext ccontext;
SeqFeatPtr cds;
GeneRefPtr grp;
Char id [64];
CharPtr lbl = NULL;
size_t len;
CharPtr ptr;
CharPtr str;
CharPtr typ = NULL;
if (rna == NULL || rna->product == NULL) return;
grp = SeqMgrGetGeneXref (rna);
if (SeqMgrGeneIsSuppressed (grp)) return;
if (grp == NULL && gene != NULL) {
grp = (GeneRefPtr) gene->data.value.ptrvalue;
}
if (grp == NULL) return;
bsp = BioseqFindFromSeqLoc (rna->product);
if (bsp == NULL) return;
SeqIdWrite (bsp->id, id, PRINTID_TEXTID_ACC_VER, sizeof (id) - 1);
typ = RnaTypeLabel (rna);
lbl = StringSaveNoNull (label);
cds = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_CDREGION, 0, &ccontext);
len = StringLen (organism) + StringLen (grp->locus_tag) + StringLen (grp->locus) +
StringLen (id) + StringLen (" transcript variant") + StringLen (lbl) +
StringLen (" mRNA, complete cds.") + StringLen (typ) + 20;
str = (CharPtr) MemNew (len * sizeof (Char));
if (str == NULL) return;
str [0] = '\0';
if (StringDoesHaveText (organism)) {
StringCat (str, organism);
}
if (lbl != NULL) {
StringCat (str, " ");
ptr = StringStr (lbl, ", transcript variant ");
if (ptr != NULL) {
*ptr = '\0';
ptr += 2;
StringCat (str, lbl);
if (StringDoesHaveText (grp->locus)) {
StringCat (str, " (");
StringCat (str, grp->locus);
StringCat (str, ")");
}
StringCat (str, ", ");
StringCat (str, ptr);
} else {
StringCat (str, lbl);
if (StringDoesHaveText (grp->locus)) {
StringCat (str, " (");
StringCat (str, grp->locus);
StringCat (str, ")");
}
}
}
StringCat (str, ", ");
StringCat (str, typ);
StringCat (str, ".");
SeqDescrAddPointer (&(bsp->descr), Seq_descr_title, (Pointer) str);
MemFree (lbl);
}
typedef struct gcmdata {
SeqFeatPtr gene;
SeqFeatPtr feat;
CharPtr label;
} GmcData, PNTR GmcDataPtr;
static int LIBCALLBACK SortByGenePtr (
VoidPtr vp1,
VoidPtr vp2
)
{
GmcDataPtr gdp1, gdp2;
if (vp1 == NULL || vp2 == NULL) return 0;
gdp1 = (GmcDataPtr) vp1;
gdp2 = (GmcDataPtr) vp2;
if (gdp1 == NULL || gdp2 == NULL) return 0;
if (gdp1->gene > gdp2->gene) return -1;
if (gdp1->gene < gdp2->gene) return 1;
if (gdp1->feat > gdp2->feat) return -1;
if (gdp1->feat < gdp2->feat) return 1;
return 0;
}
static void LclMakeSmartRnaTitles (
BioseqPtr bsp,
CharPtr organism
)
{
SeqMgrFeatContext context;
GmcDataPtr gdp, head;
GeneRefPtr grp;
Int2 i, j, k, numgene, numrna;
SeqFeatPtr sfp;
if (bsp == NULL) return;
numgene = 0;
numrna = 0;
sfp = SeqMgrGetNextFeature (bsp, NULL, 0, 0, &context);
while (sfp != NULL) {
switch (sfp->data.choice) {
case SEQFEAT_GENE :
numgene++;
break;
case SEQFEAT_RNA :
numrna++;
break;
default :
break;
}
sfp = SeqMgrGetNextFeature (bsp, sfp, 0, 0, &context);
}
/* if (numgene == 0) return; */
if (numrna > 0) {
head = (GmcDataPtr) MemNew (sizeof (GmcData) * (size_t) (numrna + 1));
if (head != NULL) {
gdp = head;
sfp = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_RNA, 0, &context);
while (sfp != NULL) {
if (sfp->product != NULL) {
gdp->feat = sfp;
gdp->label = context.label;
grp = SeqMgrGetGeneXref (sfp);
if (grp == NULL || (! SeqMgrGeneIsSuppressed (grp))) {
gdp->gene = SeqMgrGetOverlappingGene (sfp->location, NULL);
}
gdp++;
}
sfp = SeqMgrGetNextFeature (bsp, sfp, SEQFEAT_RNA, 0, &context);
}
HeapSort (head, (size_t) numrna, sizeof (GmcData), SortByGenePtr);
for (i = 0; i < numrna; i += j) {
sfp = head [i].gene;
for (j = 1; i + j < numrna && sfp == head [i + j].gene; j++) continue;
if (j == 1) {
/* no alt splicing */
LclMakeOneRnaTitle (head [i].feat, head [i].gene, head [i].label, organism, FALSE);
} else {
/* is alt splicing */
for (k = 0; k < j; k++) {
LclMakeOneRnaTitle (head [i + k].feat, head [i + k].gene, head [i + k].label, organism, TRUE);
}
}
}
}
MemFree (head);
}
}
static SeqIdPtr MakeIdFromLocusTag (
SeqFeatPtr mrna
)
{
Char buf [64], suffix [8];
SeqFeatPtr gene;
GeneRefPtr grp;
Int4Ptr iptr;
if (mrna == NULL) return NULL;
suffix [0] = '\0';
grp = SeqMgrGetGeneXref (mrna);
if (grp != NULL) {
if (SeqMgrGeneIsSuppressed (grp)) return NULL;
}
if (grp == NULL) {
gene = SeqMgrGetOverlappingGene (mrna->location, NULL);
if (gene != NULL) {
grp = (GeneRefPtr) gene->data.value.ptrvalue;
iptr = (Int4Ptr) gene->idx.scratch;
if (iptr != NULL) {
(*iptr)++;
sprintf (suffix, "_%ld", (long) (*iptr));
}
}
}
if (grp != NULL) {
if (StringDoesHaveText (grp->locus_tag)) {
/* StringCpy (buf, "lcl|"); */
StringCpy (buf, "gnl|MTRACK|");
StringCat (buf, grp->locus_tag);
StringCat (buf, suffix);
return MakeSeqID (buf);
}
}
return NULL;
}
static SeqIdPtr MakeIdFromTscriptID (
SeqFeatPtr mrna
)
{
GBQualPtr gbq;
SeqIdPtr sip;
if (mrna == NULL) return NULL;
for (gbq = mrna->qual; gbq != NULL; gbq = gbq->next) {
if (StringICmp (gbq->qual, "orig_transcript_id") != 0) continue;
if (StringHasNoText (gbq->val)) continue;
sip = MakeSeqID (gbq->val);
if (sip != NULL) return sip;
}
return NULL;
}
static SeqIdPtr MakeIdFromProtein (
BioseqPtr pbsp
)
{
Char buf [128], tmp [128];
SeqIdPtr sip;
if (pbsp == NULL) return NULL;
sip = SeqIdFindBestAccession (pbsp->id);
if (sip == NULL) return NULL;
SeqIdWrite (sip, buf, PRINTID_TEXTID_ACC_VER, sizeof (buf) - 1);
if (StringHasNoText (buf)) return NULL;
StringCpy (tmp, "gnl|MTRACK|");
StringCat (tmp, buf);
StringCat (tmp, "_mrna");
return MakeSeqID (tmp);
}
static void AddGeneralIdFromQual (
BioseqPtr bsp,
SeqFeatPtr sfp,
CharPtr genDb,
CharPtr genQual,
CharPtr suffix
)
{
GBQualPtr gbq;
SeqIdPtr sip;
Char tmp [128];
CharPtr val = NULL;
if (bsp == NULL || sfp == NULL || StringHasNoText (genDb)) return;
if (StringHasNoText (genQual)) {
genQual = "standard_name";
}
for (gbq = sfp->qual; gbq != NULL; gbq = gbq->next) {
if (StringICmp (gbq->qual, genQual) != 0) continue;
val = gbq->val;
}
if (StringHasNoText (val)) return;
StringCpy (tmp, "gnl|");
StringCat (tmp, genDb);
StringCat (tmp, "|");
StringCat (tmp, val);
if (StringDoesHaveText (suffix)) {
StringCat (tmp, suffix);
}
sip = MakeSeqID (tmp);
if (sip == NULL) return;
sip->next = bsp->id;
bsp->id = sip;
}
static void InstantiateMrnaIntoProt (
SeqFeatPtr cds,
SeqFeatPtr mrna,
Int2Ptr ctrp,
N2GPtr ngp
)
{
ByteStorePtr bs;
Int4 len;
BioseqPtr mbsp, pbsp;
MolInfoPtr mip;
SeqEntryPtr msep, psep;
Boolean partial5, partial3;
CharPtr rnaseq;
ValNodePtr vnp;
if (cds == NULL || mrna == NULL || ngp == NULL) return;
if (cds->product == NULL || mrna->product != NULL) return;
pbsp = BioseqFindFromSeqLoc (cds->product);
if (pbsp == NULL) return;
psep = SeqMgrGetSeqEntryForData (pbsp);
if (psep == NULL) return;
rnaseq = GetSequenceByFeature (mrna);
if (rnaseq == NULL) return;
len = (Int4) StringLen (rnaseq);
bs = BSNew (len + 2);
if (bs == NULL) return;
BSWrite (bs, (VoidPtr) rnaseq, len);
MemFree (rnaseq);
mbsp = BioseqNew ();
if (mbsp == NULL) return;
mbsp->repr = Seq_repr_raw;
mbsp->mol = Seq_mol_rna;
mbsp->seq_data_type = Seq_code_iupacna;
mbsp->seq_data = (SeqDataPtr) bs;
mbsp->length = BSLen (bs);
BioseqPack (mbsp);
/* now adds _# suffix to general Seq-id if ambiguous - but messed up Drosophila record, so only use feature ID */
/*
mbsp->id = MakeIdFromLocusTag (mrna);
*/
if (ngp->byTscriptID) {
mbsp->id = MakeIdFromTscriptID (mrna);
} else if (ngp->useProtID) {
mbsp->id = MakeIdFromProtein (pbsp);
}
if (StringDoesHaveText (ngp->genDb)) {
AddGeneralIdFromQual (mbsp, mrna, ngp->genDb, ngp->genQual, NULL);
if (mbsp->id == NULL) {
AddGeneralIdFromQual (mbsp, cds, ngp->genDb, ngp->genQual, ".mrna");
}
}
if (mbsp->id == NULL) {
mbsp->id = MakeNewProteinSeqIdEx (mrna->location, NULL, NULL, ctrp);
}
CheckSeqLocForPartial (mrna->location, &partial5, &partial3);
SeqMgrAddToBioseqIndex (mbsp);
msep = SeqEntryNew ();
if (msep == NULL) return;
msep->choice = 1;
msep->data.ptrvalue = (Pointer) mbsp;
SeqMgrSeqEntry (SM_BIOSEQ, (Pointer) mbsp, msep);
mip = MolInfoNew ();
if (mip == NULL) return;
mip->biomol = MOLECULE_TYPE_MRNA;
if (partial5 && partial3) {
mip->completeness = 5;
} else if (partial5) {
mip->completeness = 3;
} else if (partial3) {
mip->completeness = 4;
}
vnp = CreateNewDescriptor (msep, Seq_descr_molinfo);
if (vnp != NULL) {
vnp->data.ptrvalue = (Pointer) mip;
}
SetSeqFeatProduct (mrna, mbsp);
/* add cDNA to protein, promoting to nuc-prot set component of genomic product set */
AddSeqEntryToSeqEntry (psep, msep, FALSE);
}
static void RemoveOrigIDs (
SeqFeatPtr sfp
)
{
GBQualPtr gbq, nextqual;
GBQualPtr PNTR prevqual;
if (sfp == NULL) return;
gbq = sfp->qual;
prevqual = (GBQualPtr PNTR) &(sfp->qual);
while (gbq != NULL) {
nextqual = gbq->next;
if (StringICmp (gbq->qual, "orig_protein_id") == 0 ||
StringICmp (gbq->qual, "orig_transcript_id") == 0) {
*(prevqual) = gbq->next;
gbq->next = NULL;
GBQualFree (gbq);
} else {
prevqual = (GBQualPtr PNTR) &(gbq->next);
}
gbq = nextqual;
}
}
static void RemoveOrigPrefix (
SeqFeatPtr sfp,
Pointer userdata
)
{
GBQualPtr gbq;
if (sfp == NULL) return;
/* copy smaller string without orig_ prefix into existing allocated memory */
for (gbq = sfp->qual; gbq != NULL; gbq = gbq->next) {
if (StringICmp (gbq->qual, "orig_protein_id") == 0) {
StringCpy (gbq->qual, "protein_id");
} else if (StringICmp (gbq->qual, "orig_transcript_id") == 0) {
StringCpy (gbq->qual, "transcript_id");
}
}
}
static void RemoveFeatScratch (
SeqFeatPtr sfp,
Pointer userdata
)
{
if (sfp == NULL) return;
if (sfp->idx.scratch == NULL) return;
sfp->idx.scratch = MemFree (sfp->idx.scratch);
}
typedef struct loopdata {
Int2 count;
SeqFeatPtr mrna;
} LoopData, PNTR LoopDataPtr;
static Boolean LIBCALLBACK FindSingleMrnaProc (
SeqFeatPtr sfp,
SeqMgrFeatContextPtr context
)
{
LoopDataPtr ldp;
ldp = (LoopDataPtr) context->userdata;
if (sfp->idx.scratch == NULL) {
(ldp->count)++;
ldp->mrna = sfp;
}
return TRUE;
}
static Boolean CdsIsPseudo (
SeqFeatPtr cds
)
{
SeqMgrFeatContext gcontext;
SeqFeatPtr gene;
GeneRefPtr grp;
if (cds == NULL) return FALSE;
if (cds->pseudo) return TRUE;
grp = SeqMgrGetGeneXref (cds);
if (grp != NULL) {
return grp->pseudo;
}
gene = SeqMgrGetOverlappingGene (cds->location, &gcontext);
if (gene == NULL) return FALSE;
if (gene->pseudo) return TRUE;
grp = (GeneRefPtr) gene->data.value.ptrvalue;
if (grp == NULL) return FALSE;
return grp->pseudo;
}
static void LoopThroughCDSs (
BioseqPtr bsp,
Int2Ptr ctrp,
N2GPtr ngp
)
{
SeqFeatPtr cds;
Int2 count;
SeqMgrFeatContext fcontext;
Boolean goOn;
LoopData ld;
CharPtr str;
/* loop through CDS features, finding single unused mRNA partner */
goOn = TRUE;
while (goOn) {
goOn = FALSE;
cds = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_CDREGION, 0, &fcontext);
while (cds != NULL) {
if (cds->idx.scratch == NULL) {
ld.count = 0;
ld.mrna = NULL;
count = SeqMgrGetAllOverlappingFeatures (cds->location, FEATDEF_mRNA, NULL, 0,
CHECK_INTERVALS, (Pointer) &ld, FindSingleMrnaProc);
if (ld.count == 1 && ld.mrna != NULL) {
InstantiateMrnaIntoProt (cds, ld.mrna, ctrp, ngp);
cds->idx.scratch = (Pointer) MemNew (sizeof (Int4));
ld.mrna->idx.scratch = (Pointer) MemNew (sizeof (Int4));
goOn = TRUE;
} else {
str = fcontext.label;
if (StringHasNoText (str)) {
str = "?";
}
Message (MSG_POSTERR, " CDS '%s' has %d overlapping mRNA", str, (int) ld.count);
}
}
cds = SeqMgrGetNextFeature (bsp, cds, SEQFEAT_CDREGION, 0, &fcontext);
}
}
/* if any CDSs were not promoted, record failure */
cds = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_CDREGION, 0, &fcontext);
while (cds != NULL) {
if (cds->idx.scratch == NULL) {
if (cds->product == NULL && CdsIsPseudo (cds)) {
/* do not complain about pseudo CDS not having product or matching mRNA */
} else {
if (ngp != NULL) {
ngp->failure = TRUE;
}
str = fcontext.label;
if (StringHasNoText (str)) {
str = "?";
}
Message (MSG_POSTERR, "Failed to match CDS '%s' to mRNA", str);
}
}
cds = SeqMgrGetNextFeature (bsp, cds, SEQFEAT_CDREGION, 0, &fcontext);
}
}
static Boolean ReciprocalTranscriptIDs (
SeqFeatPtr cds,
SeqFeatPtr mrna
)
{
GBQualPtr gbq;
CharPtr tid1 = NULL, tid2 = NULL;
if (cds == NULL || mrna == NULL) return FALSE;
for (gbq = cds->qual; gbq != NULL; gbq = gbq->next) {
if (StringICmp (gbq->qual, "orig_transcript_id") != 0) continue;
if (StringHasNoText (gbq->val)) continue;
tid1 = gbq->val;
}
for (gbq = mrna->qual; gbq != NULL; gbq = gbq->next) {
if (StringICmp (gbq->qual, "orig_transcript_id") != 0) continue;
if (StringHasNoText (gbq->val)) continue;
tid2 = gbq->val;
}
if (tid1 == NULL || tid2 == NULL) return FALSE;
if (StringICmp (tid1, tid2) == 0) return TRUE;
return FALSE;
}
static SeqFeatPtr GetCDSByProteinID (
SeqFeatPtr mrna
)
{
BioseqPtr bsp;
SeqFeatPtr cds;
GBQualPtr gbq;
SeqIdPtr sip;
if (mrna == NULL) return NULL;
for (gbq = mrna->qual; gbq != NULL; gbq = gbq->next) {
if (StringICmp (gbq->qual, "orig_protein_id") != 0) continue;
if (StringHasNoText (gbq->val)) continue;
sip = MakeSeqID (gbq->val);
if (sip == NULL) continue;
bsp = BioseqFind (sip);
if (bsp == NULL) continue;
cds = SeqMgrGetCDSgivenProduct (bsp, NULL);
if (cds != NULL) return cds;
}
return NULL;
}
static SeqFeatPtr GetmRNAByFeatureID (
SeqFeatPtr cds
)
{
SeqFeatPtr mRNA = NULL;
SeqFeatXrefPtr xref;
ObjectIdPtr oip;
Char buf [32];
if (cds == NULL) return NULL;
xref = cds->xref;
while (xref != NULL && mRNA == NULL) {
if (xref->id.choice == 3) {
oip = (ObjectIdPtr) xref->id.value.ptrvalue;
if (oip != NULL) {
if (StringDoesHaveText (oip->str)) {
mRNA = SeqMgrGetFeatureByFeatID (cds->idx.entityID, NULL, oip->str, NULL, NULL);
} else {
sprintf (buf, "%ld", (long) oip->id);
mRNA = SeqMgrGetFeatureByFeatID (cds->idx.entityID, NULL, buf, NULL, NULL);
}
}
}
xref = xref->next;
}
return mRNA;
}
static void InstantiateNCRNA (
SeqFeatPtr ncrna,
SeqEntryPtr top,
CharPtr organism,
Int2Ptr ctrp,
N2GPtr ngp
)
{
Uint1 biomol = 0;
ByteStorePtr bs;
BioseqPtr bsp;
SeqFeatPtr copy, temp;
GeneRefPtr grp;
Int4 len;
CharPtr locus = NULL;
MolInfoPtr mip;
Boolean partial5, partial3;
CharPtr prod = NULL;
RNAGenPtr rgp;
CharPtr rnaseq;
RnaRefPtr rrp;
SeqDescrPtr sdp;
SeqEntryPtr sep;
CharPtr ttl = NULL;
CharPtr typ;
if (ncrna == NULL || top == NULL || ngp == NULL) return;
if (ncrna->product != NULL) return;
rrp = (RnaRefPtr) ncrna->data.value.ptrvalue;
if (rrp == NULL) return;
switch (rrp->type) {
case RNA_TYPE_premsg:
biomol = MOLECULE_TYPE_PRE_MRNA;
break;
case RNA_TYPE_rRNA:
biomol = MOLECULE_TYPE_RRNA;
break;
case RNA_TYPE_snRNA:
biomol = MOLECULE_TYPE_SNRNA;
break;
case RNA_TYPE_scRNA:
biomol = MOLECULE_TYPE_SCRNA;
break;
case RNA_TYPE_snoRNA:
biomol = MOLECULE_TYPE_SNORNA;
break;
case RNA_TYPE_ncRNA:
biomol = MOLECULE_TYPE_NCRNA;
break;
default:
return;
}
rnaseq = GetSequenceByFeature (ncrna);
if (rnaseq == NULL) return;
len = (Int4) StringLen (rnaseq);
bs = BSNew (len + 2);
if (bs == NULL) return;
BSWrite (bs, (VoidPtr) rnaseq, len);
MemFree (rnaseq);
bsp = BioseqNew ();
if (bsp == NULL) return;
bsp->repr = Seq_repr_raw;
bsp->mol = Seq_mol_rna;
bsp->seq_data_type = Seq_code_iupacna;
bsp->seq_data = (SeqDataPtr) bs;
bsp->length = BSLen (bs);
BioseqPack (bsp);
if (ngp->byTscriptID) {
bsp->id = MakeIdFromTscriptID (ncrna);
}
if (StringDoesHaveText (ngp->genDb)) {
AddGeneralIdFromQual (bsp, ncrna, ngp->genDb, ngp->genQual, NULL);
}
if (bsp->id == NULL) {
bsp->id = MakeNewProteinSeqIdEx (ncrna->location, NULL, NULL, ctrp);
}
CheckSeqLocForPartial (ncrna->location, &partial5, &partial3);
SeqMgrAddToBioseqIndex (bsp);
sep = SeqEntryNew ();
if (sep == NULL) return;
sep->choice = 1;
sep->data.ptrvalue = (Pointer) bsp;
SeqMgrSeqEntry (SM_BIOSEQ, (Pointer) bsp, sep);
SetSeqFeatProduct (ncrna, bsp);
AddSeqEntryToSeqEntry (top, sep, FALSE);
mip = MolInfoNew ();
if (mip == NULL) return;
mip->biomol = biomol;
if (partial5 && partial3) {
mip->completeness = 5;
} else if (partial5) {
mip->completeness = 3;
} else if (partial3) {
mip->completeness = 4;
}
SeqDescrAddPointer (&(bsp->descr), Seq_descr_molinfo, (Pointer) mip);
grp = LclCopyGeneWork (ncrna, bsp);
if (grp != NULL) {
locus = grp->locus;
if (StringHasNoText (locus)) {
locus = grp->desc;
}
}
if (rrp->ext.choice == 1) {
prod = (CharPtr) rrp->ext.value.ptrvalue;
} else if (rrp->ext.choice == 3) {
rgp = (RNAGenPtr) rrp->ext.value.ptrvalue;
if (rgp != NULL) {
prod = rgp->product;
}
}
if (StringHasNoText (prod)) {
for (sdp = bsp->descr; sdp != NULL; sdp = sdp->next) {
if (sdp->choice != Seq_descr_comment) continue;
prod = (CharPtr) sdp->data.ptrvalue;
}
}
len = StringLen (organism) + StringLen (prod) + StringLen (locus);
if (len < 1) return;
ttl = (CharPtr) MemNew (sizeof (Char) * (len + 100));
if (ttl == NULL) return;
*ttl = '\0';
if (StringDoesHaveText (organism)) {
StringCat (ttl, organism);
}
if (StringDoesHaveText (prod)) {
StringCat (ttl, " ");
StringCat (ttl, prod);
}
if (StringDoesHaveText (locus)) {
StringCat (ttl, " (");
StringCat (ttl, locus);
StringCat (ttl, ")");
}
typ = RnaTypeLabel (ncrna);
StringCat (ttl, ", ");
StringCat (ttl, typ);
StringCat (ttl, ".");
if (StringDoesHaveText (ttl)) {
SeqDescrAddPointer (&(bsp->descr), Seq_descr_title, (Pointer) StringSave (ttl));
}
RemoveOrigIDs (ncrna);
CheckSeqLocForPartial (ncrna->location, &partial5, &partial3);
/* copy ncrna feature fields to paste into new ncrna feature */
temp = AsnIoMemCopy (ncrna,
(AsnReadFunc) SeqFeatAsnRead,
(AsnWriteFunc) SeqFeatAsnWrite);
if (temp == NULL) return;
/* make new ncrna feature on full-length of instantiated RNA */
copy = CreateNewFeatureOnBioseq (bsp, SEQFEAT_RNA, NULL);
if (copy == NULL) {
SeqFeatFree (temp);
return;
}
/* paste fields from temp copy of original ncrna */
copy->data.value.ptrvalue = temp->data.value.ptrvalue;
copy->partial = temp->partial;
copy->excpt = temp->excpt;
copy->comment = temp->comment;
copy->qual = temp->qual;
copy->title = temp->title;
copy->ext = temp->ext;
copy->cit = temp->cit;
copy->exp_ev = temp->exp_ev;
copy->xref = temp->xref;
copy->dbxref = temp->dbxref;
copy->pseudo = temp->pseudo;
copy->except_text = temp->except_text;
SetSeqLocPartial (copy->location, partial5, partial3);
SeqLocFree (temp->location);
SeqLocFree (temp->product);
MemFree (temp); /* do not SeqFeatFree */
}
static void PromoteNonCoding (
SeqEntryPtr sep,
Uint2 entityID,
CharPtr filename,
N2GPtr ngp,
SeqDescrPtr descr
)
{
BioSourcePtr biop;
BioseqPtr bsp;
BioseqSetPtr bssp;
Int2 ctr = 1;
SeqMgrFeatContext nccontext;
SeqFeatPtr ncrna;
SeqEntryPtr old, tmp, top;
ObjMgrDataPtr omdptop;
ObjMgrData omdata;
CharPtr organism;
OrgRefPtr orp;
Uint2 parenttype;
Pointer parentptr;
SeqDescrPtr sdp;
if (sep == NULL) return;
if (sep->choice == 2) {
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return;
/* recursively visit components of genbank set */
if (bssp->_class == BioseqseqSet_class_genbank) {
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
PromoteNonCoding (sep, entityID, filename, ngp, descr);
}
return;
}
}
if (SeqMgrFeaturesAreIndexed (entityID) == 0) {
SeqMgrIndexFeatures (entityID, NULL);
}
top = sep;
if (StringHasNoText (filename)) {
filename = "?";
}
bsp = FindNucBioseq (top);
if (bsp == NULL) {
Message (MSG_OK, "Unable to find nucleotide Bioseq in %s", filename);
if (ngp != NULL) {
ngp->failure = TRUE;
}
return;
}
if (sep->choice == 1) {
SaveSeqEntryObjMgrData (top, &omdptop, &omdata);
GetSeqEntryParent (top, &parentptr, &parenttype);
bssp = BioseqSetNew ();
if (bssp == NULL) return;
bssp->_class = BioseqseqSet_class_gen_prod_set;
tmp = SeqEntryNew ();
if (tmp == NULL) return;
tmp->choice = 1;
tmp->data.ptrvalue = sep->data.ptrvalue;
bssp->seq_set = tmp;
sep->choice = 2;
sep->data.ptrvalue = bssp;
SeqMgrLinkSeqEntry (top, parenttype, parentptr);
RestoreSeqEntryObjMgrData (top, omdptop, &omdata);
SeqMgrClearFeatureIndexes (entityID, NULL);
SeqMgrIndexFeatures (entityID, NULL);
}
old = SeqEntrySetScope (top);
organism = NULL;
for (sdp = descr; sdp != NULL; sdp = sdp->next) {
if (sdp->choice != Seq_descr_source) continue;
biop = (BioSourcePtr) sdp->data.ptrvalue;
if (biop == NULL) continue;
orp = biop->org;
if (orp == NULL) continue;
if (! StringHasNoText (orp->taxname)) {
organism = orp->taxname;
}
}
ctr = (Int2) VisitBioseqsInSep (top, NULL, NULL) + 1;
ncrna = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_RNA, 0, &nccontext);
while (ncrna != NULL) {
InstantiateNCRNA (ncrna, top, organism, &ctr, ngp);
ncrna = SeqMgrGetNextFeature (bsp, ncrna, SEQFEAT_RNA, 0, &nccontext);
}
SeqEntrySetScope (old);
SeqMgrClearFeatureIndexes (entityID, NULL);
}
static void NPStoGPS (
SeqEntryPtr sep,
Uint2 entityID,
CharPtr filename,
N2GPtr ngp,
SeqDescrPtr descr
)
{
BioSourcePtr biop;
BioseqPtr bsp;
BioseqSetPtr bssp;
SeqFeatPtr cds, gene, mrna, sfp, lastsfp;
Int2 ctr = 1;
SeqMgrFeatContext fcontext, gcontext, mcontext;
GeneRefPtr grp;
Int4Ptr iptr;
SeqEntryPtr old, top;
CharPtr organism;
OrgRefPtr orp;
Uint2 parenttype;
Pointer parentptr;
SeqAnnotPtr sap, annot, lastsap;
SeqDescrPtr sdp;
if (sep == NULL || sep->choice != 2) return;
bssp = (BioseqSetPtr) sep->data.ptrvalue;
if (bssp == NULL) return;
/* recursively visit components of genbank set */
if (bssp->_class == BioseqseqSet_class_genbank) {
for (sep = bssp->seq_set; sep != NULL; sep = sep->next) {
NPStoGPS (sep, entityID, filename, ngp, descr);
}
return;
}
if (bssp->_class != BioseqseqSet_class_nuc_prot) return;
if (SeqMgrFeaturesAreIndexed (entityID) == 0) {
SeqMgrIndexFeatures (entityID, NULL);
}
top = sep;
if (StringHasNoText (filename)) {
filename = "?";
}
bsp = FindNucBioseq (top);
if (bsp == NULL) {
Message (MSG_OK, "Unable to find nucleotide Bioseq in %s", filename);
if (ngp != NULL) {
ngp->failure = TRUE;
}
return;
}
GetSeqEntryParent (top, &parentptr, &parenttype);
bssp->_class = BioseqseqSet_class_gen_prod_set;
/* move feature table from nuc-prot set to nucleotide bioseq */
sap = bssp->annot;
bssp->annot = NULL;
annot = bsp->annot;
if (annot == NULL) {
bsp->annot = sap;
} else if (sap == NULL) {
} else if (sap->next == NULL && annot->next == NULL &&
sap->type == 1 && annot->type == 1 &&
sap->data != NULL && annot->data != NULL) {
lastsfp = (SeqFeatPtr) annot->data;
while (lastsfp->next != NULL) {
lastsfp = lastsfp->next;
}
sfp = (SeqFeatPtr) sap->data;
lastsfp->next = sfp;
sap->data = NULL;
SeqAnnotFree (sap);
} else {
lastsap = bsp->annot;
while (lastsap->next != NULL) {
lastsap = lastsap->next;
}
lastsap->next = sap;
}
old = SeqEntrySetScope (top);
ctr = (Int2) VisitBioseqsInSep (top, NULL, NULL) + 1;
if (ngp->byTscriptID) {
/* use orig_protein_id and orig_transcript_id gbquals */
mrna = SeqMgrGetNextFeature (bsp, NULL, 0, FEATDEF_mRNA, &fcontext);
while (mrna != NULL) {
cds = GetCDSByProteinID (mrna);
if (cds != NULL) {
if (ReciprocalTranscriptIDs (cds, mrna)) {
InstantiateMrnaIntoProt (cds, mrna, &ctr, ngp);
RemoveOrigIDs (cds);
RemoveOrigIDs (mrna);
} else {
Message (MSG_POSTERR, "CDS and mRNA have non-reciprocal protein_id/transcript_id qualifiers");
}
}
mrna = SeqMgrGetNextFeature (bsp, mrna, 0, FEATDEF_mRNA, &fcontext);
}
} else if (ngp->byFeatID) {
/* trust feature ID cross-references */
cds = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_CDREGION, 0, &fcontext);
while (cds != NULL) {
mrna = GetmRNAByFeatureID (cds);
if (mrna != NULL) {
InstantiateMrnaIntoProt (cds, mrna, &ctr, ngp);
}
cds = SeqMgrGetNextFeature (bsp, cds, SEQFEAT_CDREGION, 0, &fcontext);
}
} else {
/* count mRNAs per overlapping gene */
mrna = SeqMgrGetNextFeature (bsp, NULL, 0, FEATDEF_mRNA, &mcontext);
while (mrna != NULL) {
grp = SeqMgrGetGeneXref (mrna);
if (grp == NULL) {
gene = SeqMgrGetOverlappingGene (mrna->location, NULL);
if (gene != NULL) {
iptr = (Int4Ptr) gene->idx.scratch;
if (iptr == NULL) {
iptr = (Int4Ptr) MemNew (sizeof (Int4));
gene->idx.scratch = (Pointer) iptr;
}
if (iptr != NULL) {
(*iptr)++;
}
}
}
mrna = SeqMgrGetNextFeature (bsp, mrna, 0, FEATDEF_mRNA, &mcontext);
}
/* only leave genes with multiple mRNAs marked */
gene = SeqMgrGetNextFeature (bsp, NULL, SEQFEAT_GENE, 0, &gcontext);
while (gene != NULL) {
iptr = (Int4Ptr) gene->idx.scratch;
if (iptr != NULL) {
if (*iptr == 1) {
/* if count was 1, clear scratch */
gene->idx.scratch = MemFree (gene->idx.scratch);
} else {
/* if count was > 1, just reset count, do not clear scratch */
*iptr = 0;
}
}
gene = SeqMgrGetNextFeature (bsp, gene, SEQFEAT_GENE, 0, &gcontext);
}
/* make cDNA bioseq from mRNA feature, package with protein product */
LoopThroughCDSs (bsp, &ctr, ngp);
VisitFeaturesInSep (top, NULL, RemoveFeatScratch);
}
SeqMgrLinkSeqEntry (top, parenttype, parentptr);
SeqEntrySetScope (old);
SeqMgrClearFeatureIndexes (bssp->idx.entityID, NULL);
/* need to reindex to get mRNA and CDS features from cDNA and protein */
SeqMgrIndexFeatures (bssp->idx.entityID, NULL);
VisitSetsInSet (bssp, NULL, LclMakeNucProtCDS);
/* need to reindex before copying genes, instantiating protein titles */
SeqMgrIndexFeatures (bssp->idx.entityID, NULL);
VisitFeaturesInSep (top, NULL, LclCopyGene);
/* need to reindex before instantiating mRNA titles */
SeqMgrIndexFeatures (bssp->idx.entityID, NULL);
organism = NULL;
for (sdp = descr; sdp != NULL; sdp = sdp->next) {
if (sdp->choice != Seq_descr_source) continue;
biop = (BioSourcePtr) sdp->data.ptrvalue;
if (biop == NULL) continue;
orp = biop->org;
if (orp == NULL) continue;
if (! StringHasNoText (orp->taxname)) {
organism = orp->taxname;
}
}
if (ngp->smarttitle) {
LclMakeSmartRnaTitles (bsp, organism);
} else {
mrna = SeqMgrGetNextFeature (bsp, NULL, 0, FEATDEF_mRNA, &mcontext);
while (mrna != NULL) {
LclAddMrnaTitles (mrna->product, organism, ngp->refSeqTitles);
mrna = SeqMgrGetNextFeature (bsp, mrna, 0, FEATDEF_mRNA, &mcontext);
}
}
SeqMgrClearFeatureIndexes (bssp->idx.entityID, NULL);
move_cds (top);
}
static void ProcessOneRecord (
CharPtr filename,
Pointer userdata
)
{
AsnIoPtr aip;
BioseqPtr bsp;
ValNodePtr bsplist;
BioseqSetPtr bssp;
Pointer dataptr = NULL;
Uint2 datatype, entityID = 0;
SeqDescrPtr descr1, descr2;
Char file [FILENAME_MAX], id [42], path [PATH_MAX];
FILE *fp;
N2GPtr ngp;
SeqEntryPtr nsep, sep;
CharPtr ptr, str;
SeqIdPtr sip;
if (StringHasNoText (filename)) return;
ngp = (N2GPtr) userdata;
if (ngp == NULL) return;
fp = FileOpen (filename, "r");
if (fp == NULL) {
Message (MSG_POSTERR, "Failed to open '%s'", filename);
return;
}
dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, FALSE, FALSE);
FileClose (fp);
entityID = ObjMgrRegister (datatype, dataptr);
if (entityID < 1 || dataptr == NULL) {
Message (MSG_POSTERR, "Data read failed for input file '%s'", filename);
return;
}
if (datatype == OBJ_SEQSUB || datatype == OBJ_SEQENTRY ||
datatype == OBJ_BIOSEQ || datatype == OBJ_BIOSEQSET) {
sep = GetTopSeqEntryForEntityID (entityID);
if (sep == NULL) {
sep = SeqEntryNew ();
if (sep != NULL) {
if (datatype == OBJ_BIOSEQ) {
bsp = (BioseqPtr) dataptr;
sep->choice = 1;
sep->data.ptrvalue = bsp;
SeqMgrSeqEntry (SM_BIOSEQ, (Pointer) bsp, sep);
} else if (datatype == OBJ_BIOSEQSET) {
bssp = (BioseqSetPtr) dataptr;
sep->choice = 2;
sep->data.ptrvalue = bssp;
SeqMgrSeqEntry (SM_BIOSEQSET, (Pointer) bssp, sep);
} else {
sep = SeqEntryFree (sep);
}
}
sep = GetTopSeqEntryForEntityID (entityID);
}
if (sep != NULL) {
bsplist = NULL;
if (ngp->lock) {
bsplist = LockFarComponents (sep);
}
str = StringRChr (filename, DIRDELIMCHR);
if (str != NULL) {
str++;
} else {
str = filename;
}
StringCpy (file, str);
bsp = FindNucBioseq (sep);
id [0] = '\0';
if (bsp != NULL) {
sip = SeqIdFindWorst (bsp->id);
SeqIdWrite (sip, id, PRINTID_REPORT, sizeof (id));
Message (MSG_POST, "Processing %s (%s)", id, file);
}
SeqMgrIndexFeatures (entityID, NULL);
/* remove pubs and biosources from nuc-prot set and nucleotide bioseq */
nsep = FindNucSeqEntry (sep);
descr1 = ExtractBioSourceAndPubs (sep);
descr2 = ExtractBioSourceAndPubs (nsep);
if (descr1 == NULL) {
descr1 = descr2;
} else if (descr2 != NULL) {
ValNodeLink (&descr1, descr2);
}
/* convert from nuc-prot set to genomic-product set */
NPStoGPS (sep, entityID, file, ngp, descr1);
if (ngp->noncoding) {
PromoteNonCoding (sep, entityID, file, ngp, descr1);
}
if (ngp->byTscriptID) {
VisitFeaturesInSep (sep, NULL, RemoveOrigPrefix);
}
if (ngp->removeunnecxref) {
SeqMgrClearFeatureIndexes (entityID, NULL);
SeqMgrIndexFeatures (entityID, NULL);
VisitFeaturesInSep (sep, NULL, RemoveUnnecessaryGeneXrefs);
}
/* put pubs and biosources onto genomic-product set */
ReplaceBioSourceAndPubs (sep, descr1);
if (ngp->failure) {
Message (MSG_POST, "Not saving %s", file);
} else if (StringDoesHaveText (ngp->outfile)) {
aip = AsnIoOpen (ngp->outfile, "w");
if (aip != NULL) {
SeqEntryAsnWrite (sep, aip, NULL);
AsnIoClose (aip);
}
} else {
str = StringRChr (filename, DIRDELIMCHR);
if (str != NULL) {
*str = '\0';
str++;
} else {
str = filename;
}
if (StringDoesHaveText (ngp->results)) {
StringCpy (path, ngp->results);
} else {
StringCpy (path, filename);
}
if (str != NULL) {
ptr = StringRChr (str, '.');
if (ptr != NULL) {
*ptr = '\0';
}
StringCpy (file, str);
StringCat (file, ".gps");
FileBuildPath (path, NULL, file);
aip = AsnIoOpen (path, "w");
if (aip != NULL) {
SeqEntryAsnWrite (sep, aip, NULL);
AsnIoClose (aip);
}
}
}
bsplist = UnlockFarComponents (bsplist);
}
} else {
Message (MSG_POSTERR, "Datatype %d not recognized", (int) datatype);
}
ObjMgrFree (datatype, dataptr);
}
/* Args structure contains command-line arguments */
typedef enum {
p_argInputPath = 0,
r_argOutputPath,
i_argInputFile,
o_argOutputFile,
f_argFilter,
x_argSuffix,
R_argRemote,
L_argLockFar,
T_argUseTscriptID,
F_argUseFeatID,
P_argUseProtID,
G_argGeneralID,
D_argRefSeqTitles,
Q_argSmartTitle,
N_argNonCoding,
U_argUnnecXref
} Arguments;
Args myargs [] = {
{"Path to Files", NULL, NULL, NULL,
TRUE, 'p', ARG_STRING, 0.0, 0, NULL},
{"Path for Results", NULL, NULL, NULL,
TRUE, 'r', ARG_STRING, 0.0, 0, NULL},
{"Single Input File", "stdin", NULL, NULL,
TRUE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
{"Single Output File", "stdout", NULL, NULL,
TRUE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
{"Substring Filter", NULL, NULL, NULL,
TRUE, 'f', ARG_STRING, 0.0, 0, NULL},
{"File Selection Suffix", ".ent", NULL, NULL,
TRUE, 'x', ARG_STRING, 0.0, 0, NULL},
{"Remote Fetching from ID", "F", NULL, NULL,
TRUE, 'R', ARG_BOOLEAN, 0.0, 0, NULL},
{"Lock Components in Advance", "F", NULL, NULL,
TRUE, 'L', ARG_BOOLEAN, 0.0, 0, NULL},
{"Map by Transcript ID", "F", NULL, NULL,
TRUE, 'T', ARG_BOOLEAN, 0.0, 0, NULL},
{"Map by Feature ID", "F", NULL, NULL,
TRUE, 'F', ARG_BOOLEAN, 0.0, 0, NULL},
{"mRNA ID from Protein", "F", NULL, NULL,
TRUE, 'P', ARG_BOOLEAN, 0.0, 0, NULL},
{"General ID Database", NULL, NULL, NULL,
TRUE, 'G', ARG_STRING, 0.0, 0, NULL},
{"RefSeq mRNA Titles", "F", NULL, NULL,
TRUE, 'D', ARG_BOOLEAN, 0.0, 0, NULL},
{"Special mRNA Titles", "F", NULL, NULL,
TRUE, 'Q', ARG_BOOLEAN, 0.0, 0, NULL},
{"Promote Non-Coding RNAs", "F", NULL, NULL,
TRUE, 'N', ARG_BOOLEAN, 0.0, 0, NULL},
{"Remove Unnecessary Gene Xrefs", "F", NULL, NULL,
TRUE, 'U', ARG_BOOLEAN, 0.0, 0, NULL},
};
Int2 Main (void)
{
Char app [64], genDbAndQual [128];
CharPtr directory, filter, infile, outfile, ptr, results, suffix;
N2GData ngd;
Boolean remote;
/* standard setup */
ErrSetFatalLevel (SEV_MAX);
ErrClearOptFlags (EO_SHOW_USERSTR);
ErrSetLogfile ("stderr", ELOG_APPEND);
UseLocalAsnloadDataAndErrMsg ();
ErrPathReset ();
if (! AllObjLoad ()) {
Message (MSG_FATAL, "AllObjLoad failed");
return 1;
}
if (! SubmitAsnLoad ()) {
Message (MSG_FATAL, "SubmitAsnLoad failed");
return 1;
}
if (! FeatDefSetLoad ()) {
Message (MSG_FATAL, "FeatDefSetLoad failed");
return 1;
}
if (! SeqCodeSetLoad ()) {
Message (MSG_FATAL, "SeqCodeSetLoad failed");
return 1;
}
if (! GeneticCodeTableLoad ()) {
Message (MSG_FATAL, "GeneticCodeTableLoad failed");
return 1;
}
/* process command line arguments */
sprintf (app, "nps2gps %s", NPS2GPSAPPLICATION);
if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
return 0;
}
MemSet ((Pointer) &ngd, 0, sizeof (N2GData));
ngd.failure = FALSE;
ngd.lock = (Boolean) myargs [L_argLockFar].intvalue;
ngd.byTscriptID = (Boolean) myargs [T_argUseTscriptID].intvalue;
ngd.byFeatID = (Boolean) myargs [F_argUseFeatID].intvalue;
ngd.useProtID = (Boolean) myargs [P_argUseProtID].intvalue;
ngd.refSeqTitles = (Boolean) myargs [D_argRefSeqTitles].intvalue;
ngd.smarttitle = (Boolean) myargs [Q_argSmartTitle].intvalue;
ngd.noncoding = (Boolean) myargs [N_argNonCoding].intvalue;
ngd.removeunnecxref = (Boolean) myargs [U_argUnnecXref].intvalue;
genDbAndQual [0] = '\0';
StringNCpy_0 (genDbAndQual, myargs [G_argGeneralID].strvalue, sizeof (genDbAndQual));
ngd.genDb = genDbAndQual;
ptr = StringChr (genDbAndQual, ':');
if (ptr != NULL) {
*ptr = '\0';
ptr++;
ngd.genQual = ptr;
}
directory = (CharPtr) myargs [p_argInputPath].strvalue;
results = (CharPtr) myargs [r_argOutputPath].strvalue;
if (StringHasNoText (results)) {
results = NULL;
}
infile = (CharPtr) myargs [i_argInputFile].strvalue;
outfile = (CharPtr) myargs [o_argOutputFile].strvalue;
filter = (CharPtr) myargs [f_argFilter].strvalue;
suffix = (CharPtr) myargs [x_argSuffix].strvalue;
remote = (Boolean) myargs [R_argRemote].intvalue;
/* register fetch function */
if (remote) {
PubSeqFetchEnable ();
}
/* process input file */
if (StringDoesHaveText (directory)) {
ngd.results = results;
DirExplore (directory, filter, suffix, TRUE, ProcessOneRecord, (Pointer) &ngd);
} else if (StringDoesHaveText (infile) && StringDoesHaveText (outfile)) {
ngd.outfile = outfile;
ProcessOneRecord (infile, (Pointer) &ngd);
}
/* close fetch function */
if (remote) {
PubSeqFetchDisable ();
}
if (ngd.failure) return 1;
return 0;
}
|