getChargeFromInChI

PURPOSE ^

return the charge from a given InChI string

SYNOPSIS ^

function charge=getChargeFromInChI(InChI)

DESCRIPTION ^

return the charge from a given InChI string

INPUT
 InChI string

 OUTPUT
 charge

 Ronan Fleming 23 Sept 09

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function charge=getChargeFromInChI(InChI)
0002 %return the charge from a given InChI string
0003 %
0004 %INPUT
0005 % InChI string
0006 %
0007 % OUTPUT
0008 % charge
0009 %
0010 % Ronan Fleming 23 Sept 09
0011 
0012 k = strfind(InChI, '/q');
0013 if isempty(k)
0014     charge=0;
0015 else
0016     %disp(InChI)
0017     %check if it has a composite formula
0018     indDots=findstr('.',getFormulaFromInChI(InChI));
0019     if isempty(indDots)
0020         if strcmp(InChI(k+2),'+')
0021             %positive charge
0022             sgn=1;
0023         else
0024             sgn=-1;
0025         end
0026         if length(InChI)<k+4
0027             charge=sgn*str2num(InChI(k+3:k+3));
0028         else
0029             if strcmp(InChI(k+4),'/')
0030                 charge=sgn*str2num(InChI(k+3:k+3));
0031             else
0032                 if length(InChI)<k+5
0033                     charge=sgn*str2num(InChI(k+3:k+4));
0034                 else
0035                     if strcmp(InChI(k+5),'/')
0036                         charge=sgn*str2num(InChI(k+3:k+4));
0037                     else
0038                         disp(InChI)
0039                         error('Charge too high')
0040                     end
0041                 end
0042             end
0043         end
0044     else
0045         %todo - cleanup, this code is a bit messy but seems to work
0046         totalCharge=0;
0047         for d=1:length(indDots)+1
0048             while  strcmp(InChI(k+2),';')
0049                 k=k+1;
0050             end
0051             if strcmp(InChI(k+2),'+') || strcmp(InChI(k+2),'-')
0052                 if strcmp(InChI(k+2),'+')
0053                     %positive charge
0054                     sgn=1;
0055                 else
0056                     sgn=-1;
0057                 end
0058             else
0059                 if strcmp(InChI(k+2),'/')
0060                     break;
0061                 else
0062                     disp(InChI)
0063                     sgn=0;
0064                     warning(['Not valid charge: ' InChI(k+2)])
0065                     break;
0066                     %error(InChI(k+2))
0067                 end
0068             end
0069             if length(InChI)<k+4
0070                 charge=sgn*str2num(InChI(k+3:k+3));
0071                 k=k+2;
0072             else
0073                 if strcmp(InChI(k+4),'/') || strcmp(InChI(k+4),';')
0074                     charge=sgn*str2num(InChI(k+3:k+3));
0075                     k=k+2;
0076                 else
0077                     if length(InChI)<k+5 
0078                         charge=sgn*str2num(InChI(k+3:k+4));
0079                         k=k+3;
0080                     else
0081                         if strcmp(InChI(k+5),'/')  || strcmp(InChI(k+5),';')
0082                             charge=sgn*str2num(InChI(k+3:k+4));
0083                             k=k+3;
0084                         else
0085                             disp(InChI)
0086                             error('Charge too high')
0087                         end
0088                     end
0089                 end
0090             end
0091             d=d+1;
0092             totalCharge=totalCharge+charge;
0093         end
0094         charge=totalCharge;
0095     end
0096 end

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003