import sys ############################################################################### # Read a spectrum from a file and return it as a dictionary. def readSpectrum (fileName): # Create an empty spectrum. mySpectrum = {} # Open the file for reading. myFile = open(fileName, "r") # Read line by line. for line in myFile: line = line.rstrip() # Parse the line. words = line.split() if (len(words) != 2): print "Invalid line (%s)." % line sys.exit(1) # Round m/z to the nearest integer. mz = int(float(words[0]) + 0.5) # Store this peak. mySpectrum[mz] = float(words[1]) myFile.close() print "Read %d peaks from %s." % (len(mySpectrum.keys()), fileName) return(mySpectrum) ############################################################################### # MAIN ############################################################################### USAGE = """USAGE: compare-spectra.py This program computes the scalar product between two spectra. The spectra are provided in two files, with two columns per file. The first column is the m/z and the second is the corresponding intensity. The spectra are placed into 1-Da bins on the m/z axis before the scalar product is computed. """ # Parse the command line. if (len(sys.argv) != 3): print " sys.exit(1) observedFileName = sys.argv[1] theoreticalFileName = sys.argv[2] # Read both spectra. observedSpectrum = readSpectrum(observedFileName) theoreticalSpectrum = readSpectrum(theoreticalFileName) # Compute the scalar product. score = 0.0 for mz in theoreticalSpectrum.keys(): if (observedSpectrum.has_key(mz)): score += observedSpectrum[mz] * theoreticalSpectrum[mz] print score