lpdxSCC.py 7.73 KB
Newer Older
mirandaa's avatar
mirandaa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
#!/usr/bin/python

import os, sys, math
from optparse import OptionParser

sys.path.append('..' + os.sep + 'lib')

from lpdxDataStr import *
from lpdxSCTools import *
from lpdxTools import *
from lpdxUITools import *
from lpdxParser import *

#  -c / --csv          : the comma separated file for output
#  -d / --dump         : the scan dump file

def main():

	optParser = OptionParser(usage="Usage: lpdxSCC.py <subcommand> [options] [args]\n\n\
Availible subcommands:\n\
\t msc (mass to sum composition) [mass] [sf-constraint]\n\
\t scm (sum composition to mass) [sum composition]\n\
\t sfsc (sf-constraint to sum composition) [sum composition]\n\
\t corrDP (compare 2 mass spectra with the dot-product correlation) [*.csv1] [*.csv2] [tolerance in ppm]\n\
\t corrPC (compare 2 mass spectra with the Pearson correlation) [*.csv1] [*.csv2] [tolerance in ppm]\n")

	optParser.add_option("-a", "--accuracy", dest="accuracy",
                  help="Set accuracy for sum composition calculation. Default is 5 ppm")

	(options, args) = optParser.parse_args()

	# open input mfql file
	if len(args) > 0:

		if options.accuracy:
				accuracy = 1000000 / float(options.accuracy)
		else:
			accuracy = 1000000 / 5

		if not args[0] or not args[1]:
			print "You forgot the arguments/subcommands!"
		else:
			if args[0] == "msc":
				elscp = parseElemSeq(args[2])
				rslt = lpdxSCTools.calcSFbyMass(float(args[1]), elscp, accuracy)
				rsltlist = []
				for i in rslt:
					rsltlist.append((i.getWeight(), i))
				#	rsltlist = sorted(rsltlist)
				if rsltlist == []:
					print "No sum composition found for %s with m/z %2.f" % (elscp, float(args[1]))
				for mass, scp in rsltlist:
					print "%.4f" % mass, scp, "error: %.4f" % (float(args[1]) - scp.getWeight())

			elif args[0] == "scm":
				print "Check, if you did not forget to add the charge!"
				elscp = parseElemSeq(args[1])
				rslt = elscp.getWeight()
				print "Weight is:", rslt, "; Double Bounds are:", elscp.get_DB()

			elif args[0] == "sfsc":
				elscp = parseElemSeq(args[1])
				for i in elscp.get_norangeElemSeq():
					print i, i.getWeight()

			elif args[0] == "corrDP":

				res = 1000000 / float(args[3])

				# open the spectra files
				s1 = open(args[1], 'r')
				spec1 = []
				for line in s1.readlines():
					spec1.append([float(line.split(',')[0]), float(line.split(',')[1])])
				s1.close()
				s2 = open(args[2], 'r')
				spec2 = []
				for line in s2.readlines():
					spec2.append([float(line.split(',')[0]), float(line.split(',')[1])])
				s2.close()

				# match both spectra. The result are vectors VSpec1/2 with the same dimension
				spec1.sort(cmp = lambda x, y: cmp(x[0], y[0]), reverse = False)
				spec2.sort(cmp = lambda x, y: cmp(x[0], y[0]), reverse = False)

				vSpec1 = []
				vSpec2 = []
				
				sum = 0
				peak = 0
				while peak < max(len(spec1), len(spec2)) - 1:
					
					t = spec1[peak][0] / res

					if spec2[peak][0] - t < spec1[peak][0] and spec1[peak][0] < spec2[peak][0] + t:
						if peak < len(spec1) - 1 and peak < len(spec2) - 1:
							if not (spec2[peak][0] - t < spec1[peak + 1][0] and spec1[peak + 1][0] < spec2[peak][0] + t):
								vSpec1.append(spec1[peak])
								vSpec2.append(spec2[peak])

							else:
								if abs(spec1[peak][0] - spec2[peak][0]) > abs(spec1[peak + 1][0] - spec2[peak][0]):
									vSpec1.append(spec1[peak])
									vSpec2.append([0.0, 0.0])
									spec2.insert(peak, [0.0, 0.0])

								else:
									vSpec1.append(spec1[peak])
									vSpec2.append(spec2[peak])

					elif spec1[peak][0] < spec2[peak][0]:
						vSpec1.append(spec1[peak])
						vSpec2.append([0.0, 0.0])
						spec2.insert(peak, [0.0, 0.0])

					elif spec1[peak][0] > spec2[peak][0]:
						vSpec1.append([0.0, 0.0])
						vSpec2.append(spec2[peak])
						spec1.insert(peak, [0.0, 0.0])

					sum += abs(spec1[peak][0] - spec2[peak][0])
					peak += 1

				# calc mean of vectors vSpec1/2 (which is the expectation value)
				sumInt = 0.0
				for p in vSpec1:
					sumInt += p[1]
				meanVSpec1 = sumInt / len(vSpec1)

				sumInt = 0.0
				for p in vSpec2:
					sumInt += p[1]
				meanVSpec2 = sumInt / len(vSpec2)

				# substract mean from the intensities of vSpec1/2 to center the 2 vectors
				for p in vSpec1:
					p[1] = p[1] - meanVSpec1

				for p in vSpec2:
					p[1] = p[1] - meanVSpec2
				
				# calc geometrical length of vectors vSpec1/2
				sum = 0.0
				for p in vSpec1:
					sum += p[1] * p[1]
				lenghtVSpec1 = math.sqrt(sum)

				sum = 0.0
				for p in vSpec2:
					sum += p[1] * p[1]
				lenghtVSpec2 = math.sqrt(sum)

				# calc the dot product
				sum = 0.0
				for index in range(len(vSpec1)):
					sum += vSpec1[index][1] * vSpec2[index][1]

				# calc the ankle
				phi = math.acos(sum / (lenghtVSpec1 * lenghtVSpec2))
				
				print 'dot product: %.4f, similarity: %.2f %%' % (sum, 100 - (phi * 100) / math.pi)

			elif args[0] == "corrPC":

				res = 1000000 / float(args[3])

				# open the spectra files
				s1 = open(args[1], 'r')
				spec1 = []
				for line in s1.readlines():
					spec1.append([float(line.split(',')[0]), float(line.split(',')[1])])
				s1.close()
				s2 = open(args[2], 'r')
				spec2 = []
				for line in s2.readlines():
					spec2.append([float(line.split(',')[0]), float(line.split(',')[1])])
				s2.close()

				# match both spectra. The result are vectors VSpec1/2 with the same dimension
				spec1.sort(cmp = lambda x, y: cmp(x[0], y[0]), reverse = False)
				spec2.sort(cmp = lambda x, y: cmp(x[0], y[0]), reverse = False)

				vSpec1 = []
				vSpec2 = []
				
				sum = 0
				peak = 0
				while peak < max(len(spec1), len(spec2)) - 1:
					
					t = spec1[peak][0] / res

					if spec2[peak][0] - t < spec1[peak][0] and spec1[peak][0] < spec2[peak][0] + t:
						if peak < len(spec1) - 1 and peak < len(spec2) - 1:
							if not (spec2[peak][0] - t < spec1[peak + 1][0] and spec1[peak + 1][0] < spec2[peak][0] + t):
								vSpec1.append(spec1[peak])
								vSpec2.append(spec2[peak])

							else:
								if abs(spec1[peak][0] - spec2[peak][0]) > abs(spec1[peak + 1][0] - spec2[peak][0]):
									vSpec1.append(spec1[peak])
									vSpec2.append([0.0, 0.0])
									spec2.insert(peak, [0.0, 0.0])

								else:
									vSpec1.append(spec1[peak])
									vSpec2.append(spec2[peak])

					elif spec1[peak][0] < spec2[peak][0]:
						vSpec1.append(spec1[peak])
						vSpec2.append([0.0, 0.0])
						spec2.insert(peak, [0.0, 0.0])

					elif spec1[peak][0] > spec2[peak][0]:
						vSpec1.append([0.0, 0.0])
						vSpec2.append(spec2[peak])
						spec1.insert(peak, [0.0, 0.0])

					sum += abs(spec1[peak][0] - spec2[peak][0])
					peak += 1

				# calc mean of vectors vSpec1/2 (which is the expectation value)
				sumVSpec1 = 0.0
				for p in vSpec1:
					sumVSpec1 += p[1]
				meanVSpec1 = sumVSpec1 / len(vSpec1)

				sumVSpec2 = 0.0
				for p in vSpec2:
					sumVSpec2 += p[1]
				meanVSpec2 = sumVSpec2 / len(vSpec2)

				# calc standard deviation sVSpec
				sumVSpec1quad = 0.0
				for p in vSpec1:
					sumVSpec1quad += p[1] * p[1]
				
				sumVSpec2quad = 0.0
				for p in vSpec2:
					sumVSpec2quad += p[1] * p[1]

				sVSpec1 = math.sqrt(len(vSpec1) * sumVSpec1quad - (sumVSpec1 * sumVSpec1))
				sVSpec2 = math.sqrt(len(vSpec2) * sumVSpec2quad - (sumVSpec2 * sumVSpec2))

				# substract mean from the intensities of vSpec1/2 to center the 2 vectors
				for p in vSpec1:
					p[1] = p[1] - meanVSpec1

				for p in vSpec2:
					p[1] = p[1] - meanVSpec2
				
				# calc the dot product
				sumDP = 0.0
				for index in range(len(vSpec1)):
					sumDP += vSpec1[index][1] * vSpec2[index][1]

				# calc Pearson correlation
				r = sumDP / ((len(vSpec1) - 1) * sVSpec1 * sVSpec2)

				# significance test
				t = r * math.sqrt((len(vSpec1) - 2) / math.sqrt(1 - (r * r)))

				print 'correlation r: %.4f,\nsignificance t: %.4f' % (r, t)
			else:
				print "No valid command:", args[0]

if __name__ == "__main__":
	main()