Skip to content

Instantly share code, notes, and snippets.

@Spebby
Last active November 21, 2022 19:41
Show Gist options
  • Save Spebby/60ed2d2d98f2aa2c296f01ca9b20f34a to your computer and use it in GitHub Desktop.
Save Spebby/60ed2d2d98f2aa2c296f01ca9b20f34a to your computer and use it in GitHub Desktop.
protein weight calculator
#!/usr/bin/env python3
"""
Uses three sources of input and produces two forms of output regarding proteins and
their expected molecular weights for the purpose of mass spectrometry.
"""
import sys
try:
with open(sys.argv[1], mode="w") as file:
aminoMono = {}
totalLength, totalWeight, length = 0, 0, 0
with open("/srv/datasets/amino-monoisotopic-mass") as input_file:
for line in input_file:
line = line.split()
aminoMono[line[0]] = float(line[1])
for line in sys.stdin:
line = line.strip()
weight = 0
for char in line:
weight += aminoMono[char]
totalLength += len(line)
totalWeight += weight
length += 1
print(f"{line} {len(line)} {weight:.3f}", file=file)
print(f"{length} {totalLength} {totalWeight:.3f}")
except IndexError:
print("Why are you wasting electricity?")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment