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