Skip to content

Commit 1d5830b

Browse files
better check for existing samples (#103)
* better check for existing samples * cleaner code, avoid creation of unneccessary experiment objects * use nicer variable names
1 parent b1537fc commit 1d5830b

File tree

1 file changed

+21
-36
lines changed

1 file changed

+21
-36
lines changed

drop-boxes/register-hlatyping-dropbox/register-hlatyping.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,18 @@ def process(transaction):
6767
print "The identifier "+identifier+" did not match the pattern Q[A-Z]{4}\d{3}\w{2} or checksum"
6868

6969
search_service = transaction.getSearchService()
70-
sc = SearchCriteria()
71-
sc.addMatchClause(SearchCriteria.MatchClause.createAttributeMatch(SearchCriteria.MatchClauseAttribute.CODE, parentCode))
72-
foundSamples = search_service.searchForSamples(sc)
70+
searchCriteria = SearchCriteria()
71+
projectCriteria = SearchCriteria()
72+
projectCriteria.addMatchClause(SearchCriteria.MatchClause.createAttributeMatch(SearchCriteria.MatchClauseAttribute.PROJECT, project));
73+
searchCriteria.addSubCriteria(SearchSubCriteria.createExperimentCriteria(projectCriteria))
74+
foundSamples = search_service.searchForSamples(searchCriteria)
7375
if len(foundSamples) > 0:
74-
parentSampleIdentifier = foundSamples[0].getSampleIdentifier()
7576
space = foundSamples[0].getSpace()
77+
parentSampleIdentifier = "/"+space+"/"+parentCode
7678
else:
77-
search_service = transaction.getSearchService()
78-
sc = SearchCriteria()
79-
pc = SearchCriteria()
80-
pc.addMatchClause(SearchCriteria.MatchClause.createAttributeMatch(SearchCriteria.MatchClauseAttribute.PROJECT, project));
81-
sc.addSubCriteria(SearchSubCriteria.createExperimentCriteria(pc))
82-
foundSamples = search_service.searchForSamples(sc)
83-
if len(foundSamples) > 0:
84-
space = foundSamples[0].getSpace()
85-
parentSampleIdentifier = "/"+space+"/"+parentCode
86-
else:
87-
# no sample found in this project, they are probably not indexed yet. try parsing space from file name instead
88-
space = name.split("_"+parentCode)[0]
89-
parentSampleIdentifier = "/"+space+"/"+parentCode
90-
sa = transaction.getSampleForUpdate(parentSampleIdentifier)
79+
# no sample found in this project, they are probably not indexed yet. try parsing space from file name instead
80+
space = name.split("_"+parentCode)[0]
81+
parentSampleIdentifier = "/"+space+"/"+parentCode
9182

9283
# register new experiment and sample
9384
existingExperimentIDs = []
@@ -104,9 +95,6 @@ def process(transaction):
10495
numberOfExperiments += 1
10596
newExpID = '/' + space + '/' + project + '/' + project + 'E' +str(numberOfExperiments)
10697

107-
newHLATypingExperiment = transaction.createNewExperiment(newExpID, "Q_NGS_HLATYPING")
108-
newHLATypingExperiment.setPropertyValue('Q_CURRENT_STATUS', 'FINISHED')
109-
11098
if os.path.isdir(incomingPath):
11199
for root, subFolders, files in os.walk(incomingPath):
112100
if subFolders:
@@ -127,25 +115,22 @@ def process(transaction):
127115
mhcClass = "MHC_CLASS_I"
128116
mhcSuffix = "1"
129117
# does HLA sample of this class already exist?
130-
hlaCode = 'HLA' + mhcSuffix + parentCode
131-
sc = SearchCriteria()
132-
sc.addMatchClause(SearchCriteria.MatchClause.createAttributeMatch(
133-
SearchCriteria.MatchClauseAttribute.CODE, hlaCode))
134-
foundSamples = search_service.searchForSamples(sc)
135-
if len(foundSamples) < 1:
136-
newHLATypingSample = transaction.createNewSample('/' + space + '/' + hlaCode, "Q_NGS_HLATYPING")
137-
newHLATypingSample.setParentSampleIdentifiers([sa.getSampleIdentifier()])
138-
newHLATypingSample.setExperiment(newHLATypingExperiment)
139-
newHLATypingSample.setPropertyValue("Q_HLA_CLASS", mhcClass)
140-
else:
141-
newHLATypingSample = transaction.getSampleForUpdate(foundSamples[0].getSampleIdentifier())
142-
143-
newHLATypingSample.setPropertyValue("Q_HLA_TYPING", resultContent)
118+
HLASampleID = "/"+space+"/"+'HLA' + mhcSuffix + parentCode
119+
HLATypingSample = transaction.getSampleForUpdate(HLASampleID)
120+
if not HLATypingSample:
121+
HLATypingSample = transaction.createNewSample(HLASampleID, "Q_NGS_HLATYPING")
122+
HLATypingSample.setParentSampleIdentifiers([parentSampleIdentifier])
123+
newHLATypingExperiment = transaction.createNewExperiment(newExpID, "Q_NGS_HLATYPING")
124+
newHLATypingExperiment.setPropertyValue('Q_CURRENT_STATUS', 'FINISHED')
125+
HLATypingSample.setExperiment(newHLATypingExperiment)
126+
HLATypingSample.setPropertyValue("Q_HLA_CLASS", mhcClass)
127+
128+
HLATypingSample.setPropertyValue("Q_HLA_TYPING", resultContent)
144129

145130
# create new dataset
146131
dataSet = transaction.createNewDataSet("Q_NGS_HLATYPING_DATA")
147132
dataSet.setMeasuredData(False)
148-
dataSet.setSample(newHLATypingSample)
133+
dataSet.setSample(HLATypingSample)
149134

150135
transaction.moveFile(resultPath, dataSet)
151136

0 commit comments

Comments
 (0)