Method: Bio::PDB#initialize

Defined in:
lib/bio/db/pdb/pdb.rb

#initialize(str) ⇒ PDB

Creates a new Bio::PDB object from given str.



1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
# File 'lib/bio/db/pdb/pdb.rb', line 1444

def initialize(str)
  #Aha! Our entry into the world of PDB parsing, we initialise a PDB
  #object with the whole PDB file as a string
  #each PDB has an array of the lines of the original file
  #a bit memory-tastic! A hash of records and an array of models
  #also has an id

  @data = str.split(/[\r\n]+/)
  @hash = {}
  @models = []
  @id = nil

  #Flag to say whether the current line is part of a continuation
  cont = false

  #Empty current model
  cModel    = Model.new
  cChain    = nil #Chain.new
  cResidue  = nil #Residue.new
  cLigand   = nil #Heterogen.new
  c_atom    = nil

  #Goes through each line and replace that line with a PDB::Record
  @data.collect! do |line|
    #Go to next if the previous line was contiunation able, and
    #add_continuation returns true. Line is added by add_continuation
    next if cont and cont = cont.add_continuation(line)

    #Make the new record
    f = Record.get_record_class(line).new.initialize_from_string(line)
    #p f
    #Set cont
    cont = f if f.continue?
    #Set the hash to point to this record either by adding to an
    #array, or on it's own
    key = f.record_name
    if a = @hash[key] then
      a << f
    else
      @hash[key] = [ f ]
    end

    # Do something for ATOM and HETATM
    if key == 'ATOM' or key == 'HETATM' then
      if cChain and f.chainID == cChain.id
        chain = cChain
      else
        if chain = cModel[f.chainID]
          cChain = chain unless cChain
        else
          # If we don't have chain, add a new chain
          newChain = Chain.new(f.chainID, cModel)
          cModel.addChain(newChain)
          cChain = newChain
          chain = newChain
        end
        # chain might be changed, clearing cResidue and cLigand
        cResidue = nil
        cLigand = nil
      end
    end

    case key
    when 'ATOM'
      c_atom = f
      residueID = Residue.get_residue_id_from_atom(f)

      if cResidue and residueID == cResidue.id
        residue = cResidue
      else
        if residue = chain.get_residue_by_id(residueID)
          cResidue = residue unless cResidue
        else
          # add a new residue
          newResidue = Residue.new(f.resName, f.resSeq, f.iCode, chain)
          chain.addResidue(newResidue)
          cResidue = newResidue
          residue = newResidue
        end
      end
      
      f.residue = residue
      residue.addAtom(f)

    when 'HETATM'
      c_atom = f
      residueID = Heterogen.get_residue_id_from_atom(f)

      if cLigand and residueID == cLigand.id
        ligand = cLigand
      else
        if ligand = chain.get_heterogen_by_id(residueID)
          cLigand = ligand unless cLigand
        else
          # add a new heterogen
          newLigand = Heterogen.new(f.resName, f.resSeq, f.iCode, chain)
          chain.addLigand(newLigand)
          cLigand = newLigand
          ligand = newLigand
          #Each model has a special solvent chain. (for compatibility)
          if f.resName == 'HOH'
            cModel.addSolvent(newLigand)
          end
        end
      end

      f.residue = ligand
      ligand.addAtom(f)

    when 'MODEL'
      c_atom = nil
      cChain = nil
      cResidue = nil
      cLigand = nil
      if cModel.model_serial or cModel.chains.size > 0 then
        self.addModel(cModel)
      end
      cModel = Model.new(f.serial)

    when 'TER'
      if c_atom
        c_atom.ter = f
      else
        #$stderr.puts "Warning: stray TER?"
      end
    when 'SIGATM'
      if c_atom
        #$stderr.puts "Warning: duplicated SIGATM?" if c_atom.sigatm
        c_atom.sigatm = f
      else
        #$stderr.puts "Warning: stray SIGATM?"
      end
    when 'ANISOU'
      if c_atom
        #$stderr.puts "Warning: duplicated ANISOU?" if c_atom.anisou
        c_atom.anisou = f
      else
        #$stderr.puts "Warning: stray ANISOU?"
      end
    when 'SIGUIJ'
      if c_atom and c_atom.anisou
        #$stderr.puts "Warning: duplicated SIGUIJ?" if c_atom.anisou.siguij
        c_atom.anisou.siguij = f
      else
        #$stderr.puts "Warning: stray SIGUIJ?"
      end

    else
      c_atom = nil

    end
    f
  end #each
  #At the end we need to add the final model
  self.addModel(cModel)
  @data.compact!
end