Module: Sylfy::Utils

Defined in:
lib/sylfy/utils/keyhash.rb,
lib/sylfy/utils/reactionkey.rb

Defined Under Namespace

Modules: Keyhash

Constant Summary collapse

COFACTORS =

COFACTORS = [

["FAD-FADH2", ["VWWQXMAJTJZDQX-UYBVJOGSSA-N"], ["YPZRHBJKEMOYQH-UYBVJOGSSA-N"], "B"],
["FMN-FMNH2", ["FVTCRASFADXXNN-SCRDCRAPSA-N"], ["YTNIXZGTHTVJBW-SCRDCRAPSA-N"], "C"],
["NAD-NADH", ["BAWFJGJZGIEFAR-NNYOXOHSSA-O"], ["BOPGDPNILDQYTO-NNYOXOHSSA-N"], "D"],
["NADP-NADPH", ["XJLXINKUBYWONI-NNYOXOHSSA-O"], ["ACFIXJIJDZMPPO-NNYOXOHSSA-N"], "E"]

]

[]
PORTERS =
[]

Class Method Summary collapse

Class Method Details

.reactionkey(leftin, rightin, direction = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sylfy/utils/reactionkey.rb', line 68

def self.reactionkey(leftin, rightin, direction = nil)
			
    #key[:first].push(item[0][0..13])
    #key[:second].push(item[0][15..22])
    #key[:flag].push(item[0][23])
    #key[:ver].push(item[0][24])
    #key[:indicator].push(item[0][26])
    #
			#AAAAAAAAAAAAAA-BBBBBBBBFV-P
			#AAAAAAAAAAAAAA is the first hash block.
			#It is a 14-character hash encoding the basic (Mobile-H) InChI layer, that is, molecular skeleton (connectivity).
			#BBBBBBBB is the second hash block.
			#It is an 8-character hash of the remaining layers (except for the "/p" segment, which accounts for added or removed protons and is not hashed at all; instead, the number of protons is indicated at the end of the InChIKey). In all cases, this block encodes stereochemistry and isotopic substitution information, associated with molecular connectivity expressed by the first block. In case of Non-standard InChI, it also encodes information on the exact position of tautomeric hydrogens (if any), as well as on the related stereo/isotopic data.
			#F is a flag which indicates the kind of InChIKey.
			#It is 'S' for Standard InChIKey (produced out of Standard InChI) and 'N' for Non-standard.
			#V is an InChI version character.
			#It is 'A' for version 1 ( 'B' is reserved for version 2, etc. ).
			#P is a [de]protonation indicator.
			#
    #
    # input InChIKey;coef
    
    version = "A"
    
			reverse_sign = {'<=' => '=>', '=>' => '<=', '<=>' => '<=>', '<?>' => '<?>'}
			sign_symbol = {'<=' => 'B', '=>' => 'F', '<=>' => 'R', '<?>' => 'U'}
			
    both = []
    
    if direction && sign_symbol.keys.include?(direction) && leftin && rightin && !leftin.empty? && !rightin.empty? && leftin != rightin
				
				left = leftin.dup
				right = rightin.dup
				
				cofactor_symbol = "A"
				
				COFACTORS.each do |cofactor|
					if left.include_all?(cofactor[1]) && right.include_all?(cofactor[2])
						left.delete_if {|x| cofactor[1].include?(x.split(/;/)[0]) }
						right.delete_if {|x| cofactor[2].include?(x.split(/;/)[0]) }
						cofactor_symbol = cofactor[3]
					elsif left.include_all?(cofactor[2]) && right.include_all?(cofactor[1])
						left.delete_if {|x| cofactor[2].include?(x.split(/;/)[0]) }
						right.delete_if {|x| cofactor[1].include?(x.split(/;/)[0]) }
						cofactor_symbol = cofactor[3]
					end
				end
				
				if (left.sort <=> right.sort) < 0
					dsign = sign_symbol[direction]
					both = [left.sort, right.sort]
				else
					dsign = sign_symbol[reverse_sign[direction]]
					both = [right.sort, left.sort]
				end
				
			elsif leftin != rightin && ((leftin && !leftin.empty?) || (rightin && rightin.empty?))
				left = leftin ? leftin.dup : []
				right = rightin ? rightin.dup : []
				
				cofactor_symbol = "Z"
				
				PORTERS.each do |cofactor|
					if left.include_all?(cofactor[1]) && right.include_all?(cofactor[2])
						left.delete_if {|x| cofactor[1]._key_include?(x) }
						right.delete_if {|x| cofactor[2]._key_include?(x) }
						cofactor_symbol = cofactor[3]
					elsif left.include_all?(cofactor[2]) && right.include_all?(cofactor[1])
						left.delete_if {|x| cofactor[2]._key_include?(x) }
						right.delete_if {|x| cofactor[1]._key_include?(x) }
						cofactor_symbol = cofactor[3]
					end
				end
				
				if (left.sort <=> right.sort) < 0
					dsign = "A"
					both = [left.sort, right.sort]
				else
					dsign = "Z"
					both = [right.sort, left.sort]
				end
			else
				return nil
    end
    
    
    code = {:first => [], :second => [], :flag => [], :ver => [], :charge => [], :coef => []}
    both.each do |side|
				part = {:first => [], :second => [], :flag => [], :ver => [], :charge => [], :coef => []}
				side.each do |item|
					part[:first].push(item[0..13])
					part[:second].push(item[15..22])
					part[:flag].push(item[23])
					part[:ver].push(item[24])
					part[:charge].push(item[26])
					part[:coef].push(item.split(/;/)[1] ? item.split(/;/)[1].to_f : 1.0)
				end
				
				part.each_pair do |k, v|
					code[k].push([:first, :second].include?(k) ? v.join('+') : v)
				end
			end
			
			# construct code string
			codeString = {}
			codeString[:A] = Sylfy::Utils::Keyhash.key(code[:first].join('/'))
			codeString[:B] = Sylfy::Utils::Keyhash.key(code[:second].join('/'), false)
			codeString[:v] = code[:ver]._key_version?
			codeString[:F] = codeString[:version] != "X" ? code[:flag]._key_version? : "X"
			codeString[:H] = code[:charge]._key_cal_charge
			codeString[:E] = code[:coef]._key_cal_coef
			codeString[:CC] = "#{["A", "Z"].include?(dsign) ? "Z" : "A"}#{cofactor_symbol}"
				
			# construct key AAAAAAAAAAAAAA-BBBBBBBBFvV-HE-CC-D
    # A calculate from list of First part of InChIKey
    # B calculate from list of Second part of InChIKey
    # F Flag S for standard X for not
    # E sum of co-efficient
    # H charge B for balance and X for not
    # C indicate cofactor code
    # v indicate version of InChIKey
    # V indicate version of this key function
    # D indicate direction
			# return "#{codeString[:A]}-#{codeString[:B]}#{codeString[:F]}#{codeString[:v]}#{version}-#{codeString[:H]}#{codeString[:E]}-#{codeString[:CC]}-#{dsign}"
			return "#{codeString[:A]}-#{codeString[:B]}#{codeString[:F]}#{codeString[:v]}#{version}-#{codeString[:H]}#{codeString[:E]}-#{dsign}"
end