Project

General

Profile

Download (5.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.cdm.model.molecular;
5

    
6
import java.util.Arrays;
7

    
8
import javax.persistence.Entity;
9
import javax.persistence.FetchType;
10
import javax.persistence.Lob;
11
import javax.persistence.ManyToOne;
12
import javax.xml.bind.annotation.XmlAccessType;
13
import javax.xml.bind.annotation.XmlAccessorType;
14
import javax.xml.bind.annotation.XmlElement;
15
import javax.xml.bind.annotation.XmlIDREF;
16
import javax.xml.bind.annotation.XmlRootElement;
17
import javax.xml.bind.annotation.XmlSchemaType;
18
import javax.xml.bind.annotation.XmlType;
19

    
20
import org.hibernate.annotations.Cascade;
21
import org.hibernate.annotations.CascadeType;
22
import org.hibernate.annotations.Type;
23
import org.hibernate.envers.Audited;
24

    
25
import eu.etaxonomy.cdm.model.common.VersionableEntity;
26

    
27
/**
28
 * @author a.mueller
29
 */
30
@XmlAccessorType(XmlAccessType.FIELD)
31
@XmlType(name = "SingleReadAlignment", propOrder = {
32
	"consensusAlignment",
33
	"singleRead",
34
	"shifts",
35
	"editedSequence",
36
	"reverseComplement"
37
})
38
@XmlRootElement(name = "SingleReadAlignment")
39
@Entity
40
@Audited
41
public class SingleReadAlignment extends VersionableEntity {
42
	private static final long serialVersionUID = 6141518347067279304L;
43

    
44
	/** @see #getDnaMarker() */
45
	@XmlElement(name = "ConsensusAlignment")
46
    @XmlIDREF
47
    @XmlSchemaType(name = "IDREF")
48
    @ManyToOne(fetch = FetchType.LAZY)
49
	//for now we do not cascade but expect the user to save the sequence manually
50
	private Sequence consensusAlignment;
51
	
52
	/** @see #getDnaMarker() */
53
	@XmlElement(name = "SingleRead")
54
    @XmlIDREF
55
    @XmlSchemaType(name = "IDREF")
56
    @ManyToOne(fetch = FetchType.LAZY)
57
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
58
	private SingleRead singleRead;
59
	
60
	//TODO XML mapping / user type
61
	@Type(type="shiftUserType")
62
    private Shift[] shifts = new Shift[0];
63
	
64
	@XmlElement(name = "EditedSequence")
65
    @Lob
66
    private String editedSequence;
67
	
68
	@XmlElement(name = "ReverseComplement")
69
    private boolean reverseComplement;
70
	
71
	
72
	public static class Shift implements Cloneable{
73
		public int position;
74
		public int shift;
75
		
76
		public Shift(){};
77
		public Shift(int position, int steps) {
78
			this.position = position; 
79
			this.shift = steps;
80
		}
81
		
82
		@Override
83
		public String toString(){
84
			return String.valueOf(position) + "," + String.valueOf(shift);
85
		}
86
		@Override
87
		protected Object clone() throws CloneNotSupportedException {
88
			return super.clone();
89
		}
90
	}
91
	
92
//****************** FACTORY *******************/
93
	
94
	public static SingleReadAlignment NewInstance(Sequence consensusSequence, SingleRead singleRead){
95
		return new SingleReadAlignment(consensusSequence, singleRead, null, null);
96
	}
97
	
98
	public static SingleReadAlignment NewInstance(Sequence consensusSequence, SingleRead singleRead,
99
			Shift[] shifts, String editedSequence){
100
		return new SingleReadAlignment(consensusSequence, singleRead, shifts, editedSequence);
101
	}
102

    
103
// ***************** CONSTRUCTOR *************************/	
104
	
105
	protected SingleReadAlignment(){};
106
	
107
	private SingleReadAlignment(Sequence consensusAlignment, SingleRead singleRead,
108
			Shift[] shifts, String editedSequence){
109
		setConsensusAlignment(consensusAlignment);
110
		setSingleRead(singleRead);
111
		this.shifts = shifts;
112
		this.editedSequence = editedSequence;
113
	}
114
	
115

    
116
// ****************** GETTER / SETTER ***********************/	
117
	
118
	//consensus sequence
119
	public Sequence getConsensusSequence() {
120
		return consensusAlignment;
121
	}
122
	public void setConsensusAlignment(Sequence consensusAlignment) {
123
		if (this.consensusAlignment != null && this.consensusAlignment.getSingleReadAlignments().contains(this)){
124
			this.consensusAlignment.removeSingleReadAlignment(this);
125
		}
126
		this.consensusAlignment = consensusAlignment;
127
		if (consensusAlignment != null && ! consensusAlignment.getSingleReadAlignments().contains(this)){
128
			consensusAlignment.addSingleReadAlignment(this);
129
		}	
130
	}
131

    
132
	public SingleRead getSingleRead() {
133
		return singleRead;
134
	}
135
	public void setSingleRead(SingleRead singleRead) {
136
//		if (this.singleRead != null xxx){
137
//			this.singleRead.removeSingleReadAlignment(this);
138
//		}
139
		this.singleRead = singleRead;
140
//		if (singleRead != null && singleRead.getSingleReadAlignments().contains(this)){
141
//			singleRead.addSingleReadAlignment(this);
142
//		}	
143
	}
144
	
145
	//shifts
146
	public Shift[] getShifts() {
147
		return shifts == null ? new Shift[0] : shifts;
148
	}
149
	public void setShifts(Shift[] shifts) {
150
		this.shifts = shifts;
151
		if (shifts == null){
152
			shifts = new Shift[0];
153
		}
154
	}
155

    
156
	//edited sequence
157
	public String getEditedSequence() {
158
		return editedSequence;
159
	}
160

    
161
	public void setEditedSequence(String editedSequence) {
162
		this.editedSequence = editedSequence;
163
	}
164
	
165

    
166
	public boolean isReverseComplement() {
167
		return reverseComplement;
168
	}
169

    
170
	public void setReverseComplement(boolean reverseComplement) {
171
		this.reverseComplement = reverseComplement;
172
	}
173

    
174
// ******************* CLONE *********************/
175

    
176

    
177

    
178
	@Override
179
	public Object clone() throws CloneNotSupportedException {
180
		SingleReadAlignment result = (SingleReadAlignment)super.clone();
181
		
182
		//deep copy shifts
183
		Shift[] oldShifts = this.getShifts();
184
		int shiftLength = oldShifts.length;
185
		Shift[] newShift = new Shift[shiftLength];
186
		for (int i = 0; i< shiftLength; i++){
187
			Shift oldShift = oldShifts[i];
188
			newShift[0] = (Shift)oldShift.clone();
189
		}
190
		
191
		//all other objects can be reused
192
		return result;
193
	}
194

    
195
}
(12-12/14)