1 |
lpetzo |
372 |
package proteinstructure; |
2 |
|
|
|
3 |
|
|
import java.io.IOException; |
4 |
|
|
import java.util.Iterator; |
5 |
|
|
import java.util.TreeMap; |
6 |
|
|
|
7 |
|
|
import sadp.ContactMap; |
8 |
|
|
import sadp.IOUtil; |
9 |
|
|
import sadp.SADP; |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* Instances of this class create two so called aligned graphs given two input |
13 |
|
|
* graphs and a non-crossing matching between the nodes of the graphs. We |
14 |
|
|
* denote with G1={V1,E1} the first input graph and with G2={V2,E2} the second |
15 |
|
|
* input graph where Vx is the set of nodes and Ex the set of edges (x={1,2}) |
16 |
|
|
* of the respective graph. The node matching M=V1 x V2. We denote the weight |
17 |
|
|
* of an arbitrary edge e with w(e). Each output graph has the following |
18 |
|
|
* properties: |
19 |
|
|
* <ul> |
20 |
|
|
* <li>The number of <b>nodes</b> is the same in both output graphs.</li> |
21 |
|
|
* <li>The number of <b>edges</b> of any output graph corresponds to the number |
22 |
|
|
* edges in the respective input graph.</li> |
23 |
|
|
* <li>Let i,j in V1, i < j, and k,l in V2, k < l. If (i,j) in E1 and (k,l) in E2 |
24 |
|
|
* and (i,l) in M and (j,k) in M then w(i',j')=w(k',l')=w(i,j)+w(k,l) where |
25 |
|
|
* i',j',k',l' denote the new indices of the original nodes in the output |
26 |
|
|
* graphs.</li> |
27 |
|
|
* </ul> |
28 |
|
|
* @author Lars Petzold |
29 |
|
|
* |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
public class PairwiseAlignmentGraphConverter { |
33 |
|
|
|
34 |
|
|
private Graph alignedG1 = null; |
35 |
|
|
private Graph alignedG2 = null; |
36 |
|
|
private EdgeSet commonEdges = new EdgeSet(); |
37 |
|
|
private boolean isCommonEdgeFilled = false; |
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* Constructs two aligned graphs based on the given matching pointed to by |
41 |
|
|
* the passed iterator. This constructor requires the graphs to hold |
42 |
|
|
* sequence information. |
43 |
|
|
* @param it iterator pointing to a matching |
44 |
|
|
* @param g1 first graph |
45 |
|
|
* @param g2 second graph |
46 |
|
|
* @param fi indicates the index of the first node in the matching (note: |
47 |
|
|
* the first node does not necessarily need to be referenced in |
48 |
|
|
* the matching. Do not confuse this with the value of the lowest |
49 |
|
|
* node index in the matching which could be greater, though |
50 |
|
|
* never less!) |
51 |
|
|
*/ |
52 |
lpetzo |
385 |
public PairwiseAlignmentGraphConverter( Iterator<Edge> it, Graph g1, Graph g2, int fi ) |
53 |
|
|
throws AlignmentConstructionError { |
54 |
lpetzo |
372 |
this( new PairwiseAlignmentConverter(it,g1.getSequence(),g2.getSequence(),"1","2",fi).getAlignment(), "1", "2", g1, g2 ); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* |
59 |
|
|
* @param a |
60 |
|
|
* @param tag1 |
61 |
|
|
* @param tag2 |
62 |
|
|
* @param g1 |
63 |
|
|
* @param g2 |
64 |
|
|
*/ |
65 |
|
|
public PairwiseAlignmentGraphConverter( Alignment a, String tag1, String tag2, Graph g1, Graph g2 ) { |
66 |
|
|
alignedG1 = convertGraph(a,tag1,tag2,g1,g2); |
67 |
|
|
isCommonEdgeFilled = true; |
68 |
|
|
alignedG2 = convertGraph(a,tag2,tag1,g2,g1); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
/** |
72 |
|
|
* Creates aligned graph for g1 given g2. |
73 |
|
|
* |
74 |
|
|
* @param a alignment between the two graphs |
75 |
|
|
* @param tag1 tag-name for g1 in the alignment |
76 |
|
|
* @param tag2 tag-name for g2 in the alignment |
77 |
|
|
* @param g1 graph to be converted |
78 |
|
|
* @param g2 graph g1 is aligned to |
79 |
|
|
*/ |
80 |
|
|
private Graph convertGraph( Alignment a, String tag1, String tag2, Graph g1, Graph g2 ) { |
81 |
|
|
TreeMap<Integer,String> nodes = new TreeMap<Integer, String>(); |
82 |
|
|
EdgeSet contacts = new EdgeSet(); |
83 |
|
|
String sequence = a.getAlignedSequence(tag1); |
84 |
|
|
|
85 |
|
|
Iterator<Edge> it = g1.getContactIterator(); |
86 |
|
|
Edge oE1=new Edge(0,0); // holds 'o'riginal 'E'dge in g1 |
87 |
|
|
int i1=0,j1=0; // positions in the gapped sequence for tag1 |
88 |
|
|
int p2=0,q2=0; // positions in the ungapped sequence for tag2 |
89 |
|
|
|
90 |
|
|
// make the contacts |
91 |
|
|
while( it.hasNext() ) { |
92 |
|
|
oE1 = it.next(); |
93 |
|
|
i1 = a.seq2al(tag1,oE1.i); |
94 |
|
|
j1 = a.seq2al(tag1,oE1.j); |
95 |
|
|
p2 = a.al2seq(tag2,i1); |
96 |
|
|
q2 = a.al2seq(tag2,j1); |
97 |
|
|
|
98 |
|
|
// 'm'apped 'E'dge, +1 as the node numbering in the graph |
99 |
|
|
// corresponds to the one found in the pdb file which usually |
100 |
|
|
// starts with 1 |
101 |
|
|
Edge mE1 = new Edge(i1+1,j1+1); |
102 |
|
|
|
103 |
|
|
// add contact with the contact-anchors being mapped to the aligned sequence |
104 |
|
|
contacts.add(mE1); |
105 |
|
|
|
106 |
|
|
if( !(p2 == -1 || q2 == -1) ) { |
107 |
|
|
// 'o'riginal 'E'dge in g'2' |
108 |
|
|
Edge oE2 = new Edge(p2,q2); |
109 |
|
|
|
110 |
|
|
// assign weight to 'mE' in 'contacts' |
111 |
|
|
if( g2.hasContact(oE2) ) { |
112 |
|
|
// conserved contacts get the sum of weights of both source contacts |
113 |
|
|
mE1.weight = oE1.weight + oE2.weight; |
114 |
|
|
if( !isCommonEdgeFilled ) { |
115 |
|
|
commonEdges.add(mE1); |
116 |
|
|
} |
117 |
|
|
} else { |
118 |
|
|
// unconserved contact get the weight of original contact in g1 only |
119 |
|
|
mE1.weight = oE1.weight; |
120 |
|
|
} |
121 |
|
|
} else { |
122 |
|
|
mE1.weight = oE1.weight; |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
// make the nodes |
127 |
|
|
for( int i=0; i<sequence.length(); ++i ) { |
128 |
|
|
if( sequence.charAt(i) == '-' ) { |
129 |
|
|
nodes.put(i+1, AAinfo.getGapCharacterThreeLetter()); |
130 |
|
|
} else { |
131 |
|
|
nodes.put( i+1, AAinfo.oneletter2threeletter(sequence.substring(i,i+1)) ); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
return new Graph(contacts, nodes, sequence, g1.getCutoff(), g1.getContactType(), g1.getPdbCode(), g1.getChainCode(), g1.getPdbChainCode(), g1.getModel(), null); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
/** |
140 |
lpetzo |
385 |
* Gets the aligned graph corresponding to the first graph passed to the |
141 |
|
|
* constructor. |
142 |
lpetzo |
372 |
* @return a graph |
143 |
|
|
*/ |
144 |
|
|
public Graph getFirstGraph() { |
145 |
|
|
return alignedG1; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
/** |
149 |
lpetzo |
385 |
* Gets the aligned graph corresponding to the first graph passed to the |
150 |
|
|
* constructor. |
151 |
lpetzo |
372 |
* @return a graph |
152 |
|
|
*/ |
153 |
|
|
public Graph getSecondGraph() { |
154 |
|
|
return alignedG2; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
/** |
158 |
|
|
* Gets common edge in the resulting graphs. |
159 |
|
|
* @return an edge-set containing common edge only |
160 |
|
|
* */ |
161 |
|
|
public EdgeSet getCommonEdges() { |
162 |
|
|
return commonEdges; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* @param args |
167 |
|
|
*/ |
168 |
|
|
public static void main(String[] args) { |
169 |
|
|
|
170 |
|
|
ContactMap x = IOUtil.read(args[0]); |
171 |
|
|
ContactMap y = IOUtil.read(args[1]); |
172 |
|
|
|
173 |
|
|
SADP sadp = new SADP(x,y); |
174 |
|
|
sadp.run(); |
175 |
|
|
|
176 |
|
|
EdgeSet matching = sadp.getMatching(); |
177 |
|
|
Graph g1 = new Graph(x,null,6.0,AAinfo.getAllContactTypes().iterator().next(),null,null,null,1,null); |
178 |
|
|
Graph g2 = new Graph(y,null,6.0,AAinfo.getAllContactTypes().iterator().next(),null,null,null,1,null); |
179 |
lpetzo |
385 |
|
180 |
|
|
PairwiseAlignmentGraphConverter pac = null; |
181 |
lpetzo |
372 |
|
182 |
|
|
try { |
183 |
lpetzo |
385 |
pac = new PairwiseAlignmentGraphConverter(matching.iterator(),g1,g2,0); |
184 |
|
|
} catch(Exception e) { |
185 |
|
|
System.err.println(e.getMessage()); |
186 |
|
|
System.exit(-1); |
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
try { |
190 |
lpetzo |
372 |
pac.getFirstGraph().write_graph_to_file(args[0]+".cm-aglappe"); |
191 |
|
|
} catch (IOException e) { |
192 |
|
|
System.out.println("Error: Writing aligned graph g1 to file failed!"); |
193 |
|
|
} |
194 |
|
|
|
195 |
|
|
try { |
196 |
|
|
pac.getSecondGraph().write_graph_to_file(args[1]+".cm-aglappe"); |
197 |
|
|
} catch (IOException e) { |
198 |
|
|
System.out.println("Error: Writing aligned graph g1 to file failed!"); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
//System.out.println("Common edges: "+pac.getCommonEdges().toString()); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
} |