I have a bunch of text files, all in the same format (this is a snippet below, real file is longer):
Molecular weight = 43057.32 Residues = 391
Average Residue Weight = 110.121 Charge = -10.0
Isoelectric Point = 4.8926
Residue Number Mole% DayhoffStat
A = Ala 24 6.138 0.714
B = Asx 0 0.000 0.000
C = Cys 9 2.302 0.794
Property Residues Number Mole%
Tiny (A+C+G+S+T) 135 34.527
Small (A+B+C+D+G+N+P+S+T+V) 222 56.777
Aliphatic (A+I+L+V) 97 24.808
I have to extract all these variables and process them. I was going to write some code that goes through each line one at a time and extracts the relevant info through a series of split, strip etc. functions.
This is a pretty common task people do with python, so i got to thinking there must be an easier method for this.
Is there any module or method out there to allow something like:
template = """
Molecular weight = {0} Residues = {1}
Average Residue Weight = {2} Charge = {3}
Isoelectric Point = {4}
Residue Number Mole% DayhoffStat
A = Ala {4} {5} {6}
B = Asx {7} {8} {9}
C = Cys {10} {11} {12}
Property Residues Number Mole%
Tiny (A+C+G+S+T) {14} {15}
Small (A+B+C+D+G+N+P+S+T+V) {16} {17}
Aliphatic (A+I+L+V) {18} {19}"""
and then, to extract the vars from a another input file following the above format you would do the following:
list_of_vars = Parse(template, infile)
Note that while the same variable will appear in every file on the same line, they can be shifted a few characters to the right depending on how big the value preceding it on the same line is.
The files are the output from emboss pepstats in case anyone was wondering.
SOLUTION
Thanks everyone for the quick replies. The solution here was to use findall function in the re module. Here is a simple example below:
import re
class TemplateParser:
def __init__(self, template):
self.m_template = template.replace('{}', r'[\s]*([\d\-\.]+)[\s]*')
def ParseString(self, filename):
return re.findall(self.m_template, filename, re.DOTALL|re.MULTILINE)[0]
template = """
Molecular weight = {} Residues = {}
Average Residue Weight = {} Charge = {}
Isoelectric Point = {}
Residue Number Mole% DayhoffStat
A = Ala {} {} {}
B = Asx {} {} {}
C = Cys {} {} {}
Property Residues Number Mole%
Tiny \(A\+C\+G\+S\+T\) {} {}
Small \(A\+B\+C\+D\+G\+N\+P\+S\+T\+V\) {} {}
Aliphatic \(A\+I\+L\+V\) {} {}"""
The ParseString function the successfully returns a list of strings which I can then process. As the files are always the same format, I was able to process all my files successfully. I only had two issues.
1) As you can see above i've had so escape all regex characters in my template file, which isn't that big of a deal.
2) As I also mentioned above, this template is just a small snippit of the actual files I need to parse. When I tried this with my real data, re threw the following error:
"sorry, but this version only supports 100 named groups" AssertionError: sorry, but this version only supports 100 named groups
I worked around this by splitting my template string into 3 pieces, ran the ParseString function 3 times with the 3 different templates, and added the list results together.
Thanks again!