コード例 #1
0
ファイル: sequences.cpp プロジェクト: bredelings/argweaver
Sites *arghmm_read_sites(const char *filename,
                         int subregion_start=-1, int subregion_end=-1)
{
    Sites *sites = new Sites();
    bool results = read_sites(filename, sites, subregion_start, subregion_end);
    if (!results) {
        delete sites;
        return NULL;
    }
    return sites;
}
コード例 #2
0
ファイル: sequences.cpp プロジェクト: bredelings/argweaver
// Read a Sites alignment file
bool read_sites(const char *filename, Sites *sites,
                int subregion_start, int subregion_end)
{
    FILE *infile;
    if ((infile = fopen(filename, "r")) == NULL) {
        printError("cannot read file '%s'", filename);
        return false;
    }

    bool result = read_sites(infile, sites, subregion_start, subregion_end);

    fclose(infile);

    return result;
}
コード例 #3
0
int main( int argc, char **argv)
{
/* Index default to -1 as flage for special human consideration */
int i,j,k,nrow, ncol, *sites, **site_members,nsites, nspecies,*table,index=-1,total_columns=0,column_differs=0,column_4d_differs=0,*spec_breakdown,same;
int **base_count,**bases_in_4d;
char **seq_name, **site_species, **species,*token,*spec_name,ref;

/* Needed for Webb's library functions */
argv0 = "analyze";
if ((argc != 3) && (argc != 4)  )
    print_usage();


/* Determine which species to use as reference */
if(argc == 4)
    {
    token = strtok(argv[3], "=");
    spec_name = strtok(NULL, " \n");
    }
else
    spec_name="human";

/* Read the alignment */
site_species = read_malign(argv[1], &nrow, &ncol, &seq_name);

/* Now read the 4d sites... */
site_members = read_sites(argv[2], &nspecies, &nsites, &species, &sites);

/* Make sure nspecies and nrow correspond -subtract one because of implicit human in 4d*/
if(nspecies != (nrow-1))
    fatal("Species in alignment must correspond to number of species in 4d sites");

/* Build conversion table */
table=ckalloc(nspecies * sizeof(int));

for(i=0;i<nspecies;i++)
    for(j=0;j<nrow;j++)
	if(SAME_STRING(species[i],seq_name[j]))
	    table[i]=j;


/* Build species breakdown table */
spec_breakdown=ckalloc(nspecies * sizeof(int));
	  
for(i=0;i<nspecies;i++)
    spec_breakdown[i]=0;

/* Find species */	   
if(!SAME_STRING(spec_name,"human")){
    for(i=0;i<nspecies;i++)
	if(SAME_STRING(species[i],spec_name))
	    index=i;

    if(index==-1)
	fatal("Species not found");

}


/* Build count tables */
base_count=ckalloc(ALPHA_SIZE * sizeof(int *));
for(i=0;i<ALPHA_SIZE;i++)
    base_count[i]=ckalloc((ALPHA_SIZE+1) * sizeof(int));

bases_in_4d=ckalloc(ALPHA_SIZE * sizeof(int *));
for(i=0;i<ALPHA_SIZE;i++)
    bases_in_4d[i]=ckalloc((ALPHA_SIZE+1) * sizeof(int)); 

/* Initialize base count arrays */
for(i=0;i<ALPHA_SIZE;i++)
    for(j=0;j<ALPHA_SIZE;j++)
	base_count[i][j]=(bases_in_4d[i][j]=0);

/* For each 4d site */
for(i=0;i<nsites;i++)
    {
    /* If the species is present... Special exception for human*/
    if(index==-1 || site_members[index][i])
	{
	/* Generate total bases */	
	
	total_columns++;
	same=1;
	
	if(index!=-1)
	    ref=toupper(site_species[table[index]][sites[i]]);
	else
	    ref=toupper(site_species[0][sites[i]]);
	
	/* Check for homogeneity */
	    for(j=0;j<nspecies;j++)
		{
		if(j==index)
		    continue;
		if(toupper(site_species[table[j]][sites[i]])!=ref)
		    same=0;
		}
	    if(!same)
		{
		column_differs++;
		/* Find mix of bases */
		for(j=0;j<nspecies;j++)
		    {
		    if(j==index)
			continue;
		    base_count[translate(ref)][translate(site_species[table[j]][sites[i]])]++;
		    }
		
		}
	    /* Check for homogeneity within 4d species */
	    same=1;
	    
	    for(j=0;j<nspecies;j++)
		{
		if(j==index)
		    continue;
		/* If species is present at site */
		if(site_members[j][i])
		    {
		    spec_breakdown[j]++;
		    if(toupper(site_species[table[j]][sites[i]]) != ref)
			{
			same=0;
			}
		    }
		}
	    if(!same)
		{
		column_4d_differs++;
		/* Find mix of bases */
		for(j=0;j<nspecies;j++)
		    {
		    if(j==index)
			continue;
		    if(site_members[j][i])
			bases_in_4d[translate(ref)][translate(site_species[table[j]][sites[i]])]++;
		    }
		
		}
	}
    }


printf("\n\n");
printf("Results of Bases Substitution at 4d sites.\n");
printf("------------------------------------------\n\n");

printf("Considering all species (%s as Reference)\n",spec_name);
printf("--------------------------------------------\n\n");
print_totals(total_columns, column_differs, &base_count, "Human");

printf("\nConsidering species which share the site (%s as Reference)\n",spec_name);
printf("-------------------------------------------------------------\n\n");
print_totals(total_columns, column_4d_differs, &bases_in_4d, "Human");
printf("\n");

/* Result of species breakdown */
for(i=0;i<nspecies;i++)
    {
    if(index==i)
	continue;
    printf("Species %-8s occured %5d (%2.2f %)\n",species[i],spec_breakdown[i],(float)(spec_breakdown[i] * 100)/total_columns);
    }
return 0;
}
コード例 #4
0
ファイル: main.c プロジェクト: rkrug/grass-ci
int main(int argc, char *argv[])
{
    int fd, maskfd;
    CELL *mask;
    DCELL *dcell;
    struct GModule *module;
    struct History history;
    int row, col;
    int searchrow, searchcolumn, pointsfound;
    int *shortlistrows = NULL, *shortlistcolumns = NULL;
    long ncells = 0;
    double north, east;
    double dist;
    double sum1, sum2, interp_value;
    int n;
    double p;
    struct
    {
        struct Option *input, *npoints, *power, *output, *dfield, *col;
    } parm;
    struct
    {
        struct Flag *noindex;
    } flag;
    struct cell_list
    {
        int row, column;
        struct cell_list *next;
    };
    struct cell_list **search_list = NULL, **search_list_start = NULL;
    int max_radius, radius;
    int searchallpoints = 0;
    char *tmpstr1, *tmpstr2;

    G_gisinit(argv[0]);

    module = G_define_module();
    G_add_keyword(_("vector"));
    G_add_keyword(_("surface"));
    G_add_keyword(_("interpolation"));
    G_add_keyword(_("IDW"));
    module->description =
        _("Provides surface interpolation from vector point data by Inverse "
          "Distance Squared Weighting.");

    parm.input = G_define_standard_option(G_OPT_V_INPUT);

    parm.dfield = G_define_standard_option(G_OPT_V_FIELD);

    parm.col = G_define_standard_option(G_OPT_DB_COLUMN);
    parm.col->required = NO;
    parm.col->label = _("Name of attribute column with values to interpolate");
    parm.col->description = _("If not given and input is 2D vector map then category values are used. "
                              "If input is 3D vector map then z-coordinates are used.");
    parm.col->guisection = _("Values");

    parm.output = G_define_standard_option(G_OPT_R_OUTPUT);

    parm.npoints = G_define_option();
    parm.npoints->key = "npoints";
    parm.npoints->key_desc = "count";
    parm.npoints->type = TYPE_INTEGER;
    parm.npoints->required = NO;
    parm.npoints->description = _("Number of interpolation points");
    parm.npoints->answer = "12";
    parm.npoints->guisection = _("Settings");

    parm.power = G_define_option();
    parm.power->key = "power";
    parm.power->type = TYPE_DOUBLE;
    parm.power->answer = "2.0";
    parm.power->label = _("Power parameter");
    parm.power->description =
        _("Greater values assign greater influence to closer points");
    parm.power->guisection = _("Settings");

    flag.noindex = G_define_flag();
    flag.noindex->key = 'n';
    flag.noindex->label = _("Don't index points by raster cell");
    flag.noindex->description = _("Slower but uses"
                                  " less memory and includes points from outside region"
                                  " in the interpolation");
    flag.noindex->guisection = _("Settings");

    if (G_parser(argc, argv))
        exit(EXIT_FAILURE);

    if (sscanf(parm.npoints->answer, "%d", &search_points) != 1 ||
            search_points < 1)
        G_fatal_error(_("Illegal number (%s) of interpolation points"),
                      parm.npoints->answer);

    list =
        (struct list_Point *) G_calloc((size_t) search_points,
                                       sizeof(struct list_Point));

    p = atof(parm.power->answer);

    /* get the window, dimension arrays */
    G_get_window(&window);

    if (!flag.noindex->answer) {
        npoints_currcell = (long **)G_malloc(window.rows * sizeof(long *));
        points =
            (struct Point ***)G_malloc(window.rows * sizeof(struct Point **));


        for (row = 0; row < window.rows; row++) {
            npoints_currcell[row] =
                (long *)G_malloc(window.cols * sizeof(long));
            points[row] =
                (struct Point **)G_malloc(window.cols *
                                          sizeof(struct Point *));

            for (col = 0; col < window.cols; col++) {
                npoints_currcell[row][col] = 0;
                points[row][col] = NULL;
            }
        }
    }

    /* read the elevation points from the input sites file */
    read_sites(parm.input->answer, parm.dfield->answer,
               parm.col->answer, flag.noindex->answer);

    if (npoints == 0)
        G_fatal_error(_("No points found"));
    nsearch = npoints < search_points ? npoints : search_points;

    if (!flag.noindex->answer) {
        /* Arbitrary point to switch between searching algorithms. Could do
         * with refinement PK */
        if ((window.rows * window.cols) / npoints > 400) {
            /* Using old algorithm.... */
            searchallpoints = 1;
            ncells = 0;

            /* Make an array to contain the row and column indices that have
             * sites in them; later will just search through all these. */
            for (searchrow = 0; searchrow < window.rows; searchrow++)
                for (searchcolumn = 0; searchcolumn < window.cols;
                        searchcolumn++)
                    if (npoints_currcell[searchrow][searchcolumn] > 0) {
                        shortlistrows = (int *)G_realloc(shortlistrows,
                                                         (1 +
                                                          ncells) *
                                                         sizeof(int));
                        shortlistcolumns =
                            (int *)G_realloc(shortlistcolumns,
                                             (1 + ncells) * sizeof(int));
                        shortlistrows[ncells] = searchrow;
                        shortlistcolumns[ncells] = searchcolumn;
                        ncells++;
                    }
        }
        else {
            /* Fill look-up table of row and column offsets for
             * doing a circular region growing search looking for sites */
            /* Use units of column width */
            max_radius = (int)(0.5 + sqrt(window.cols * window.cols +
                                          (window.rows * window.ns_res /
                                           window.ew_res) * (window.rows *
                                                   window.ns_res /
                                                   window.ew_res)));

            search_list =
                (struct cell_list **)G_malloc(max_radius *
                                              sizeof(struct cell_list *));
            search_list_start =
                (struct cell_list **)G_malloc(max_radius *
                                              sizeof(struct cell_list *));

            for (radius = 0; radius < max_radius; radius++)
                search_list[radius] = NULL;

            for (row = 0; row < window.rows; row++)
                for (col = 0; col < window.cols; col++) {
                    radius = (int)sqrt(col * col +
                                       (row * window.ns_res / window.ew_res) *
                                       (row * window.ns_res / window.ew_res));
                    if (search_list[radius] == NULL)
                        search_list[radius] =
                            search_list_start[radius] =
                                G_malloc(sizeof(struct cell_list));
                    else
                        search_list[radius] =
                            search_list[radius]->next =
                                G_malloc(sizeof(struct cell_list));

                    search_list[radius]->row = row;
                    search_list[radius]->column = col;
                    search_list[radius]->next = NULL;
                }
        }
    }

    /* allocate buffers, etc. */

    dcell = Rast_allocate_d_buf();

    if ((maskfd = Rast_maskfd()) >= 0)
        mask = Rast_allocate_c_buf();
    else
        mask = NULL;


    fd = Rast_open_new(parm.output->answer, DCELL_TYPE);

    /* GTC Count of window rows */
    G_asprintf(&tmpstr1, n_("%d row", "%d rows", window.rows), window.rows);
    /* GTC Count of window columns */
    G_asprintf(&tmpstr2, n_("%d column", "%d columns", window.cols), window.cols);
    /* GTC First argument is map name, second - message about number of rows, third - columns. */
    G_important_message(_("Interpolating raster map <%s> (%s, %s)..."),
                        parm.output->answer, tmpstr1, tmpstr2);
    G_free(tmpstr1);
    G_free(tmpstr2);

    north = window.north + window.ns_res / 2.0;
    for (row = 0; row < window.rows; row++) {
        G_percent(row, window.rows, 1);

        if (mask)
            Rast_get_c_row(maskfd, mask, row);

        north -= window.ns_res;
        east = window.west - window.ew_res / 2.0;
        for (col = 0; col < window.cols; col++) {
            east += window.ew_res;
            /* don't interpolate outside of the mask */
            if (mask && mask[col] == 0) {
                Rast_set_d_null_value(&dcell[col], 1);
                continue;
            }

            /* If current cell contains more than nsearch points just average
             * all the points in this cell and don't look in any others */

            if (!(flag.noindex->answer) && npoints_currcell[row][col] >= nsearch) {
                sum1 = 0.0;
                for (i = 0; i < npoints_currcell[row][col]; i++)
                    sum1 += points[row][col][i].z;

                interp_value = sum1 / npoints_currcell[row][col];
            }
            else {
                if (flag.noindex->answer)
                    calculate_distances_noindex(north, east);
                else {
                    pointsfound = 0;
                    i = 0;

                    if (searchallpoints == 1) {
                        /* If there aren't many sites just check them all to find
                         * the nearest */
                        for (n = 0; n < ncells; n++)
                            calculate_distances(shortlistrows[n],
                                                shortlistcolumns[n], north,
                                                east, &pointsfound);
                    }
                    else {
                        radius = 0;
                        while (pointsfound < nsearch) {
                            /* Keep widening the search window until we find
                             * enough points */
                            search_list[radius] = search_list_start[radius];
                            while (search_list[radius] != NULL) {
                                /* Always */
                                if (row <
                                        (window.rows - search_list[radius]->row)
                                        && col <
                                        (window.cols -
                                         search_list[radius]->column)) {
                                    searchrow =
                                        row + search_list[radius]->row;
                                    searchcolumn =
                                        col + search_list[radius]->column;
                                    calculate_distances(searchrow,
                                                        searchcolumn, north,
                                                        east, &pointsfound);
                                }

                                /* Only if at least one offset is not 0 */
                                if ((search_list[radius]->row > 0 ||
                                        search_list[radius]->column > 0) &&
                                        row >= search_list[radius]->row &&
                                        col >= search_list[radius]->column) {
                                    searchrow =
                                        row - search_list[radius]->row;
                                    searchcolumn =
                                        col - search_list[radius]->column;
                                    calculate_distances(searchrow,
                                                        searchcolumn, north,
                                                        east, &pointsfound);
                                }

                                /* Only if both offsets are not 0 */
                                if (search_list[radius]->row > 0 &&
                                        search_list[radius]->column > 0) {
                                    if (row <
                                            (window.rows -
                                             search_list[radius]->row) &&
                                            col >= search_list[radius]->column) {
                                        searchrow =
                                            row + search_list[radius]->row;
                                        searchcolumn =
                                            col - search_list[radius]->column;
                                        calculate_distances(searchrow,
                                                            searchcolumn,
                                                            north, east,
                                                            &pointsfound);
                                    }
                                    if (row >= search_list[radius]->row &&
                                            col <
                                            (window.cols -
                                             search_list[radius]->column)) {
                                        searchrow =
                                            row - search_list[radius]->row;
                                        searchcolumn =
                                            col + search_list[radius]->column;
                                        calculate_distances(searchrow,
                                                            searchcolumn,
                                                            north, east,
                                                            &pointsfound);
                                    }
                                }

                                search_list[radius] =
                                    search_list[radius]->next;
                            }
                            radius++;
                        }
                    }
                }

                /* interpolate */
                sum1 = 0.0;
                sum2 = 0.0;
                for (n = 0; n < nsearch; n++) {
                    if ((dist = sqrt(list[n].dist))) {
                        sum1 += list[n].z / pow(dist, p);
                        sum2 += 1.0 / pow(dist, p);
                    }
                    else {
                        /* If one site is dead on the centre of the cell, ignore
                         * all the other sites and just use this value.
                         * (Unlikely when using floating point numbers?) */
                        sum1 = list[n].z;
                        sum2 = 1.0;
                        break;
                    }
                }
                interp_value = sum1 / sum2;
            }
            dcell[col] = (DCELL) interp_value;
        }
        Rast_put_d_row(fd, dcell);
    }
    G_percent(1, 1, 1);

    Rast_close(fd);

    /* writing history file */
    Rast_short_history(parm.output->answer, "raster", &history);
    Rast_command_history(&history);
    Rast_write_history(parm.output->answer, &history);

    G_done_msg(" ");

    exit(EXIT_SUCCESS);
}
コード例 #5
0
ファイル: gmx_amide_map.c プロジェクト: mreppert/g_amide
// Read amide map from text file. 
int read_amide_map( char* fnm, t_amide_map *p_map, int verbose ) {
	t_amide_map map;
	FILE* fp = fopen(fnm, "r");
	if(fp==NULL) {
		printf("Error opening map file. Please check input.\n");
		return 0;
	}

	const int nelec = 10;
	char line[maxchar];

	map.name[0] = '\0';
	map.nsites = 0;
	map.ncoupsites = 0;
	map.npaths = 0;
	map.freq = 0.0;
	map.proshift = 0.0;
	map.dimNNFSN[0] = 0;
	map.dimNNFSC[0] = 0;
	map.dimNNC[0] = 0;
	map.dimNNFSN[1] = 0;
	map.dimNNFSC[1] = 0;
	map.dimNNC[1] = 0;
	map.dipset = 0;
	map.tcset = 0;
	int i,j,k;
	for(i=0; i<nelec; i++) {
		map.elec_used[i] = 0;
	}

	while(fgets(line, maxchar, fp)!=NULL) {
		if(!strncmp(line, "SITES:", 6)) {
			if(sscanf(line, "%*s%d", &map.nsites)>0) {
				if(!read_sites(fp, &map)) {
					printf("Error reading map sites. Please check input.\n");
					return 0;
				}
				for(i=0; i<map.nsites; i++) {
					for(j=0; j<nelec; j++) {
						map.MapSites[i].shift[j] = 0.0;
						map.MapSites[i].dipx[j] = 0.0;
						map.MapSites[i].dipy[j] = 0.0;
						map.MapSites[i].dipz[j] = 0.0;
					}
				}
				if(verbose){
					 printf("Number of sites: %d\n", map.nsites);
					for(i=0; i<map.nsites; i++) {
						printf("\tSite %d: ", i);
						for(j=0; j<map.MapSites[i].natoms; j++) {
							for(k=0; k<map.MapSites[i].AtomPaths[j].length; k++) {
								printf("%s", map.MapSites[i].AtomPaths[j].Path[k]);
								if(k<map.MapSites[i].AtomPaths[j].length-1) printf("-");
							}
							if(j<map.MapSites[i].natoms-1) printf(" %s ", path_delim);
						}
						printf("\n");
					}
					printf("\n");
				}
			} else {
				printf("Error parsing number of sites from map file %s.\n", fnm);
				printf("The bad line seems to be: \n\t%s", line);
				return 0;
			}
		} else if(!strncmp(line, "FREQ:", 5)) {
			if(sscanf(line, "%*s%f", &map.freq)>0) {
				if(verbose) printf("Successfully set gas phase frequency at %6.2f cm-1\n\n", map.freq);
			} else {
				printf("Error setting gas phase frequency.\n");
				printf("The bad line seems to be: \n\t%s\n", line);
				return 0;
			}
		} else if(!strncmp(line, "PROSHIFT:", 9)) {
			if(sscanf(line, "%*s%f", &map.proshift)>0) {
				if(verbose) printf("Successfully set proline shift at %6.2f cm-1\n\n", map.proshift);
			} else {
				printf("Error setting proline shift frequency.\n");
				printf("The bad line seems to be: \n\t%s\n", line);
				return 0;
			}
		} else if(!strncmp(line, "DIP:", 4)) {
			if(sscanf(line, "%*s%f%f%f", &map.dip[0], &map.dip[1], &map.dip[2])==3) {
				if(verbose) printf("Successfully set gas phase dipole: \t%6.4f\t%6.4f\t%6.4f\n\n", map.dip[0], map.dip[1], map.dip[2]);
				map.dipset = 1;
			} else {
				printf("Error setting gas phase transition dipole moment.\n");
				printf("The bad line seems to be: \n\t%s\n", line);
				return 0;
			}
		} else if(!strncmp(line, "SHIFT:", 6)) {
			if(!read_site_coeff(fp, &map, line)) {
				printf("Error reading site freq coefficients. Please check input.\n");
				return 0;
			} else {
				if(verbose) {
					printf("Successfully read site freq coefficients: \n");
					for(i=0; i<map.nsites; i++) {
						for(j=0; j<nelec; j++) {
							printf("%6.1f\t", map.MapSites[i].shift[j]);
						}
						printf("\n");
					}
					printf("\n");
				}
			}
		} else if(!strncmp(line, "DIPX:", 5)) {
			if(!read_site_coeff(fp, &map, line)) {
				printf("Error reading site dipx coefficients. Please check input.\n");
			} else {
				if(verbose) {
					printf("Successfully read site dipx coefficients: \n");
					for(i=0; i<map.nsites; i++) {
						for(j=0; j<nelec; j++) {
							printf("%6.3f\t", map.MapSites[i].dipx[j]);
						}
						printf("\n");
					}
					printf("\n");
				}
			}
		} else if(!strncmp(line, "DIPY:", 5)) {
			if(!read_site_coeff(fp, &map, line)) {
				printf("Error reading site dipy coefficients. Please check input.\n");
			} else {
				if(verbose) {
					printf("Successfully read site dipy coefficients: \n");
					for(i=0; i<map.nsites; i++) {
						for(j=0; j<nelec; j++) {
							printf("%6.3f\t", map.MapSites[i].dipy[j]);
						}
						printf("\n");
					}
					printf("\n");
				}
			}
		} else if(!strncmp(line, "DIPZ:", 5)) {
			if(!read_site_coeff(fp, &map, line)) {
				printf("Error reading site dipz coefficients. Please check input.\n");
			} else {
				if(verbose) {
					printf("Successfully read site dipz coefficients: \n");
					for(i=0; i<map.nsites; i++) {
						for(j=0; j<nelec; j++) {
							printf("%6.3f\t", map.MapSites[i].dipz[j]);
						}
						printf("\n");
					}
					printf("\n");
				}
			}
		} else if(!strncmp(line, "NNFSN:", 6)) {
			if(sscanf(line, "%*s%d%d", &map.dimNNFSN[0], &map.dimNNFSN[1])==2) {
				if(map.dimNNFSN[0]*map.dimNNFSN[1]==0) break;
				if(!allocate_2d_array_real(&map.NNFSN, map.dimNNFSN[0], map.dimNNFSN[1])) {
					map.dimNNFSN[0] = 0;
					map.dimNNFSN[1] = 0;
					printf("Error allocating memory for NNFSN map\n");
					return 0;
				}
				for(i=0; i<map.dimNNFSN[0]; i++) {
					j=0;
					if(fgets(line, maxchar, fp)!=NULL) {
						char* p;
						p = strtok(line, mat_delim);
						do {
							if(sscanf(p, "%f", &map.NNFSN[i][j])!=1) break;
							p = strtok(NULL, mat_delim);
							j++;
						} while( (p!=NULL) && j<map.dimNNFSN[1] );
					} 
					if( j==map.dimNNFSN[1] ) {
						if(i==map.dimNNFSN[0]-1) {
							if(verbose) {
								printf("\nSuccessfully read %d-by-%d NNFSN map\n", map.dimNNFSN[0], map.dimNNFSN[1]);
								int k,l;
								for(k=0; k<map.dimNNFSN[0]; k++) {
									for(l=0; l<map.dimNNFSN[1]; l++) printf("%5.2f\t", map.NNFSN[k][l]);
									printf("\n");
								}
							}
						}
					} else {
						printf("Error reading NNFSN parameters (expected %d-by-%d matrix).\n", map.dimNNFSN[0], map.dimNNFSN[1]);
						return 0;
					}
				}
			} else {
				printf("Error reading dimension of N-terminal nearest-neighbor frequency shift map.\n");
				return 0;
			}
		} else if(!strncmp(line, "NNFSC:", 6)) {
			if(sscanf(line, "%*s%d%d", &map.dimNNFSC[0], &map.dimNNFSC[1])==2) {
				if( (map.dimNNFSC[0]*map.dimNNFSC[1])==0 ) break;
				if(!allocate_2d_array_real(&map.NNFSC, map.dimNNFSC[0], map.dimNNFSC[1])) {
					map.dimNNFSC[0] = 0;
					map.dimNNFSC[1] = 0;
					printf("Error allocating memory for NNFSC map\n");
					return 0;
				}
				
				for(i=0; i<map.dimNNFSC[0]; i++) {
					j=0;
					if(fgets(line, maxchar, fp)!=NULL) {
						char* p;
						p = strtok(line, mat_delim);
						do {
							if(sscanf(p, "%f", &map.NNFSC[i][j])!=1) break;
							p = strtok(NULL, mat_delim);
							j++;
						} while( (p!=NULL) && j<map.dimNNFSC[1] );
					} 
					if( j==map.dimNNFSC[1] ) {
						if(i==map.dimNNFSC[0]-1) {
							if(verbose) {
								printf("\nSuccessfully read %d-by-%d NNFSC map\n", map.dimNNFSC[0], map.dimNNFSC[1]);
								int k,l;
								for(k=0; k<map.dimNNFSC[0]; k++) {
									for(l=0; l<map.dimNNFSC[1]; l++) printf("%5.2f\t", map.NNFSC[k][l]);
									printf("\n");
								}
							}
						}
					} else {
						printf("Error reading NNFSC parameters (expected %d-by-%d matrix).\n", map.dimNNFSC[0], map.dimNNFSC[1]);
						return 0;
					}
				}
			} else {
				printf("Error reading dimension of C-terminal nearest-neighbor frequency shift map.\n");
				return 0;
			}
		} else if(!strncmp(line, "COUPLING:", 9)) {
			int error = 0;
			char couptype[64];
			if(sscanf(line, "%*s %s", couptype)) {
				if(!strcmp(couptype, "pdc")) {
					strcpy(map.couptype, "pdc");
				} else if(!strcmp(couptype, "tcc")) {
					strcpy(map.couptype, "tcc");
				} else error = 1;
			} else error = 1;
			if(error) {
				printf("Error reading couling model type. Please enter pdc or tcc\n");
				free_amide_map(map);
				return 0;
			}

		} else if(!strncmp(line, "COUPSITES:", 10)) {
			if(sscanf(line, "%*s%d", &map.ncoupsites)>0) {
				if(!read_coupsites(fp, &map)) {
					printf("Error reading map coupling sites. Please check input.\n");
					return 0;
				}
				for(i=0; i<map.ncoupsites; i++) {
					map.CoupSites[i].nux = 0.0;
					map.CoupSites[i].nuy = 0.0;
					map.CoupSites[i].nuz = 0.0;
					map.CoupSites[i].q = 0.0;
					map.CoupSites[i].dq = 0.0;
					map.tcset = 1;
				}
				if(verbose) {
					printf("\nLooking for %d coupling sites:", map.ncoupsites);
					for(i=0; i<map.ncoupsites; i++) {
						printf("\n\tSite %d: ", i);
						for(j=0; j<map.CoupSites[i].natoms; j++) {
							for(k=0; k<map.CoupSites[i].AtomPaths[j].length; k++) {
								printf("%s", map.CoupSites[i].AtomPaths[j].Path[k]);
								if(k<map.CoupSites[i].AtomPaths[j].length-1) printf("-");
							}
							if(j<map.CoupSites[i].natoms-1) printf(" %s ", path_delim);
						}
					}
					printf("\n");
				}
			} else {
				printf("Error parsing number of coupling sites from map file %s.\n", fnm);
				printf("The bad line seems to be: \n\t%s\n", line);
				return 0;
			}

		} else if(!strncmp(line, "NUX:", 4)) {
			if(map.ncoupsites==0) {
				printf("Error! Found NUX parameter flag with no coupling sites specified.\n");
				printf("Please ensure that COUPSITES flag precedes NUX, NUY, NUZ, Q, and DQ flags.\n");
				return 0;
			}
			for(i=0; i<map.ncoupsites; i++) {
				if( (fgets(line, maxchar, fp)==NULL) || (sscanf(line, "%f", &map.CoupSites[i].nux)!=1) ) {
					printf("Error reading NUX component for coupling site %d.\n", i);
					return 0;
				}
			}
			if(verbose) {
				printf("\nSuccessfully read all NUX components: \n");
				for(i=0; i<map.ncoupsites; i++) printf("Site %d: %6.10f\n", i, map.CoupSites[i].nux);
			}
		} else if(!strncmp(line, "NUY:", 4)) {
			if(map.ncoupsites==0) {
				printf("Error! Found NUY parameter flag with no coupling sites specified.\n");
				printf("Please ensure that COUPSITES flag precedes NUX, NUY, NUZ, Q, and DQ flags.\n");
				return 0;
			}
			for(i=0; i<map.ncoupsites; i++) {
				if( (fgets(line, maxchar, fp)==NULL) || (sscanf(line, "%f", &map.CoupSites[i].nuy)!=1) ) {
					printf("Error reading NUX component for coupling site %d.\n", i);
					return 0;
				}
			}
			if(verbose) {
				printf("\nSuccessfully read all NUY components: \n");
				for(i=0; i<map.ncoupsites; i++) printf("Site %d: %6.10f\n", i, map.CoupSites[i].nuy);
			}
		} else if(!strncmp(line, "NUZ:", 4)) {
			if(map.ncoupsites==0) {
				printf("Error! Found NUZ parameter flag with no coupling sites specified.\n");
				printf("Please ensure that COUPSITES flag precedes NUX, NUY, NUZ, Q, and DQ flags.\n");
				return 0;
			}
			for(i=0; i<map.ncoupsites; i++) {
				if( (fgets(line, maxchar, fp)==NULL) || (sscanf(line, "%f", &map.CoupSites[i].nuz)!=1) ) {
					printf("Error reading NUY component for coupling site %d.\n", i);
					return 0;
				}
			}
			if(verbose) {
				printf("\nSuccessfully read all NUZ components: \n");
				for(i=0; i<map.ncoupsites; i++) printf("Site %d: %6.10f\n", i, map.CoupSites[i].nuz);
			}
		} else if(!strncmp(line, "Q:", 2)) {
			if(map.ncoupsites==0) {
				printf("Error! Found Q parameter flag with no coupling sites specified.\n");
				printf("Please ensure that COUPSITES flag precedes NUX, NUY, NUZ, Q, and DQ flags.\n");
				return 0;
			}
			for(i=0; i<map.ncoupsites; i++) {
				if( (fgets(line, maxchar, fp)==NULL) || (sscanf(line, "%f", &map.CoupSites[i].q)!=1) ) {
					printf("Error reading Q component for coupling site %d.\n", i);
					return 0;
				}
			}
			if(verbose) {
				printf("\nSuccessfully read all Q components: \n");
				for(i=0; i<map.ncoupsites; i++) printf("Site %d: %6.10f\n", i, map.CoupSites[i].q);
			}
		} else if(!strncmp(line, "DQ:", 3)) {
			if(map.ncoupsites==0) {
				printf("Error! Found DQ parameter flag with no coupling sites specified.\n");
				printf("Please ensure that COUPSITES flag precedes NUX, NUY, NUZ, Q, and DQ flags.\n");
				return 0;
			}
			for(i=0; i<map.ncoupsites; i++) {
				if( (fgets(line, maxchar, fp)==NULL) || (sscanf(line, "%f", &map.CoupSites[i].dq)!=1) ) {
					printf("Error reading DQ component for coupling site %d.\n", i);
					return 0;
				}
			}
			if(verbose) {
				printf("\nSuccessfully read all DQ components: \n");
				for(i=0; i<map.ncoupsites; i++) printf("Site %d: %6.10f\n", i, map.CoupSites[i].dq);
			}
		} else if(!strncmp(line, "NNC:", 4)) {
			if(sscanf(line, "%*s%d%d", &map.dimNNC[0], &map.dimNNC[1])==2) {
				if(map.dimNNC[0]==0 || map.dimNNC[1]==0) break;
				map.NNC = (real**) malloc(map.dimNNC[0]*sizeof(real*));
				if(map.NNC==NULL) {
					map.dimNNC[0] = 0;
					map.dimNNC[1] = 0;
					printf("Error allocating memory for NNC map\n");
					return 0;
				}
				for(i=0; i<map.dimNNC[0]; i++) {
					map.NNC[i] = (real*) malloc(map.dimNNC[1]*sizeof(real));
					if(map.NNC[i]==NULL) {
						printf("Error allocating memory for NNC map\n");
						int ix;
						for(ix=0; ix<i; ix++) free(map.NNC[ix]);
						free(map.NNC);
						return 0;
					}
				}
				for(i=0; i<map.dimNNC[0]; i++) {
					j=0;
					if(fgets(line, maxchar, fp)!=NULL) {
						char* p;
						p = strtok(line, mat_delim);
						do {
							if(sscanf(p, "%f", &map.NNC[i][j])!=1) break;
							p = strtok(NULL, mat_delim);
							j++;
						} while( (p!=NULL) && j<map.dimNNC[1] );
					} 
					if( j==map.dimNNC[1] ) {
						if(i==map.dimNNC[0]-1) {
							if(verbose) {
								printf("\nSuccessfully read %d-by-%d NNC map\n", map.dimNNC[0], map.dimNNC[1]);
								int k,l;
								for(k=0; k<map.dimNNC[0]; k++) {
									for(l=0; l<map.dimNNC[1]; l++) printf("%5.2f\t", map.NNC[k][l]);
									printf("\n");
								}
							}
						}
					} else {
						printf("Error reading NNC parameters (expected %d-by-%d matrix).\n", map.dimNNC[0], map.dimNNC[1]);
						return 0;
					}
				}
			} else {
				printf("Error reading dimension of nearest-neighbor coupling map.\n");
				return 0;
			}
		} else if(!strncmp(line, "DNNC:", 5)) {
			if(sscanf(line, "%*s%d%d", &map.dimDNNC[0], &map.dimDNNC[1])==2) {
				if(map.dimDNNC[0]==0 || map.dimDNNC[1]==0) break;
				map.DNNC = (real**) malloc(map.dimDNNC[0]*sizeof(real*));
				if(map.DNNC==NULL) {
					map.dimDNNC[0] = 0;
					map.dimDNNC[1] = 0;
					printf("Error allocating memory for DNNC map\n");
					return 0;
				}
				for(i=0; i<map.dimDNNC[0]; i++) {
					map.DNNC[i] = (real*) malloc(map.dimDNNC[1]*sizeof(real));
					if(map.DNNC[i]==NULL) {
						printf("Error allocating memory for DNNC map\n");
						int ix;
						for(ix=0; ix<i; ix++) free(map.DNNC[ix]);
						free(map.DNNC);
						return 0;
					}
				}
				for(i=0; i<map.dimDNNC[0]; i++) {
					j=0;
					if(fgets(line, maxchar, fp)!=NULL) {
						char* p;
						p = strtok(line, mat_delim);
						do {
							if(sscanf(p, "%f", &map.DNNC[i][j])!=1) break;
							p = strtok(NULL, mat_delim);
							j++;
						} while( (p!=NULL) && j<map.dimDNNC[1] );
					} 
					if( j==map.dimDNNC[1] ) {
						if(i==map.dimDNNC[0]-1) {
							if(verbose) {
								printf("\nSuccessfully read %d-by-%d DNNC map\n", map.dimDNNC[0], map.dimDNNC[1]);
								int k,l;
								for(k=0; k<map.dimDNNC[0]; k++) {
									for(l=0; l<map.dimDNNC[1]; l++) printf("%5.2f\t", map.DNNC[k][l]);
									printf("\n");
								}
							}
						}
					} else {
						printf("Error reading DNNC parameters (expected %d-by-%d matrix).\n", map.dimDNNC[0], map.dimDNNC[1]);
						return 0;
					}
				}
			} else {
				printf("Error reading dimension of nearest-neighbor coupling map.\n");
				return 0;
			}

		} else if(!strncmp(line, "EXCLUDE:", 8)) {
			map.npaths = 0;
			if(sscanf(line, "%*s%d", &map.npaths)==1) {
				map.ExcludedAtomPaths = (t_atom_path*) malloc(map.npaths*sizeof(t_atom_path));
				if(map.ExcludedAtomPaths==NULL) {
					map.npaths = 0;
					printf("Error allocating memory for excluded atom paths.\n");
					return 0;
				}
				for(i=0; i<map.npaths; i++) {
					if(fgets(line, maxchar, fp)!=NULL) {
						if(!set_atom_path(&map.ExcludedAtomPaths[i], line)) {
							printf("Error reading excluded atom #%d from line: \n%s", i+1, line);
							return 0;
						}
					}
				}
				if(verbose) {
					printf("Successfully read %d excluded atom paths:\n", map.npaths);
					for(i=0; i<map.npaths; i++) {
						for(j=0; j<map.ExcludedAtomPaths[i].length; j++) {
							printf("%s", map.ExcludedAtomPaths[i].Path[j]);
							if(j<map.ExcludedAtomPaths[i].length-1) printf("-");
						}
						printf("\n");
					}
				}
			} else {
				printf("Error reading number of excluded atoms.\n");
				return 0;
			}
		}
	}

	double eps = 1e-10;
	for(i=0; i<nelec; i++) {
		map.elec_used[i] = 0;
		for(j=0; j<map.nsites; j++) {
			if(abs(map.MapSites[j].shift[i])>eps) map.elec_used[i] = 1;
			if(abs(map.MapSites[j].dipx[i])>eps) map.elec_used[i] = 1;
			if(abs(map.MapSites[j].dipy[i])>eps) map.elec_used[i] = 1;
		}
	}

	if( (!strcmp(map.couptype, "tcc")) && (map.ncoupsites==0) ) {
		printf("Error! Transition charge coupling requested, but no map sites specified.\n");
		free_amide_map(map);
		return 0;
	} else if( (!strcmp(map.couptype, "pdc")) && (map.ncoupsites!=0) ) {
		printf("Error! Point dipole coupling requested, but transition charge coupling sites seem to be specified. Which model do you want to use?\n");
		free_amide_map(map);
		return 0;
	}
	*p_map = map;
	fclose(fp);
	return 1;
}