Skip to content

Instantly share code, notes, and snippets.

@timrandg
Created March 2, 2013 16:07
Show Gist options
  • Select an option

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

Select an option

Save timrandg/5071695 to your computer and use it in GitHub Desktop.
More refactoring
#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
#======================================
def peptide_list
[ "PAGS", "AGSCDLAEDAACQLQ","PAGS","PAGS","PAGS" ]
#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
collection = [] #Array.new(self.first.length){0}
for i in (0..first.length-1) do
collection[i] = collect{|row| row[i] }.sum
end
collection
end
def sum
inject{|s,n|s+n}
end
end
#===============================================================
#functions for mapping and printing peptides onto an AA sequence
#===============================================================
#returns a digital mapping of peptide alignments, or ensures zeros at each position if no peptides map.
require "English" #need this to access english $MATCH etc. used below
def map_peptides(tar, pep_list)
digit_hits = [] << Array.new(tar.length){0} #provides a length of zeros for edge case with no matches
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
digit_hits
end
def printer(arr)
printf("%5s %1s %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