3
votes

I just wrote a function that will look at a text file and count all of the instances of True and False in the text file. Here is my file

ATOM     43  CA  LYS A   5      14.038  15.691  37.608  1.00 15.15           C      True
ATOM     52  CA  CYS A   6      16.184  12.782  38.807  1.00 16.72           C      True
ATOM     58  CA  GLU A   7      17.496  12.053  35.319  1.00 14.06           C      False
ATOM     67  CA  VAL A   8      18.375  15.721  34.871  1.00 12.27           C      True
ATOM     74  CA  PHE A   9      20.066  15.836  38.288  1.00 12.13           C      False
ATOM     85  CA  GLN A  10      22.355  12.978  37.249  1.00 12.54           C      False

And here is my code

def TFCount(txtFileName):   
    with open(txtFileName, 'r') as e:
        T = 0
        F = 0
        for record in e:
            if(re.search(r'^ATOM\s+\d+\s+\CA\s+\w+\s+\w+\s+\d+\s+\d+\.\d+\s+\d+\.\d+\s+\d+\.\d+\s+\d+\.\d+\s+\d+\.\d+\s+\w+\s+\T', record)):
                T += 1
            else:
                F += 1
        print(T)
        print(F)

I apologize if my regex is long and tedious to read, but this is the only way I know of counting the number of times True occurs in the file. As you can see, each time the program encounters True, it will add 1 to the variable T, otherwise it will add 1 to the variable False. After attempting to run the program, the interpreter returns error: bad escape \C. What does this error mean? And what in my code is causing it?

1
Why escape C? There is no \C regex escape. Beginning with Python 3.6, this is treated as an error. Before, it would have been parsed as a C. It looks like a typo on your part. - Wiktor Stribiżew
Not an escape sequence \C. Most engines will just unescape it as a pass, but apparently not Python. Or, it's probably a message from the interpreter. - user557597
Do you really need to parse the line that exactly? It seems like '^ATOM.{80}T' would work just as well and be a lot easier to understand. - Mark Ransom
@MarkRansom have a very good point. PDB files have fixed columns by definition, so just checking for the value at some column is correct. In fact, a couple substr and equalities would do the trick without having to rely on regexps. Extract columns 1-6 and compare against ATOM , extract columns 13-16 and compare against ` CA . If it is the expected line, check for the T` in its own column (wwpdb.org/documentation/file-format-content/format33/…). - Poshi

1 Answers

3
votes

You have \C in the first part of the regex

r'^ATOM\s+\d+\s+\CA

you should write just CA

r'^ATOM\s+\d+\s+CA

without escaping.

Later you have the same with \T.

\X means escaped X and most of the time is a special sequence in regex, e.g. \d for a digit or \s for a whitespace.