File: prep.c

package info (click to toggle)
loki 2.4.7.4-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,172 kB
  • sloc: ansic: 38,653; yacc: 4,974; lex: 946; makefile: 333; sh: 100
file content (161 lines) | stat: -rw-r--r-- 4,840 bytes parent folder | download | duplicates (7)
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
/****************************************************************************
 *                                                                          *
 *     Loki - Programs for genetic analysis of complex traits using MCMC    *
 *                                                                          *
 *             Simon Heath - University of Washington                       *
 *                                                                          *
 *                       March 1997                                         *
 *                                                                          *
 * prep.c:                                                                  *
 *                                                                          *
 * Data preperation - reading control file, reading datafiles, recoding     *
 * factorial data, and writing out binary datafiles for loki                *
 *                                                                          *
 * Copyright (C) Simon C. Heath 1997, 2000, 2002                            *
 * This is free software.  You can distribute it and/or modify it           *
 * under the terms of the Modified BSD license, see the file COPYING        *
 *                                                                          *
 ****************************************************************************/

#include <config.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

#include "version.h"
#include "ranlib.h"
#include "utils.h"
#include "libhdr.h"
#include "scan.h"
#include "control_parse.h"
#include "compat/compat.h"
#include "getopt.h"

unsigned int RunID;
int nrm_flag,strip_vars;
loki_time lt;
static int error_check=1;

static char *LogFile;

void print_version_and_exit(void)
{
	(void)printf("%s\n",PREP_NAME);
	exit(EXIT_SUCCESS);
}

#ifdef FUNC_NAME
#undef FUNC_NAME
#endif
#define FUNC_NAME "process_loki"
static int process_loki(int argc,char *argv[])
{
	FILE *fptr;
	int err,i,c,ec_flag=0;
	
	while((c=getopt(argc,argv,"evX:d:p:"))!=-1) switch(c) {
	 case 'e':
		error_check=0;
		ec_flag=1;
		break;
	 case 'v':
		print_version_and_exit();
		break; /* Previous command never returns */
	 case 'd':
		if((i=set_file_dir(optarg))) {
			fprintf(stderr,"Error setting default file directory: %s\n",i==UTL_BAD_STAT?strerror(errno):utl_error(i));
			exit(EXIT_FAILURE);
		}
		break;
	 case 'p':
		if((i=set_file_prefix(optarg))) {
			fprintf(stderr,"Error setting default file prefix: %s\n",utl_error(i));
			exit(EXIT_FAILURE);
		}
		break;
	 case 'X':
		fputs("-X option must occur as first argument\n",stderr);
		exit(EXIT_FAILURE);
	}
	if(optind>=argc) abt(__FILE__,__LINE__,"No control file specified\n");
	init_stuff(&LogFile);
	if((fptr=fopen(argv[optind],"r"))) err=ReadControl(fptr,argv[optind],&LogFile);
	else {
		(void)printf("Couldn't open '%s' for input as control file\nAborting...\n",argv[optind]);
		exit(EXIT_FAILURE);
	}
	(void)fclose(fptr);
	if(!err) {
		if(getseed("seedfile",0)) init_ranf(135421);
		RunID=(unsigned int)(ranf()*(double)0xffffffffU);
		if(!ec_flag) error_check=syst_var[ERROR_CHECK];
		print_start_time(PREP_NAME,"w",LogFile,&lt);
		if(!scan_error_n) ReadData(LogFile);          /* Read in the datafile(s) and recode (where necessary) */
	}
	return err;
}

#ifdef FUNC_NAME
#undef FUNC_NAME
#endif
#define FUNC_NAME "main"
int main(int argc,char *argv[])
{
	int err,type=LOKI_FORMAT;
	
	lt.start_time=time(0);
	if(argc>1) {
		if(*argv[1]=='-' && argv[1][1]=='X') {
			type=check_format(argv[1]);
			optind=2;
			optreset=1;
		}
	}
	switch(type) {
	 case LOKI_FORMAT:
		err=process_loki(argc,argv);
		break;
	 case QTDT_FORMAT:
		err=process_qtdt(argc,argv,&LogFile,&error_check,&lt);
		break;
	 default:
		fputs("Input type not yet handled\n",stderr);
		exit(EXIT_FAILURE);
	}
	if(err) {
		LogFile=0;
		exit(EXIT_FAILURE);
	}
	if(!pruned_ped_size) {
		(void)printf("Zero size pedigree\nAborting...\n");
		exit(EXIT_FAILURE);
	}
	if(!scan_error_n)	{
		InitFamilies(LogFile);
		count_loops(LogFile);
		check_inbreeding(LogFile);
		check_ymark();
	}
	if(!scan_error_n && (traitlocus || n_markers)) err=Genotype_Elimination(syst_var[CORRECT_ERRORS],LogFile,error_check);
	if(!scan_error_n && !err) nrm_flag=Calculate_NRM(LogFile);
	(void)writeseed("seedfile",1);
	if(family) free(family);
	if(!scan_error_n && !err && nrm_flag>=0) {
		WriteData(LogFile);
/*		WriteXMLData(LogFile); */
		WriteReport(LogFile);
	}
	free_nodes();
	if(scan_error_n) (void)fprintf(stderr,"Errors: %d  ",scan_error_n);
	if(scan_warn_n) (void)fprintf(stderr,"Warnings: %d  ",scan_warn_n);
	if(scan_error_n || scan_warn_n) (void)fprintf(stderr,"\n");
	return sig_caught;
}