peptide_mw 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/peptide_mw.rb +90 -0
  3. metadata +41 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0edff77f1a0cfe8527d9320eddb9bc04ee5ab1505680e52defdcbbca6b7782c2
4
+ data.tar.gz: 0c0e3157a92f46caa9b47f6ddd356073a765560265d7cba46ba35eb65293dbbe
5
+ SHA512:
6
+ metadata.gz: 053a7e0d775d824b92166426248d6cef7f79f9a60a693c9453651181fe180a1331aabf1d20ce72409fe46619f900981741507c78b705afc10edb41d526fe7e8e
7
+ data.tar.gz: 99f77c9a516a37c65c256f8900ce61a7e9fb06bee329e8925a4f5a37765c9a2d852a4c2a5b253b24c44493a39e64681bd464613d8257b571571ee8b34989d20b
data/lib/peptide_mw.rb ADDED
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PeptideMw
4
+ AMINO_ACID_MASSES = {
5
+ 'A' => 71.03711, 'R' => 156.10111, 'N' => 114.04293, 'D' => 115.02694,
6
+ 'C' => 103.00919, 'E' => 129.04259, 'Q' => 128.05858, 'G' => 57.02146,
7
+ 'H' => 137.05891, 'I' => 113.08406, 'L' => 113.08406, 'K' => 128.09496,
8
+ 'M' => 131.04049, 'F' => 147.06841, 'P' => 97.05276, 'S' => 87.03203,
9
+ 'T' => 101.04768, 'W' => 186.07931, 'Y' => 163.06333, 'V' => 99.06841
10
+ }.freeze
11
+
12
+ WATER_MASS = 18.01056
13
+
14
+ AMINO_ACID_FORMULA = {
15
+ 'A' => { C: 3, H: 5, N: 1, O: 1, S: 0 },
16
+ 'R' => { C: 6, H: 12, N: 4, O: 1, S: 0 },
17
+ 'N' => { C: 4, H: 6, N: 2, O: 2, S: 0 },
18
+ 'D' => { C: 4, H: 5, N: 1, O: 3, S: 0 },
19
+ 'C' => { C: 3, H: 5, N: 1, O: 1, S: 1 },
20
+ 'E' => { C: 5, H: 7, N: 1, O: 3, S: 0 },
21
+ 'Q' => { C: 5, H: 8, N: 2, O: 2, S: 0 },
22
+ 'G' => { C: 2, H: 3, N: 1, O: 1, S: 0 },
23
+ 'H' => { C: 6, H: 7, N: 3, O: 1, S: 0 },
24
+ 'I' => { C: 6, H: 11, N: 1, O: 1, S: 0 },
25
+ 'L' => { C: 6, H: 11, N: 1, O: 1, S: 0 },
26
+ 'K' => { C: 6, H: 12, N: 2, O: 1, S: 0 },
27
+ 'M' => { C: 5, H: 9, N: 1, O: 1, S: 1 },
28
+ 'F' => { C: 9, H: 9, N: 1, O: 1, S: 0 },
29
+ 'P' => { C: 5, H: 7, N: 1, O: 1, S: 0 },
30
+ 'S' => { C: 3, H: 5, N: 1, O: 2, S: 0 },
31
+ 'T' => { C: 4, H: 7, N: 1, O: 2, S: 0 },
32
+ 'W' => { C: 11, H: 10, N: 2, O: 1, S: 0 },
33
+ 'Y' => { C: 9, H: 9, N: 1, O: 2, S: 0 },
34
+ 'V' => { C: 5, H: 9, N: 1, O: 1, S: 0 }
35
+ }.freeze
36
+
37
+ SUBSCRIPTS = '₀₁₂₃₄₅₆₇₈₉'
38
+
39
+ class << self
40
+ # Calculate monoisotopic molecular weight of a peptide sequence.
41
+ #
42
+ # @param sequence [String] Single-letter amino acid codes (e.g., "AGILM")
43
+ # @return [Float] Molecular weight in Daltons
44
+ # @raise [ArgumentError] If sequence is empty or contains invalid characters
45
+ def calculate_molecular_weight(sequence)
46
+ raise ArgumentError, 'Sequence cannot be nil or empty' if sequence.nil? || sequence.strip.empty?
47
+
48
+ seq = sequence.upcase
49
+ seq.each_char do |aa|
50
+ raise ArgumentError, "Invalid amino acid: '#{aa}'" unless AMINO_ACID_MASSES.key?(aa)
51
+ end
52
+
53
+ seq.chars.sum { |aa| AMINO_ACID_MASSES[aa] } + WATER_MASS
54
+ end
55
+
56
+ # Calculate molecular formula with Unicode subscripts.
57
+ #
58
+ # @param sequence [String] Single-letter amino acid codes
59
+ # @return [String] Molecular formula (e.g., "C₂₃H₄₁N₅O₇S₁")
60
+ # @raise [ArgumentError] If sequence is empty or contains invalid characters
61
+ def calculate_molecular_formula(sequence)
62
+ raise ArgumentError, 'Sequence cannot be nil or empty' if sequence.nil? || sequence.strip.empty?
63
+
64
+ seq = sequence.upcase
65
+ seq.each_char do |aa|
66
+ raise ArgumentError, "Invalid amino acid: '#{aa}'" unless AMINO_ACID_FORMULA.key?(aa)
67
+ end
68
+
69
+ counts = { C: 0, H: 0, N: 0, O: 0, S: 0 }
70
+
71
+ seq.each_char do |aa|
72
+ formula = AMINO_ACID_FORMULA[aa]
73
+ counts.each_key { |element| counts[element] += formula[element] }
74
+ end
75
+
76
+ # Add water (H2O)
77
+ counts[:H] += 2
78
+ counts[:O] += 1
79
+
80
+ "C#{to_subscript(counts[:C])}H#{to_subscript(counts[:H])}N#{to_subscript(counts[:N])}" \
81
+ "O#{to_subscript(counts[:O])}S#{to_subscript(counts[:S])}"
82
+ end
83
+
84
+ private
85
+
86
+ def to_subscript(number)
87
+ number.to_s.chars.map { |d| SUBSCRIPTS[d.to_i] }.join
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,41 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peptide_mw
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - pepwiki
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Calculate monoisotopic molecular weight from peptide sequences
13
+ email:
14
+ - kentresearch@proton.me
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/peptide_mw.rb
20
+ homepage: https://github.com/pepwiki/peptide-ruby
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '3.0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubygems_version: 3.6.9
39
+ specification_version: 4
40
+ summary: Zero-dependency peptide molecular weight calculator
41
+ test_files: []