~ubuntu-branches/ubuntu/edgy/ncbi-tools6/edgy

« back to all changes in this revision

Viewing changes to demo/cleanasn.c

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-07-19 23:28:07 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060719232807-et3cdmcjgmnyleyx
Tags: 6.1.20060507-3ubuntu1
Re-merge with Debian

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
*
30
30
* Version Creation Date:   10/19/99
31
31
*
32
 
* $Revision: 6.1 $
 
32
* $Revision: 6.3 $
33
33
*
34
34
* File Description: 
35
35
*
45
45
#include <ncbi.h>
46
46
#include <objall.h>
47
47
#include <objsset.h>
 
48
#include <objfdef.h>
48
49
#include <objsub.h>
49
50
#include <sequtil.h>
50
51
#include <sqnutils.h>
 
52
#include <explore.h>
51
53
#include <toasn3.h>
52
 
 
53
 
static void CleanupOneRecord (CharPtr directory, CharPtr filename)
 
54
#include <pmfapi.h>
 
55
#include <tax3api.h>
 
56
#ifdef INTERNAL_NCBI_CLEANASN
 
57
#include <accpubseq.h>
 
58
#endif
 
59
 
 
60
#define CLEANASN_APP_VER "1.1"
 
61
 
 
62
CharPtr CLEANASN_APPLICATION = CLEANASN_APP_VER;
 
63
 
 
64
static void RemoveFeatUser (
 
65
  SeqFeatPtr sfp,
 
66
  Pointer userdata
 
67
)
 
68
 
 
69
{
 
70
  if (sfp == NULL) return;
 
71
  if (sfp->ext != NULL) {
 
72
    sfp->ext = UserObjectFree (sfp->ext);
 
73
  }
 
74
}
 
75
 
 
76
static void RemoveFeatDbxref (
 
77
  SeqFeatPtr sfp,
 
78
  Pointer userdata
 
79
)
 
80
 
 
81
{
 
82
  DbtagPtr    dbt;
 
83
  ValNodePtr  next, vnp;
 
84
 
 
85
  if (sfp == NULL) return;
 
86
  for (vnp = sfp->dbxref; vnp != NULL; vnp = next) {
 
87
    next = vnp->next;
 
88
    dbt = (DbtagPtr) vnp->data.ptrvalue;
 
89
    DbtagFree (dbt);
 
90
    MemFree (vnp);
 
91
  }
 
92
  sfp->dbxref = NULL;
 
93
}
 
94
 
 
95
typedef struct dummysmfedata {
 
96
  Int4  max;
 
97
  Int4  num_at_max;
 
98
} DummySmfeData, PNTR DummySmfePtr;
 
99
 
 
100
static Boolean LIBCALLBACK CADummySMFEProc (
 
101
  SeqFeatPtr sfp,
 
102
  SeqMgrFeatContextPtr context
 
103
)
 
104
 
 
105
 
 
106
{
 
107
  DummySmfePtr  dsp;
 
108
  Int4          len;
 
109
 
 
110
  if (sfp == NULL || context == NULL) return TRUE;
 
111
  dsp = context->userdata;
 
112
  if (dsp == NULL) return TRUE;
 
113
 
 
114
  len = SeqLocLen (sfp->location);
 
115
  if (len < dsp->max) {
 
116
    dsp->max = len;
 
117
    dsp->num_at_max = 1;
 
118
  } else if (len == dsp->max) {
 
119
    (dsp->num_at_max)++;
 
120
  }
 
121
 
 
122
  return TRUE;
 
123
}
 
124
 
 
125
static void RemoveUnnecGeneXref (
 
126
  SeqFeatPtr sfp,
 
127
  Pointer userdata
 
128
)
 
129
 
 
130
{
 
131
  Int2                 count;
 
132
  SeqFeatXrefPtr       curr, next;
 
133
  DummySmfeData        dsd;
 
134
  SeqMgrFeatContext    fcontext;
 
135
  SeqFeatXrefPtr PNTR  last;
 
136
  GeneRefPtr           grp, grpx;
 
137
  SeqFeatPtr           sfpx;
 
138
  CharPtr              syn1, syn2;
 
139
 
 
140
  if (sfp == NULL || sfp->data.choice == SEQFEAT_GENE) return;
 
141
  grp = SeqMgrGetGeneXref (sfp);
 
142
  if (grp == NULL || SeqMgrGeneIsSuppressed (grp)) return;
 
143
  sfpx = SeqMgrGetOverlappingGene (sfp->location, &fcontext);
 
144
  if (sfpx == NULL || sfpx->data.choice != SEQFEAT_GENE) return;
 
145
  grpx = (GeneRefPtr) sfpx->data.value.ptrvalue;
 
146
  if (grpx == NULL) return;
 
147
 
 
148
  if ((StringDoesHaveText (grp->locus)) &&
 
149
       (StringDoesHaveText (grpx->locus))) {
 
150
    if ((StringICmp (grp->locus, grpx->locus) != 0)) return;
 
151
  } else if (StringDoesHaveText (grp->locus_tag) &&
 
152
             StringDoesHaveText (grp->locus_tag)) {
 
153
    if ((StringICmp (grp->locus_tag, grpx->locus_tag) != 0)) return;
 
154
  } else if (grp->syn != NULL && grpx->syn != NULL) {
 
155
    syn1 = (CharPtr) grp->syn->data.ptrvalue;
 
156
    syn2 = (CharPtr) grpx->syn->data.ptrvalue;
 
157
    if ((StringDoesHaveText (syn1)) && (StringDoesHaveText (syn2))) {
 
158
      if ((StringICmp (syn1, syn2) != 0)) return;
 
159
    }
 
160
  }
 
161
 
 
162
  MemSet ((Pointer) &dsd, 0, sizeof (DummySmfeData));
 
163
  dsd.max = INT4_MAX;
 
164
  dsd.num_at_max = 0;
 
165
  count = SeqMgrGetAllOverlappingFeatures (sfp->location, FEATDEF_GENE,
 
166
                                           NULL, 0, LOCATION_SUBSET,
 
167
                                           (Pointer) &dsd, CADummySMFEProc);
 
168
 
 
169
  if (dsd.num_at_max < 2) {
 
170
    last = (SeqFeatXrefPtr PNTR) &(sfp->xref);
 
171
    curr = sfp->xref;
 
172
    while (curr != NULL) {
 
173
      next = curr->next;
 
174
      if (curr->data.choice == SEQFEAT_GENE) {
 
175
        *last = next;
 
176
        curr->next = NULL;
 
177
        SeqFeatXrefFree (curr);
 
178
      } else {
 
179
        last = &(curr->next);
 
180
      }
 
181
      curr = next;
 
182
    }
 
183
  }
 
184
}
 
185
 
 
186
static void CleanupOneRecord (
 
187
  CharPtr directory,
 
188
  CharPtr results,
 
189
  CharPtr filename,
 
190
  CharPtr clean,
 
191
  CharPtr link,
 
192
  CharPtr feat,
 
193
  Boolean taxon
 
194
)
54
195
 
55
196
{
56
197
  AsnIoPtr     aip;
69
210
  fp = FileOpen (path, "r");
70
211
  if (fp == NULL) return;
71
212
 
72
 
  dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID, FALSE, FALSE, TRUE, FALSE);
 
213
  dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, &entityID, FALSE,
 
214
                                    FALSE, TRUE, FALSE);
73
215
 
74
216
  FileClose (fp);
75
217
 
76
218
  sep = GetTopSeqEntryForEntityID (entityID);
77
219
  if (sep != NULL) {
78
 
    SeriousSeqEntryCleanup (sep, NULL, NULL);
 
220
 
 
221
    if (StringChr (clean, 'b') != NULL) {
 
222
      BasicSeqEntryCleanup (sep);
 
223
    }
 
224
    if (StringChr (clean, 's') != NULL) {
 
225
      SeriousSeqEntryCleanup (sep, NULL, NULL);
 
226
    }
 
227
 
 
228
    if (taxon) {
 
229
      Taxon3ReplaceOrgInSeqEntry (sep, FALSE);
 
230
    }
 
231
 
 
232
    if (StringChr (link, 'o') != NULL) {
 
233
      SeqMgrIndexFeatures (entityID, 0);
 
234
      LinkCDSmRNAbyOverlap (sep);
 
235
    }
 
236
    if (StringChr (link, 'p') != NULL) {
 
237
      SeqMgrIndexFeatures (entityID, 0);
 
238
      LinkCDSmRNAbyProduct (sep);
 
239
    }
 
240
    if (StringChr (link, 'r') != NULL) {
 
241
      SeqMgrIndexFeatures (entityID, 0);
 
242
      ReassignFeatureIDs (sep);
 
243
    }
 
244
 
 
245
    if (StringChr (feat, 'u') != NULL) {
 
246
      VisitFeaturesInSep (sep, NULL, RemoveFeatUser);
 
247
    }
 
248
    if (StringChr (feat, 'd') != NULL) {
 
249
      VisitFeaturesInSep (sep, NULL, RemoveFeatDbxref);
 
250
    }
 
251
    if (StringChr (feat, 'r') != NULL) {
 
252
      SeqMgrIndexFeatures (entityID, 0);
 
253
      VisitFeaturesInSep (sep, NULL, RemoveUnnecGeneXref);
 
254
    }
 
255
 
 
256
    StringNCpy_0 (path, results, sizeof (path));
 
257
    FileBuildPath (path, NULL, filename);
79
258
 
80
259
    aip = AsnIoOpen (path, "w");
81
260
    if (aip != NULL) {
92
271
  ObjMgrFreeByEntityID (entityID);
93
272
}
94
273
 
 
274
/* Args structure contains command-line arguments */
 
275
 
 
276
#define p_argInputPath      0
 
277
#define r_argOutputPath     1
 
278
#define c_argClean          2
 
279
#define l_argLink           3
 
280
#define f_argFeat           4
 
281
#define t_argTaxonLookup    5
 
282
#define R_argRemote         6
 
283
 
95
284
Args myargs [] = {
96
 
  {"Path to files", NULL, NULL, NULL,
97
 
    FALSE, 'p', ARG_STRING, 0.0, 0, NULL}
 
285
  {"Path to Files", NULL, NULL, NULL,
 
286
    FALSE, 'p', ARG_STRING, 0.0, 0, NULL},
 
287
  {"Path for Results", NULL, NULL, NULL,
 
288
    TRUE, 'r', ARG_STRING, 0.0, 0, NULL},
 
289
  {"Cleanup (b BasicSeqEntryCleanup, s SeriousSeqEntryCleanup)", NULL, NULL, NULL,
 
290
    TRUE, 'c', ARG_STRING, 0.0, 0, NULL},
 
291
  {"Link (o LinkCDSmRNAbyOverlap, p LinkCDSmRNAbyProduct, r ReassignFeatureIDs)", NULL, NULL, NULL,
 
292
    TRUE, 'l', ARG_STRING, 0.0, 0, NULL},
 
293
  {"Feature (u Remove User Object, d Remove db_xref, r Remove Redundant Gene xref)", NULL, NULL, NULL,
 
294
    TRUE, 'f', ARG_STRING, 0.0, 0, NULL},
 
295
  {"Taxonomy Lookup", "F", NULL, NULL,
 
296
    TRUE, 't', ARG_BOOLEAN, 0.0, 0, NULL},
 
297
  {"Remote Fetching from ID", "F", NULL, NULL,
 
298
    TRUE, 'R', ARG_BOOLEAN, 0.0, 0, NULL},
98
299
};
99
300
 
100
301
Int2 Main (void)
101
302
 
102
303
{
103
 
  ValNodePtr  dir, vnp;
 
304
  Char        app [64];
 
305
  CharPtr     clean, feat, link;
 
306
  CharPtr     directory, results;
 
307
  ValNodePtr  head, vnp;
 
308
  Boolean     remote, taxon;
 
309
 
 
310
  /* standard setup */
104
311
 
105
312
  ErrSetFatalLevel (SEV_MAX);
106
313
  ErrClearOptFlags (EO_SHOW_USERSTR);
107
314
  UseLocalAsnloadDataAndErrMsg ();
108
315
  ErrPathReset ();
109
316
 
 
317
  /* finish resolving internal connections in ASN.1 parse tables */
 
318
 
110
319
  if (! AllObjLoad ()) {
111
320
    Message (MSG_FATAL, "AllObjLoad failed");
112
321
    return 1;
115
324
    Message (MSG_FATAL, "SubmitAsnLoad failed");
116
325
    return 1;
117
326
  }
 
327
  if (! FeatDefSetLoad ()) {
 
328
    Message (MSG_FATAL, "FeatDefSetLoad failed");
 
329
    return 1;
 
330
  }
118
331
  if (! SeqCodeSetLoad ()) {
119
332
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
120
333
    return 1;
124
337
    return 1;
125
338
  }
126
339
 
127
 
  if (! GetArgs ("cleanasn", sizeof (myargs) / sizeof (Args), myargs)) {
128
 
    return 0;
129
 
  }
130
 
  if (StringHasNoText (myargs [0].strvalue)) {
131
 
    return 0;
132
 
  }
133
 
 
134
 
  dir = DirCatalog ((CharPtr) myargs [0].strvalue);
135
 
  for (vnp = dir; vnp != NULL; vnp = vnp->next) {
 
340
  /* process command line arguments */
 
341
 
 
342
  sprintf (app, "cleanasn %s", CLEANASN_APPLICATION);
 
343
  if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
 
344
    return 0;
 
345
  }
 
346
 
 
347
  directory = (CharPtr) myargs [p_argInputPath].strvalue;
 
348
  if (StringHasNoText (directory)) {
 
349
    Message (MSG_FATAL, "You must supply an input directory (-p).\nUse -p . to specify the current directory.\n\n");
 
350
    return 1;
 
351
  }
 
352
  results = (CharPtr) myargs [r_argOutputPath].strvalue;
 
353
  if (StringHasNoText (results)) {
 
354
    results = directory;
 
355
  }
 
356
 
 
357
  clean = myargs [c_argClean].strvalue;
 
358
  link = myargs [l_argLink].strvalue;
 
359
  feat = myargs [f_argFeat].strvalue;
 
360
 
 
361
  taxon = (Boolean) myargs [t_argTaxonLookup].intvalue;
 
362
  remote = (Boolean) myargs [R_argRemote].intvalue;
 
363
 
 
364
  if (remote) {
 
365
#ifdef INTERNAL_NCBI_CLEANASN
 
366
    if (! PUBSEQBioseqFetchEnable ("cleanasn", FALSE)) {
 
367
      Message (MSG_POSTERR, "PUBSEQBioseqFetchEnable failed");
 
368
      return 1;
 
369
    }
 
370
#else
 
371
    PubSeqFetchEnable ();
 
372
#endif
 
373
  }
 
374
 
 
375
  head = DirCatalog (directory);
 
376
  for (vnp = head; vnp != NULL; vnp = vnp->next) {
136
377
    if (vnp->choice == 0) {
137
 
      CleanupOneRecord (myargs [0].strvalue, (CharPtr) vnp->data.ptrvalue);
 
378
      CleanupOneRecord (directory, results,
 
379
                        (CharPtr) vnp->data.ptrvalue,
 
380
                        clean, link, feat, taxon);
138
381
    }
139
382
  }
140
 
  ValNodeFreeData (dir);
 
383
  ValNodeFreeData (head);
 
384
 
 
385
  if (remote) {
 
386
#ifdef INTERNAL_NCBI_CLEANASN
 
387
    PUBSEQBioseqFetchDisable ();
 
388
#else
 
389
    PubSeqFetchDisable ();
 
390
#endif
 
391
  }
141
392
 
142
393
  return 0;
143
394
}