ref #10322, ref #10472 further remove model objects, use DerivationEvent and some...
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / molecular / SingleReadAlignment.java
1 /**
2 *
3 */
4 package eu.etaxonomy.cdm.model.molecular;
5
6 import java.io.Serializable;
7
8 import javax.persistence.Entity;
9 import javax.persistence.FetchType;
10 import javax.persistence.Lob;
11 import javax.persistence.OneToOne;
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 "firstSeqPosition",
38 "leftCutPosition",
39 "rightCutPosition"
40 })
41 @XmlRootElement(name = "SingleReadAlignment")
42 @Entity
43 @Audited
44 public class SingleReadAlignment extends VersionableEntity implements Serializable {
45 private static final long serialVersionUID = 6141518347067279304L;
46
47 /** @see #getDnaMarker() */
48 @XmlElement(name = "ConsensusAlignment")
49 @XmlIDREF
50 @XmlSchemaType(name = "IDREF")
51 @OneToOne(fetch = FetchType.LAZY)
52 //for now we do not cascade but expect the user to save the sequence manually
53 private Sequence consensusAlignment;
54
55 /** @see #getDnaMarker() */
56 @XmlElement(name = "SingleRead")
57 @XmlIDREF
58 @XmlSchemaType(name = "IDREF")
59 @OneToOne(fetch = FetchType.LAZY)
60 @Cascade({CascadeType.SAVE_UPDATE, CascadeType.MERGE})
61 private SingleRead singleRead;
62
63 //TODO XML mapping / user type
64 @Type(type="shiftUserType")
65 private Shift[] shifts = new Shift[0];
66
67 private Integer firstSeqPosition;
68
69 private Integer leftCutPosition;
70
71 private Integer rightCutPosition;
72
73 @XmlElement(name = "EditedSequence")
74 @Lob
75 private String editedSequence;
76
77 @XmlElement(name = "ReverseComplement")
78 private boolean reverseComplement;
79
80
81 public static class Shift implements Cloneable, Serializable {
82 public int position;
83 public int shift;
84
85 public Shift(){};
86 public Shift(int position, int steps) {
87 this.position = position;
88 this.shift = steps;
89 }
90
91 @Override
92 public String toString(){
93 return String.valueOf(position) + "," + String.valueOf(shift);
94 }
95 @Override
96 protected Object clone() throws CloneNotSupportedException {
97 return super.clone();
98 }
99 }
100
101 //****************** FACTORY *******************/
102
103 public static SingleReadAlignment NewInstance(Sequence consensusSequence, SingleRead singleRead){
104 return new SingleReadAlignment(consensusSequence, singleRead, null, null);
105 }
106
107 public static SingleReadAlignment NewInstance(Sequence consensusSequence, SingleRead singleRead,
108 Shift[] shifts, String editedSequence){
109 return new SingleReadAlignment(consensusSequence, singleRead, shifts, editedSequence);
110 }
111
112 // ***************** CONSTRUCTOR *************************/
113
114 protected SingleReadAlignment(){};
115
116 private SingleReadAlignment(Sequence consensusAlignment, SingleRead singleRead,
117 Shift[] shifts, String editedSequence){
118 setConsensusAlignment(consensusAlignment);
119 setSingleRead(singleRead);
120 this.shifts = shifts;
121 this.editedSequence = editedSequence;
122 }
123
124
125 // ****************** GETTER / SETTER ***********************/
126
127 //consensus sequence
128 public Sequence getConsensusSequence() {
129 return consensusAlignment;
130 }
131 public void setConsensusAlignment(Sequence consensusAlignment) {
132 if (this.consensusAlignment != null && this.consensusAlignment.getSingleReadAlignments().contains(this)){
133 this.consensusAlignment.removeSingleReadAlignment(this);
134 }
135 this.consensusAlignment = consensusAlignment;
136 if (consensusAlignment != null && ! consensusAlignment.getSingleReadAlignments().contains(this)){
137 consensusAlignment.addSingleReadAlignment(this);
138 }
139 }
140
141 public SingleRead getSingleRead() {
142 return singleRead;
143 }
144 public void setSingleRead(SingleRead singleRead) {
145 // if (this.singleRead != null xxx){
146 // this.singleRead.removeSingleReadAlignment(this);
147 // }
148 this.singleRead = singleRead;
149 // if (singleRead != null && singleRead.getSingleReadAlignments().contains(this)){
150 // singleRead.addSingleReadAlignment(this);
151 // }
152 }
153
154 //shifts
155 public Shift[] getShifts() {
156 return shifts == null ? new Shift[0] : shifts;
157 }
158 public void setShifts(Shift[] shifts) {
159 this.shifts = shifts;
160 if (shifts == null){
161 shifts = new Shift[0];
162 }
163 }
164
165 //edited sequence
166 public String getEditedSequence() {
167 return editedSequence;
168 }
169
170 public void setEditedSequence(String editedSequence) {
171 this.editedSequence = editedSequence;
172 }
173
174
175 public boolean isReverseComplement() {
176 return reverseComplement;
177 }
178
179 public void setReverseComplement(boolean reverseComplement) {
180 this.reverseComplement = reverseComplement;
181 }
182
183 // ******************* CLONE *********************/
184
185 @Override
186 public SingleReadAlignment clone() throws CloneNotSupportedException {
187
188 SingleReadAlignment result = (SingleReadAlignment)super.clone();
189
190 //deep copy shifts
191 Shift[] oldShifts = this.getShifts();
192 int shiftLength = oldShifts.length;
193 Shift[] newShift = new Shift[shiftLength];
194 for (int i = 0; i< shiftLength; i++){
195 Shift oldShift = oldShifts[i];
196 newShift[0] = (Shift)oldShift.clone();
197 }
198
199 //all other objects can be reused
200 return result;
201 }
202
203 /**
204 * Returns the position in the {@link Sequence sequence}
205 * this {@link SingleReadAlignment single read align} is attached to
206 * where the output of the visible part of the pherogram starts.
207 * @return a valid index in the sequence carrying this data area
208 * @see http://bioinfweb.info/LibrAlign/Documentation/api/latest/info/bioinfweb/libralign/dataarea/implementations/pherogram/PherogramArea.html#getFirstSeqPos
209 *
210 */
211 public Integer getFirstSeqPosition() {
212 return firstSeqPosition;
213 }
214
215 /**
216 * @see #getFirstSeqPosition()
217 */
218 public void setFirstSeqPosition(Integer firstSeqPosition) {
219 this.firstSeqPosition = firstSeqPosition;
220 }
221
222 /**
223 * Returns the first base call index of the pherogram which has not been cut off.
224 * @return a base call index > 0
225 * @see http://bioinfweb.info/LibrAlign/Documentation/api/latest/info/bioinfweb/libralign/pherogram/PherogramComponent.html#getLeftCutPosition
226 */
227 public Integer getLeftCutPosition() {
228 return leftCutPosition;
229 }
230
231 /**
232 * @param see {@link #getLeftCutPosition()}
233 */
234 public void setLeftCutPosition(Integer leftCutPosition) {
235 this.leftCutPosition = leftCutPosition;
236 }
237
238 /**
239 * Returns the first base call index of the pherogram that has been cut off (so that the length of the visible
240 * area of the pherogram can be calculated as getRightCutPosition()
241 * @return a base call inde
242 * @see http://bioinfweb.info/LibrAlign/Documentation/api/latest/info/bioinfweb/libralign/pherogram/PherogramComponent.html#getRightCutPosition
243 */
244 public Integer getRightCutPosition() {
245 return rightCutPosition;
246 }
247
248 /**
249 * @param see {@link #getRightCutPosition()}
250 */
251 public void setRightCutPosition(Integer rightCutPosition) {
252 this.rightCutPosition = rightCutPosition;
253 }
254
255 }