~ubuntu-branches/ubuntu/saucy/ncbi-tools6/saucy

« back to all changes in this revision

Viewing changes to demo/replicon.c

  • Committer: Package Import Robot
  • Author(s): Aaron M. Ucko, Andreas Tille
  • Date: 2012-06-24 22:54:29 UTC
  • mfrom: (15.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20120624225429-b91zwa0d9xanjlqb
Tags: 6.1.20120620-2
[ Andreas Tille ]
debian/upstream: Strings containing ': ' need to be quoted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*   replicon.c
 
2
* ===========================================================================
 
3
*
 
4
*                            PUBLIC DOMAIN NOTICE
 
5
*            National Center for Biotechnology Information (NCBI)
 
6
*
 
7
*  This software/database is a "United States Government Work" under the
 
8
*  terms of the United States Copyright Act.  It was written as part of
 
9
*  the author's official duties as a United States Government employee and
 
10
*  thus cannot be copyrighted.  This software/database is freely available
 
11
*  to the public for use. The National Library of Medicine and the U.S.
 
12
*  Government do not place any restriction on its use or reproduction.
 
13
*  We would, however, appreciate having the NCBI and the author cited in
 
14
*  any work or product based on this material
 
15
*
 
16
*  Although all reasonable efforts have been taken to ensure the accuracy
 
17
*  and reliability of the software and data, the NLM and the U.S.
 
18
*  Government do not and cannot warrant the performance or results that
 
19
*  may be obtained by using this software or data. The NLM and the U.S.
 
20
*  Government disclaim all warranties, express or implied, including
 
21
*  warranties of performance, merchantability or fitness for any particular
 
22
*  purpose.
 
23
*
 
24
* ===========================================================================
 
25
*
 
26
* File Name:  replicon.c
 
27
*
 
28
* Author:  Colleen Bollin
 
29
*
 
30
* Version Creation Date:   Feb. 1, 2012
 
31
*
 
32
* $Revision: 1.4 $
 
33
*
 
34
* File Description:
 
35
*
 
36
* Modifications:
 
37
* --------------------------------------------------------------------------
 
38
* Date     Name        Description of modification
 
39
* -------  ----------  -----------------------------------------------------
 
40
*
 
41
*
 
42
* ==========================================================================
 
43
*/
 
44
 
 
45
#include <ncbi.h>
 
46
#include <objall.h>
 
47
#include <objsset.h>
 
48
#include <objsub.h>
 
49
#include <objfdef.h>
 
50
#include <sequtil.h>
 
51
#include <salutil.h>
 
52
#include <edutil.h>
 
53
#include <seqport.h>
 
54
#include <gather.h>
 
55
#include <sqnutils.h>
 
56
#include <subutil.h>
 
57
#include <toasn3.h>
 
58
#include <valid.h>
 
59
#include <asn2gnbk.h>
 
60
#include <explore.h>
 
61
#include <tofasta.h>
 
62
#include <simple.h>
 
63
#include <suggslp.h>
 
64
#include <toporg.h>
 
65
#include <aliparse.h>
 
66
#include <util/creaders/alnread.h>
 
67
#include <pmfapi.h>
 
68
#include <tax3api.h>
 
69
#ifdef INTERNAL_NCBI_TBL2ASN
 
70
#include <accpubseq.h>
 
71
#endif
 
72
#define NLM_GENERATED_CODE_PROTO
 
73
#include <objmacro.h>
 
74
#include <macroapi.h>
 
75
 
 
76
#define REPLICON_APP_VER "1.0"
 
77
 
 
78
CharPtr REPLICON_APPLICATION = REPLICON_APP_VER;
 
79
 
 
80
 
 
81
 
 
82
static void PopulateRepliconIdBuf (BioseqPtr bsp, CharPtr buf, Int4 buf_size)
 
83
{
 
84
  SeqIdPtr sip_local = NULL, sip_general = NULL, sip, sip_next;
 
85
 
 
86
  sip = bsp->id;
 
87
  while (sip != NULL && sip_local == NULL) {
 
88
    if (sip->choice == SEQID_LOCAL) {
 
89
      sip_local = sip;
 
90
    } else if (sip->choice == SEQID_GENERAL) {
 
91
      sip_general = sip;
 
92
    }
 
93
    sip = sip->next;
 
94
  }
 
95
  sip = NULL;
 
96
  if (sip_local != NULL) {
 
97
    sip = sip_local;
 
98
  } else if (sip_general != NULL) {
 
99
    sip = sip_general;
 
100
  } else {
 
101
    sip = SeqIdFindBest (bsp->id, SEQID_GENBANK);
 
102
  }
 
103
 
 
104
  sip_next = sip->next;
 
105
  sip->next = NULL;
 
106
  SeqIdWrite (sip, buf, PRINTID_FASTA_LONG, buf_size - 1);
 
107
  sip->next = sip_next;
 
108
}
 
109
 
 
110
 
 
111
typedef struct tablefiles {
 
112
  FILE *complete;
 
113
  FILE *incomplete;
 
114
  ValNodePtr chr_list;
 
115
} TableFilesData, PNTR TableFilesPtr;
 
116
 
 
117
 
 
118
static void MakeTable (BioseqPtr bsp, Pointer data)
 
119
{
 
120
  SeqMgrDescContext context;
 
121
  SeqDescPtr sdp;
 
122
  BioSourcePtr biop;
 
123
  Char         buf[PATH_MAX];
 
124
  CharPtr      chr_name = "ANONYMOUS";
 
125
  CharPtr      loc_str  = "UNKNOWN";
 
126
  CharPtr      type_str = "UNKNOWN";
 
127
  MolInfoPtr   mip;
 
128
  TableFilesPtr t;
 
129
  CharPtr      col3fmt = "%s\t%s\t%s\n";
 
130
  CharPtr      col3;
 
131
  Int4         len;
 
132
 
 
133
  if (bsp == NULL || ISA_aa(bsp->mol)) {
 
134
    return;
 
135
  }
 
136
  t = (TableFilesPtr) data;
 
137
 
 
138
  PopulateRepliconIdBuf (bsp, buf, sizeof (buf));
 
139
 
 
140
  sdp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_source, &context);
 
141
  if (sdp == NULL || (biop = (BioSourcePtr) sdp->data.ptrvalue) == NULL) {
 
142
    printf ("ERROR! No BioSource for %s\n", buf);
 
143
  } else {
 
144
    chr_name = GetRepliconChromosomeName (biop);
 
145
    loc_str = GetRepliconLocation (biop);
 
146
    type_str = GetRepliconType (biop);
 
147
    sdp = SeqMgrGetNextDescriptor (bsp, NULL, Seq_descr_molinfo, &context);
 
148
    if (sdp == NULL || (mip = (MolInfoPtr) sdp->data.ptrvalue) == NULL || mip->completeness != 1) {
 
149
      /* not complete - looking for organelles */
 
150
      if (chr_name != NULL) {
 
151
        if (loc_str == NULL || type_str == NULL) {
 
152
          printf ("ERROR! Unrecognized BioSource.genome value!\n");
 
153
        } else {
 
154
          fprintf (t->incomplete, "%s\t%s\n", buf, chr_name);
 
155
          len = StringLen (col3fmt) + StringLen (chr_name) + StringLen (loc_str) + StringLen (type_str);
 
156
          col3 = (CharPtr) MemNew (sizeof (Char) * len);
 
157
          sprintf (col3, col3fmt, chr_name, loc_str, type_str);
 
158
          ValNodeAddPointer (&(t->chr_list), 0, col3);
 
159
        }
 
160
      }
 
161
    } else {
 
162
      /* complete */
 
163
      if (chr_name == NULL || loc_str == NULL || type_str == NULL) {
 
164
        printf ("ERROR! Unrecognized BioSource.genome value!\n");
 
165
      } else if (t != NULL && t->complete != NULL) {  
 
166
        fprintf (t->complete, "%s\t%s\t%s\t%s\n", buf, chr_name, loc_str, type_str);
 
167
      } else {
 
168
        printf ("%s\t%s\t%s\t%s\n", buf, chr_name, loc_str, type_str);
 
169
      }
 
170
    }
 
171
    chr_name = MemFree (chr_name);
 
172
    loc_str = MemFree (loc_str);
 
173
    type_str = MemFree (type_str);
 
174
  }
 
175
}
 
176
/* Args structure contains command-line arguments */ 
 
177
typedef enum {
 
178
  i_argInputFile = 0,
 
179
  c_argCompleteOuputFile,
 
180
  o_argIncompleteOrgFile,
 
181
  s_argIncompleteSeqFile
 
182
} Arguments;
 
183
 
 
184
 
 
185
Args myargs [] = {
 
186
  {"File List File", NULL, NULL, NULL,
 
187
    TRUE, 'i', ARG_FILE_IN, 0.0, 0, NULL},
 
188
  {"Complete Output File", NULL, NULL, NULL,
 
189
    TRUE, 'c', ARG_FILE_OUT, 0.0, 0, NULL},
 
190
  {"Incomplete Org Output File", NULL, NULL, NULL,
 
191
    TRUE, 'o', ARG_FILE_OUT, 0.0, 0, NULL},
 
192
  {"Incomplete Seq Output File", NULL, NULL, NULL,
 
193
    TRUE, 's', ARG_FILE_OUT, 0.0, 0, NULL},
 
194
};
 
195
 
 
196
Int2 Main (void)
 
197
 
 
198
{
 
199
  Char        app [64];
 
200
  CharPtr     input_file, complete_output_file, incomplete_org_file, incomplete_seq_file;
 
201
  FILE *      fp;
 
202
  FILE *      fi;
 
203
  Pointer     dataptr;
 
204
  Uint2       datatype;
 
205
  TableFilesData t;
 
206
  ValNodePtr     vnp;
 
207
  Int4           i;
 
208
  ReadBufferData rbd;
 
209
  CharPtr        line;
 
210
 
 
211
  /* standard setup */
 
212
 
 
213
  ErrSetFatalLevel (SEV_MAX);
 
214
  ErrSetMessageLevel (SEV_MAX);
 
215
  ErrClearOptFlags (EO_SHOW_USERSTR);
 
216
  UseLocalAsnloadDataAndErrMsg ();
 
217
  ErrPathReset ();
 
218
 
 
219
  /* finish resolving internal connections in ASN.1 parse tables */
 
220
 
 
221
  if (! AllObjLoad ()) {
 
222
    Message (MSG_FATAL, "AllObjLoad failed");
 
223
    return 1;
 
224
  }
 
225
  if (! SubmitAsnLoad ()) {
 
226
    Message (MSG_FATAL, "SubmitAsnLoad failed");
 
227
    return 1;
 
228
  }
 
229
  if (! FeatDefSetLoad ()) {
 
230
    Message (MSG_FATAL, "FeatDefSetLoad failed");
 
231
    return 1;
 
232
  }
 
233
  if (! SeqCodeSetLoad ()) {
 
234
    Message (MSG_FATAL, "SeqCodeSetLoad failed");
 
235
    return 1;
 
236
  }
 
237
  if (! GeneticCodeTableLoad ()) {
 
238
    Message (MSG_FATAL, "GeneticCodeTableLoad failed");
 
239
    return 1;
 
240
  }
 
241
 
 
242
 
 
243
  /* process command line arguments */
 
244
  sprintf (app, "replicon %s", REPLICON_APPLICATION);
 
245
  if (! GetArgs (app, sizeof (myargs) / sizeof (Args), myargs)) {
 
246
    return 0;
 
247
  }
 
248
 
 
249
  input_file = (CharPtr) myargs [i_argInputFile].strvalue;
 
250
  complete_output_file = (CharPtr) myargs [c_argCompleteOuputFile].strvalue;
 
251
  incomplete_org_file = (CharPtr) myargs [o_argIncompleteOrgFile].strvalue;
 
252
  incomplete_seq_file = (CharPtr) myargs [s_argIncompleteSeqFile].strvalue;
 
253
 
 
254
  if (StringHasNoText (input_file)) {
 
255
    Message (MSG_FATAL, "Must supply input file.");
 
256
    return 1;
 
257
  }
 
258
  if (StringHasNoText (complete_output_file)) {
 
259
    Message (MSG_FATAL, "Must supply filename for complete replicons.");
 
260
    return 1;
 
261
  }
 
262
  if (StringHasNoText (incomplete_org_file)) {
 
263
    Message (MSG_FATAL, "Must supply filename for list of incomplete replicon sources.");
 
264
    return 1;
 
265
  }
 
266
  if (StringHasNoText (incomplete_seq_file)) {
 
267
    Message (MSG_FATAL, "Must supply filename for list of incomplete replicon sequences.");
 
268
    return 1;
 
269
  }
 
270
  
 
271
 
 
272
  t.complete = FileOpen (complete_output_file, "w");
 
273
  if (t.complete == NULL) {
 
274
    Message (MSG_FATAL, "Unable to open %s", complete_output_file);
 
275
    return 1;
 
276
  }
 
277
  t.incomplete = FileOpen (incomplete_seq_file, "w");
 
278
  if (t.incomplete == NULL) {
 
279
    Message (MSG_FATAL, "Unable to open %s", incomplete_seq_file);
 
280
    return 1;
 
281
  }
 
282
  t.chr_list = NULL;
 
283
  
 
284
  fi = FileOpen (input_file, "r");
 
285
  if (fi == NULL) {
 
286
    Message (MSG_FATAL, "Unable to open %s", input_file);
 
287
    return 1;
 
288
  }
 
289
 
 
290
  rbd.fp = fi;
 
291
  rbd.current_data = NULL;
 
292
 
 
293
  line = AbstractReadFunction (&rbd);
 
294
  while (line != NULL) {
 
295
    fp = FileOpen (line, "r");
 
296
    if (fp == NULL) {
 
297
      Message (MSG_FATAL, "Unable to open %s", line);
 
298
      return 1;
 
299
    }
 
300
    while ((dataptr = ReadAsnFastaOrFlatFile (fp, &datatype, NULL, FALSE, FALSE, TRUE, FALSE)) != NULL) {
 
301
      switch (datatype) {
 
302
        case OBJ_SEQENTRY:
 
303
          VisitBioseqsInSep ((SeqEntryPtr) dataptr, &t, MakeTable);
 
304
          break;
 
305
        case OBJ_BIOSEQSET:
 
306
          VisitBioseqsInSet ((BioseqSetPtr) dataptr, &t, MakeTable);
 
307
          break;
 
308
        case OBJ_BIOSEQ:
 
309
          MakeTable ((BioseqPtr) dataptr, &t);
 
310
          break;
 
311
        default:
 
312
          Message (MSG_ERROR, "Unrecognized data type %d", datatype);
 
313
          break;
 
314
      }
 
315
      ObjMgrFree (datatype, dataptr);
 
316
    }
 
317
    FileClose (fp);
 
318
    line = AbstractReadFunction (&rbd);
 
319
  }
 
320
  FileClose (fi);
 
321
 
 
322
  FileClose (t.complete);
 
323
  FileClose (t.incomplete);
 
324
  fp = FileOpen (incomplete_org_file, "w");
 
325
  if (fp == NULL) {
 
326
    Message (MSG_FATAL, "Unable to open %s", incomplete_org_file);
 
327
    return 1;
 
328
  }
 
329
  t.chr_list = ValNodeSort (t.chr_list, SortVnpByString);
 
330
  ValNodeUnique (&(t.chr_list), SortVnpByString, ValNodeFreeData);
 
331
  for (vnp = t.chr_list; vnp != NULL; vnp = vnp->next) {
 
332
    fprintf (fp, "%s", (CharPtr)vnp->data.ptrvalue);
 
333
  }
 
334
  FileClose (fp);
 
335
  return 0;
 
336
}
 
337