Skip to content

Instantly share code, notes, and snippets.

@timrandg
Created March 1, 2013 16:35
Show Gist options
  • Select an option

  • Save timrandg/5065883 to your computer and use it in GitHub Desktop.

Select an option

Save timrandg/5065883 to your computer and use it in GitHub Desktop.
Refactoring peptide aligner
#command-line usage:
#% ruby /Users/timrand/Dropbox/NAT1/peptide_mapper3.rb peptide_file target_file
# 1 M 0
# 2 S 0
# 3 P 8 [][][][][][][][]
# 4 A 8 [][][][][][][][]
# 5 G 8 [][][][][][][][]
# 6 S 8 [][][][][][][][]
# 7 C 8 [][][][][][][][]
# 8 D 8 [][][][][][][][]
# 9 L 5 [][][][][]
# 10 A 5 [][][][][]
# 11 E 1 []
# 12 D 1 []
# 13 A 1 []
# 14 A 1 []
# 15 C 1 []
# 16 Q 2 [][]
# 17 L 2 [][]
# 18 Q 2 [][]
# 19 C 2 [][]
# 20 V 2 [][]
#======================================
#Define the target and the peptide list
#======================================
require "English"
def peptide_list
[ "PAGSXXAGS", "AGSCDLXAEDAACQLQ"]
#File.readlines(ARGV[0]).collect{|line| line.chomp}
end
def target
"MSPAGSCDLAEDAACQLQCVIDCAAELLAEDCLLAEDPAGSCD"
#File.readlines(ARGV[1]).collect{|line| line.chomp}
end
#======================================
#======================================
class String
def digitize_as(dig)
gsub(/./,dig.to_s)
end
def each_with_index(&blk)
split('').each_with_index(&blk)
end
alias :dig_as :digitize_as
end
class Array
# returns an array of column sums from an Array with rows of arrays with digits
# [[0,0,1,1,0,0],
# [0,0,0,1,1,0]].summed_by_site
# => [0,0,1,2,1,0]
def summed_by_site
unless self.empty? #edge case: could be no peptides sites in target, so self is an empty array, then we skip to end
collection = Array.new(self.first.length){0}
for r in self do
for i in (0..first.length-1) do
collection[i] = collection[i] + r[i]
end
end
end
collection || [] #but if you skipped to get here, there is no collection, so we have to return []
end
end
#===============================================================
#functions for mapping and printing peptides onto an AA sequence
#===============================================================
#returns a digital mapping of peptide alignments
def map_peptides(tar, pep_list)
digit_hits = []
for pep in pep_list do
#the block is only entered if the scan finds a match
tar.scan(pep){ digit_hits << ($PREMATCH.dig_as(0)+$MATCH.dig_as(1)+$POSTMATCH.dig_as(0)).split('').collect(&:to_i) }
end
case digit_hits #return either digit_hits or an array with all zeros
when [] then digit_hits << Array.new(tar.length){0}
else digit_hits
end
end
def printer arr
printf("%5s %s %2s %s\n" % arr)
end
def graph_bar(i)
scalar = 1
'[]' * i * scalar
end
aligned_map = map_peptides(target, peptide_list)
target.each_with_index{ |aa,i| printer [i+1, aa, aligned_map.summed_by_site[i], graph_bar(aligned_map.summed_by_site[i])] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment