~ubuntu-branches/ubuntu/breezy/garlic/breezy

« back to all changes in this revision

Viewing changes to load_sequence.c

  • Committer: Bazaar Package Importer
  • Author(s): zhaoway
  • Date: 2001-04-24 07:09:13 UTC
  • Revision ID: james.westby@ubuntu.com-20010424070913-uzpupnwdfhmliebz
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 Damir Zucic */
 
2
 
 
3
/*=============================================================================
 
4
 
 
5
                                load_sequence.c
 
6
 
 
7
Purpose:
 
8
        Load sequence from file.
 
9
 
 
10
Input:
 
11
        (1) Pointer to RuntimeS structure.
 
12
 
 
13
Output:
 
14
        (1) Sequence stored to the sequence buffer.
 
15
        (2) Return value.
 
16
 
 
17
Return value:
 
18
        (1) Positive on success.
 
19
        (2) Negative on failure.
 
20
 
 
21
Notes:
 
22
        (1) The sequence is expected in three letters code or  in FASTA
 
23
            format  (one letter code,  but the first character in title
 
24
            line is '>').  If not in FASTA format,  one letter code may
 
25
            be missinterpreted as valid three letters code. File format
 
26
            is free,  but the maximal  input  line  should  not  exceed
 
27
            STRINGSIZE.
 
28
 
 
29
        (2) Space, comma, tab, semicolon and newline are interpreted as
 
30
            separators. Lines beginning with # (numbersign) are treated
 
31
            as comments.  Empty lines are ignored. This applies to both
 
32
            three letters code and to FASTA format.
 
33
 
 
34
        (3) The original  command string  is used  because the copy was
 
35
            converted to uppercase.
 
36
 
 
37
========includes:============================================================*/
 
38
 
 
39
#include <stdio.h>
 
40
 
 
41
#include <string.h>
 
42
#include <ctype.h>
 
43
 
 
44
#include <X11/Xlib.h>
 
45
#include <X11/Xutil.h>
 
46
#include <X11/Xos.h>
 
47
#include <X11/Xatom.h>
 
48
 
 
49
#include "defines.h"
 
50
#include "typedefs.h"
 
51
 
 
52
/*======function prototypes:=================================================*/
 
53
 
 
54
char            *ExtractToken_ (char *, int, char *, char *);
 
55
int             ReadFasta_ (RuntimeS *, char *);
 
56
FILE            *OpenFileForReading_ (char *);
 
57
void            InitHyphob_ (RuntimeS *);
 
58
 
 
59
/*======load sequence from file:=============================================*/
 
60
 
 
61
int LoadSequence_ (RuntimeS *runtimeSP)
 
62
{
 
63
int             max_length, i;
 
64
char            lineA[STRINGSIZE];
 
65
char            *remainderP;
 
66
char            tokenA[STRINGSIZE];
 
67
FILE            *fileP;
 
68
char            *P;
 
69
int             n;
 
70
size_t          residueI = 0;
 
71
int             token_length;
 
72
int             numberF;
 
73
size_t          offset;
 
74
 
 
75
/* The maximal residue name length: */
 
76
max_length = RESNAMESIZE - 1;
 
77
 
 
78
/* Zero initialize the sequence buffer: */
 
79
runtimeSP->residuesN = 0;
 
80
for (i = 0; i < runtimeSP->sequence_buffer_size; i++)
 
81
        {
 
82
        *(runtimeSP->sequenceP + i) = '\0';
 
83
        }
 
84
 
 
85
/* Copy the original command string: */
 
86
strncpy (lineA, runtimeSP->curr_commandA, STRINGSIZE - 1);
 
87
lineA[STRINGSIZE - 1] = '\0';
 
88
 
 
89
/* Skip two tokens: */
 
90
remainderP = ExtractToken_ (tokenA, STRINGSIZE, lineA, " \t\n");
 
91
if (!remainderP) return -1;
 
92
remainderP = ExtractToken_ (tokenA, STRINGSIZE, remainderP, " \t\n");
 
93
if (!remainderP) return -2;
 
94
 
 
95
/* The third token should contain the file name: */
 
96
remainderP = ExtractToken_ (tokenA, STRINGSIZE, remainderP, " \t\n");
 
97
if (!remainderP)
 
98
        {
 
99
        strcpy (runtimeSP->messageA, "File name missing!");
 
100
        runtimeSP->message_length = strlen (runtimeSP->messageA);
 
101
        return -3;
 
102
        }
 
103
 
 
104
/* Try to interpret file as FASTA (one letter code): */
 
105
if (ReadFasta_ (runtimeSP, tokenA) > 0) return 1;
 
106
 
 
107
/* If this point is reached, the input file was not in FASTA format. */
 
108
 
 
109
/* Try to open file: */
 
110
fileP = OpenFileForReading_ (tokenA);
 
111
if (fileP == NULL)
 
112
        {
 
113
        strcpy (runtimeSP->messageA, "Failed to open file!");
 
114
        runtimeSP->message_length = strlen (runtimeSP->messageA);
 
115
        return -4;
 
116
        }
 
117
 
 
118
/* Read file, line by line: */
 
119
while (fgets (lineA, STRINGSIZE, fileP))
 
120
        {
 
121
        /* Lines beginning with # are treated as comments: */
 
122
        if (lineA[0] == '#') continue;
 
123
 
 
124
        /* Convert to uppercase: */
 
125
        P = lineA;
 
126
        while ((n = *P++) != '\0') *(P - 1) = toupper (n);
 
127
 
 
128
        /* Parse line: */
 
129
        remainderP = lineA;
 
130
        while ((remainderP = ExtractToken_ (tokenA, STRINGSIZE,
 
131
                                            remainderP, " ,;\t\n")) != NULL)
 
132
                {
 
133
                /* Check  the token length - it should */
 
134
                /* not contain more than max_length characters: */
 
135
                token_length = strlen (tokenA);
 
136
                if (token_length > max_length)
 
137
                        {
 
138
                        sprintf (runtimeSP->messageA,
 
139
                                 "Bad residue name: %s", tokenA);
 
140
                        runtimeSP->message_length =
 
141
                                                strlen (runtimeSP->messageA);
 
142
                        fclose (fileP);
 
143
                        return -5;
 
144
                        }
 
145
 
 
146
                /* If this token contains nothing but digits, ignore it: */
 
147
                numberF = 1;
 
148
                for (i = 0; i < token_length; i++)
 
149
                        {
 
150
                        if ((isdigit (tokenA[i]) == 0) &&
 
151
                            (tokenA[i] != '-') && (tokenA[i] != '+'))
 
152
                                {
 
153
                                numberF = 0;
 
154
                                break;
 
155
                                }
 
156
                        }
 
157
                if (numberF) continue;
 
158
 
 
159
                /* Check is there enough space left in the buffer: */
 
160
                offset = max_length * residueI;
 
161
                if (offset > runtimeSP->sequence_buffer_size - 10 * max_length)
 
162
                        {
 
163
                        strcpy (runtimeSP->messageA, "Sequence too long!");
 
164
                        runtimeSP->message_length =
 
165
                                                strlen (runtimeSP->messageA);
 
166
                        fclose (fileP);
 
167
                        return -6;
 
168
                        }
 
169
 
 
170
                /* Copy the residue name to the sequence buffer: */
 
171
                P = runtimeSP->sequenceP + offset;
 
172
                strncpy (P, tokenA, max_length);
 
173
 
 
174
                /* Update the residue index: */
 
175
                residueI++;
 
176
                }
 
177
        }
 
178
 
 
179
/* Close file: */
 
180
fclose (fileP);
 
181
 
 
182
/* Store the number of residues: */
 
183
runtimeSP->residuesN = residueI;
 
184
 
 
185
/* Initialize serial numbers: */
 
186
for (residueI = 0; residueI < runtimeSP->residuesN; residueI++)
 
187
        {
 
188
        *(runtimeSP->serialIP + residueI) = residueI + 1;
 
189
        }
 
190
 
 
191
/* Initialize disulfide flags: */
 
192
for (residueI = 0; residueI < runtimeSP->residuesN; residueI++)
 
193
        {
 
194
        *(runtimeSP->disulfideFP + residueI) = 0;
 
195
        }
 
196
 
 
197
/* Initialize hydrophobicity values: */
 
198
InitHyphob_ (runtimeSP);
 
199
 
 
200
/* Return positive value on success: */
 
201
return 2;
 
202
}
 
203
 
 
204
/*===========================================================================*/
 
205
 
 
206