2
* International Chemical Identifier (InChI)
4
* Software version 1.03
7
* Originally developed at NIST
8
* Modifications and additions by IUPAC and the InChI Trust
10
* The InChI library and programs are free software developed under the
11
* auspices of the International Union of Pure and Applied Chemistry (IUPAC);
12
* you can redistribute this software and/or modify it under the terms of
13
* the GNU Lesser General Public License as published by the Free Software
15
* http://www.opensource.org/licenses/lgpl-2.1.php
20
The code in this #include file reads InChI AuxInfo
23
/****************************************************************************/
24
#define MIN_BOND_LENGTH (1.0e-6)
25
#define INCHI_LINE_LEN 512 /*1024*/ /*256*/
26
#define INCHI_LINE_ADD 384 /*128*/ /*64*/
27
/* Note: (INCHI_LINE_LEN - INCHI_LINE_ADD) > (length of the longest item: szCoord) = 33 */
28
/*****************************************************************************/
30
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
32
#define AB_MAX_WELL_DEFINED_PARITY inchi_max(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) /* 1, 2 => well defined parities, uncluding 'unknown' */
33
#define AB_MIN_WELL_DEFINED_PARITY inchi_min(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) /* min(INCHI_PARITY_ODD, INCHI_PARITY_EVEN) */
34
#define ATOM_PARITY_WELL_DEF(X) (AB_MIN_WELL_DEFINED_PARITY <= (X) && (X) <= AB_MAX_WELL_DEFINED_PARITY)
36
#define inchi_NUMH2(AT,CUR_AT) ((AT[CUR_AT].num_iso_H[0]>0?AT[CUR_AT].num_iso_H[0]:0) +AT[CUR_AT].num_iso_H[1]+AT[CUR_AT].num_iso_H[2]+AT[CUR_AT].num_iso_H[3])
38
#define SB_PARITY_FLAG 0x38 /* disconnected structure has undef. parity */
39
#define SB_PARITY_SHFT 3
40
#define SB_PARITY_MASK 0x07
41
#define SB_PARITY_1(X) (X & SB_PARITY_MASK) /* refers to connected structure */
42
#define SB_PARITY_2(X) (((X) >> SB_PARITY_SHFT) & SB_PARITY_MASK) /* refers to connected structure */
55
void FreeInchi_Atom( inchi_Atom **at );
56
inchi_Atom *CreateInchi_Atom( int num_atoms );
57
void FreeInchi_Input( inchi_Input *inp_at_data );
58
S_SHORT *is_in_the_slist( S_SHORT *pathAtom, S_SHORT nNextAtom, int nPathLen );
59
int is_element_a_metal( char szEl[] );
68
void FreeInchi_Stereo0D( inchi_Stereo0D **stereo0D );
69
inchi_Stereo0D *CreateInchi_Stereo0D( int num_stereo0D );
70
int Extract0DParities( inp_ATOM *at, int nNumAtoms, inchi_Stereo0D *stereo0D,
71
int num_stereo0D, char *pStrErr, int *err,
72
int vABParityUnknown);
77
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
85
/******************************************************************************************************/
86
void FreeInchi_Atom( inchi_Atom **at )
93
/******************************************************************************************************/
94
inchi_Atom *CreateInchi_Atom( int num_atoms )
96
inchi_Atom *p = (inchi_Atom* ) inchi_calloc(num_atoms, sizeof(inchi_Atom) );
99
/******************************************************************************************************/
100
void FreeInchi_Input( inchi_Input *inp_at_data )
102
FreeInchi_Atom( &inp_at_data->atom );
103
FreeInchi_Stereo0D( &inp_at_data->stereo0D );
104
memset( inp_at_data, 0, sizeof(*inp_at_data) );
106
/*************************************************************************/
107
S_SHORT *is_in_the_slist( S_SHORT *pathAtom, S_SHORT nNextAtom, int nPathLen )
109
for ( ; nPathLen && *pathAtom != nNextAtom; nPathLen--, pathAtom++ )
111
return nPathLen? pathAtom : NULL;
113
/************************************************/
114
int is_element_a_metal( char szEl[] )
116
static const char szMetals[] = "K;V;Y;W;U;"
117
"Li;Be;Na;Mg;Al;Ca;Sc;Ti;Cr;Mn;Fe;Co;Ni;Cu;Zn;Ga;Rb;Sr;Zr;"
118
"Nb;Mo;Tc;Ru;Rh;Pd;Ag;Cd;In;Sn;Sb;Cs;Ba;La;Ce;Pr;Nd;Pm;Sm;"
119
"Eu;Gd;Tb;Dy;Ho;Er;Tm;Yb;Lu;Hf;Ta;Re;Os;Ir;Pt;Au;Hg;Tl;Pb;"
120
"Bi;Po;Fr;Ra;Ac;Th;Pa;Np;Pu;Am;Cm;Bk;Cf;Es;Fm;Md;No;Lr;Rf;";
121
const int len = strlen(szEl);
124
if ( 0 < len && len <= 2 &&
125
isalpha( UCINT szEl[0] ) && isupper( szEl[0] ) &&
126
(p = strstr(szMetals, szEl) ) && p[len] == ';' ) {
128
return 1; /*return AtType_Metal;*/
137
/******************************************************************************************************/
138
inchi_Stereo0D *CreateInchi_Stereo0D( int num_stereo0D )
140
return (inchi_Stereo0D* ) inchi_calloc(num_stereo0D, sizeof(inchi_Stereo0D) );
142
/******************************************************************************************************/
143
void FreeInchi_Stereo0D( inchi_Stereo0D **stereo0D )
145
if ( stereo0D && *stereo0D ) {
146
inchi_free( *stereo0D );
152
#define INPUT_FILE INCHI_IOSTREAM
154
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
157
#define INChITo_Atom ll_INChIToInchi_Atom
159
#define INChITo_Atom ee_INChIToIbChI_Atom
160
#define FindToken e_FindToken
161
#define LoadLine e_LoadLine
164
#define AT_NUM_BONDS(AT) (AT).num_bonds
165
#define ATOM_NUMBER AT_NUM
166
#define IN_NEIGH_LIST is_in_the_slist
167
/*#define INPUT_FILE INCHI_IOSTREAM*/
168
#define Create_Atom CreateInchi_Atom
169
#define AT_BONDS_VAL(AT,I) AT[I].num_iso_H[0]
170
#define ISOLATED_ATOM (-15)
171
#define NUM_ISO_Hk(AT,I,K) AT[I].num_iso_H[K+1]
172
#define IS_METAL_ATOM(AT,I) is_element_a_metal( AT[I].elname )
176
#define inchi_Atom inp_ATOM
177
#define AT_NUM_BONDS(AT) (AT).valence
178
#define ATOM_NUMBER AT_NUMB
179
#define IN_NEIGH_LIST is_in_the_list
180
#define inchi_NUMH2(AT,N) NUMH(AT,N)
181
#define INChITo_Atom cc_INChIToInpAtom
182
/*#define INPUT_FILE FILE*/
183
#define Create_Atom CreateInpAtom
184
#define AT_BONDS_VAL(AT,I) AT[I].chem_bonds_valence
185
#define ISOLATED_ATOM 15
186
#define NUM_ISO_Hk(AT,I,K) AT[I].num_iso_H[K]
187
#define IS_METAL_ATOM(AT,I) is_el_a_metal( AT[I].el_number )
191
/*****************************************************************************/
192
/* local prototypes */
193
char *FindToken( INCHI_IOSTREAM *inp_molfile, int *bTooLongLine, const char *sToken, int lToken,
194
char *szLine, int nLenLine, char *p, int *res );
195
char *LoadLine( INPUT_FILE *inp_molfile, int *bTooLongLine, int *bItemIsOver, char **s,
196
char *szLine, int nLenLine, int nMinLen2Load, char *p, int *res );
199
int INChITo_Atom(INPUT_FILE *inp_molfile, MOL_COORD **szCoord,
200
inchi_Stereo0D **stereo0D, int *num_stereo0D,
201
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inchi_Atom **at,
203
int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
204
long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr );
207
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
208
/*****************************************************************************/
209
int INChIToInchi_Atom ( INCHI_IOSTREAM *inp_molfile, inchi_Stereo0D **stereo0D, int *num_stereo0D,
210
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType,
211
inchi_Atom **at, int max_num_at,
212
int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
213
long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr );
215
int INChIToInchi_Atom ( INCHI_IOSTREAM *inp_molfile, inchi_Stereo0D **stereo0D, int *num_stereo0D,
216
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType,
217
inchi_Atom **at, int max_num_at,
218
int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
219
long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr )
221
return INChITo_Atom ( inp_molfile, NULL, stereo0D, num_stereo0D,
222
bDoNotAddH, vABParityUnknown, nInputType, at, max_num_at,
223
num_dimensions, num_bonds, pSdfLabel, pSdfValue,
224
Id, pInpAtomFlags, err, pStrErr );
227
int INChIToInpAtom ( INCHI_IOSTREAM *inp_molfile, MOL_COORD **szCoord,
228
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inp_ATOM **at,
230
int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
231
long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr );
232
int INChIToInpAtom ( INCHI_IOSTREAM *inp_molfile, MOL_COORD **szCoord,
233
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inp_ATOM **at,
235
int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
236
long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr )
238
return INChITo_Atom ( inp_molfile, szCoord, NULL, NULL,
239
bDoNotAddH, vABParityUnknown, nInputType, at, max_num_at,
240
num_dimensions, num_bonds, pSdfLabel, pSdfValue,
241
Id, pInpAtomFlags, err, pStrErr );
244
/*****************************************************************************/
245
char *FindToken( INCHI_IOSTREAM *inp_molfile, int *bTooLongLine, const char *sToken, int lToken,
246
char *szLine, int nLenLine, char *p, int *res )
251
while ( !(q = strstr( p, sToken ) ) ) {
252
if ( (q = strrchr( p, '/' )) && (q + lToken > szLine + *res) ) {
253
*res -= q - szLine; /* res = the length of the szLine to be left in */
254
memmove( szLine, q, *res + 1);
258
if ( !*bTooLongLine ||
259
0 > (res2 = inchi_ios_getsTab1( szLine + *res, nLenLine - *res - 1,
260
inp_molfile, bTooLongLine ) ) ) {
261
/* the line is over or end of file */
271
/*****************************************************************************/
272
char *LoadLine( INCHI_IOSTREAM *inp_molfile, int *bTooLongLine, int *bItemIsOver, char **s,
273
char *szLine, int nLenLine, int nMinLen2Load, char *p, int *res )
275
int pos = p - szLine, res2;
276
if ( !*bItemIsOver && nLenLine - (*res - pos) > nMinLen2Load ) {
277
/* load the next portion if possible */
280
memmove( szLine, p, *res+1 );
287
res2 = inchi_ios_getsTab1( szLine + *res, nLenLine - *res - 1, inp_molfile, bTooLongLine );
289
*bItemIsOver = ( (*s = strchr( p + *res, '/') ) || !*bTooLongLine );
297
/*****************************************************************************/
298
int INChITo_Atom(INCHI_IOSTREAM *inp_molfile, MOL_COORD **szCoord,
299
inchi_Stereo0D **stereo0D, int *num_stereo0D,
300
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType, inchi_Atom **at,
302
int *num_dimensions, int *num_bonds, char *pSdfLabel, char *pSdfValue,
303
long *Id, INCHI_MODE *pInpAtomFlags, int *err, char *pStrErr )
305
int num_atoms = 0, bFindNext = 0, len, bHeaderRead, bItemIsOver, bErrorMsg, bRestoreInfo;
306
int bFatal = 0, num_struct = 0;
307
int i, k, k2, res, bond_type, bond_stereo1, bond_stereo2, bond_char, neigh, bond_parity, bond_parityNM;
308
int bTooLongLine, res2, bTooLongLine2, pos, hlen, hk;
310
char szLine[INCHI_LINE_LEN], szNextLine[INCHI_LINE_ADD], *p, *q, *s, parity;
311
int b2D=0, b3D=0, b23D, nNumBonds = 0, bNonZeroXYZ, bNonMetal;
312
int len_stereo0D = 0, max_len_stereo0D = 0;
313
inchi_Stereo0D *atom_stereo0D = NULL;
314
inchi_Atom *atom = NULL;
315
MOL_COORD *pszCoord = NULL;
316
INCHI_MODE InpAtomFlags = 0; /* 0 or FLAG_INP_AT_NONCHIRAL or FLAG_INP_AT_CHIRAL */
317
static const char szIsoH[] = "hdt";
319
static const char sStructHdrPln[] = "Structure:";
320
static const char sStructHdrPlnNoLblVal[] = " is missing";
321
static char sStructHdrPlnAuxStart[64] =""; /*"$1.1Beta/";*/
322
static int lenStructHdrPlnAuxStart = 0;
323
static const char sStructHdrPlnRevAt[] = "/rA:";
324
static const char sStructHdrPlnRevBn[] = "/rB:";
325
static const char sStructHdrPlnRevXYZ[] = "/rC:";
328
if ( !lenStructHdrPlnAuxStart ) {
329
lenStructHdrPlnAuxStart = sprintf( sStructHdrPlnAuxStart, "AuxInfo=" );
333
if ( *at && max_num_at ) {
334
memset( *at, 0, max_num_at * sizeof(**at) );
336
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
337
if ( stereo0D && num_stereo0D ) {
338
if ( *stereo0D && *num_stereo0D ) {
339
max_len_stereo0D = *num_stereo0D;
340
memset( *stereo0D, 0, max_len_stereo0D * sizeof( **stereo0D ));
342
max_len_stereo0D = 0;
346
if ( szCoord && *szCoord ) {
347
inchi_free( *szCoord );
354
bHeaderRead = bErrorMsg = bRestoreInfo = 0;
355
*num_dimensions = *num_bonds = 0;
357
/*************************************************************/
358
/* extract reversibility info from plain text INChI format */
359
/*************************************************************/
360
if ( nInputType == INPUT_INCHI_PLAIN ) {
361
bHeaderRead = hk = 0;
362
while ( 0 < (res = inchi_ios_getsTab( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) ) {
364
/********************* find and interpret structure header ************/
365
if ( !bTooLongLine &&
366
(hlen=sizeof(sStructHdrPln)-1, !memcmp(szLine, sStructHdrPln, hlen)) ) {
370
/* structure number */
371
longID = strtol( p, &q, 10 );
372
if ( q && q[0] == '.' && q[1] == ' ' ) {
375
p = p + strspn( p, " \n\r" );
387
if ( q = strchr( p, '=' ) ) {
388
/* '=' separates label name from the value */
389
len = inchi_min( q-p+1, MAX_SDF_HEADER-1);
391
mystrncpy( pSdfLabel, p, len );
392
LtrimRtrim( pSdfLabel, &len );
395
q = p + (int)strlen( p );
397
len = inchi_min( q-p+1, MAX_SDF_VALUE-1);
399
mystrncpy( pSdfValue, p, len );
405
if ( q = strstr( p, sStructHdrPlnNoLblVal ) ) {
406
len = inchi_min( q-p+1, MAX_SDF_HEADER-1);
408
mystrncpy( pSdfLabel, p, len );
417
bErrorMsg = bRestoreInfo = 0;
419
if ( !memcmp( szLine, sStructHdrPlnAuxStart, lenStructHdrPlnAuxStart) ) {
420
/* found the header of the AuxInfo, read AuxInfo head of the line */
421
if ( !bHeaderRead ) {
433
/* check for empty "AuxInfo=ver//" */
434
p = strchr( szLine + lenStructHdrPlnAuxStart, '/' );
435
if ( p && p[1] == '/' && (!p[2] || '\n' == p[2]) ) {
436
goto bypass_end_of_INChI_plain;
438
/***************** search for atoms block (plain) **********************/
440
sToken = sStructHdrPlnRevAt;
441
lToken = sizeof(sStructHdrPlnRevAt)-1;
442
/* search for sToken in the line; load next segments of the line if sToken has not found */
443
p = FindToken( inp_molfile, &bTooLongLine, sToken, lToken,
444
szLine, sizeof(szLine), p, &res );
446
*err = INCHI_INP_ERROR_ERR;
447
num_atoms = INCHI_INP_ERROR_RET;
448
MOLFILE_ERR_SET (*err, 0, "Missing atom data");
449
goto bypass_end_of_INChI_plain;
451
/* atoms block started */
453
res2 = bTooLongLine2 = -1;
454
bItemIsOver = (s = strchr( p, '/') ) || !bTooLongLine;
456
p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
457
szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
460
num_atoms = strtol( p, &q, 10 );
461
if ( !num_atoms || !q || !*q ) {
462
num_atoms = 0; /* no atom data */
463
goto bypass_end_of_INChI_plain;
466
/* Molfile chirality flag */
469
InpAtomFlags |= FLAG_INP_AT_CHIRAL;
473
InpAtomFlags |= FLAG_INP_AT_NONCHIRAL;
478
if ( num_atoms > max_num_at ) {
482
memset( *at, 0, max_num_at * sizeof( **at ) );
487
atom = Create_Atom( num_atoms+1 );
489
num_atoms = INCHI_INP_FATAL_RET; /* was -1; error */
490
*err = INCHI_INP_FATAL_ERR;
491
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
492
goto bypass_end_of_INChI_plain;
495
if ( stereo0D && *stereo0D ) {
496
if ( num_atoms > max_len_stereo0D ) {
497
FreeInchi_Stereo0D( stereo0D );
499
memset( *stereo0D, 0, max_len_stereo0D * sizeof( **stereo0D ) );
500
atom_stereo0D = *stereo0D;
503
if ( !stereo0D || !*stereo0D ) {
504
max_len_stereo0D = num_atoms+1;
505
atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D );
506
if ( !atom_stereo0D ) {
507
num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
508
*err = INCHI_INP_FATAL_ERR;
509
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
510
goto bypass_end_of_INChI_plain;
514
/* element, first char */
515
if ( !isalpha( UCINT *p ) || !isupper( UCINT *p ) || i >= num_atoms ) {
516
break; /* end of atoms block */
518
atom[i].elname[0] = *p ++;
519
/* element, second char */
520
if ( isalpha( UCINT *p ) && islower( UCINT *p ) ) {
521
atom[i].elname[1] = *p ++;
523
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
525
atom[i].el_number = get_periodic_table_number( atom[i].elname );
527
/* bonds' valence + number of non-isotopic H */
528
if ( isdigit( UCINT *p ) ) {
529
AT_BONDS_VAL(atom,i) = (char)strtol( p, &q, 10 );
530
if ( !AT_BONDS_VAL(atom,i) )
531
AT_BONDS_VAL(atom,i) = ISOLATED_ATOM; /* same convention as in MOLfile, found zero bonds valence */
535
atom[i].charge = (*p == '+')? 1 : (*p == '-')? -1 : 0;
536
if ( atom[i].charge ) {
538
if ( isdigit( UCINT *p ) ) {
539
atom[i].charge *= (S_CHAR)(strtol( p, &q, 10 ) & CHAR_MASK);
546
if ( isdigit( UCINT *p ) ) {
547
atom[i].radical = (S_CHAR)strtol( p, &q, 10 );
554
if ( isdigit( UCINT *p ) ) {
555
int mw = strtol( p, &q, 10 );
557
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
558
atom[i].isotopic_mass = mw;
560
mw -= get_atw_from_elnum( atom[i].el_number );
563
atom[i].iso_atw_diff = mw;
570
parity = INCHI_PARITY_ODD;
574
parity = INCHI_PARITY_EVEN;
578
parity = INCHI_PARITY_UNKNOWN;
582
parity = INCHI_PARITY_UNDEFINED;
590
atom_stereo0D[len_stereo0D].central_atom = i;
591
atom_stereo0D[len_stereo0D].parity = parity;
592
atom_stereo0D[len_stereo0D].type = INCHI_StereoType_Tetrahedral;
595
/* isotopic h, d, t */
596
for ( k = 0; k < NUM_H_ISOTOPES; k ++ ) {
597
if ( *p == szIsoH[k] ) {
598
NUM_ISO_Hk(atom,i,k) = 1;
600
if ( isdigit( UCINT *p ) ) {
601
NUM_ISO_Hk(atom,i,k) = (char)strtol( p, &q, 10 );
608
if ( !bItemIsOver || i != num_atoms || s && p != s ) {
609
num_atoms = INCHI_INP_ERROR_RET; /* error */
610
*err = INCHI_INP_ERROR_ERR;
611
MOLFILE_ERR_SET (*err, 0, "Wrong number of atoms");
612
goto bypass_end_of_INChI_plain;
615
/***************** search for bonds block (plain) and read it *****************/
617
sToken = sStructHdrPlnRevBn;
618
lToken = sizeof(sStructHdrPlnRevBn)-1;
619
/* search for sToken in the line; load next segments of the line if sToken has not found */
620
p = FindToken( inp_molfile, &bTooLongLine, sToken, lToken,
621
szLine, sizeof(szLine), p, &res );
623
num_atoms = INCHI_INP_ERROR_RET; /* error */
624
*err = INCHI_INP_ERROR_ERR;
625
MOLFILE_ERR_SET (*err, 0, "Missing bonds data");
626
goto bypass_end_of_INChI_plain;
628
/* bonds block started */
630
res2 = bTooLongLine2 = -1;
631
bItemIsOver = (s = strchr( p, '/') ) || !bTooLongLine;
632
if ( 1 == num_atoms ) {
633
/* needed because the next '/' may be still out of szLine */
634
p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
635
szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
637
while ( i < num_atoms ) {
638
p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
639
szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
640
if ( i >= num_atoms || s && p >= s ) {
641
break; /* end of bonds (plain) */
643
/* bond, first char */
649
if ( !isalpha( UCINT *p ) ) {
650
num_atoms = INCHI_INP_ERROR_RET; /* error */
651
*err = INCHI_INP_ERROR_ERR;
652
MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
653
goto bypass_end_of_INChI_plain;
659
bond_parity = INCHI_PARITY_ODD;
663
bond_parity = INCHI_PARITY_EVEN;
667
bond_parity = INCHI_PARITY_UNKNOWN;
671
bond_parity = INCHI_PARITY_UNDEFINED;
681
bond_parityNM = INCHI_PARITY_ODD;
685
bond_parityNM = INCHI_PARITY_EVEN;
689
bond_parityNM = INCHI_PARITY_UNKNOWN;
693
bond_parityNM = INCHI_PARITY_UNDEFINED;
704
/* neighbor of the current atom */
705
if ( !isdigit( UCINT *p ) ) {
706
num_atoms = INCHI_INP_ERROR_RET; /* error */
707
*err = INCHI_INP_ERROR_ERR;
708
MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
709
goto bypass_end_of_INChI_plain;
711
neigh = (int)strtol( p, &q, 10 )-1;
713
if ( i >= num_atoms || neigh >= num_atoms ) {
714
num_atoms = INCHI_INP_ERROR_RET; /* error */
715
*err = INCHI_INP_ERROR_ERR;
716
MOLFILE_ERR_SET (*err, 0, "Bond to nonexistent atom");
717
goto bypass_end_of_INChI_plain;
720
bond_stereo1 = bond_stereo2 = 0;
722
/* bond type & 2D stereo */
723
switch( bond_char ) {
725
bond_type = INCHI_BOND_TYPE_SINGLE;
726
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1EITHER;
727
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2EITHER;
730
bond_type = INCHI_BOND_TYPE_SINGLE;
731
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2EITHER;
732
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1EITHER;
735
bond_type = INCHI_BOND_TYPE_DOUBLE;
737
bond_stereo2 = INCHI_BOND_STEREO_DOUBLE_EITHER;
740
bond_type = INCHI_BOND_TYPE_SINGLE;
743
bond_type = INCHI_BOND_TYPE_DOUBLE;
746
bond_type = INCHI_BOND_TYPE_TRIPLE;
749
bond_type = INCHI_BOND_TYPE_ALTERN;
752
bond_type = INCHI_BOND_TYPE_SINGLE;
753
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1UP;
754
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2UP;
757
bond_type = INCHI_BOND_TYPE_SINGLE;
758
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2UP;
759
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1UP;
762
bond_type = INCHI_BOND_TYPE_SINGLE;
763
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1DOWN;
764
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2DOWN;
767
bond_type = INCHI_BOND_TYPE_SINGLE;
768
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2DOWN;
769
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1DOWN;
772
num_atoms = INCHI_INP_ERROR_RET; /* error */
773
*err = INCHI_INP_ERROR_ERR;
774
MOLFILE_ERR_SET (*err, 0, "Wrong bond type");
775
goto bypass_end_of_INChI_plain;
777
k = AT_NUM_BONDS(atom[i]) ++;
778
atom[i].bond_type[k] = bond_type;
779
atom[i].bond_stereo[k] = bond_stereo1;
780
atom[i].neighbor[k] = (ATOM_NUMBER)neigh;
781
k2 = AT_NUM_BONDS(atom[neigh]) ++;
782
atom[neigh].bond_type[k2] = bond_type;
783
atom[neigh].bond_stereo[k2] = bond_stereo2;
784
atom[neigh].neighbor[k2] = (ATOM_NUMBER)i;
785
bond_parity |= (bond_parityNM << SB_PARITY_SHFT);
788
if ( max_len_stereo0D <= len_stereo0D ) {
789
/* realloc atom_Stereo0D */
790
inchi_Stereo0D *new_atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D+num_atoms );
791
if ( !new_atom_stereo0D ) {
792
num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
793
*err = INCHI_INP_FATAL_ERR;
794
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
795
goto bypass_end_of_INChI_plain;
797
memcpy( new_atom_stereo0D, atom_stereo0D, len_stereo0D * sizeof(*atom_stereo0D) );
798
FreeInchi_Stereo0D( &atom_stereo0D );
799
atom_stereo0D = new_atom_stereo0D;
800
max_len_stereo0D += num_atoms;
802
/* (a) i may be allene endpoint and neigh = allene middle point or
803
(b) i may be allene middle point and neigh = allene endpoint
804
!!!!! CURRENTLY ONLY (b) IS ALLOWED !!!!!
806
atom_stereo0D[len_stereo0D].neighbor[1] = neigh; /* neigh < i */
807
atom_stereo0D[len_stereo0D].neighbor[2] = i;
808
atom_stereo0D[len_stereo0D].parity = bond_parity;
809
atom_stereo0D[len_stereo0D].type = INCHI_StereoType_DoubleBond; /* incl allenes & cumulenes */
813
if ( !bItemIsOver || i != num_atoms || s && p != s ) {
814
num_atoms = INCHI_INP_ERROR_RET; /* error */
815
*err = INCHI_INP_ERROR_ERR;
816
MOLFILE_ERR_SET (*err, 0, "Wrong number of bonds");
817
goto bypass_end_of_INChI_plain;
820
/***************** search for coordinates block (plain) **********************/
822
sToken = sStructHdrPlnRevXYZ;
823
lToken = sizeof(sStructHdrPlnRevXYZ)-1;
824
/* search for sToken in the line; load next segments of the line if sToken has not found */
825
p = FindToken( inp_molfile, &bTooLongLine, sToken, lToken,
826
szLine, sizeof(szLine), p, &res );
828
num_atoms = INCHI_INP_ERROR_RET; /* error */
829
*err = INCHI_INP_ERROR_ERR;
830
MOLFILE_ERR_SET (*err, 0, "Missing atom coordinates data");
831
goto bypass_end_of_INChI_plain;
833
/* coordinates block started */
834
if ( pszCoord = (MOL_COORD*)inchi_malloc(inchi_max(num_atoms,1) * sizeof(MOL_COORD)) ) {
835
memset( pszCoord, ' ', inchi_max(num_atoms,1) * sizeof(MOL_COORD));
837
num_atoms = INCHI_INP_FATAL_RET; /* allocation error */
838
*err = INCHI_INP_FATAL_ERR;
839
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
840
goto bypass_end_of_INChI_plain;
843
res2 = bTooLongLine2 = -1;
844
bItemIsOver = (s = strchr( p, '/') ) || !bTooLongLine;
845
while ( i < num_atoms ) {
846
p = LoadLine( inp_molfile, &bTooLongLine, &bItemIsOver, &s,
847
szLine, sizeof(szLine), INCHI_LINE_ADD, p, &res );
848
if ( i >= num_atoms || s && p >= s ) {
849
break; /* end of bonds (plain) */
852
/* coord, first char */
854
for ( k = 0; k < NUM_COORD; k ++ ) {
855
pszCoord[i][LEN_COORD*k + 4] = '0';
861
for ( k = 0; k < 3; k ++ ) {
865
pszCoord[i][LEN_COORD*k + 4] = '0';
870
pszCoord[i][LEN_COORD*k + 4] = '0';
874
xyz = strtod( p, &q );
875
bNonZeroXYZ = fabs(xyz) > MIN_BOND_LENGTH;
877
memcpy( pszCoord[i]+LEN_COORD*k, p, q-p );
882
pszCoord[i][LEN_COORD*k + 4] = '0';
901
p ++; /* end of this triple of coordinates */
904
num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
905
*err = INCHI_INP_ERROR_ERR;
906
MOLFILE_ERR_SET (*err, 0, "Wrong atom coordinates data");
907
goto bypass_end_of_INChI_plain;
910
if ( !bItemIsOver || s && p != s || i != num_atoms ) {
911
num_atoms = INCHI_INP_ERROR_RET; /* error */
912
*err = INCHI_INP_ERROR_ERR;
913
MOLFILE_ERR_SET (*err, 0, "Wrong number of coordinates");
914
goto bypass_end_of_INChI_plain;
916
} /* end of coordinates */
917
/* set special valences and implicit H (xml) */
922
int a1, a2, n1, n2, valence;
923
int chem_bonds_valence;
924
int nX=0, nY=0, nZ=0, nXYZ;
926
/* special valences */
927
for ( bNonMetal = 0; bNonMetal < 1; bNonMetal ++ ) {
928
for ( a1 = 0; a1 < num_atoms; a1 ++ ) {
929
int num_bond_type[MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE + 1];
930
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
932
int bHasMetalNeighbor=0;
934
memset( num_bond_type, 0, sizeof(num_bond_type) );
936
valence = AT_BONDS_VAL(atom, a1); /* save atom valence if available */
937
AT_BONDS_VAL(atom, a1) = 0;
938
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
940
atom[a1].orig_at_number = a1+1;
943
for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
944
bond_type = atom[a1].bond_type[n1] - MIN_INPUT_BOND_TYPE;
945
if ( bond_type < 0 || bond_type > MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE ) {
947
MOLFILE_ERR_SET (*err, 0, "Unknown bond type in InChI aux assigned as a single bond");
950
num_bond_type[ bond_type ] ++;
953
neigh = atom[a1].neighbor[n1];
954
nX |= (fabs(atom[a1].x - atom[neigh].x) > MIN_BOND_LENGTH);
955
nY |= (fabs(atom[a1].y - atom[neigh].y) > MIN_BOND_LENGTH);
956
nZ |= (fabs(atom[a1].z - atom[neigh].z) > MIN_BOND_LENGTH);
959
chem_bonds_valence = 0;
960
for ( n1 = 0; MIN_INPUT_BOND_TYPE + n1 <= 3 && MIN_INPUT_BOND_TYPE + n1 <= MAX_INPUT_BOND_TYPE; n1 ++ ) {
961
chem_bonds_valence += (MIN_INPUT_BOND_TYPE + n1) * num_bond_type[n1];
963
if ( MIN_INPUT_BOND_TYPE <= INCHI_BOND_TYPE_ALTERN && INCHI_BOND_TYPE_ALTERN <= MAX_INPUT_BOND_TYPE &&
964
( n2 = num_bond_type[INCHI_BOND_TYPE_ALTERN-MIN_INPUT_BOND_TYPE] ) ) {
965
/* accept input aromatic bonds for now */
968
chem_bonds_valence += 3; /* =A- */
971
chem_bonds_valence += 4; /* =A< */
974
/* if 1 or >= 4 aromatic bonds then replace such bonds with single bonds */
975
for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
976
if ( atom[a1].bond_type[n1] == INCHI_BOND_TYPE_ALTERN ) {
978
a2 = atom[a1].neighbor[n1];
979
p1 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
981
atom[a1].bond_type[n1] =
982
atom[a2].bond_type[p1-atom[a2].neighbor] = INCHI_BOND_TYPE_SINGLE;
984
*err = -2; /* Program error */
985
MOLFILE_ERR_SET (*err, 0, "Program error interpreting InChI aux");
987
goto bypass_end_of_INChI_plain; /* no structure */
991
chem_bonds_valence += n2;
992
*err |= 32; /* Unrecognized aromatic bond(s) replaced with single */
993
MOLFILE_ERR_SET (*err, 0, "Atom has 1 or more than 3 aromatic bonds");
997
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
998
/*************************************************************************************
1000
* Set number of hydrogen atoms
1004
num_iso_H = atom[a1].num_iso_H[1] + atom[a1].num_iso_H[2] + atom[a1].num_iso_H[3];
1005
if ( valence == ISOLATED_ATOM ) {
1006
atom[a1].num_iso_H[0] = 0;
1008
if ( valence && valence >= chem_bonds_valence ) {
1009
atom[a1].num_iso_H[0] = valence - chem_bonds_valence;
1011
if ( valence || bDoNotAddH ) {
1012
atom[a1].num_iso_H[0] = 0;
1014
if ( !bDoNotAddH ) {
1015
atom[a1].num_iso_H[0] = -1; /* auto add H */
1019
/* added 2006-07-19 to process aromatic bonds same way as from molfile */
1020
if ( n2 && !valence ) {
1021
int num_H = NUMH(atom, a1); /* only isotopic */
1022
int chem_valence = chem_bonds_valence;
1023
int bUnusualValenceArom =
1024
detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
1025
atom[a1].radical, chem_valence,
1026
num_H, atom[a1].valence );
1027
int bUnusualValenceNoArom =
1028
detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
1029
atom[a1].radical, chem_valence-1,
1030
num_H, atom[a1].valence );
1031
#if ( CHECK_AROMBOND2ALT == 1 )
1032
if ( bUnusualValenceArom && !bUnusualValenceNoArom && 0 == nBondsValToMetal( atom, a1) )
1034
if ( bUnusualValenceArom && !bUnusualValenceNoArom )
1037
/* typically NH in 5-member aromatic ring */
1038
chem_bonds_valence --;
1041
if ( n2 && valence ) {
1042
/* atom has aromatic bonds AND the chemical valence is known */
1043
int num_H = NUMH(atom, a1);
1044
int chem_valence = chem_bonds_valence + num_H;
1045
if ( valence == chem_valence-1 ) {
1046
/* typically NH in 5-member aromatic ring */
1047
chem_bonds_valence --;
1051
atom[a1].chem_bonds_valence = chem_bonds_valence;
1052
atom[a1].num_H = get_num_H( atom[a1].elname, atom[a1].num_H, atom[a1].num_iso_H, atom[a1].charge, atom[a1].radical,
1053
atom[a1].chem_bonds_valence,
1055
0, bDoNotAddH, bHasMetalNeighbor );
1060
if ( b23D && nNumBonds ) {
1064
*num_dimensions = b3D? 3 : b2D? 2 : 0;
1065
*num_bonds = nNumBonds;
1067
/*======= 0D parities =================================*/
1068
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1069
if ( len_stereo0D > 0 && atom_stereo0D && stereo0D ) {
1070
*stereo0D = atom_stereo0D;
1071
*num_stereo0D = len_stereo0D;
1073
FreeInchi_Stereo0D( &atom_stereo0D );
1074
*num_stereo0D = len_stereo0D = 0;
1077
for ( i = 0; i < len_stereo0D; i ++ ) {
1078
ATOM_NUMBER *p1, *p2;
1079
int sb_ord_from_a1 = -1, sb_ord_from_a2 = -1, bEnd1 = 0, bEnd2 = 0;
1080
switch( atom_stereo0D[i].type ) {
1082
case INCHI_StereoType_Tetrahedral:
1083
a1 = atom_stereo0D[i].central_atom;
1084
if ( atom_stereo0D[i].parity && (AT_NUM_BONDS(atom[a1]) == 3 || AT_NUM_BONDS(atom[a1]) == 4) ) {
1086
if ( AT_NUM_BONDS(atom[a1]) == 3 ) {
1087
atom_stereo0D[i].neighbor[kk++] = a1;
1089
for ( ii = 0; ii < AT_NUM_BONDS(atom[a1]); ii ++ ) {
1090
atom_stereo0D[i].neighbor[kk++] = atom[a1].neighbor[ii];
1096
case INCHI_StereoType_DoubleBond:
1097
#define MAX_CHAIN_LEN 20
1098
a1 = atom_stereo0D[i].neighbor[1];
1099
a2 = atom_stereo0D[i].neighbor[2];
1100
p1 = IN_NEIGH_LIST( atom[a1].neighbor, (ATOM_NUMBER)a2, AT_NUM_BONDS(atom[a1]) );
1101
p2 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
1103
atom_stereo0D[i].type = INCHI_StereoType_None;
1104
atom_stereo0D[i].central_atom = NO_ATOM;
1105
atom_stereo0D[i].neighbor[0] =
1106
atom_stereo0D[i].neighbor[3] = -1;
1107
*err |= 64; /* Error in cumulene stereo */
1108
MOLFILE_ERR_SET (*err, 0, "0D stereobond not recognized");
1111
/* streobond, allene, or cumulene */
1113
sb_ord_from_a1 = p1 - atom[a1].neighbor;
1114
sb_ord_from_a2 = p2 - atom[a2].neighbor;
1116
if ( AT_NUM_BONDS(atom[a1]) == 2 &&
1117
atom[a1].bond_type[0] + atom[a1].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
1118
0 == inchi_NUMH2(atom, a1) &&
1119
(AT_NUM_BONDS(atom[a2]) != 2 ||
1120
atom[a2].bond_type[0] + atom[a2].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
1121
bEnd2 = 1; /* a2 is the end-atom, a1 is middle atom */
1123
if ( AT_NUM_BONDS(atom[a2]) == 2 &&
1124
atom[a2].bond_type[0] + atom[a2].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
1125
0 == inchi_NUMH2(atom, a2) &&
1126
(AT_NUM_BONDS(atom[a1]) != 2 ||
1127
atom[a1].bond_type[0] + atom[a1].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
1128
bEnd1 = 1; /* a1 is the end-atom, a2 is middle atom */
1131
if ( bEnd2 + bEnd1 == 1 ) {
1132
/* allene or cumulene */
1133
ATOM_NUMBER chain[MAX_CHAIN_LEN+1], prev, cur, next;
1134
if ( bEnd2 && !bEnd1 ) {
1138
sb_ord_from_a1 = sb_ord_from_a2;
1140
sb_ord_from_a2 = -1;
1145
chain[len++] = next;
1146
while ( len < MAX_CHAIN_LEN ) { /* arbitrary very high upper limit to prevent infinite loop */
1149
/* follow double bond path && avoid going back */
1150
if ( AT_NUM_BONDS(atom[cur]) == 2 &&
1151
atom[cur].bond_type[0]+atom[cur].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
1152
0 == inchi_NUMH2(atom, cur) ) {
1153
next = atom[cur].neighbor[atom[cur].neighbor[0] == prev];
1154
chain[len++] = next;
1160
(p2 = IN_NEIGH_LIST( atom[cur].neighbor, (ATOM_NUMBER)prev, AT_NUM_BONDS(atom[cur]))) ) {
1161
sb_ord_from_a2 = p2 - atom[cur].neighbor;
1163
/* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
1164
atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[sb_ord_from_a1 == 0];
1165
atom_stereo0D[i].neighbor[1] = a1;
1166
atom_stereo0D[i].neighbor[2] = a2;
1167
atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[sb_ord_from_a2 == 0];
1169
atom_stereo0D[i].central_atom = chain[len/2];
1170
atom_stereo0D[i].type = INCHI_StereoType_Allene;
1172
atom_stereo0D[i].central_atom = NO_ATOM;
1176
atom_stereo0D[i].type = INCHI_StereoType_None;
1177
atom_stereo0D[i].central_atom = NO_ATOM;
1178
atom_stereo0D[i].neighbor[0] =
1179
atom_stereo0D[i].neighbor[3] = -1;
1180
*err |= 64; /* Error in cumulene stereo */
1181
MOLFILE_ERR_SET (*err, 0, "Cumulene stereo not recognized (0D)");
1184
#undef MAX_CHAIN_LEN
1186
/****** a normal possibly stereogenic bond -- not an allene or cumulene *******/
1187
/* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
1188
sb_ord_from_a1 = p1 - atom[a1].neighbor;
1189
sb_ord_from_a2 = p2 - atom[a2].neighbor;
1190
atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[p1 == atom[a1].neighbor];
1191
atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[p2 == atom[a2].neighbor];
1192
atom_stereo0D[i].central_atom = NO_ATOM;
1194
if ( atom_stereo0D[i].type != INCHI_StereoType_None &&
1195
sb_ord_from_a1 >= 0 && sb_ord_from_a2 >= 0 &&
1196
ATOM_PARITY_WELL_DEF( SB_PARITY_2(atom_stereo0D[i].parity) ) ) {
1197
/* Detected well-defined disconnected stereo
1198
* locate first non-metal neighbors */
1199
int a, n, j, /* k,*/ sb_ord, cur_neigh, min_neigh;
1200
for ( k = 0; k < 2; k ++ ) {
1201
a = k? atom_stereo0D[i].neighbor[2] : atom_stereo0D[i].neighbor[1];
1202
sb_ord = k? sb_ord_from_a2 : sb_ord_from_a1;
1203
min_neigh = num_atoms;
1204
for ( n = j = 0; j < AT_NUM_BONDS(atom[a]); j ++ ) {
1205
cur_neigh = atom[a].neighbor[j];
1206
if ( j != sb_ord && !IS_METAL_ATOM(atom, cur_neigh) ) {
1207
min_neigh = inchi_min( cur_neigh, min_neigh );
1210
if ( min_neigh < num_atoms ) {
1211
atom_stereo0D[i].neighbor[k?3:0] = min_neigh;
1213
MOLFILE_ERR_SET (*err, 0, "Cannot find non-metal stereobond neighor (0D)");
1221
/* end of 0D parities extraction */
1224
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1226
/* transfer atom_stereo0D[] to atom[] */
1227
if ( len_stereo0D ) {
1228
Extract0DParities( atom, num_atoms, atom_stereo0D, len_stereo0D,
1229
pStrErr, err, vABParityUnknown );
1232
if ( pInpAtomFlags ) {
1233
/* save chirality flag */
1234
*pInpAtomFlags |= InpAtomFlags;
1241
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1243
#if( FIX_READ_AUX_MEM_LEAK == 1 )
1244
/* 2005-08-04 avoid memory leak */
1245
if ( atom_stereo0D && !(stereo0D && *stereo0D == atom_stereo0D) ) {
1246
FreeInchi_Stereo0D( &atom_stereo0D );
1250
*szCoord = pszCoord;
1255
inchi_free( pszCoord );
1258
goto bypass_end_of_INChI_plain;
1259
/*return num_atoms;*/
1262
if ( atom_stereo0D ) {
1263
FreeInchi_Stereo0D( &atom_stereo0D );
1265
/* end of struct. reading cycle */
1267
if ( *err == INCHI_INP_ERROR_ERR ) {
1270
*err = INCHI_INP_EOF_ERR;
1271
return INCHI_INP_EOF_RET; /* no more data */
1273
bypass_end_of_INChI_plain:
1275
if ( num_atoms == INCHI_INP_ERROR_RET && atom_stereo0D ) {
1276
if ( stereo0D && *stereo0D == atom_stereo0D ) {
1280
FreeInchi_Stereo0D( &atom_stereo0D );
1282
while ( bTooLongLine &&
1283
0 < inchi_ios_getsTab1( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) {
1286
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1294
inchi_free( pszCoord );
1303
/***********************************************************/
1304
/* extract reversibility info from xml text INChI format */
1306
/* OBSOLETE CODE because InChI output in XML */
1307
/* does not exist anymore. Unsupported. */
1309
/***********************************************************/
1310
if ( nInputType == INPUT_INCHI_XML ) {
1312
static const char sStructHdrXml[] = "<structure";
1313
static const char sStructHdrXmlEnd[] = "</structure";
1314
static const char sStructHdrXmlNumber[] = "number=\"";
1315
static const char sStructHdrXmlIdName[] = "id.name=\"";
1316
static const char sStructHdrXmlIdValue[] = "id.value=\"";
1317
static const char sStructMsgXmlErr[] = "<message type=\"error (no InChI)\" value=\"";
1318
static const char sStructMsgXmlErrFatal[] = "<message type=\"fatal (aborted)\" value=\"";
1319
static const char sStructRevXmlRevHdr[] = "<reversibility>";
1320
static const char sStructRevXmlRevAt[] = "<atoms>";
1321
static const char sStructRevXmlRevAtEnd[] = "</atoms>";
1322
static const char sStructRevXmlRevBn[] = "<bonds>";
1323
static const char sStructRevXmlRevBnEnd[] = "</bonds>";
1324
static const char sStructRevXmlRevXYZ[] = "<xyz>";
1325
static const char sStructRevXmlRevXYZEnd[]= "</xyz>";
1326
static const char sStructAuxXml[] = "<identifier.auxiliary-info";
1327
static const char sStructAuxXmlEnd[] = "</identifier.auxiliary-info";
1328
int bInTheAuxInfo = 0;
1330
while ( 0 < (res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) ) {
1332
/********************* find and interpret structure header ************/
1333
if ( !memcmp(szLine, sStructHdrXml, sizeof(sStructHdrXml)-1) ) {
1335
p = szLine + sizeof(sStructHdrXml)-1;
1338
/* structure number */
1339
if ( q = strstr( p, sStructHdrXmlNumber ) ) {
1340
p = q + sizeof(sStructHdrXmlNumber)-1;
1341
longID = strtol( p, &q, 10);
1342
if ( q && *q == '\"' )
1346
pSdfLabel[0] = '\0';
1349
pSdfValue[0] = '\0';
1352
if ( q = strstr( p, sStructHdrXmlIdName ) ) {
1353
p = q + sizeof(sStructHdrXmlIdName)-1;
1354
q = strchr( p, '\"' );
1356
len = inchi_min( q-p+1, MAX_SDF_HEADER-1);
1358
mystrncpy( pSdfLabel, p, len );
1364
if ( q = strstr( p, sStructHdrXmlIdValue ) ) {
1365
p = q + sizeof(sStructHdrXmlIdValue)-1;
1366
q = strchr( p, '\"' );
1368
len = inchi_min( q-p+1, MAX_SDF_VALUE-1);
1370
mystrncpy( pSdfValue, p, len );
1378
bErrorMsg = bRestoreInfo = 0;
1380
if ( bHeaderRead && (bFatal=0, len=sizeof(sStructMsgXmlErr)-1, !memcmp(szLine, sStructMsgXmlErr, len)) ||
1381
bHeaderRead && (len=sizeof(sStructMsgXmlErrFatal)-1, !memcmp(szLine, sStructMsgXmlErrFatal, len))&&(bFatal=1)) {
1383
q = strchr( p, '\"' );
1384
if ( q && !bFindNext ) {
1390
MOLFILE_ERR_SET (*err, 0, p);
1393
*err = bFatal? INCHI_INP_FATAL_ERR : INCHI_INP_ERROR_ERR;
1394
num_atoms = bFatal? INCHI_INP_FATAL_RET : INCHI_INP_ERROR_RET;
1395
goto bypass_end_of_INChI;
1397
if ( bHeaderRead && !memcmp(szLine, sStructAuxXml, sizeof(sStructAuxXml)-1) ) {
1400
if ( bHeaderRead && !memcmp(szLine, sStructAuxXmlEnd, sizeof(sStructAuxXmlEnd)-1) ) {
1401
*err = INCHI_INP_ERROR_ERR;
1402
num_atoms = INCHI_INP_ERROR_RET;
1403
MOLFILE_ERR_SET (*err, 0, "Missing reversibility info" );
1404
goto bypass_end_of_INChI; /* reversibility info not found */
1406
if ( bHeaderRead && bInTheAuxInfo && !memcmp(szLine, sStructRevXmlRevHdr, sizeof(sStructRevXmlRevHdr)-1) ) {
1407
/*********************** atoms xml ***************************/
1409
res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1411
num_atoms = INCHI_INP_EOF_RET; /* no data, probably end of file */
1412
*err = INCHI_INP_EOF_ERR;
1413
goto bypass_end_of_INChI;
1415
if ( memcmp(szLine, sStructRevXmlRevAt, sizeof(sStructRevXmlRevAt)-1) ) {
1416
bHeaderRead = 0; /* invalid reversibility info; look for another header */
1419
/* read (the head of) the atoms line */
1420
res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1422
num_atoms = INCHI_INP_EOF_RET; /* no data */
1423
*err = INCHI_INP_EOF_ERR;
1424
goto bypass_end_of_INChI;
1427
num_atoms = strtol( p, &q, 10 );
1428
if ( !num_atoms || !q || !*q ) {
1429
num_atoms = INCHI_INP_EOF_RET; /* no atom data */
1430
*err = INCHI_INP_EOF_ERR;
1431
goto bypass_end_of_INChI;
1434
/* Molfile chirality flag */
1437
InpAtomFlags |= FLAG_INP_AT_CHIRAL;
1441
InpAtomFlags |= FLAG_INP_AT_NONCHIRAL;
1446
if ( num_atoms > max_num_at ) {
1450
memset( *at, 0, max_num_at * sizeof( **at ) );
1454
if ( !at || !*at ) {
1455
atom = Create_Atom( num_atoms+1 );
1457
num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1458
*err = INCHI_INP_FATAL_ERR;
1459
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1460
goto bypass_end_of_INChI;
1463
if ( stereo0D && *stereo0D ) {
1464
if ( num_atoms > max_len_stereo0D ) {
1465
FreeInchi_Stereo0D( stereo0D );
1467
memset( *stereo0D, 0, max_len_stereo0D * sizeof( **stereo0D ) );
1468
atom_stereo0D = *stereo0D;
1471
if ( !stereo0D || !*stereo0D ) {
1472
max_len_stereo0D = num_atoms+1;
1473
atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D );
1474
if ( !atom_stereo0D ) {
1475
num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1476
*err = INCHI_INP_FATAL_ERR;
1477
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1478
goto bypass_end_of_INChI;
1484
res2 = bTooLongLine2 = -1;
1486
/* read all atoms xml */
1487
while ( i < num_atoms ) {
1489
if ( !bItemIsOver && (int)sizeof(szLine)-res + pos > (int)sizeof(szNextLine) ) {
1490
/* load next line if possible */
1491
res2 = inchi_ios_gets( szNextLine, sizeof(szNextLine)-1, inp_molfile, &bTooLongLine2 );
1492
if ( res2 > 0 && memcmp(szNextLine, sStructRevXmlRevAtEnd, sizeof(sStructRevXmlRevAtEnd)-1) ) {
1494
res -= pos; /* number of chars left to process in szLine */
1495
memmove( szLine, p, res*sizeof(szLine[0]) ); /* move them to the start of the line */
1497
memcpy( szLine+res, szNextLine, (res2+1)*sizeof(szNextLine[0]) );
1500
bTooLongLine = bTooLongLine2;
1506
/* element, first char */
1507
if ( !isalpha( UCINT *p ) || !isupper( UCINT *p ) || i >= num_atoms ) {
1508
bHeaderRead = 0; /* wrong atom data */
1509
num_atoms = INCHI_INP_ERROR_RET; /* was 0, error */
1510
*err = INCHI_INP_ERROR_ERR; /* 40 */
1511
MOLFILE_ERR_SET (*err, 0, "Wrong atoms data");
1512
goto bypass_end_of_INChI;
1514
atom[i].elname[0] = *p ++;
1515
/* element, second char */
1516
if ( isalpha( UCINT *p ) && islower( UCINT *p ) ) {
1517
atom[i].elname[1] = *p ++;
1519
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1521
atom[i].el_number = get_periodic_table_number( atom[i].elname );
1523
/* bonds' valence */
1524
if ( isdigit( UCINT *p ) ) {
1525
AT_BONDS_VAL(atom,i) = (char)strtol( p, &q, 10 );
1526
if ( !AT_BONDS_VAL(atom,i) )
1527
AT_BONDS_VAL(atom,i) = ISOLATED_ATOM; /* same convention as in MOLfile, found zero bonds valence */
1531
atom[i].charge = (*p == '+')? 1 : (*p == '-')? -1 : 0;
1532
if ( atom[i].charge ) {
1534
if ( isdigit( UCINT *p ) ) {
1535
atom[i].charge *= (S_CHAR)(strtol( p, &q, 10 ) & CHAR_MASK);
1542
if ( isdigit( UCINT *p ) ) {
1543
atom[i].radical = (S_CHAR)strtol( p, &q, 10 );
1550
if ( isdigit( UCINT *p ) ) {
1551
int mw = strtol( p, &q, 10 );
1553
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1554
atom[i].isotopic_mass = mw;
1556
mw -= get_atw_from_elnum( atom[i].el_number );
1559
atom[i].iso_atw_diff = mw;
1566
parity = INCHI_PARITY_ODD;
1570
parity = INCHI_PARITY_EVEN;
1574
parity = INCHI_PARITY_UNKNOWN;
1578
parity = INCHI_PARITY_UNDEFINED;
1586
atom_stereo0D[len_stereo0D].central_atom = i;
1587
atom_stereo0D[len_stereo0D].parity = parity;
1588
atom_stereo0D[len_stereo0D].type = INCHI_StereoType_Tetrahedral;
1591
/* isotopic h, d, t */
1592
for ( k = 0; k < NUM_H_ISOTOPES; k ++ ) {
1593
if ( *p == szIsoH[k] ) {
1594
NUM_ISO_Hk(atom,i,k) = 1;
1596
if ( isdigit( UCINT *p ) ) {
1597
NUM_ISO_Hk(atom,i,k) = (char)strtol( p, &q, 10 );
1604
if ( !bItemIsOver || p - szLine != res || i != num_atoms ) {
1605
num_atoms = INCHI_INP_ERROR_RET; /* error */
1606
*err = INCHI_INP_ERROR_ERR;
1607
MOLFILE_ERR_SET (*err, 0, "Wrong number of atoms");
1608
goto bypass_end_of_INChI;
1610
/********************** bonds xml ****************************/
1611
res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1613
num_atoms = 0; /* no data */
1614
goto bypass_end_of_INChI;
1616
if ( memcmp(szLine, sStructRevXmlRevBn, sizeof(sStructRevXmlRevBn)-1) ) {
1617
bHeaderRead = 0; /* invalid reversibility info; look for another header */
1620
/* read (the head of) the xml bonds line */
1621
res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1623
num_atoms = INCHI_INP_ERROR_RET; /* was 0; error: no data -- eof? */
1624
*err = INCHI_INP_ERROR_ERR;
1625
goto bypass_end_of_INChI;
1629
res2 = bTooLongLine2 = -1;
1631
if ( !memcmp(szLine, sStructRevXmlRevBnEnd, sizeof(sStructRevXmlRevBnEnd)-1) ) {
1632
/* empty bonds section */
1636
/* read all bonds (xml), starting from atom 1 (not 0) */
1637
while ( i < num_atoms ) {
1639
if ( !bItemIsOver && (int)sizeof(szLine)-res + pos > (int)sizeof(szNextLine) ) {
1640
/* load next line if possible */
1641
res2 = inchi_ios_gets( szNextLine, sizeof(szNextLine)-1, inp_molfile, &bTooLongLine2 );
1642
if ( res2 > 0 && memcmp(szNextLine, sStructRevXmlRevBnEnd, sizeof(sStructRevXmlRevBnEnd)-1) ) {
1644
res -= pos; /* number of chars left to process in szLine */
1645
memmove( szLine, p, res*sizeof(szLine[0]) ); /* move them to the start of the line */
1647
memcpy( szLine+res, szNextLine, (res2+1)*sizeof(szNextLine[0]) );
1650
bTooLongLine = bTooLongLine2;
1656
if ( i >= num_atoms ) {
1659
/* bond, first char */
1665
if ( !isalpha( UCINT *p ) ) {
1666
num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1667
*err = INCHI_INP_ERROR_ERR;
1668
MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
1669
goto bypass_end_of_INChI;
1675
bond_parity = INCHI_PARITY_ODD;
1679
bond_parity = INCHI_PARITY_EVEN;
1683
bond_parity = INCHI_PARITY_UNKNOWN;
1687
bond_parity = INCHI_PARITY_UNDEFINED;
1694
if ( bond_parity ) {
1697
bond_parityNM = INCHI_PARITY_ODD;
1701
bond_parityNM = INCHI_PARITY_EVEN;
1705
bond_parityNM = INCHI_PARITY_UNKNOWN;
1709
bond_parityNM = INCHI_PARITY_UNDEFINED;
1720
/* neighbor of the current atom */
1721
if ( !isdigit( UCINT *p ) ) {
1722
num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1723
*err = INCHI_INP_ERROR_ERR;
1724
MOLFILE_ERR_SET (*err, 0, "Wrong bonds data");
1725
goto bypass_end_of_INChI;
1727
neigh = (int)strtol( p, &q, 10 )-1;
1729
if ( i >= num_atoms || neigh >= num_atoms ) {
1730
num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1731
*err = INCHI_INP_ERROR_ERR;
1732
MOLFILE_ERR_SET (*err, 0, "Bond to nonexistent atom");
1733
goto bypass_end_of_INChI;
1736
bond_stereo1 = bond_stereo2 = 0;
1738
/* bond type & 2D stereo */
1739
switch( bond_char ) {
1741
bond_type = INCHI_BOND_TYPE_SINGLE;
1742
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1EITHER;
1743
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2EITHER;
1746
bond_type = INCHI_BOND_TYPE_SINGLE;
1747
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2EITHER;
1748
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1EITHER;
1751
bond_type = INCHI_BOND_TYPE_DOUBLE;
1753
bond_stereo2 = INCHI_BOND_STEREO_DOUBLE_EITHER;
1756
bond_type = INCHI_BOND_TYPE_SINGLE;
1759
bond_type = INCHI_BOND_TYPE_DOUBLE;
1762
bond_type = INCHI_BOND_TYPE_TRIPLE;
1765
bond_type = INCHI_BOND_TYPE_ALTERN;
1768
bond_type = INCHI_BOND_TYPE_SINGLE;
1769
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1UP;
1770
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2UP;
1773
bond_type = INCHI_BOND_TYPE_SINGLE;
1774
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2UP;
1775
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1UP;
1778
bond_type = INCHI_BOND_TYPE_SINGLE;
1779
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_1DOWN;
1780
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_2DOWN;
1783
bond_type = INCHI_BOND_TYPE_SINGLE;
1784
bond_stereo1 = INCHI_BOND_STEREO_SINGLE_2DOWN;
1785
bond_stereo2 = INCHI_BOND_STEREO_SINGLE_1DOWN;
1788
num_atoms = INCHI_INP_ERROR_RET; /* error */
1789
*err = INCHI_INP_ERROR_ERR;
1790
MOLFILE_ERR_SET (*err, 0, "Wrong bond type");
1791
goto bypass_end_of_INChI;
1793
k = AT_NUM_BONDS(atom[i]) ++;
1794
atom[i].bond_type[k] = bond_type;
1795
atom[i].bond_stereo[k] = bond_stereo1;
1796
atom[i].neighbor[k] = (ATOM_NUMBER)neigh;
1797
k2 = AT_NUM_BONDS(atom[neigh]) ++;
1798
atom[neigh].bond_type[k2] = bond_type;
1799
atom[neigh].bond_stereo[k2] = bond_stereo2;
1800
atom[neigh].neighbor[k2] = (ATOM_NUMBER)i;
1801
bond_parity |= (bond_parityNM << SB_PARITY_SHFT);
1803
if ( bond_parity ) {
1804
if ( max_len_stereo0D <= len_stereo0D ) {
1805
/* realloc atom_Stereo0D */
1806
inchi_Stereo0D *new_atom_stereo0D = CreateInchi_Stereo0D( max_len_stereo0D+num_atoms );
1807
if ( !new_atom_stereo0D ) {
1808
num_atoms = INCHI_INP_FATAL_RET; /* fatal error: cannot allocate */
1809
*err = INCHI_INP_FATAL_ERR;
1810
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1811
goto bypass_end_of_INChI;
1813
memcpy( new_atom_stereo0D, atom_stereo0D, len_stereo0D * sizeof(*atom_stereo0D) );
1814
FreeInchi_Stereo0D( &atom_stereo0D );
1815
atom_stereo0D = new_atom_stereo0D;
1816
max_len_stereo0D += num_atoms;
1818
/* (a) i may be allene endpoint and neigh = allene middle point or
1819
(b) i may be allene middle point and neigh = allene endpoint
1820
!!!!! CURRENTLY ONLY (b) IS ALLOWED !!!!!
1822
atom_stereo0D[len_stereo0D].neighbor[1] = neigh; /* neigh < i */
1823
atom_stereo0D[len_stereo0D].neighbor[2] = i;
1824
atom_stereo0D[len_stereo0D].parity = bond_parity;
1825
atom_stereo0D[len_stereo0D].type = INCHI_StereoType_DoubleBond; /* incl allenes & cumulenes */
1829
if ( !bItemIsOver || p - szLine != res || i != num_atoms ) {
1830
num_atoms = INCHI_INP_ERROR_RET; /* error in input data */
1831
*err = INCHI_INP_ERROR_ERR;
1832
MOLFILE_ERR_SET (*err, 0, "Wrong number of bonds");
1833
goto bypass_end_of_INChI;
1835
/********************** coordinates xml ****************************/
1836
if ( pszCoord = (MOL_COORD*)inchi_malloc(inchi_max(num_atoms,1) * sizeof(MOL_COORD)) ) {
1837
memset( pszCoord, ' ', inchi_max(num_atoms,1) * sizeof(MOL_COORD));
1838
res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine );
1840
/* compare the header */
1841
memcmp(szLine, sStructRevXmlRevXYZ, sizeof(sStructRevXmlRevXYZ)-1) ||
1842
/* read (the head of) the coordinates (xml) line */
1843
0 >= (res = inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ))) {
1844
num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1845
*err = INCHI_INP_ERROR_ERR;
1846
MOLFILE_ERR_SET (*err, 0, "Missing atom coordinates data");
1847
goto bypass_end_of_INChI;
1851
res2 = bTooLongLine2 = -1;
1853
if ( !memcmp(szLine, sStructRevXmlRevXYZEnd, sizeof(sStructRevXmlRevXYZEnd)-1) ) {
1854
/* empty bonds section */
1858
/* read all coordinates (xml), starting from atom 1 (not 0) */
1859
while ( i < num_atoms ) {
1861
if ( !bItemIsOver && (int)sizeof(szLine)-res + pos > (int)sizeof(szNextLine) ) {
1862
/* load next line if possible */
1863
res2 = inchi_ios_gets( szNextLine, sizeof(szNextLine)-1, inp_molfile, &bTooLongLine2 );
1864
if ( res2 > 0 && memcmp(szNextLine, sStructRevXmlRevXYZEnd, sizeof(sStructRevXmlRevXYZEnd)-1) ) {
1866
res -= pos; /* number of chars left to process in szLine */
1867
memmove( szLine, p, res*sizeof(szLine[0]) ); /* move them to the start of the line */
1869
memcpy( szLine+res, szNextLine, (res2+1)*sizeof(szNextLine[0]) );
1872
bTooLongLine = bTooLongLine2;
1878
/* coord, first char */
1880
for ( k = 0; k < NUM_COORD; k ++ ) {
1881
pszCoord[i][LEN_COORD*k + 4] = '0';
1887
for ( k = 0; k < 3; k ++ ) {
1891
pszCoord[i][LEN_COORD*k + 4] = '0';
1896
pszCoord[i][LEN_COORD*k + 4] = '0';
1900
xyz = strtod( p, &q );
1901
bNonZeroXYZ = fabs(xyz) > MIN_BOND_LENGTH;
1903
memcpy( pszCoord[i]+LEN_COORD*k, p, q-p );
1908
pszCoord[i][LEN_COORD*k + 4] = '0';
1927
p ++; /* end of this triple of coordinates */
1930
num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1931
*err = INCHI_INP_ERROR_ERR;
1932
MOLFILE_ERR_SET (*err, 0, "Wrong atom coordinates data");
1933
goto bypass_end_of_INChI;
1936
if ( !bItemIsOver || p - szLine != res || i != num_atoms ) {
1937
num_atoms = INCHI_INP_ERROR_RET; /* error in input data: atoms, bonds & coord must be present together */
1938
*err = INCHI_INP_ERROR_ERR;
1939
MOLFILE_ERR_SET (*err, 0, "Wrong number of coordinates");
1940
goto bypass_end_of_INChI;
1942
} else { /* allocation failed */
1943
num_atoms = INCHI_INP_FATAL_RET;
1944
*err = INCHI_INP_FATAL_ERR;
1945
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
1946
goto bypass_end_of_INChI;
1948
/* set special valences and implicit H (xml) */
1953
int a1, a2, n1, n2, valence;
1954
int chem_bonds_valence;
1955
int nX=0, nY=0, nZ=0, nXYZ;
1957
/* special valences */
1958
for ( bNonMetal = 0; bNonMetal < 1 /*2*/; bNonMetal ++ ) {
1959
for ( a1 = 0; a1 < num_atoms; a1 ++ ) {
1960
int num_bond_type[MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE + 1];
1961
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1963
int bHasMetalNeighbor=0;
1965
memset( num_bond_type, 0, sizeof(num_bond_type) );
1967
valence = AT_BONDS_VAL(atom, a1); /* save atom valence if available */
1968
AT_BONDS_VAL(atom, a1) = 0;
1969
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
1971
atom[a1].orig_at_number = a1+1;
1974
for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
1975
bond_type = atom[a1].bond_type[n1] - MIN_INPUT_BOND_TYPE;
1976
if ( bond_type < 0 || bond_type > MAX_INPUT_BOND_TYPE - MIN_INPUT_BOND_TYPE ) {
1977
bond_type = 0; /* cannot happen */
1978
MOLFILE_ERR_SET (*err, 0, "Unknown bond type in InChI aux assigned as a single bond");
1981
num_bond_type[ bond_type ] ++;
1984
neigh = atom[a1].neighbor[n1];
1985
nX |= (fabs(atom[a1].x - atom[neigh].x) > MIN_BOND_LENGTH);
1986
nY |= (fabs(atom[a1].y - atom[neigh].y) > MIN_BOND_LENGTH);
1987
nZ |= (fabs(atom[a1].z - atom[neigh].z) > MIN_BOND_LENGTH);
1990
chem_bonds_valence = 0;
1991
for ( n1 = 0; MIN_INPUT_BOND_TYPE + n1 <= 3 && MIN_INPUT_BOND_TYPE + n1 <= MAX_INPUT_BOND_TYPE; n1 ++ ) {
1992
chem_bonds_valence += (MIN_INPUT_BOND_TYPE + n1) * num_bond_type[n1];
1994
if ( MIN_INPUT_BOND_TYPE <= INCHI_BOND_TYPE_ALTERN && INCHI_BOND_TYPE_ALTERN <= MAX_INPUT_BOND_TYPE &&
1995
( n2 = num_bond_type[INCHI_BOND_TYPE_ALTERN-MIN_INPUT_BOND_TYPE] ) ) {
1996
/* accept input aromatic bonds for now */
1999
chem_bonds_valence += 3; /* =A- */
2002
chem_bonds_valence += 4; /* =A< */
2005
/* if 1 or >= 4 aromatic bonds then replace such bonds with single bonds */
2006
for ( n1 = 0; n1 < AT_NUM_BONDS(atom[a1]); n1 ++ ) {
2007
if ( atom[a1].bond_type[n1] == INCHI_BOND_TYPE_ALTERN ) {
2009
a2 = atom[a1].neighbor[n1];
2010
p1 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
2012
atom[a1].bond_type[n1] =
2013
atom[a2].bond_type[p1-atom[a2].neighbor] = INCHI_BOND_TYPE_SINGLE;
2015
*err = -2; /* Program error */
2016
MOLFILE_ERR_SET (*err, 0, "Program error interpreting InChI aux");
2018
goto bypass_end_of_INChI; /* no structure */
2022
chem_bonds_valence += n2;
2023
*err |= 32; /* Unrecognized aromatic bond(s) replaced with single */
2024
MOLFILE_ERR_SET (*err, 0, "Atom has 1 or more than 3 aromatic bonds");
2029
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2030
/*************************************************************************************
2032
* Set number of hydrogen atoms
2036
num_iso_H = atom[a1].num_iso_H[1] + atom[a1].num_iso_H[2] + atom[a1].num_iso_H[3];
2037
if ( valence == ISOLATED_ATOM ) {
2038
atom[a1].num_iso_H[0] = 0;
2040
if ( valence && valence >= chem_bonds_valence ) {
2041
atom[a1].num_iso_H[0] = valence - chem_bonds_valence;
2043
if ( valence || bDoNotAddH ) {
2044
atom[a1].num_iso_H[0] = 0;
2046
if ( !bDoNotAddH ) {
2047
atom[a1].num_iso_H[0] = -1; /* auto add H */
2051
/* added 2006-07-19 to process aromatic bonds same way as from molfile */
2052
if ( n2 && !valence ) {
2053
int num_H = NUMH(atom, a1); /* only isotopic */
2054
int chem_valence = chem_bonds_valence;
2055
int bUnusualValenceArom =
2056
detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
2057
atom[a1].radical, chem_valence,
2058
num_H, atom[a1].valence );
2059
int bUnusualValenceNoArom =
2060
detect_unusual_el_valence( (int)atom[a1].el_number, atom[a1].charge,
2061
atom[a1].radical, chem_valence-1,
2062
num_H, atom[a1].valence );
2063
#if ( CHECK_AROMBOND2ALT == 1 )
2064
if ( bUnusualValenceArom && !bUnusualValenceNoArom && 0 == nBondsValToMetal( atom, a1) )
2066
if ( bUnusualValenceArom && !bUnusualValenceNoArom )
2069
/* typically NH in 5-member aromatic ring */
2070
chem_bonds_valence --;
2073
if ( n2 && valence ) {
2074
/* atom has aromatic bonds AND the chemical valence is known */
2075
int num_H = NUMH(atom, a1);
2076
int chem_valence = chem_bonds_valence + num_H;
2077
if ( valence == chem_valence-1 ) {
2078
/* typically NH in 5-member aromatic ring */
2079
chem_bonds_valence --;
2083
atom[a1].chem_bonds_valence = chem_bonds_valence;
2084
atom[a1].num_H = get_num_H( atom[a1].elname, atom[a1].num_H, atom[a1].num_iso_H, atom[a1].charge, atom[a1].radical,
2085
atom[a1].chem_bonds_valence,
2087
0, bDoNotAddH, bHasMetalNeighbor );
2092
if ( b23D && nNumBonds ) {
2096
*num_dimensions = b3D? 3 : b2D? 2 : 0;
2097
*num_bonds = nNumBonds;
2099
/*======= 0D parities =================================*/
2100
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2101
if ( len_stereo0D > 0 && atom_stereo0D && stereo0D ) {
2102
*stereo0D = atom_stereo0D;
2103
*num_stereo0D = len_stereo0D;
2105
FreeInchi_Stereo0D( &atom_stereo0D );
2106
*num_stereo0D = len_stereo0D = 0;
2109
for ( i = 0; i < len_stereo0D; i ++ ) {
2110
ATOM_NUMBER *p1, *p2;
2111
int sb_ord_from_a1 = -1, sb_ord_from_a2 = -1, bEnd1 = 0, bEnd2 = 0;
2112
switch( atom_stereo0D[i].type ) {
2114
case INCHI_StereoType_Tetrahedral:
2115
a1 = atom_stereo0D[i].central_atom;
2116
if ( atom_stereo0D[i].parity && (AT_NUM_BONDS(atom[a1]) == 3 || AT_NUM_BONDS(atom[a1]) == 4) ) {
2118
if ( AT_NUM_BONDS(atom[a1]) == 3 ) {
2119
atom_stereo0D[i].neighbor[kk++] = a1;
2121
for ( ii = 0; ii < AT_NUM_BONDS(atom[a1]); ii ++ ) {
2122
atom_stereo0D[i].neighbor[kk++] = atom[a1].neighbor[ii];
2128
case INCHI_StereoType_DoubleBond:
2129
#define MAX_CHAIN_LEN 20
2130
a1 = atom_stereo0D[i].neighbor[1];
2131
a2 = atom_stereo0D[i].neighbor[2];
2132
p1 = IN_NEIGH_LIST( atom[a1].neighbor, (ATOM_NUMBER)a2, AT_NUM_BONDS(atom[a1]) );
2133
p2 = IN_NEIGH_LIST( atom[a2].neighbor, (ATOM_NUMBER)a1, AT_NUM_BONDS(atom[a2]) );
2135
atom_stereo0D[i].type = INCHI_StereoType_None;
2136
atom_stereo0D[i].central_atom = NO_ATOM;
2137
atom_stereo0D[i].neighbor[0] =
2138
atom_stereo0D[i].neighbor[3] = -1;
2139
*err |= 64; /* Error in cumulene stereo */
2140
MOLFILE_ERR_SET (*err, 0, "0D stereobond not recognized");
2143
/* streobond, allene, or cumulene */
2145
sb_ord_from_a1 = p1 - atom[a1].neighbor;
2146
sb_ord_from_a2 = p2 - atom[a2].neighbor;
2148
if ( AT_NUM_BONDS(atom[a1]) == 2 &&
2149
atom[a1].bond_type[0] + atom[a1].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
2150
0 == inchi_NUMH2(atom, a1) &&
2151
(AT_NUM_BONDS(atom[a2]) != 2 ||
2152
atom[a2].bond_type[0] + atom[a2].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
2153
bEnd2 = 1; /* a2 is the end-atom, a1 is middle atom */
2155
if ( AT_NUM_BONDS(atom[a2]) == 2 &&
2156
atom[a2].bond_type[0] + atom[a2].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
2157
0 == inchi_NUMH2(atom, a2) &&
2158
(AT_NUM_BONDS(atom[a1]) != 2 ||
2159
atom[a1].bond_type[0] + atom[a1].bond_type[1] != 2*INCHI_BOND_TYPE_DOUBLE ) ) {
2160
bEnd1 = 1; /* a1 is the end-atom, a2 is middle atom */
2163
if ( bEnd2 + bEnd1 == 1 ) {
2164
/* allene or cumulene */
2165
ATOM_NUMBER chain[MAX_CHAIN_LEN+1], prev, cur, next;
2166
if ( bEnd2 && !bEnd1 ) {
2170
sb_ord_from_a1 = sb_ord_from_a2;
2172
sb_ord_from_a2 = -1;
2177
chain[len++] = next;
2178
while ( len < MAX_CHAIN_LEN ) { /* arbitrary very high upper limit to prevent infinite loop */
2181
/* follow double bond path && avoid going back */
2182
if ( AT_NUM_BONDS(atom[cur]) == 2 &&
2183
atom[cur].bond_type[0]+atom[cur].bond_type[1] == 2*INCHI_BOND_TYPE_DOUBLE &&
2184
0 == inchi_NUMH2(atom, cur) ) {
2185
next = atom[cur].neighbor[atom[cur].neighbor[0] == prev];
2186
chain[len++] = next;
2192
(p2 = IN_NEIGH_LIST( atom[cur].neighbor, (ATOM_NUMBER)prev, AT_NUM_BONDS(atom[cur]))) ) {
2193
sb_ord_from_a2 = p2 - atom[cur].neighbor;
2195
/* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
2196
atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[sb_ord_from_a1 == 0];
2197
atom_stereo0D[i].neighbor[1] = a1;
2198
atom_stereo0D[i].neighbor[2] = a2;
2199
atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[sb_ord_from_a2 == 0];
2201
atom_stereo0D[i].central_atom = chain[len/2];
2202
atom_stereo0D[i].type = INCHI_StereoType_Allene;
2204
atom_stereo0D[i].central_atom = NO_ATOM;
2208
atom_stereo0D[i].type = INCHI_StereoType_None;
2209
atom_stereo0D[i].central_atom = NO_ATOM;
2210
atom_stereo0D[i].neighbor[0] =
2211
atom_stereo0D[i].neighbor[3] = -1;
2212
*err |= 64; /* Error in cumulene stereo */
2213
MOLFILE_ERR_SET (*err, 0, "Cumulene stereo not recognized (0D)");
2216
#undef MAX_CHAIN_LEN
2218
/****** a normal possibly stereogenic bond -- not an allene or cumulene *******/
2219
/* by design we need to pick up the first non-stereo-bond-neighbor as "sn"-atom */
2220
sb_ord_from_a1 = p1 - atom[a1].neighbor;
2221
sb_ord_from_a2 = p2 - atom[a2].neighbor;
2222
atom_stereo0D[i].neighbor[0] = atom[a1].neighbor[p1 == atom[a1].neighbor];
2223
atom_stereo0D[i].neighbor[3] = atom[a2].neighbor[p2 == atom[a2].neighbor];
2224
atom_stereo0D[i].central_atom = NO_ATOM;
2226
if ( atom_stereo0D[i].type != INCHI_StereoType_None &&
2227
sb_ord_from_a1 >= 0 && sb_ord_from_a2 >= 0 &&
2228
ATOM_PARITY_WELL_DEF( SB_PARITY_2(atom_stereo0D[i].parity) ) ) {
2229
/* Detected well-defined disconnected stereo
2230
* locate first non-metal neighbors */
2231
int a, n, j, /* k,*/ sb_ord, cur_neigh, min_neigh;
2232
for ( k = 0; k < 2; k ++ ) {
2233
a = k? atom_stereo0D[i].neighbor[2] : atom_stereo0D[i].neighbor[1];
2234
sb_ord = k? sb_ord_from_a2 : sb_ord_from_a1;
2235
min_neigh = num_atoms;
2236
for ( n = j = 0; j < AT_NUM_BONDS(atom[a]); j ++ ) {
2237
cur_neigh = atom[a].neighbor[j];
2238
if ( j != sb_ord && !IS_METAL_ATOM(atom, cur_neigh) ) {
2239
min_neigh = inchi_min( cur_neigh, min_neigh );
2242
if ( min_neigh < num_atoms ) {
2243
atom_stereo0D[i].neighbor[k?3:0] = min_neigh;
2245
MOLFILE_ERR_SET (*err, 0, "Cannot find non-metal stereobond neighor (0D)");
2253
/* end of 0D parities extraction */
2256
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2258
/* transfer atom_stereo0D[] to atom[] */
2259
if ( len_stereo0D ) {
2260
Extract0DParities( atom, num_atoms, atom_stereo0D, len_stereo0D,
2261
pStrErr, err, vABParityUnknown );
2264
if ( pInpAtomFlags ) {
2265
/* save chirality flag */
2266
*pInpAtomFlags |= InpAtomFlags;
2273
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2276
*szCoord = pszCoord;
2281
inchi_free( pszCoord );
2283
goto bypass_end_of_INChI;
2284
/*return num_atoms;*/
2287
if ( atom_stereo0D ) {
2288
FreeInchi_Stereo0D( &atom_stereo0D );
2290
/* end of struct. reading cycle, code never used? */
2292
if ( *err == INCHI_INP_ERROR_ERR ) {
2295
*err = INCHI_INP_EOF_ERR;
2296
return INCHI_INP_EOF_RET; /* no more data */
2298
bypass_end_of_INChI:
2300
if ( num_atoms == INCHI_INP_ERROR_RET && atom_stereo0D ) {
2301
if ( stereo0D && *stereo0D == atom_stereo0D ) {
2305
FreeInchi_Stereo0D( &atom_stereo0D );
2307
if ( !memcmp(szLine, sStructHdrXmlEnd, sizeof(sStructHdrXmlEnd)-1) )
2309
if ( !memcmp(szLine, sStructHdrXml, sizeof(sStructHdrXml)-1) )
2312
while ( num_struct > 0 && 0 < inchi_ios_gets( szLine, sizeof(szLine)-1, inp_molfile, &bTooLongLine ) ) {
2313
if ( !memcmp(szLine, sStructHdrXmlEnd, sizeof(sStructHdrXmlEnd)-1) )
2316
if ( !memcmp(szLine, sStructHdrXml, sizeof(sStructHdrXml)-1) )
2327
#undef IN_NEIGH_LIST
2330
#if( defined(INCHI_LIBRARY) || defined(INCHI_MAIN) )
2337
#undef IN_NEIGH_LIST
2344
#undef ISOLATED_ATOM
2346
#undef IS_METAL_ATOM
2352
/**********************************************************************************/
2353
int INChIToInchi_Input( INCHI_IOSTREAM *inp_molfile, inchi_Input *orig_at_data, int bMergeAllInputStructures,
2354
int bDoNotAddH, int vABParityUnknown, INPUT_TYPE nInputType,
2355
char *pSdfLabel, char *pSdfValue, long *lSdfId, INCHI_MODE *pInpAtomFlags,
2356
int *err, char *pStrErr )
2358
/* inp_ATOM *at = NULL; */
2359
int num_dimensions_new;
2360
int num_inp_bonds_new;
2361
int num_inp_atoms_new;
2363
inchi_Atom *at_new = NULL;
2364
inchi_Atom *at_old = NULL;
2365
inchi_Stereo0D *stereo0D_new = NULL;
2366
inchi_Stereo0D *stereo0D_old = NULL;
2367
int nNumAtoms = 0, nNumStereo0D = 0;
2368
MOL_COORD *szCoordNew = NULL;
2369
MOL_COORD *szCoordOld = NULL;
2376
/*FreeOrigAtData( orig_at_data );*/
2381
at_old = orig_at_data? orig_at_data->atom : NULL; /* save pointer to the previous allocation */
2382
stereo0D_old = orig_at_data? orig_at_data->stereo0D : NULL;
2385
INChIToInchi_Atom( inp_molfile, orig_at_data? &stereo0D_new:NULL, &num_inp_0D_new,
2386
bDoNotAddH, vABParityUnknown, nInputType,
2387
orig_at_data? &at_new:NULL, MAX_ATOMS,
2388
&num_dimensions_new, &num_inp_bonds_new,
2389
pSdfLabel, pSdfValue, lSdfId, pInpAtomFlags, err, pStrErr );
2390
if ( num_inp_atoms_new <= 0 && !*err ) {
2391
MOLFILE_ERR_SET (*err, 0, "Empty structure");
2394
if ( orig_at_data && !num_inp_atoms_new && 10 < *err && *err < 20 && orig_at_data->num_atoms > 0 && bMergeAllInputStructures ) {
2395
*err = 0; /* end of file */
2398
if ( num_inp_atoms_new > 0 && orig_at_data ) {
2399
/* merge pOrigDataTmp + orig_at_data => pOrigDataTmp; */
2400
nNumAtoms = num_inp_atoms_new + orig_at_data->num_atoms;
2401
nNumStereo0D = num_inp_0D_new + orig_at_data->num_stereo0D;
2402
if ( nNumAtoms >= MAX_ATOMS ) {
2403
MOLFILE_ERR_SET (*err, 0, "Too many atoms");
2405
orig_at_data->num_atoms = -1;
2408
/* the first structure */
2409
orig_at_data->atom = at_new; at_new = NULL;
2410
orig_at_data->num_atoms = num_inp_atoms_new; num_inp_atoms_new = 0;
2411
orig_at_data->stereo0D = stereo0D_new; stereo0D_new = NULL;
2412
orig_at_data->num_stereo0D = num_inp_0D_new; num_inp_0D_new = 0;
2414
if ( orig_at_data->atom = CreateInchi_Atom( nNumAtoms ) ) {
2415
/* switch at_new <--> orig_at_data->at; */
2416
if ( orig_at_data->num_atoms ) {
2417
memcpy( orig_at_data->atom, at_old, orig_at_data->num_atoms * sizeof(orig_at_data->atom[0]) );
2418
/* adjust numbering in the newly read structure */
2419
for ( i = 0; i < num_inp_atoms_new; i ++ ) {
2420
for ( j = 0; j < at_new[i].num_bonds; j ++ ) {
2421
at_new[i].neighbor[j] += orig_at_data->num_atoms;
2425
FreeInchi_Atom( &at_old );
2426
/* copy newly read structure */
2427
memcpy( orig_at_data->atom + orig_at_data->num_atoms,
2429
num_inp_atoms_new * sizeof(orig_at_data->atom[0]) );
2430
/* cpy newly read 0D stereo */
2431
if ( num_inp_0D_new > 0 && stereo0D_new ) {
2432
if ( orig_at_data->stereo0D = CreateInchi_Stereo0D( nNumStereo0D ) ) {
2433
memcpy( orig_at_data->stereo0D, stereo0D_old, orig_at_data->num_stereo0D * sizeof(orig_at_data->stereo0D[0]) );
2434
/* adjust numbering in the newly read structure */
2435
for ( i = 0; i < num_inp_0D_new; i ++ ) {
2436
if ( stereo0D_new[i].central_atom >= 0 ) {
2437
stereo0D_new[i].central_atom += orig_at_data->num_atoms;
2439
for ( j = 0; j < 4; j ++ ) {
2440
stereo0D_new[i].neighbor[j] += orig_at_data->num_atoms;
2443
FreeInchi_Stereo0D( &stereo0D_old );
2444
memcpy( orig_at_data->stereo0D+orig_at_data->num_stereo0D,
2446
num_inp_0D_new * sizeof(orig_at_data->stereo0D[0]) );
2449
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
2455
/* update lengths */
2456
orig_at_data->num_atoms += num_inp_atoms_new;
2457
orig_at_data->num_stereo0D += num_inp_0D_new;
2459
MOLFILE_ERR_SET (*err, 0, "Out of RAM");
2463
if ( num_inp_atoms_new > 0 ) {
2464
nNumAtoms += num_inp_atoms_new;
2466
FreeInchi_Atom( &at_new );
2467
num_inp_atoms_new = 0;
2468
FreeInchi_Stereo0D( &stereo0D_new );
2471
} while ( !*err && bMergeAllInputStructures );
2474
orig_at_data->num_components =
2475
MarkDisconnectedComponents( orig_at_data );
2476
if ( orig_at_data->num_components == 0 ) {
2477
MOLFILE_ERR_SET (*err, 0, "No components found");
2480
if ( orig_at_data->num_components < 0 ) {
2481
MOLFILE_ERR_SET (*err, 0, "Too many components");
2487
inchi_free( szCoordNew );
2490
inchi_free( at_new );
2494
if ( ReconcileAllCmlBondParities( orig_at_data->atom, orig_at_data->num_atoms ) ) {
2495
MOLFILE_ERR_SET (*err, 0, "Cannot reconcile stereobond parities");
2496
if (!orig_at_data->num_atoms) {
2503
FreeInchi_Input( orig_at_data );
2505
if ( *err && !(10 < *err && *err < 20) && pStrErr && !pStrErr[0] ) {
2506
MOLFILE_ERR_SET (*err, 0, "Unknown error"); /* <BRKPT> */
2508
return orig_at_data? orig_at_data->num_atoms : nNumAtoms;
2514
#undef AB_MAX_WELL_DEFINED_PARITY
2515
#undef AB_MIN_WELL_DEFINED_PARITY
2516
#include "extr_ct.h"
2517
/****************************************************************************************/
2518
int Extract0DParities( inp_ATOM *at, int nNumAtoms, inchi_Stereo0D *stereo0D,
2519
int num_stereo0D, char *pStrErr, int *err,
2520
int vABParityUnknown)
2522
/* vABParityUnknown holds actual value of an internal constant signifying */
2523
/* unknown parity: either the same as for undefined parity (default==standard) */
2524
/* or a specific one (non-std; requested by SLUUD switch). */
2526
if ( stereo0D && num_stereo0D > 0 ) {
2527
int i0D, a2, k, k_prev, type, j, j1, j2, len, parity, parityNM;
2528
int sb_ord_from_i1, sb_ord_from_i2, sn_ord_from_i1, sn_ord_from_i2;
2529
AT_NUMB i1n, i2n, i1, i2;
2530
for ( i0D = 0; i0D < num_stereo0D; i0D ++ ) {
2531
parity = (stereo0D[i0D].parity & SB_PARITY_MASK);
2532
parityNM = (stereo0D[i0D].parity & SB_PARITY_FLAG) >> SB_PARITY_SHFT;
2533
if ( parity == INCHI_PARITY_NONE ||
2534
parity != INCHI_PARITY_ODD && parity != INCHI_PARITY_EVEN &&
2535
parity != INCHI_PARITY_UNKNOWN && parity != INCHI_PARITY_UNDEFINED ) {
2537
sprintf( szTemp, "#%d", i0D+1 );
2538
MOLFILE_ERR_SET (*err, 0, "Wrong 0D stereo descriptor(s):");
2539
MOLFILE_ERR_SET (*err, 0, szTemp);
2540
continue; /* warning */
2542
type = stereo0D[i0D].type;
2543
a2 = stereo0D[i0D].central_atom; /* central atom or -1 */
2546
sb_ord_from_i1 = sb_ord_from_i2 = sn_ord_from_i1 = sn_ord_from_i2 = -1;
2547
i1n = i2n = i1 = i2 = MAX_ATOMS+1;
2549
if ( (type == INCHI_StereoType_Tetrahedral ||
2550
type == INCHI_StereoType_Allene ) &&
2551
0 <= a2 && a2 < nNumAtoms ||
2552
type == INCHI_StereoType_DoubleBond &&
2554
/* test the quadruplet */
2555
for ( j = 0, k_prev = -1; j < 4; j ++, k_prev = k ) {
2556
k = stereo0D[i0D].neighbor[j];
2557
if ( k < 0 || k >= nNumAtoms || k_prev == k )
2559
/* tetrahedral atom connectivity test */
2560
if ( type == INCHI_StereoType_Tetrahedral &&
2562
!is_in_the_list( at[a2].neighbor, (AT_NUMB)k, at[a2].valence) ) {
2565
/* Double bond, Cumulene and allene are tested in the next if() */
2568
/* find in the adjacency lists the double bond neighbor that leads to the opposite atom */
2569
if ( j == 4 && (type == INCHI_StereoType_Allene ||
2570
type == INCHI_StereoType_DoubleBond) ) {
2571
AT_NUMB *p1 = NULL, *p2 = NULL, *q1 = NULL, *q2 = NULL;
2572
i1n = (AT_NUMB)stereo0D[i0D].neighbor[0];
2573
i1 = (AT_NUMB)stereo0D[i0D].neighbor[1];
2574
i2 = (AT_NUMB)stereo0D[i0D].neighbor[2];
2575
i2n = (AT_NUMB)stereo0D[i0D].neighbor[3];
2576
/* find q1 and q2 */
2577
if ( !(q1 = is_in_the_list( at[i1].neighbor, i1n, at[i1].valence)) ||
2578
!(q2 = is_in_the_list( at[i2].neighbor, i2n, at[i2].valence)) ) {
2579
j = -2; /* error flag */
2581
/* allene or cumulene; follow double bonds from i1 to i2 */
2582
if ( !(p1 = is_in_the_list( at[i1].neighbor, i2, at[i1].valence)) ) {
2583
/* at[i1] and at[i2] are not connected: can be only allene or cumulene */
2584
AT_NUMB prev, cur, next;
2585
int num_dbond, i, next_ord, half_len;
2589
while ( len < 20 ) { /* arbitrary very high upper limit to prevent infinite loop */
2592
for ( i = 0, num_dbond = 0; i < at[cur].valence; i ++ ) {
2593
/* follow double bond path && avoid going back */
2594
if ( at[cur].bond_type[i] == BOND_TYPE_DOUBLE &&
2595
prev != at[cur].neighbor[i] ) {
2596
next = at[cur].neighbor[i];
2601
if ( num_dbond == 1 && next != i1 ) {
2604
sb_ord_from_i1 = next_ord;
2606
if ( type == INCHI_StereoType_Allene && next == (AT_NUMB)a2 ) {
2613
if ( cur == i2 && prev != cur && 0 == num_dbond && len > 1 &&
2614
(p2 = is_in_the_list( at[i2].neighbor, prev, at[i2].valence)) &&
2615
(type != INCHI_StereoType_Allene || len == 2*half_len )) {
2616
sb_ord_from_i2 = p2 - at[i2].neighbor;
2617
sn_ord_from_i1 = q1 - at[i1].neighbor;
2618
sn_ord_from_i2 = q2 - at[i2].neighbor;
2620
j = -5; /* error flag */
2623
/* allene must have been already processed, otherwise error */
2624
if ( type == INCHI_StereoType_Allene ) {
2625
/* error: atoms #1 and #2 of allene are connected */
2626
j = -3; /* error flag */
2628
/* double bond only; the bond type is not checked because at the end
2629
of the normalization it may happen to be alternating */
2630
if ( type == INCHI_StereoType_DoubleBond &&
2631
(p2 = is_in_the_list( at[i2].neighbor, i1, at[i2].valence) ) ) {
2632
sb_ord_from_i1 = p1 - at[i1].neighbor;
2633
sb_ord_from_i2 = p2 - at[i2].neighbor;
2634
sn_ord_from_i1 = q1 - at[i1].neighbor;
2635
sn_ord_from_i2 = q2 - at[i2].neighbor;
2637
j = -4; /* error flag */
2642
sprintf( szTemp, "#%d", i0D+1 );
2643
MOLFILE_ERR_SET (*err, 0, "Wrong 0D stereo descriptor(s):");
2644
MOLFILE_ERR_SET (*err, 0, szTemp);
2645
continue; /* error */
2649
case INCHI_StereoType_None:
2651
case INCHI_StereoType_DoubleBond:
2652
case INCHI_StereoType_Allene:
2653
for ( j1 = 0; j1 < MAX_NUM_STEREO_BONDS && at[i1].sb_parity[j1]; j1 ++ )
2655
for ( j2 = 0; j2 < MAX_NUM_STEREO_BONDS && at[i2].sb_parity[j2]; j2 ++ )
2657
if ( j1 < MAX_NUM_STEREO_BONDS && j2 < MAX_NUM_STEREO_BONDS &&
2658
sb_ord_from_i1 >= 0 && sb_ord_from_i2 >= 0 &&
2659
sn_ord_from_i1 >= 0 && sn_ord_from_i2 >= 0) {
2662
case INCHI_PARITY_ODD:
2663
at[i1].sb_parity[j1] = AB_PARITY_ODD;
2664
at[i2].sb_parity[j2] = AB_PARITY_EVEN;
2666
case INCHI_PARITY_EVEN:
2667
at[i1].sb_parity[j1] = AB_PARITY_ODD;
2668
at[i2].sb_parity[j2] = AB_PARITY_ODD;
2670
case INCHI_PARITY_UNDEFINED:
2671
at[i1].sb_parity[j1] = AB_PARITY_UNDF;
2672
at[i2].sb_parity[j2] = AB_PARITY_UNDF;
2675
if ( parity == INCHI_PARITY_UNKNOWN )
2677
at[i1].sb_parity[j1] = vABParityUnknown;
2678
at[i2].sb_parity[j2] = vABParityUnknown;
2682
at[i1].sb_parity[j1] = AB_PARITY_NONE;
2683
at[i2].sb_parity[j2] = AB_PARITY_NONE;
2688
switch( parityNM ) {
2689
case INCHI_PARITY_ODD:
2690
at[i1].sb_parity[j1] |= AB_PARITY_ODD << SB_PARITY_SHFT;
2691
at[i2].sb_parity[j2] |= AB_PARITY_EVEN << SB_PARITY_SHFT;
2693
case INCHI_PARITY_EVEN:
2694
at[i1].sb_parity[j1] |= AB_PARITY_ODD << SB_PARITY_SHFT;
2695
at[i2].sb_parity[j2] |= AB_PARITY_ODD << SB_PARITY_SHFT;
2697
case INCHI_PARITY_UNDEFINED:
2698
at[i1].sb_parity[j1] |= AB_PARITY_UNDF << SB_PARITY_SHFT;
2699
at[i2].sb_parity[j2] |= AB_PARITY_UNDF << SB_PARITY_SHFT;
2702
if ( parityNM == INCHI_PARITY_UNKNOWN )
2704
at[i1].sb_parity[j1] |= vABParityUnknown << SB_PARITY_SHFT;
2705
at[i2].sb_parity[j2] |= vABParityUnknown << SB_PARITY_SHFT;
2710
at[i1].sb_ord[j1] = sb_ord_from_i1;
2711
at[i1].sn_ord[j1] = sn_ord_from_i1;
2712
at[i1].sn_orig_at_num[j1] = at[i1n].orig_at_number;
2714
at[i2].sb_ord[j2] = sb_ord_from_i2;
2715
at[i2].sn_ord[j2] = sn_ord_from_i2;
2716
at[i2].sn_orig_at_num[j2] = at[i2n].orig_at_number;
2719
case INCHI_StereoType_Tetrahedral:
2721
case INCHI_PARITY_ODD:
2722
at[a2].p_parity = AB_PARITY_ODD;
2724
case INCHI_PARITY_EVEN:
2725
at[a2].p_parity = AB_PARITY_EVEN;
2727
case INCHI_PARITY_UNDEFINED:
2728
at[a2].p_parity = AB_PARITY_UNDF;
2731
if (parity == INCHI_PARITY_UNKNOWN )
2733
at[a2].p_parity = vABParityUnknown;
2739
for ( j = 0; j < 4; j ++ ) {
2740
k = stereo0D[i0D].neighbor[j];
2741
at[a2].p_orig_at_num[j] = at[k].orig_at_number;
2748
/* take care of Unknown stereobonds: */
2749
/* copy their Unknown stereo descriptors to at->bond_stereo (2005-03-01) */
2750
/* Note: to this stage, unk/undef set to what was requested */
2751
/*( through vABParityUnknown ) (2009-12-12) */
2752
FixUnkn0DStereoBonds(at, nNumAtoms);
2754
#ifdef INCHI_LIBRARY
2756
if ( k = ReconcileAllCmlBondParities( at, nNumAtoms, 0 ) ) {
2758
sprintf( szErrCode, "%d", k);
2759
AddMOLfileError( pStrErr, "0D Parities Reconciliation failed:" );
2760
AddMOLfileError( pStrErr, szErrCode );