gint reaxmd_output_coords_read(gpointer import) { gchar *fullname, *line; FILE *fp; #if DEBUG_READ_DCD printf("Adding [dcd] data to file: %s\n", import_fullpath(import)); #endif fullname = parse_extension_set(import_fullpath(import), "dcd"); if (!g_file_test(fullname, G_FILE_TEST_EXISTS)) { gui_text_show(ERROR, "Failed to find associated .dcd file.\n"); g_free(fullname); return(1); } // TODO - if existing frames - assume replace mode // otherwise create new frame at each step /* NB: binary file data */ fp = fopen(fullname, "rb"); while (fp) { line = file_read_line(fp); if (!line) break; g_free(line); } return(1); }
gint read_xml_frame(FILE *fp, struct model_pak *model) { gchar *line; GMarkupParser xml_parser; GMarkupParseContext *xml_context; xml_system_table = g_hash_table_new_full(&g_str_hash, &hash_strcmp, &g_free, NULL); /* TODO - think more about this... */ xml_model = model; /* setup context parse (ie callbacks) */ xml_parser.start_element = &xml_start_element; xml_parser.end_element = &xml_end_element; xml_parser.text = &xml_parse_text; xml_parser.passthrough = NULL; xml_parser.error = NULL; xml_context = g_markup_parse_context_new(&xml_parser, 0, model, NULL); /* read in blocks (lines) of text */ line = file_read_line(fp); while (line) { /* parse the line */ if (!g_markup_parse_context_parse(xml_context, line, strlen(line), NULL)) { printf("read_xml() : parsing error.\n"); g_free(line); g_markup_parse_context_free(xml_context); g_hash_table_destroy(xml_system_table); return(1); } g_free(line); line = file_read_line(fp); } /* cleanup */ if (!g_markup_parse_context_end_parse(xml_context, NULL)) printf("read_xml() : errors occurred reading file.\n"); g_markup_parse_context_free(xml_context); g_hash_table_destroy(xml_system_table); return(0); }
/** * Prints all lines of a file, preceded by its number. * @param fp opened file pointer. */ void file_print(FILE *fp) { char line[FILE_LINE_LENGTH]; int i; for(i = 0; file_read_line(fp, line) != FILE_FINISHED; ++i) printf("%d: %s", i, line); printf("\n"); }
static rc_t read_spot_data_3lines(FastqFileInfo* file, FileReadData* sd, uint8_t best_word, uint8_t best_score, int qualType) { rc_t rc = 0; file->line = NULL; /* discard defline */ /* read sequence */ if( (rc = read_multiline_seq_or_qual(file, '+', &sd->read.seq)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading sequence data"); } if( !pstring_is_fasta(&sd->read.seq) ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcCorrupt); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=expected sequence data"); } /* next defline */ if( (rc = file_read_line(file, false)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading quality defline"); } if( file->line[0] != '+' ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcCorrupt); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=expected '+' on quality defline"); } if( file->line_len != 1 ) { /* there may be just '+' on quality defline */ FileReadData d; uint8_t score = parse_spot_name(file->file, &d, &file->line[1], file->line_len - 1, best_word); /* sometimes quality defline may NOT contain barcode and readid, so score will be lower than bestscore, but must be at least == 1 with none empty line, which means that name was found */ if( score < 1 ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcCorrupt); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=spot name not found"); } if( pstring_cmp(&sd->name, &d.name) != 0 || (score == best_score && (pstring_cmp(&sd->barcode, &d.barcode) != 0 || sd->read.read_id != d.read.read_id)) ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcCorrupt); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=quality defline do not match sequence defline"); } } file->line = NULL; /* discard defline */ if( (rc = read_multiline_seq_or_qual(file, '@', &sd->read.qual)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=failed to read quality"); } if( sd->read.qual.len <= 0 ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcEmpty); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=quality"); } sd->read.qual_type = qualType; sd->ready = true; return 0; }
/*! \brief Reads a line from the supplied file and interprets it as long value. This is almost equivalent to \code fscanf(file, "%ld", value) \endcode, save that fscanf() seems to eat all following LFs, while this function only reads one. \param file The file. \param value The pointer to where the read value shall be written. \return \c ~0, if a number could be read, 0 otherwise. */ static int file_read_line_long(FILE *file, long *value) { char buffer[32]; int result; result = file_read_line(file, buffer, sizeof(buffer)); if (!result) return result; if (sscanf(buffer, "%ld\n", value) != 1) return 0; return 1; }
struct washer_config_list_node *read_configuration (const char *conf_file) { FILE *f; int N; char *line; char *value_str; struct washer_config_list_node *node; struct washer_config_entry *entry; f = fopen (conf_file, "r"); if (f == NULL) { err ("read_configuration: can't open file %s", conf_file); return NULL; } N = 0; node = NULL; while (1) { if (file_read_line (&line, f) < 0 || line == NULL) break; if (config_line_ok (line)) { node = washer_config_list_alloc (node); entry = &node->entry; value_str = strchr (line, ':'); *value_str = '\0'; // finish type name string entry->type = malloc (strlen (line) + 1); strcpy (entry->type, line); value_str ++; sscanf (value_str, "%d", &entry->val); } else info ("configuration file syntax error: %s", line); free (line); } fclose (f); return node; }
gchar *get_next_keyword(FILE *fp, gchar *line, gint newline, gint *ret) { static int i; int num_tokens; gchar *text; static gchar **buff; gchar *keyword; if (newline) { buff = tokenize(line, &num_tokens); i = 0; } while (buff[i] == NULL) { /* TODO skip comment lines? */ text = file_read_line(fp); if (!text) { *ret = 2; return(NULL); } else { /* TODO free array of pointers */ /* g_strfreev(buff); */ buff = tokenize(text, &num_tokens); i = 0; } g_free(text); } *ret = 0; keyword = buff[i]; i++; return(keyword); }
/* TEMP - parse diffax sfc data */ void parse_sfc(FILE *fp) { gint i, n; gchar *line, *type, **buff; gdouble *sfc; GSList *list; while ((line = file_read_line(fp))) { type = g_strstrip(g_strndup(line, 6)); if (elem_symbol_test(type)) { /* tokenize everything after the atom type */ buff = tokenize(&line[6], &n); printf("[%s] ", type); list = NULL; for (i=0 ; i<n ; i++) { sfc = g_malloc(sizeof(gdouble)); *sfc = str_to_float(*(buff+i)); printf("[%f] ", *sfc); list = g_slist_prepend(list, sfc); } list = g_slist_reverse(list); printf("\n"); g_hash_table_insert(sysenv.sfc_table, type, list); g_strfreev(buff); } g_free(line); } }
/* * read fasta or quality, which maybe wrapped on 70th column width, * into asciiZ buffer */ static rc_t read_multiline_seq_or_qual(FastqFileInfo* file, const char stop, pstring* str) { rc_t rc = 0; bool append = false, optional = false; while( rc == 0 ) { if( (rc = file_read_line(file, optional)) == 0 ) { if( optional && (file->line == NULL || (file->line_len > 0 && file->line[0] == stop)) ) { /* eof or next line is defline -> stop, line stays in buffer */ break; } if( append && memchr(str->data, ' ', str->len) != NULL ) { rc = pstring_append(str, " ", 1); } if( rc == 0 && (rc = pstring_append(str, file->line, file->line_len)) == 0 ) { file->line = NULL; /* line processed */ optional = true; } append = true; } } return rc; }
/* reads from a file data for a sinlge spot, data may be partial */ static rc_t read_next_spot(const char* blk_pfx, IlluminaFileInfo* file) { rc_t rc = 0; const char* tail = file->line; if( file->ready ) { /* data still not used */ return 0; } IlluminaFileInfo_init(file); if( (rc = file_read_line(file, true)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading more data"); } else if( file->line == NULL ) { return 0; /* eof */ } switch( file->type ) { case eIlluminaNativeFileTypeQSeq: if( (rc = parse_qseq(file, file->line, file->line_len)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading qseq"); } break; case eIlluminaNativeFileTypeFasta: case eIlluminaNativeFileTypeNoise: case eIlluminaNativeFileTypeIntensity: case eIlluminaNativeFileTypeSignal: {{ /* read only common first 4 coords into name and prepend with DATA_BLOCK/@name */ if( (rc = read_spot_coord(file, file->line, file->line_len, &tail)) == 0 ) { if( blk_pfx != NULL ) { pstring tmp_name; if( (rc = pstring_copy(&tmp_name, &file->name)) == 0 && (rc = pstring_assign(&file->name, blk_pfx, strlen(blk_pfx))) == 0 && (rc = pstring_append(&file->name, ":", 1)) == 0 ) { rc = pstring_concat(&file->name, &tmp_name); } } } if( rc != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading spot coord"); } break; }} case eIlluminaNativeFileTypeQuality4: if( (rc = read_quality(file->line, file->line_len, &file->read)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading quality"); } else if( (rc = pstring_assign(&file->name, blk_pfx, strlen(blk_pfx))) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=name for quality 4"); } break; default: rc = RC(rcSRA, rcFormatter, rcReading, rcFileFormat, rcUnknown); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=processing data line"); break; } /* process tail (after coords) for some file types */ file->line_len -= tail - file->line; /* length of tail */ switch( file->type ) { case eIlluminaNativeFileTypeQSeq: case eIlluminaNativeFileTypeQuality4: default: /* completely processed before */ break; case eIlluminaNativeFileTypeFasta: if( (rc = pstring_assign(&file->read.seq, tail, file->line_len)) != 0 || !pstring_is_fasta(&file->read.seq) ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcCorrupt); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading fasta"); } break; case eIlluminaNativeFileTypeNoise: if( (rc = read_signal(tail, file->line_len, &file->read.noise)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=converting noise"); } break; case eIlluminaNativeFileTypeIntensity: if( (rc = read_signal(tail, file->line_len, &file->read.intensity)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=converting intensity"); } break; case eIlluminaNativeFileTypeSignal: if( (rc = read_signal(tail, file->line_len, &file->read.signal)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=converting signal"); } break; } file->line = NULL; file->ready = true; #if _DEBUGGING DEBUG_MSG(3, ("name:'%s' [%li:%li:%li:%li]\n", file->name.data, file->coord[0], file->coord[1], file->coord[2], file->coord[3])); if( file->read.seq.len ) { DEBUG_MSG(3, ("seq:'%.*s'\n", file->read.seq.len, file->read.seq.data)); } if( file->read.qual.len ) { DEBUG_MSG(3, ("qual{0x%x}: %u bytes\n", file->read.qual_type, file->read.qual.len)); } #endif return 0; }
/* return error */ static int l_file_read_section(int fd, int max_file_size, const char *section, xrdpList *names, xrdpList *values) { struct stream *s; char text[512]; char name[512]; char value[512]; char *lvalue; char c; int in_it; int in_it_index; int len; int index; int file_size; file_size = 32 * 1024; /* 32 K file size limit */ g_file_seek(fd, 0); in_it_index = 0; in_it = 0; g_memset(text, 0, 512); list_clear(names); list_clear(values); make_stream(s); init_stream(s, file_size); len = g_file_read(fd, s->data, file_size); if (len > 0) { s->end = s->p + len; for (index = 0; index < len; index++) { if (!s_check_rem(s, 1)) { break; } in_uint8(s, c); if ((c == '#') || (c == ';')) { file_read_line(s, text); in_it = 0; in_it_index = 0; g_memset(text, 0, 512); continue; } if (c == '[') { in_it = 1; } else if (c == ']') { if (g_strcasecmp(section, text) == 0) { file_read_line(s, text); while (file_read_line(s, text) == 0) { if (g_strlen(text) > 0) { file_split_name_value(text, name, value); list_add_item(names, (tbus) g_strdup(name)); if (value[0] == '$') { lvalue = g_getenv(value + 1); if (lvalue != 0) { list_add_item(values, (tbus) g_strdup(lvalue)); } else { list_add_item(values, (tbus) g_strdup("")); } } else { list_add_item(values, (tbus) g_strdup(value)); } } } free_stream(s); return 0; } in_it = 0; in_it_index = 0; g_memset(text, 0, 512); } else if (in_it) { text[in_it_index] = c; in_it_index++; } } } free_stream(s); return 1; }
gint get_data(FILE *fp, struct model_pak *model, gint have_basis) { gchar **buff, *line; gint num_tokens; struct core_pak *core; /* process title line */ line = file_read_line(fp); if (!line) { gui_text_show(ERROR, "unexpected end of file reading title\n"); return(2); } model->gamess.title = g_strstrip(line); /* process symmetry line */ line = file_read_line(fp); if (!line) { gui_text_show(ERROR, "unexpected end of file reading symmetry\n"); return(2); } if (g_ascii_strncasecmp(line, "c1", 2) != 0) { /* TODO handle symmetry! */ gui_text_show(WARNING, "only C1 symmetry understood at present\n"); } g_free(line); /* process coordinates */ line = file_read_line(fp); if (!line) { gui_text_show(ERROR, "unexpected end of file reading coordinates\n"); return(2); } while (g_ascii_strncasecmp(line, " $end", 5) != 0) { buff = tokenize(line, &num_tokens); /* TODO Store GAMESS label and use in .inp files */ if (num_tokens > 4) { core = new_core(elements[(int) str_to_float(*(buff+1))].symbol, model); if (model->gamess.units == GMS_ANGS) { core->x[0] = str_to_float(*(buff+2)); core->x[1] = str_to_float(*(buff+3)); core->x[2] = str_to_float(*(buff+4)); } else { core->x[0] = str_to_float(*(buff+2)) * BOHR_TO_ANGS; core->x[1] = str_to_float(*(buff+3)) * BOHR_TO_ANGS; core->x[2] = str_to_float(*(buff+4)) * BOHR_TO_ANGS; } model->cores = g_slist_append(model->cores, core); g_strfreev(buff); if (!have_basis) { model->gamess.basis = GMS_USER; /* TODO - read instead of skipping */ for(;;) { line = file_read_line(fp); if (!line) break; buff = tokenize(line, &num_tokens); g_strfreev(buff); if (!num_tokens) break; } } } /* get the next line */ g_free(line); line = file_read_line(fp); if (!line) { gui_text_show(ERROR, "unexpected end of file reading coordinates\n"); return(2); } } g_free(line); return(0); }
gint get_next_group(FILE *fp, struct model_pak *model, gint *have_basis) { gchar *line, *keyword; gint ret; GSList *keywords = NULL, *list; line = file_read_line(fp); if (!line) return(FALSE); /* TODO not a valid keyword so for the moment skip but could store */ if (g_ascii_strncasecmp(line, " $", 2) != 0) return(TRUE); if (g_ascii_strncasecmp(line, " $data", 6) == 0) { ret = get_data(fp, model, *have_basis); } else if (g_ascii_strncasecmp(line, " $basis", 7) == 0) { *have_basis = TRUE; keywords = get_gamess_keywords(fp, line+7, &ret); for (list=keywords ; list ; list=g_slist_next(list)) { keyword = (gchar *) list->data; ret = get_basis(keyword, model); } } else if (g_ascii_strncasecmp(line, " $contrl", 8) == 0) { keywords = get_gamess_keywords(fp, line+8, &ret); for (list=keywords; list ; list=g_slist_next(list)) { keyword = (gchar *) list->data; ret = get_control(keyword, model); } } else if (g_ascii_strncasecmp(line, " $system", 8) == 0) { keywords = get_gamess_keywords(fp, line+8, &ret); for (list=keywords; list ; list=g_slist_next(list)) { keyword = (gchar *) list->data; ret = get_system(keyword, model); } } else if (g_ascii_strncasecmp(line, " $statpt", 8) == 0) { keywords = get_gamess_keywords(fp, line+8, &ret); for (list=keywords; list ; list=g_slist_next(list)) { keyword = (gchar *) list->data; ret = get_statpt(keyword, model); } } else if (g_ascii_strncasecmp(line, " $dft", 5) == 0) { model->gamess.dft = TRUE; /* TODO - process functional */ } else { /* TODO - Unknown keyword, just pass through */ } free_slist(keywords); g_free(line); return(TRUE); }
gint read_dmol_frame(FILE *fp, struct model_pak *model) { gint i, num_tokens; gchar *line, **buff; struct core_pak *core; g_assert(fp != NULL); line = file_read_line(fp); while (line) { /* read cell vectors */ if (g_ascii_strncasecmp(line, "$cell", 5) == 0 && model) { for (i=0 ; i<3 ; i++) { g_free(line); line = file_read_line(fp); buff = tokenize(line, &num_tokens); if (num_tokens > 2) { model->latmat[i] = AU2ANG*str_to_float(*(buff)); model->latmat[i+3] = AU2ANG*str_to_float(*(buff+1)); model->latmat[i+6] = AU2ANG*str_to_float(*(buff+2)); } g_strfreev(buff); } model->periodic = 3; model->construct_pbc = TRUE; } /* read coordinates */ if (g_ascii_strncasecmp(line, "$coord", 5) == 0 && model) { g_free(line); line = file_read_line(fp); buff = tokenize(line, &num_tokens); while (num_tokens > 3) { if (elem_symbol_test(*buff)) { core = new_core(*buff, model); model->cores = g_slist_prepend(model->cores, core); core->x[0] = AU2ANG*str_to_float(*(buff+1)); core->x[1] = AU2ANG*str_to_float(*(buff+2)); core->x[2] = AU2ANG*str_to_float(*(buff+3)); } g_free(line); line = file_read_line(fp); g_strfreev(buff); buff = tokenize(line, &num_tokens); } g_strfreev(buff); model->fractional = FALSE; } /* terminate frame read */ if (g_ascii_strncasecmp(line, "$end", 4) == 0) return(0); g_free(line); line = file_read_line(fp); } return(1); }
GSList *gromacs_read_ff(const gchar *filename) { gint state, type, num_tokens, ub, ua, ud, i; gchar *line, **buff, **abuff, **dbuff; GSList *list=NULL; struct forcefield_pak *ff; FILE *fp; fp = fopen(filename, "rt"); if (!fp) return(NULL); /* unknown bond, angle, dihedral types */ ub = ua = ud = 0; /* stop auto skip of lines starting with # ... ie #define */ file_skip_comment(FALSE); line = file_read_line(fp); state = -1; while (line) { /* TODO - get rid of leading whitespace, so funny comment lines are properly ignored */ ff = NULL; /* directive processing */ switch(line[0]) { case '[': state = -1; if (g_strrstr(line, "bond")) state = BOND; if (g_strrstr(line, "angle")) state = ANGLE; if (g_strrstr(line, "dihedral")) state = DIHEDRAL; break; /* process #defines */ case '#': if (g_ascii_strncasecmp("#define", line, 7) == 0) { buff = get_tokens(line, 3); abuff = g_strsplit(*(buff+1), "_", -1); #if GTK_MAJOR_VERSION >= 2 && GTK_MINOR_VERSION >= 6 num_tokens = g_strv_length(abuff); #else num_tokens = 0; #endif if (abuff) { if (g_ascii_strncasecmp(*(abuff), "improper", 8) == 0) { if (num_tokens > 4) { ff = ff_type_new(FF_IMPROPER); g_snprintf(ff->atom[0], FF_MAX_SYMBOL, "%s", *(abuff+num_tokens-4)); g_snprintf(ff->atom[1], FF_MAX_SYMBOL, "%s", *(abuff+num_tokens-3)); g_snprintf(ff->atom[2], FF_MAX_SYMBOL, "%s", *(abuff+num_tokens-2)); g_snprintf(ff->atom[3], FF_MAX_SYMBOL, "%s", *(abuff+num_tokens-1)); /* TODO - Y, Z -> X ... since X is wildcard type */ for (i=4 ; i-- ; ) if (ff->atom[i][0] == 'Y' || ff->atom[i][0] == 'Z') ff->atom[i][0] = 'X'; dbuff = get_tokens(line, 6); ff->bond_value = str_to_float(*(dbuff+2)); ff->bond_units = FF_DEG; ff->data[0] = str_to_float(*(dbuff+3)); ff->data[1] = str_to_float(*(dbuff+4)); g_strfreev(dbuff); ff->atoms_current = ff->atoms_expected; ff->data_current = ff->data_expected = 2; list = g_slist_prepend(list, ff); ff = NULL; } } g_strfreev(abuff); } g_strfreev(buff); } state = -1; break; /* comment */ case ';': break; default: /* normal processing */ buff = tokenize(line, &num_tokens); switch (state) { case BOND: if (num_tokens > 4) { type = g_ascii_strtod(*(buff+2), NULL); switch (type) { case 1: ff = ff_type_new(FF_HARMONIC); g_snprintf(ff->atom[0], FF_MAX_SYMBOL, "%s", *(buff+0)); g_snprintf(ff->atom[1], FF_MAX_SYMBOL, "%s", *(buff+1)); /* NB: GROMACS uses nm rather than angs */ ff->bond_value = 10.0 * str_to_float(*(buff+3)); ff->bond_units = FF_ANG; /* nm -> ang correction */ ff->data[0] = 0.01 * str_to_float(*(buff+4)); ff->data_units = FF_KJ; ff->atoms_current = ff->atoms_expected; ff->data_current = ff->data_expected; break; case 3: g_assert(num_tokens > 5); ff = ff_type_new(FF_MORSE); g_snprintf(ff->atom[0], FF_MAX_SYMBOL, "%s", *(buff+0)); g_snprintf(ff->atom[1], FF_MAX_SYMBOL, "%s", *(buff+1)); /* nm -> ang correction */ ff->bond_value = 10.0*str_to_float(*(buff+3)); ff->bond_units = FF_ANG; ff->data[0] = str_to_float(*(buff+4)); ff->data[1] = 0.1 * str_to_float(*(buff+5)); ff->data_units = FF_KJ; ff->atoms_current = ff->atoms_expected; ff->data_current = ff->data_expected; break; default: ub++; } } break; case ANGLE: if (num_tokens > 5) { type = g_ascii_strtod(*(buff+3), NULL); switch (type) { case 1: ff = ff_type_new(FF_3B_HARMONIC); g_snprintf(ff->atom[0], FF_MAX_SYMBOL, "%s", *(buff+0)); g_snprintf(ff->atom[1], FF_MAX_SYMBOL, "%s", *(buff+1)); g_snprintf(ff->atom[2], FF_MAX_SYMBOL, "%s", *(buff+2)); ff->bond_value = str_to_float(*(buff+4)); ff->bond_units = FF_DEG; ff->data[0] = str_to_float(*(buff+5)); /* strictly - kJ/mol rad-2 */ ff->data_units = FF_KJ; ff->atoms_current = ff->atoms_expected; ff->data_current = ff->data_expected; break; default: ua++; } } break; case DIHEDRAL: if (num_tokens > 4) { type = g_ascii_strtod(*(buff+4), NULL); switch (type) { case 1: if (num_tokens < 7) { ud++; break; } ff = ff_type_new(FF_DIHEDRAL); g_snprintf(ff->atom[0], FF_MAX_SYMBOL, "%s", *(buff+0)); g_snprintf(ff->atom[1], FF_MAX_SYMBOL, "%s", *(buff+1)); g_snprintf(ff->atom[2], FF_MAX_SYMBOL, "%s", *(buff+2)); g_snprintf(ff->atom[3], FF_MAX_SYMBOL, "%s", *(buff+3)); ff->bond_value = str_to_float(*(buff+5)); ff->bond_units = FF_DEG; ff->data[0] = str_to_float(*(buff+6)); ff->data_units = FF_KJ; ff->atoms_current = ff->atoms_expected; ff->data_current = ff->data_expected; break; case 3: if (num_tokens < 11) { ud++; break; } ff = ff_type_new(FF_DIHEDRAL_RB); g_snprintf(ff->atom[0], FF_MAX_SYMBOL, "%s", *(buff+0)); g_snprintf(ff->atom[1], FF_MAX_SYMBOL, "%s", *(buff+1)); g_snprintf(ff->atom[2], FF_MAX_SYMBOL, "%s", *(buff+2)); g_snprintf(ff->atom[3], FF_MAX_SYMBOL, "%s", *(buff+3)); ff->data[0] = str_to_float(*(buff+5)); ff->data[1] = str_to_float(*(buff+6)); ff->data[2] = str_to_float(*(buff+7)); ff->data[3] = str_to_float(*(buff+8)); ff->data[4] = str_to_float(*(buff+9)); ff->data[5] = str_to_float(*(buff+10)); ff->data_units = FF_KJ; ff->atoms_current = ff->atoms_expected; ff->data_current = ff->data_expected; break; default: ud++; } } break; } /* add the object to the return list */ if (ff) list = g_slist_prepend(list, ff); g_strfreev(buff); break; } g_free(line); line = file_read_line(fp); } g_free(line); #if DEBUG_GROMACS_READ_FF printf("processed: %d items.\n", g_slist_length(list)); printf("ignored: [bonds = %d] [angles = %d] [dihedrals = %d]\n", ub, ua, ud); /* ff_dump_type(FF_IMPROPER, list); */ #endif file_skip_comment(TRUE); return(list); }
gint read_cel(gchar *filename, struct model_pak *model) { gchar *line; FILE *fp; int i; gint num_tokens, natom=0; gchar **buff; struct core_pak *core; /* checks */ g_return_val_if_fail(model != NULL, 1); g_return_val_if_fail(filename != NULL, 2); fp = fopen(filename, "rt"); if (!fp) return 3; /* 1st line - cell parameters */ line = file_read_line(fp); if (!line || strlen(line) < 5 || g_ascii_strncasecmp("cell", line, 4) != 0) { printf("The first line should start with the keyword CELL.\n"); return 4; } buff = tokenize(line+4, &num_tokens); g_free(line); if (num_tokens < 6) { g_strfreev(buff); printf("Keyword CELL should be followed by six numbers.\n"); return 5; } for (i=0; i<3; ++i) model->pbc[i] = str_to_float(buff[i]); for (i=3; i<6; ++i) model->pbc[i] = str_to_float(buff[i]) * D2R; g_strfreev(buff); /* next lines - atomic positions */ for (;;) { line = file_read_line(fp); if (!line) /*the end of file*/ { printf("No 'rgnr' symmetry line found.\n"); return 6; } else if (g_ascii_strncasecmp("rgnr", line, 4) == 0) /*no more atomic pos.*/ { break; } else if (g_ascii_strncasecmp("natom", line, 5) == 0)/*number of atoms */ /*some old .cel files have second line with number of atoms eg. "natom 6"*/ { buff = tokenize(line, &num_tokens); if (num_tokens > 1) natom = str_to_float(buff[1]); else printf("Warning: ignoring `natom' line:\n%s\n", line); g_free(line); g_strfreev(buff); } else if (strncmp(" ", line, 4) != 0) /* atomic position */ { buff = tokenize(line, &num_tokens); g_free(line); if (num_tokens < 5) { g_strfreev(buff); continue; } core = new_core(*buff, model); core->atom_label = g_strdup(buff[0]); /* in second column there is either atomic number * or something like "Mg2+" or "K+". The second form is for * " the use of different bonding states of one and the same * element (e.g. Fe2+ and Fe3+ in Fe3O4)" * FIXME how these bonding states can be interpreted in GDIS */ if (g_ascii_isdigit(buff[1][0])) core->atom_code = str_to_float(buff[1]); else { core->atom_code = elem_symbol_test(buff[1]); } for (i=0; i<3; ++i) core->x[i] = str_to_float(buff[2+i]); /* TODO interpret 2 next optional numbers: * so-called multiplied substitution and replacement factor (SOF) * and isotropic Debye-Waller factor * FIXME can they be interpreted by GDIS? -MW */ model->cores = g_slist_prepend(model->cores, core); g_strfreev(buff); } else /* replacement atom */ { /*FIXME replacement atoms are now silently ignored * how can I use this information in GDIS? - MW*/ g_free(line); } } /* last line - symmetry */ buff = tokenize(line, &num_tokens); model->sginfo.spacenum = str_to_float(buff[1]); /* FIXME/TODO how to interpret the second (optional) number? * From fileformat docs: * "sometimes there exists more than one setting of a space-group type. * Thus, a further number must be given if the structure hasn't been described * using a conventional setting (standard setting)." * http://users.omskreg.ru/~kolosov/bam/a_v/v_1/powder/details/strucdat.htm * http://users.omskreg.ru/~kolosov/bam/a_v/v_1/powder/details/setting.htm * Unfortunatelly I'm ignorant about space-groups - MW */ g_free(line); g_strfreev(buff); if (natom>0 && natom != g_slist_length(model->cores)) printf("Warning: expected %i atoms, have %i.", natom, g_slist_length(model->cores)); /* model setup */ model->fractional = TRUE; model->periodic = 3; strcpy(model->filename, filename); g_free(model->basename); model->basename = parse_strip(filename); model_prep(model); return 0; }
/*! \brief Reads a jamfile_cache from a file. Only cache entries for files, that don't have an entry in \a cache yet, are added to it. \param cache The jamfile_cache the cache stored in the file shall be added to. \param filename The name of the file containing the cache to be read. \return \c !0, if everything went fine, 0, if an error occured. */ static int read_jcache(jamfile_cache* cache, char* filename) { int result = 0; if (cache && filename) { // open file FILE *file = 0; cache->cache_file = filename; if ((file = fopen(filename, "r")) != 0) { // read the file char buffer[512]; long count = 0; int i; result = !0; // read number of cache entries result = file_read_line_long(file, &count); // read the cache entries for (i = 0; result && i < count; i++) { char entryname[PATH_MAX]; long lineCount = 0; time_t time = 0; jcache_entry entry = { 0, 0, 0 }; // entry name, time and line count if (file_read_line(file, entryname, sizeof(entryname)) && strlen(entryname) > 0 && file_read_line_long(file, &time) && file_read_line_long(file, &lineCount) && (init_jcache_entry(&entry, entryname, time, 0)) != 0) { // read the lines int j; for (j = 0; result && j < lineCount; j++) { if (fgets(buffer, sizeof(buffer), file)) { char *string = (char*)malloc(strlen(buffer) + 1); if (string) { strcpy(string, buffer); result = push_string(entry.strings, string); } else result = 0; } else { fprintf(stderr, "warning: Invalid jamfile cache: " "Unexpected end of file.\n"); result = 0; } } } else { fprintf(stderr, "warning: Invalid jamfile cache: " "Failed to read file info.\n"); result = 0; } if (result) { // add only, if there's no entry for that file yet if (find_jcache_entry(cache, entry.filename)) cleanup_jcache_entry(&entry); else result = add_jcache_entry(cache, &entry); } // cleanup on error if (!result) cleanup_jcache_entry(&entry); } // close the file fclose(file); } // else: Couldn't open cache file. Don't worry. } return result; }
gint read_gromacs_gro(gchar *filename, struct model_pak *model) { gint num_tokens; gchar *line, **buff; FILE *fp; g_assert(model != NULL); fp = fopen(filename,"rt"); if (!fp) return(1); /* skip title */ line = file_read_line(fp); g_free(line); line = file_read_line(fp); while (line) { /* FIXME - properly should do formatted read, since things can be */ /* adjacent with no spaces in between -> tokenize() will fail */ /* fprintf(fp, "%5i%5s%5s%5i%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f\n", n, "UNK", core->atom_label, 1, x[0], x[1], x[2], 0.1*core->v[0], 0.1*core->v[1], 0.1*core->v[2]); */ if (strlen(line) > 10) { /* tokenize from atom label (avoids some problems with no space separation) */ buff = tokenize(&line[10], &num_tokens); if (num_tokens > 4) { /* TODO - deal with water properly (ie set atom_types) */ if (g_ascii_strncasecmp(*(buff+0), "HW", 2) == 0) { g_free(*(buff+0)); *(buff+0) = g_strdup("H"); } if (g_ascii_strncasecmp(*(buff+0), "OW", 2) == 0) { g_free(*(buff+0)); *(buff+0) = g_strdup("O"); } if (elem_test(*(buff+0))) { struct core_pak *core = new_core(*(buff+0), model); core->x[0] = 10.0*str_to_float(*(buff+2)); core->x[1] = 10.0*str_to_float(*(buff+3)); core->x[2] = 10.0*str_to_float(*(buff+4)); model->cores = g_slist_prepend(model->cores, core); } } g_strfreev(buff); } g_free(line); line = file_read_line(fp); } model->cores = g_slist_reverse(model->cores); model_prep(model); fclose(fp); return(0); }
/* * Try to locate a key in a file */ static int file_search_or_reload(mnt_map *m, FILE *fp, char *map, char *key, char **val, void (*fn) (mnt_map *m, char *, char *)) { char key_val[INFO_MAX_LINE_LEN]; int chuck = 0; int line_no = 0; while (file_read_line(key_val, sizeof(key_val), fp)) { char *kp; char *cp; char *hash; int len = strlen(key_val); line_no++; /* * Make sure we got the whole line */ if (key_val[len - 1] != '\n') { plog(XLOG_WARNING, "line %d in \"%s\" is too long", line_no, map); chuck = 1; } else { key_val[len - 1] = '\0'; } /* * Strip comments */ hash = strchr(key_val, '#'); if (hash) *hash = '\0'; /* * Find start of key */ for (kp = key_val; *kp && isascii((unsigned char)*kp) && isspace((unsigned char)*kp); kp++) ; /* * Ignore blank lines */ if (!*kp) goto again; /* * Find end of key */ for (cp = kp; *cp && (!isascii((unsigned char)*cp) || !isspace((unsigned char)*cp)); cp++) ; /* * Check whether key matches */ if (*cp) *cp++ = '\0'; if (fn || (*key == *kp && STREQ(key, kp))) { while (*cp && isascii((unsigned char)*cp) && isspace((unsigned char)*cp)) cp++; if (*cp) { /* * Return a copy of the data */ char *dc; /* if m->cfm == NULL, not using amd.conf file */ if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX)) dc = sun_entry2amd(kp, cp); else dc = xstrdup(cp); if (fn) { (*fn) (m, xstrdup(kp), dc); } else { *val = dc; dlog("%s returns %s", key, dc); } if (!fn) return 0; } else { plog(XLOG_USER, "%s: line %d has no value field", map, line_no); } } again: /* * If the last read didn't get a whole line then * throw away the remainder before continuing... */ if (chuck) { while (fgets(key_val, sizeof(key_val), fp) && !strchr(key_val, '\n')) ; chuck = 0; } } return fn ? 0 : ENOENT; }
void parse_config(const char *conf_file, struct options *opt) { FILE *fh; struct buffer line; unsigned int lineno = 1; int errors = 0; /* Journal hasn't been opened yet */ printf("Reading configuration file at \"%s\"...\n", conf_file); if (opt->strict) { security_conf_file_check(conf_file); } fh = fopen(conf_file, "r"); if (fh == NULL) { fprintf(stderr, "Unable to open configuration file \"%s\": %s.\n", conf_file, strerror(errno)); cleanup(EXIT_IO, true); } #if DEBUG printf("Raw key/value pairs from config file:\n"); #endif /* DEBUG */ while (file_read_line(fh, conf_file, &lineno, &line)) { struct buffer key, val; bool success; int ch; if (!read_key_and_value(conf_file, lineno, &line, &key, &val)) { if (key.length) { errors++; } continue; } /* Check each possible option */ if (!strcasecmp(key.data, "Daemonize")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->daemonize = ch; } else { errors++; } } else if (!strcasecmp(key.data, "TransportProtocol")) { if (!strcasecmp(val.data, "tcp")) { opt->tproto = PROTOCOL_TCP; } else if (!strcasecmp(val.data, "udp")) { opt->tproto = PROTOCOL_UDP; } else { fprintf(stderr, "%s:%d: invalid transport protocol: \"%s\"\n", conf_file, lineno, val.data); errors++; } } else if (!strcasecmp(key.data, "InternetProtocol")) { if (!strcasecmp(val.data, "both")) { opt->iproto = PROTOCOL_BOTH; } else if (!strcasecmp(val.data, "ipv4")) { opt->iproto = PROTOCOL_IPv4; } else if (!strcasecmp(val.data, "ipv6")) { opt->iproto = PROTOCOL_IPv6; } else { fprintf(stderr, "%s:%d: invalid internet protocol: \"%s\"\n", conf_file, lineno, val.data); errors++; } } else if (!strcasecmp(key.data, "Port")) { /* atoi is ok because it returns 0 in case of failure, and 0 isn't a valid port */ int port = atoi(val.data); if (0 >= port || port > PORT_MAX) { fprintf(stderr, "%s:%d: invalid port number: \"%s\"\n", conf_file, lineno, val.data); errors++; } else { opt->port = port; } } else if (!strcasecmp(key.data, "StrictChecking")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->strict = ch; } else { errors++; } } else if (!strcasecmp(key.data, "DropPrivileges")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->drop_privileges = ch; } else { errors++; } } else if (!strcasecmp(key.data, "PidFile")) { if (!strcmp(val.data, "none")) { opt->pid_file = NULL; continue; } opt->pid_file = strdup(val.data); if (unlikely(!opt->pid_file)) { perror("Unable to allocate memory for config value"); fclose(fh); cleanup(EXIT_MEMORY, true); } } else if (!strcasecmp(key.data, "RequirePidFile")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->require_pidfile = ch; } else { errors++; } } else if (!strcasecmp(key.data, "JournalFile")) { if (!strcmp(val.data, "-")) { opt->journal_file = NULL; } else if (strcmp(val.data, "none") != 0) { opt->journal_file = strdup(val.data); if (unlikely(!opt->journal_file)) { perror("Unable to allocate memory for config value"); fclose(fh); cleanup(EXIT_MEMORY, true); } } } else if (!strcasecmp(key.data, "QuotesFile")) { opt->quotes_file = strdup(val.data); if (unlikely(!opt->quotes_file)) { perror("Unable to allocate memory for config value"); fclose(fh); cleanup(EXIT_MEMORY, true); } } else if (!strcasecmp(key.data, "QuoteDivider")) { if (!strcasecmp(val.data, "line")) { opt->linediv = DIV_EVERYLINE; } else if (!strcasecmp(val.data, "percent")) { opt->linediv = DIV_PERCENT; } else if (!strcasecmp(val.data, "file")) { opt->linediv = DIV_WHOLEFILE; } else { fprintf(stderr, "%s:%d: unsupported division type: \"%s\"\n", conf_file, lineno, val.data); errors++; } } else if (!strcasecmp(key.data, "PadQuotes")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->pad_quotes = ch; } else { errors++; } } else if (!strcasecmp(key.data, "DailyQuotes")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->is_daily = ch; } else { errors++; } } else if (!strcasecmp(key.data, "AllowBigQuotes")) { ch = str_to_bool(val.data, conf_file, lineno, &success); if (likely(success)) { opt->allow_big = ch; } else { errors++; } } else { fprintf(stderr, "%s:%d: unknown conf option: \"%s\"\n", conf_file, lineno, key.data); errors++; } lineno++; } fclose(fh); if (opt->strict && errors) { fprintf(stderr, "Your configuration file has %d issue%s. The daemon will not start.\n" "(To disable this behavior, use the --lax flag when running).\n", errors, PLURAL(errors)); cleanup(EXIT_SECURITY, true); } }
/* * Parse the stream sun_in, convert the map information to amd, write * the results to amd_out. */ static int sun2amd_convert(FILE *sun_in, FILE *amd_out) { char line_buff[INFO_MAX_LINE_LEN], *tmp, *key, *entry; int pos, line = 0, retval = 1; /* just to be safe */ memset(line_buff, 0, sizeof(line_buff)); /* Read the input line by line and do the conversion. */ while ((pos = file_read_line(line_buff, sizeof(line_buff), sun_in))) { line++; line_buff[pos - 1] = '\0'; /* remove comments */ if ((tmp = strchr(line_buff, '#')) != NULL) { *tmp = '\0'; } /* find start of key */ key = line_buff; while (*key != '\0' && isspace((unsigned char)*key)) { key++; } /* ignore blank lines */ if (*key == '\0') { continue; } /* find the end of the key and NULL terminate */ tmp = key; while (*tmp != '\0' && isspace((unsigned char)*tmp) == 0) { tmp++; } if (*tmp == '\0') { plog(XLOG_ERROR, "map line %d has no entry", line); goto err; } *tmp++ = '\0'; if (*tmp == '\0') { plog(XLOG_ERROR, "map line %d has no entry", line); goto err; } entry = tmp; /* convert the sun entry to an amd entry */ if ((tmp = sun_entry2amd(key, entry)) == NULL) { plog(XLOG_ERROR, "parse error on line %d", line); goto err; } if (fprintf(amd_out, "%s %s\n", key, tmp) < 0) { plog(XLOG_ERROR, "can't write to output stream: %s", strerror(errno)); goto err; } /* just to be safe */ memset(line_buff, 0, sizeof(line_buff)); } /* success */ retval = 0; err: return retval; }
/* reads from a file data for a sinlge spot, data may be partial */ static rc_t read_next_spot(HelicosLoaderFmt* self, HelicosFileInfo* file) { rc_t rc = 0; if( file->ready ) { /* data still not used */ return 0; } HelicosFileInfo_init(file); if( (rc = file_read_line(file, true)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading more data"); } else if( file->line == NULL ) { return 0; /* eof */ } if( file->line[0] == '@' ) { /*** fastq format **/ if( (rc = pstring_assign(&file->name, &file->line[1], file->line_len - 1)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading name"); } file->line = NULL; if( (rc = file_read_line(file, false)) != 0 || file->line_len > sizeof(file->sequence.data)-1 || (rc = pstring_assign(&file->sequence, file->line, file->line_len)) != 0 || !pstring_is_fasta(&file->sequence) ) { rc = rc ? rc : RC(rcSRA, rcFormatter, rcReading, rcData, rcUnrecognized); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading sequence"); } file->line = NULL; if( (rc = file_read_line(file, false)) != 0 || file->line[0] != '+' || file->line_len != 1 ) { rc = RC(rcSRA, rcFormatter, rcReading, rcData, rcCorrupt); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading quality defline"); } file->line = NULL; if( (rc = file_read_line(file, false)) != 0 || file->line_len > sizeof(file->quality.data)-1 || (rc = pstring_assign(&file->quality, file->line, file->line_len)) != 0 || (rc = pstring_quality_convert(&file->quality, eExperimentQualityEncoding_Ascii, 33, 0, 0x7F)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading quality"); } file->line = NULL; file->ready = true; } else if( file->line[0] == '>' ) { /** fasta format **/ if( (rc = pstring_assign(&file->name, &file->line[1], file->line_len - 1)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading name"); } file->line = NULL; if( (rc = file_read_line(file, false)) != 0 || file->line_len > sizeof(file->sequence.data)-1 || (rc = pstring_assign(&file->sequence, file->line, file->line_len)) != 0 || !pstring_is_fasta(&file->sequence) ) { rc = rc ? rc : RC(rcSRA, rcFormatter, rcReading, rcData, rcUnrecognized); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading sequence"); } file->line = NULL; file->quality.len = file->sequence.len; memset(file->quality.data,14,file->quality.len); file->ready = true; } else { rc = RC(rcSRA, rcFormatter, rcReading, rcFile, rcInvalid); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=expected '@'"); } #if _DEBUGGING DEBUG_MSG(3, ("READ: name:'%s', seq[%u]:'%s', qual[%u]\n", file->name.data, file->sequence.len, file->sequence.data, file->quality.len)); /* DEBUG_MSG(3, ("READ: name:'%s', seq[%u]:'%s', qual[%u]:'%s'\n", file->name.data, file->sequence.len, file->sequence.data, file->quality.len, file->quality.data));*/ #endif return 0; }
gint reaxmd_parse_properties(FILE *fp, struct model_pak *model, gpointer import) { gint i, j, k, n=1, time_column=0, num_tokens, num_properties; gdouble frame_time=0.0, time, start, stop, value; gchar *line, *label, **buff; const gchar *xlabel, *ylabel; GSList *item, *list; GArray **property_array; #if DEBUG_REAXMD_PARSE_PROPERTIES printf("file_reaxmd(): parsing energy file...\n"); #endif /* checks */ if (!fp) return(FALSE); if (!import) return(FALSE); /* setup internal frame counter */ if (model) { n = g_list_length(model->animate_list); i=0; frame_time = str_to_float(animate_frame_property_get(i, "Time", model)); } // NB: file_read_line() skips comments by default, so it'll auto skip the header line file_skip_comment(FALSE); // process first line for column locations of time, energy, etc ... line = file_read_line(fp); // error handling if (!line) { return(FALSE); } if (strlen(line) < 2) { return(FALSE); } buff = g_strsplit(&line[1], "|", -1); list = NULL; if (buff) { j=0; while (buff[j]) { label = g_strstrip(buff[j]); if (strlen(label)) { if (g_strrstr(label, "Time")) { time_column = j; } list = g_slist_prepend(list, g_strdup(label)); } j++; } } g_strfreev(buff); g_free(line); line = file_read_line(fp); list = g_slist_reverse(list); #if DEBUG_REAXMD_PARSE_PROPERTIES for (item=list ; item ; item=g_slist_next(item)) { printf("[%s]\n", (gchar *) item->data); } printf("Allocating %d property arrays, with start size %d\n", g_slist_length(list), n); #endif // NB: exclude the first - time /* init arrays for properties */ num_properties = g_slist_length(list); property_array = g_malloc(num_properties * sizeof(GArray)); for (k=0 ; k<num_properties ; k++) { property_array[k] = g_array_sized_new(FALSE, FALSE, sizeof(gdouble), n); } /* iterate over all output data lines */ while (line) { buff = tokenize(line, &num_tokens); if (num_tokens > time_column) { time = str_to_float(buff[time_column]); /* NEW - grab all values for property graphs */ for (j=0 ; j<num_tokens ; j++) { if (j < num_properties) { value = str_to_float(buff[j]); g_array_append_val(property_array[j], value); } } /* if output data time matches internal frame frame - store */ if (model) { if (time >= frame_time) { /* add current output data to the internally stored frame */ item = list; for (j=0 ; j<num_tokens ; j++) { if (item) { //printf("[%d][%s][%s]\n", i, (gchar *) item->data, buff[j]); // add to actual model property list (reference frame only) // leave this out now, since we're auto constructing graphs anyway /* if (!i) { property_add_ranked(j, item->data, buff[j], model); } */ animate_frame_property_put(i, item->data, buff[j], model); } else break; item = g_slist_next(item); } /* update the internal frame count */ i++; frame_time = str_to_float(animate_frame_property_get(i, "Time", model)); } } } g_strfreev(buff); g_free(line); line = file_read_line(fp); } /* iterate property arrays and construct graphs */ xlabel = g_slist_nth_data(list, time_column); for (j=0 ; j<num_properties ; j++) { // don't graph time column if (j != time_column) { gpointer data, graph; k = property_array[j]->len; data = g_array_free(property_array[j], FALSE); start = g_array_index(property_array[time_column],gdouble,0); stop = g_array_index(property_array[time_column],gdouble,k-1); #if DEBUG_REAXMD_PARSE_PROPERTIES printf("Building graph for: %s with %d data points and ", (gchar *) g_slist_nth_data(list, j), k); printf("range: %f - %f\n", start, stop); #endif /* add graph if no ! prefix */ ylabel = g_slist_nth_data(list, j); if (!g_strrstr(ylabel, "!")) { /* create and attach graph object */ label = g_strndup(ylabel, 8); graph = graph_new(label); g_free(label); graph_set_data(k, data, start, stop, graph); graph_x_label_set(xlabel, graph); graph_y_label_set(ylabel, graph); import_object_add(IMPORT_GRAPH, graph, import); } } } g_array_free(property_array[time_column], TRUE); g_free(property_array); /* cleanup */ free_slist(list); return(0); }
/* return error */ static int APP_CC l_file_read_section(int fd, int max_file_size, const char *section, struct list *names, struct list *values) { struct stream *s; char *data; char *text; char *name; char *value; char *lvalue; char c; int in_it; int in_it_index; int len; int index; int file_size; data = (char *) g_malloc(FILE_MAX_LINE_BYTES * 3, 0); text = data; name = text + FILE_MAX_LINE_BYTES; value = name + FILE_MAX_LINE_BYTES; file_size = 32 * 1024; /* 32 K file size limit */ g_file_seek(fd, 0); in_it_index = 0; in_it = 0; g_memset(text, 0, FILE_MAX_LINE_BYTES); list_clear(names); list_clear(values); make_stream(s); init_stream(s, file_size); len = g_file_read(fd, s->data, file_size); if (len > 0) { s->end = s->p + len; for (index = 0; index < len; index++) { if (!s_check_rem(s, 1)) { break; } in_uint8(s, c); if ((c == '#') || (c == ';')) { if (file_read_line(s, text, FILE_MAX_LINE_BYTES) != 0) { break; } in_it = 0; in_it_index = 0; g_memset(text, 0, FILE_MAX_LINE_BYTES); continue; } if (c == '[') { in_it = 1; } else if (c == ']') { if (g_strcasecmp(section, text) == 0) { file_read_line(s, text, FILE_MAX_LINE_BYTES); while (file_read_line(s, text, FILE_MAX_LINE_BYTES) == 0) { if (g_strlen(text) > 0) { file_split_name_value(text, name, value); list_add_item(names, (tbus)g_strdup(name)); if (value[0] == '$') { lvalue = g_getenv(value + 1); if (lvalue != 0) { list_add_item(values, (tbus)g_strdup(lvalue)); } else { list_add_item(values, (tbus)g_strdup("")); } } else { list_add_item(values, (tbus)g_strdup(value)); } } } free_stream(s); g_free(data); return 0; } in_it = 0; in_it_index = 0; g_memset(text, 0, FILE_MAX_LINE_BYTES); } else if (in_it) { text[in_it_index] = c; in_it_index++; if (in_it_index >= FILE_MAX_LINE_BYTES) { break; } } } } free_stream(s); g_free(data); return 1; }
/* reads from a file data for a sinlge spot, data may be partial */ static rc_t read_next_spot(FastqLoaderFmt* self, FastqFileInfo* file) { rc_t rc = 0; if( file->spot->ready ) { /* data still not used */ return 0; } FileReadData_init(file->spot, false); FileReadData_init(&file->spot[1], false); if( (rc = file_read_line(file, true)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading more data"); } else if( file->line == NULL ) { return 0; /* eof */ } if( find_seq_qual_by_sep(self, file, ':') || find_seq_qual_by_sep(self, file, ' ') ) { /* single line forms */ file->line = NULL; /* line consumed */ file->spot->ready = true; } else if( file->line[0] == '>' || file->line[0] == '@' ) { /* 4 or 8 line format */ FileReadData sd; uint8_t word = 0, best_word = 0; uint8_t score = 0, best_score = 0; /* find and parse spot name on defline */ do { score = parse_spot_name(file->file, &sd, &file->line[1], file->line_len - 1, ++word); if( score > best_score ) { if( (rc = pstring_copy(&file->spot->name, &sd.name)) != 0 || (rc = pstring_copy(&file->spot->barcode, &sd.barcode)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=copying read name"); } file->spot->read.read_id = sd.read.read_id; best_score = score; best_word = word; /* used below for quality defline parsing */ } } while(score != 0); if( best_score == 0 ) { rc = RC(rcSRA, rcFormatter, rcReading, rcId, rcNotFound); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=spot name not detected"); } if( file->line[0] == '@' ) { if( (rc = read_spot_data_3lines(file, file->spot, best_word, best_score, file->qualType)) != 0 ) { return rc; } /* now we MAY have 5th line in buffer so we can check if it's 2nd read for 8 lines format */ if( file->line_len != 0 && file->line != NULL && file->line[0] == '@' ) { /* try to find read id on next line */ FileReadData_init(&file->spot[1], false); if( parse_spot_name(file->file, &file->spot[1], &file->line[1], file->line_len - 1, best_word) == best_score ) { if( pstring_cmp(&file->spot->name, &file->spot[1].name) == 0 && pstring_cmp(&file->spot->barcode, &file->spot[1].barcode) == 0 && file->spot->read.read_id != file->spot[1].read.read_id ) { /* since it is different read id with same name and barcode, fill up second read */ if( (rc = read_spot_data_3lines(file, &file->spot[1], best_word, best_score, file->qualType)) != 0 ) { return rc; } } } } } else { /* 2 line seq or quality form */ file->line = NULL; /* line consumed */ /* read sequence/quality */ if( (rc = read_multiline_seq_or_qual(file, '>', &file->spot->read.seq)) != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=reading seq/qual data"); } if( file->spot->read.seq.len == 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=empty string reading seq/qual data"); } else if( !pstring_is_fasta(&file->spot->read.seq) ) { /* swap */ if( (rc = pstring_copy(&file->spot->read.qual, &file->spot->read.seq)) == 0 ) { file->spot->read.qual_type = file->qualType; pstring_clear(&file->spot->read.seq); } } file->spot->ready = true; } } else { rc = RC(rcSRA, rcFormatter, rcReading, rcFile, rcInvalid); return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=file corrupt or format unknown"); } if( rc == 0 ) { int k; for(k = 0; k < 2; k++) { FileReadData* rd = &file->spot[k]; if( rd->ready && rd->read.qual_type != ILLUMINAWRITER_COLMASK_NOTSET ) { if( file->qualOffset == 0 ) { /* detect and remember */ file->qualOffset = 33; file->qualMax = 94; rc = pstring_quality_convert(&rd->read.qual, file->qualEnc, file->qualOffset, file->qualMin, file->qualMax); if( GetRCState(rc) == rcOutofrange ) { file->qualOffset = 64; file->qualMax = 61; rc = pstring_quality_convert(&rd->read.qual, file->qualEnc, file->qualOffset, file->qualMin, file->qualMax); } } else { if(file->qualOffset == 33) file->qualMax = 94; rc = pstring_quality_convert(&rd->read.qual, file->qualEnc, file->qualOffset, file->qualMin, file->qualMax); } if( rc != 0 ) { return SRALoaderFile_LOG(file->file, klogErr, rc, "$(msg)", "msg=converting quality"); } } } } return 0; }
void command_main_loop(int argc, char **argv) { gint n, status, num_tokens; gchar *inp, *out, *ext, *tmp, *line, *base, **buff; gboolean exit=FALSE; GSList *list, *files, *item, *structures; struct file_pak *inp_pak, *out_pak; struct model_pak *model; if (argc < 2) printf("gdis> Commandline interface. Enter ? for help.\n"); /* command line */ do { /* if additional keywords - process as single command line */ if (argc > 1) { /* convert options into single string for processing */ buff = g_malloc(argc*sizeof(gchar *)); for (n=1 ; n<argc ; n++) *(buff+n-1) = g_strdup(argv[n]); *(buff+argc-1) = NULL; line = g_strjoinv(" ", buff); g_strfreev(buff); exit = TRUE; } else { /* standard repeating prompt */ printf("gdis> "); line = file_read_line(stdin); } /* quit primitive */ if (g_ascii_strncasecmp("qu", line, 2) == 0 || g_ascii_strncasecmp("ex", line, 2) == 0) exit=TRUE; /* test primitive */ if (g_ascii_strncasecmp("te", line, 2) == 0) { buff = tokenize(line, &num_tokens); for (n=1 ; n<num_tokens ; n++) test_run(*(buff+n)); g_strfreev(buff); } /* copy primitive */ if (g_ascii_strncasecmp("co", line, 2) == 0 || g_ascii_strncasecmp("cp", line, 2) == 0) { buff = tokenize(line, &num_tokens); if (num_tokens > 2) { out_pak = get_file_info(*(buff+2), BY_EXTENSION); if (!out_pak) { printf("Error: unrecognized output filetype.\n"); break; } if (!out_pak->write_file) { printf("Error: write method for this filetype does not exist.\n"); break; } /* FIXME! - this is all wrong - should be no dependence on sysenv.cwd for path */ /* since this falls over with something like prompt:>gdis cp /tmp/gok.sot /tmp/dest/gok.meta */ ext = file_extension_get(*(buff+2)); files = file_dir_list_pattern(sysenv.cwd, *(buff+1)); for (list=files ; list ; list=g_slist_next(list)) { inp_pak = get_file_info(list->data, BY_EXTENSION); inp = g_build_filename(sysenv.cwd, list->data, NULL); /* FIXME - changed to correct a problem with Florian's metadata store */ // base = parse_strip(list->data); base = g_strdup(list->data); if (inp_pak) { if (inp_pak->read_file) { model = model_new(); status = inp_pak->read_file(inp, model); structures = g_slist_find(sysenv.mal, model); #if DEBUG_COMMAND_MAIN_LOOP printf("Items loaded = %d [status = %d]\n", g_slist_length(structures), status); #endif if (g_slist_length(structures) > 1) { /* multiple output for input files that contain more than one structure */ n = 1; for (item=structures ; item ; item=g_slist_next(item)) { model = item->data; tmp = g_strdup_printf("%s_%d.%s", base, n, ext); out = g_build_filename(sysenv.cwd, tmp, NULL); status = out_pak->write_file(out, model); #if DEBUG_COMMAND_MAIN_LOOP printf("a [%s] -> [%s]\n", inp, out); #endif g_free(out); g_free(tmp); model_free(model); n++; } } else { tmp = g_strdup_printf("%s.%s", base, ext); out = g_build_filename(sysenv.cwd, tmp, NULL); g_free(tmp); status = out_pak->write_file(out, model); #if DEBUG_COMMAND_MAIN_LOOP printf("b [%s] -> [%s]\n", inp, out); #endif g_free(out); model_free(model); } } else printf("Error: read method for this filetype does not exist.\n"); } else { printf("Error: unrecognized input filetype.\n"); } g_free(base); g_free(inp); } /* CURRENT hack */ if (!files) { /* FIXME */ } g_free(ext); free_slist(files); } else printf("Error: insufficient tokens for <copy> command\n"); g_strfreev(buff); } if (g_ascii_strncasecmp("ls", line, 2) == 0) { buff = tokenize(line, &num_tokens); if (num_tokens > 1) files = file_dir_list_pattern(sysenv.cwd, *(buff+1)); else files = file_dir_list(sysenv.cwd, FALSE); for (list=files ; list ; list=g_slist_next(list)) { printf("%s\n", (gchar *) list->data); } g_strfreev(buff); } if (g_ascii_strncasecmp("cd", line, 2) == 0) { buff = tokenize(line, &num_tokens); if (num_tokens > 1) { inp = g_build_path(DIR_SEP, sysenv.cwd, *(buff+1), NULL); set_path(inp); g_free(inp); } printf("%s\n", sysenv.cwd); g_strfreev(buff); } if (g_ascii_strncasecmp("pwd", line, 3) == 0) { printf("%s\n", sysenv.cwd); } if (g_ascii_strncasecmp("?", line, 1) == 0 || g_ascii_strncasecmp("h", line, 1) == 0) { printf("GDIS v%4.2f available commands\n\n", VERSION); printf("cd <destination>\n"); printf(" - changes to target directory\n"); printf("pwd\n"); printf(" - prints the current working directory\n"); printf("ls\n"); printf(" - lists the files in the current working directory\n"); printf("copy <source> <destination>\n"); printf(" - convert between filetypes, based on extension (wildcards allowed)\n"); printf("quit\n"); printf(" - exit program\n"); } /* done -> next line */ g_free(line); } while (!exit); }