File: assignTypes.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (140 lines) | stat: -rw-r--r-- 3,255 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: assignTypes.C,v 1.15 2004/12/17 15:29:37 amoll Exp $

#include <BALL/MOLMEC/COMMON/assignTypes.h>

#include <iostream>

#include <BALL/SYSTEM/path.h>
#include <BALL/KERNEL/fragment.h>

using namespace std;

namespace BALL 
{

	AssignBaseProcessor::AssignBaseProcessor()
		: UnaryProcessor<Atom>(),
			unassigned_atoms_(),
			max_number_unassigned_atoms_()
	{
	}

	void AssignBaseProcessor::setMaximumUnassignedAtoms(Size nr)
	{
		max_number_unassigned_atoms_ = nr;
	}
	
	Size AssignBaseProcessor::getMaximumUnassignedAtoms() const
	{
		return max_number_unassigned_atoms_;
	}

	Size AssignBaseProcessor::getNumberOfUnassignedAtoms() const
	{
		return unassigned_atoms_.size();
	}

	HashSet<const Atom*>& AssignBaseProcessor::getUnassignedAtoms()
	{
		return unassigned_atoms_;
	}


	AssignTypeNameProcessor::AssignTypeNameProcessor(const String& filename, bool overwrite)
		:	overwrite_(overwrite)
	{
		Path path;
		String filepath = path.find(filename);
		if (filepath == "")
		{
			throw Exception::FileNotFound(__FILE__, __LINE__, filename);
		}
		ifstream	type_file(filepath.c_str());

		String line;
		String type;
		String name;

		while (type_file)
		{
			line.getline(type_file);
			
			name = line.getField(0);
			type = line.getField(1);
			
			type_map_[name] = type;
		}

		type_file.close();
	}

	Processor::Result	AssignTypeNameProcessor::operator () (Atom& atom)
	{
		// if the atom type shall be overwritten or the type is not yet assigned: assign it
		if (overwrite_ || atom.getTypeName() == BALL_ATOM_DEFAULT_TYPE_NAME)
		{
			// retrieve the full atom name
			String name  = atom.getFullName();
				
			// if this name is in the hash map, assign it
			if (type_map_.has(name))
			{
				atom.setTypeName(type_map_[name]);
			} 
			else 
			{
				// try to match the full name without variant extensions (-xxx)
				name = atom.getFullName(Atom::NO_VARIANT_EXTENSIONS);
				if (type_map_.has(name)) 
				{
					atom.setTypeName(type_map_[name]);				
				}
				else 
				{
					// last, not least: try wildcard matching for the residue
					name = "*:" + atom.getName();
					if (type_map_.has(name))
					{
						atom.setTypeName(type_map_[name]);				
					} 
					else 
					{	
						// we could not assign a type name - complain!
						Log.warn() << "AssignTypeNameProcessor: could not assign type name for atom " 
											 << atom.getFullName(Atom::NO_VARIANT_EXTENSIONS) << endl;
						unassigned_atoms_.insert(&atom);
						if (unassigned_atoms_.size() > max_number_unassigned_atoms_) return Processor::ABORT;
					}
				}
			}
		}

		return Processor::CONTINUE;
	}

	AssignTypeProcessor::AssignTypeProcessor(const AtomTypes& atom_types)
	{
		atom_types_ = atom_types;
	}

	Processor::Result	AssignTypeProcessor::operator () (Atom& atom)
	{
		Atom::Type type = atom_types_.getType(atom.getTypeName());
		if (type == Atom::UNKNOWN_TYPE)
		{
			Log.warn() << "AssignTypeProcessor: could not assign atom type for " << atom.getTypeName() << endl;
			unassigned_atoms_.insert(&atom);
			if (unassigned_atoms_.size() > max_number_unassigned_atoms_) return Processor::ABORT;
		} 
		else 
		{
			atom.setType(type);
		}

		return Processor::CONTINUE;
	}

} // namespace BALL