python


Python, complicated file iteration?


I am trying to iterate over an infile, create an outfile that is same as the infile at this point. Then I want to iterate over reference file and add TER records to the correct line when needed. The problem is that I have many of the same numbers in the outfile and need to add the TER record to the very last one. (In real life the reference file has different numbers after at the end that I need to preserve.
import sys
import argparse
def main(argv):
parser = argparse.ArgumentParser(description='Add TER records')
parser.add_argument('infile', help='input file (PDB format)')
parser.add_argument('outfile', help='output file (PDB format)')
parser.add_argument('reference', help =' ref')
args = parser.parse_args()
residues = []
x = 0
with open(args.infile, "r") as f, open(args.outfile, "w+") as of,open(args.reference,"r") as rf:
for line in f:
of.write(line)
for line in rf:
if line[0:3]== "TER":
resnum = line[22:27]
resnum_1 = int(resnum)
residues.append(resnum_1)
of.seek(0)
for line in of:
if line [0:4]== "ATOM":
res = line[22:27]
res_2 = int(res)
for x in residues:
if x == res_2 and res_2+1 == x+1:
of.write("TER\n")
x = 0
else:
continue
if __name__ == "__main__":
main(sys.argv)
My infile:
ATOM 1 N GLU D 384 51.765 39.857 23.514 1.00 0.00 N
ATOM 2 H1 GLU D 384 50.823 39.839 23.150 1.00 0.00 H
ATOM 3 H2 GLU D 384 51.956 39.044 24.081 1.00 0.00 H
ATOM 4 H3 GLU D 384 52.469 39.840 22.790 1.00 0.00 H
ATOM 5 CA GLU D 384 51.934 41.135 24.345 1.00 0.00 C
ATOM 6 HA GLU D 384 53.002 41.062 24.550 1.00 0.00 H
ATOM 7 CB GLU D 384 51.712 42.439 23.503 1.00 0.00 C
ATOM 8 HB2 GLU D 384 52.307 42.297 22.600 1.00 0.00 H
ATOM 9 HB3 GLU D 384 50.640 42.356 23.323 1.00 0.00 H
ATOM 10 CG GLU D 384 52.024 43.786 24.125 1.00 0.00 C
ATOM 11 HG2 GLU D 384 52.138 44.557 23.363 1.00 0.00 H
ATOM 12 HG3 GLU D 384 51.201 44.086 24.773 1.00 0.00 H
ATOM 13 CD GLU D 384 53.381 43.828 24.935 1.00 0.00 C
ATOM 14 OE1 GLU D 384 53.634 43.069 25.869 1.00 0.00 O
ATOM 15 OE2 GLU D 384 54.142 44.711 24.602 1.00 0.00 O
ATOM 16 C GLU D 384 51.078 41.093 25.570 1.00 0.00 C
ATOM 17 O GLU D 384 49.819 41.006 25.499 1.00 0.00 O
ATOM 18 N THR D 385 51.596 41.332 26.746 1.00 0.00 N
ATOM 19 H THR D 385 52.606 41.355 26.728 1.00 0.00 H
ATOM 20 CA THR D 385 50.815 41.615 27.982 1.00 0.00 C
ATOM 21 HA THR D 385 49.834 41.932 27.628 1.00 0.00 H
ATOM 22 CB THR D 385 50.763 40.416 28.984 1.00 0.00 C
ATOM 23 HB THR D 385 50.235 40.697 29.895 1.00 0.00 H
ATOM 24 CG2 THR D 385 50.195 39.154 28.283 1.00 0.00 C
ATOM 25 HG21 THR D 385 49.322 39.526 27.747 1.00 0.00 H
ATOM 26 HG22 THR D 385 50.838 38.704 27.527 1.00 0.00 H
ATOM 27 HG23 THR D 385 49.834 38.399 28.981 1.00 0.00 H
ATOM 28 OG1 THR D 385 52.133 40.236 29.373 1.00 0.00 O
ATOM 29 HG1 THR D 385 52.186 40.766 30.172 1.00 0.00
My reference (and the way the outfile TER records should look like:
ATOM 1 N GLU D 384 51.765 39.857 23.514 1.00 0.00 N
ATOM 2 H1 GLU D 384 50.823 39.839 23.150 1.00 0.00 H
ATOM 3 H2 GLU D 384 51.956 39.044 24.081 1.00 0.00 H
ATOM 4 H3 GLU D 384 52.469 39.840 22.790 1.00 0.00 H
ATOM 5 CA GLU D 384 51.934 41.135 24.345 1.00 0.00 C
ATOM 6 HA GLU D 384 53.002 41.062 24.550 1.00 0.00 H
ATOM 7 CB GLU D 384 51.712 42.439 23.503 1.00 0.00 C
ATOM 8 HB2 GLU D 384 52.307 42.297 22.600 1.00 0.00 H
ATOM 9 HB3 GLU D 384 50.640 42.356 23.323 1.00 0.00 H
ATOM 10 CG GLU D 384 52.024 43.786 24.125 1.00 0.00 C
ATOM 11 HG2 GLU D 384 52.138 44.557 23.363 1.00 0.00 H
ATOM 12 HG3 GLU D 384 51.201 44.086 24.773 1.00 0.00 H
ATOM 13 CD GLU D 384 53.381 43.828 24.935 1.00 0.00 C
ATOM 14 OE1 GLU D 384 53.634 43.069 25.869 1.00 0.00 O
ATOM 15 OE2 GLU D 384 54.142 44.711 24.602 1.00 0.00 O
ATOM 16 C GLU D 384 51.078 41.093 25.570 1.00 0.00 C
ATOM 17 O GLU D 384 49.819 41.006 25.499 1.00 0.00 O
TER
ATOM 18 N THR D 385 51.596 41.332 26.746 1.00 0.00 N
What I get however is this:
ATOM 96 HA SER D 391 45.358 52.899 33.158 1.00 0.00 H
ATOM 97 CB SER D 391 45.963 51.960 31.364 1.00 0.00 C
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
TER
1.00 0.00 H
ATOM 100 OG SER D 391 45.205 52.828 30.574 1.00 0.00 O
Can anybody help? I am new to programming as you can see:)

Related Links

Installing pygame with enthought canopy on Mac
Database: Check data type by python using cx_oracle
find the largest palindrome from the product of 3 numbers in python
Google App Engine deferred.defer() error 404
solve an equation containing integrals
How can I convert a SHA256 hash from integer to string and back?
Urllib2 timeot doesn't work
Exception running Python Flask app on AWS Elastic Beanstalk
Finding mean of a values in a dictionary
Searching for a particular string in python
Vectorized implementation of an image convolve function
Using variables from a returned function as arguments in another function
Why use user nobody nogroup in upstart services or daemon services
Grouping a list in python specifically
How to use python to loop through all possible results in postgresql?
Python: multiple assignment vs. individual assignment speed

Categories

HOME
cntk
ibm-watson-cognitive
erlang
vb6
performancecounter
firebase-app-indexing
command
h2
specflow
jrebel
simpy
proguard
eval
slim-3
metronic
x11
xamarin-studio
automata
x-frame-options
uisplitviewcontroller
backpropagation
nmake
lmdb
expand
backup-strategies
windows-10-iot-core
xen
inkscape
restful-url
rule
service-fabric-stateful
crop
cppunit
matlab-cvst
shutdown
decompress
nuget-server
httplib2
in-memory-database
pdfkit
libreadline
c64
tomee
easing
mongoose-populate
radians
scalar
faraday
selecteditem
asynccallback
interactive-brokers
intellitest
vips
paypal-nvp
integral
twython
team-build
viper-architecture
isml
callstack
jai
temp-tables
log4cplus
method-overriding
power-law
eclipse-classpath
code-testing
korma
dynamic-proxy
exiv2
baucis
dig
vmware-server
ienumerator
cgimageref
objcmongodb
abnf
cbcentralmanager
osi
couchdb-lucene
cadisplaylink
icefaces-3
macruby
driving-directions
flexicious
msn
recordset
microsoft.ink
gears
visual-studio-dbpro
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App