Skip to content

Instantly share code, notes, and snippets.

@RasmusFonseca
Created January 2, 2017 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RasmusFonseca/f19948833b51e14517fc1163c51b02e2 to your computer and use it in GitHub Desktop.
Save RasmusFonseca/f19948833b51e14517fc1163c51b02e2 to your computer and use it in GitHub Desktop.
Extracting protein features as .tsv using blosum62 from a fasta file
import numpy as np
import sys
# Amino acid substitution matrix
blosum62 ={
"C":[9,-1,-1,-3,0,-3,-3,-3,-4,-3,-3,-3,-3,-1,-1,-1,-1,-2,-2,-2],
"S":[-1,4,1,-1,1,0,1,0,0,0,-1,-1,0,-1,-2,-2,-2,-2,-2,-3],
"T":[-1,1,4,1,-1,1,0,1,0,0,0,-1,0,-1,-2,-2,-2,-2,-2,-3],
"P":[-3,-1,1,7,-1,-2,-1,-1,-1,-1,-2,-2,-1,-2,-3,-3,-2,-4,-3,-4],
"A":[0,1,-1,-1,4,0,-1,-2,-1,-1,-2,-1,-1,-1,-1,-1,-2,-2,-2,-3],
"G":[-3,0,1,-2,0,6,-2,-1,-2,-2,-2,-2,-2,-3,-4,-4,0,-3,-3,-2],
"N":[-3,1,0,-2,-2,0,6,1,0,0,-1,0,0,-2,-3,-3,-3,-3,-2,-4],
"D":[-3,0,1,-1,-2,-1,1,6,2,0,-1,-2,-1,-3,-3,-4,-3,-3,-3,-4],
"E":[-4,0,0,-1,-1,-2,0,2,5,2,0,0,1,-2,-3,-3,-3,-3,-2,-3],
"Q":[-3,0,0,-1,-1,-2,0,0,2,5,0,1,1,0,-3,-2,-2,-3,-1,-2],
"H":[-3,-1,0,-2,-2,-2,1,1,0,0,8,0,-1,-2,-3,-3,-2,-1,2,-2],
"R":[-3,-1,-1,-2,-1,-2,0,-2,0,1,0,5,2,-1,-3,-2,-3,-3,-2,-3],
"K":[-3,0,0,-1,-1,-2,0,-1,1,1,-1,2,5,-1,-3,-2,-3,-3,-2,-3],
"M":[-1,-1,-1,-2,-1,-3,-2,-3,-2,0,-2,-1,-1,5,1,2,-2,0,-1,-1],
"I":[-1,-2,-2,-3,-1,-4,-3,-3,-3,-3,-3,-3,-3,1,4,2,1,0,-1,-3],
"L":[-1,-2,-2,-3,-1,-4,-3,-4,-3,-2,-3,-2,-2,2,2,4,3,0,-1,-2],
"V":[-1,-2,-2,-2,0,-3,-3,-3,-2,-2,-3,-3,-2,1,3,1,4,-1,-1,-3],
"F":[-2,-2,-2,-4,-2,-3,-3,-3,-3,-3,-1,-3,-3,0,0,0,-1,6,3,1],
"Y":[-2,-2,-2,-3,-2,-3,-2,-3,-2,-1,2,-2,-2,-1,-1,-1,-1,3,7,2],
"W":[-2,-3,-3,-4,-3,-2,-4,-4,-3,-2,-2,-3,-3,-1,-3,-2,-3,1,2,11]
}
# Read sequences
print("Reading sequences")
seq_names = []
sequences = []
with open(sys.argv[1]) as f:
for line in f.readlines():
if line[0] == '>': seq_names.append(line[1:].strip())
elif len(line)>3: sequences.append(line.strip())
# Convert each sequence into a feature-vector with 20 entries per amino acid
print("Generating feature list")
feature_list = []
for seq in sequences:
features = []
for aa in seq:
features += blosum62[aa]
feature_list.append(features)
np.savetxt(sys.argv[2], feature_list, fmt="%d", delimiter='\t')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment