| •1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Utility to obtain paths to important analysis files from SMRT Link jobs, |
| 5 |
compatible with multiple applications and versions. |
| 6 |
""" |
| 7 |
|
| 8 |
import logging |
| 9 |
import os |
| 10 |
import os.path as op |
| 11 |
import sys |
| 12 |
|
| 13 |
from pbcommand.models.common import FileTypes |
| 14 |
from pbcommand.services._service_access_layer import get_smrtlink_client, run_client_with_retry |
| 15 |
from pbcommand.utils import setup_log |
| 16 |
from pbcommand.cli import get_default_argparser_with_base_opts, pacbio_args_runner |
| 17 |
|
| 18 |
log = logging.getLogger(__name__) |
| 19 |
__version__ = "0.1.1" |
| 20 |
|
| 21 |
|
| 22 |
class ResolverFailure(Exception): |
| 23 |
pass |
| 24 |
|
| 25 |
|
| 26 |
class ResourceTypes: |
| 27 |
JOB_PATH = "path" |
| 28 |
ALIGNMENTS = "alignments" |
| 29 |
PREASSEMBLY = "preassembly" |
| 30 |
POLISHED_ASSEMBLY = "polished-assembly" |
| 31 |
MAPPING_STATS = "mapping-stats" |
| 32 |
SUBREADS_ENTRY = "subreads-in" |
| 33 |
|
| 34 |
ALL = [JOB_PATH, ALIGNMENTS, PREASSEMBLY, |
| 35 |
POLISHED_ASSEMBLY, MAPPING_STATS, SUBREADS_ENTRY] |
| 36 |
|
| 37 |
@staticmethod |
| 38 |
def from_string(s): |
| 39 |
if s in ResourceTypes.ALL: |
| 40 |
return s |
| 41 |
else: |
| 42 |
raise KeyError("Unknown resource type '%s'" % s) |
| 43 |
|
| 44 |
|
| 45 |
def _is_report(ds_file): |
| 46 |
return ds_file.file_type_id == FileTypes.REPORT.file_type_id |
| 47 |
|
| 48 |
|
| 49 |
def _is_alignments(ds_file): |
| 50 |
return ds_file.file_type_id in [FileTypes.DS_ALIGN.file_type_id, |
| 51 |
FileTypes.DS_ALIGN_CCS.file_type_id] |
| 52 |
|
| 53 |
|
| 54 |
def _get_by_partial_source_id(ds_files, source_id_str): |
| 55 |
for ds_file in ds_files: |
| 56 |
if source_id_str in ds_file.source_id: |
| 57 |
return ds_file.path |
| 58 |
|
| 59 |
|
| 60 |
ALIGNMENT_SOURCES = [ |
| 61 |
"mapped", |
| 62 |
"consolidated_xml", |
| 63 |
"consolidate_alignments-out-0", |
| 64 |
"consolidate_alignments_ccs-out-0", |
| 65 |
"datastore_to_alignments-out-0", |
| 66 |
"datastore_to_ccs_alignments-out-0" |
| 67 |
] |
| 68 |
|
| 69 |
|
| 70 |
def _find_alignments(datastore): |
| 71 |
alignments = [f for f in datastore.files.values() if _is_alignments(f)] |
| 72 |
if len(alignments) == 1: |
| 73 |
return alignments[0].path |
| 74 |
for source in ALIGNMENT_SOURCES: |
| 75 |
for ds_file in alignments: |
| 76 |
source_id = ds_file.source_id.split(".")[-1] |
| 77 |
if source_id == source: |
| 78 |
return ds_file.path |
| 79 |
raise ResolverFailure("Can't find alignments output for job") |
| 80 |
|
| 81 |
|
| 82 |
class Resolver: |
| 83 |
def __init__(self, client): |
| 84 |
self._client = client |
| 85 |
|
| 86 |
def _get_job_datastore_reports(self, job_id): |
| 87 |
datastore = self._client.get_analysis_job_datastore(job_id) |
| 88 |
return [f for f in datastore.files.values() if _is_report(f)] |
| 89 |
|
| 90 |
def resolve_alignments(self, job_id): |
| 91 |
datastore = self._client.get_analysis_job_datastore(job_id) |
| 92 |
return _find_alignments(datastore) |
| 93 |
|
| 94 |
def resolve_preassembly_stats(self, job_id): |
| 95 |
ds_files = self._get_job_datastore_reports(job_id) |
| 96 |
return _get_by_partial_source_id(ds_files, "preassembly") |
| 97 |
|
| 98 |
def resolve_polished_assembly_stats(self, job_id): |
| 99 |
ds_files = self._get_job_datastore_reports(job_id) |
| 100 |
return _get_by_partial_source_id(ds_files, "polished_assembly") |
| 101 |
|
| 102 |
def resolve_mapping_stats(self, job_id): |
| 103 |
ds_files = self._get_job_datastore_reports(job_id) |
| 104 |
return _get_by_partial_source_id(ds_files, "mapping_stats") |
| 105 |
|
| 106 |
def resolve_job(self, job_id): |
| 107 |
return self._client.get_job_by_id(job_id).path |
| 108 |
|
| 109 |
def resolve_input_subreads(self, job_id): |
| 110 |
eps = self._client.get_analysis_job_entry_points(job_id) |
| 111 |
subread_ids = [] |
| 112 |
for ep in eps: |
| 113 |
if ep.dataset_metatype == FileTypes.DS_SUBREADS.file_type_id: |
| 114 |
subread_ids.append(ep.dataset_uuid) |
| 115 |
if len(subread_ids) == 0: |
| 116 |
raise ResolverFailure( |
| 117 |
"Can't find a SubreadSet entry point for this job") |
| 118 |
elif len(subread_ids) > 1: |
| 119 |
raise ResolverFailure( |
| 120 |
"Multiple SubreadSet entry points found for this job") |
| 121 |
return self._client.get_subreadset_by_id(subread_ids[0])["path"] |
| 122 |
|
| 123 |
|
| 124 |
def run_args(args): |
| 125 |
def _run_resolver(client): |
| 126 |
resolver = Resolver(client) |
| 127 |
resource = None |
| 128 |
if args.resource_type == ResourceTypes.JOB_PATH: |
| 129 |
resource = resolver.resolve_job(args.job_id) |
| 130 |
elif args.resource_type == ResourceTypes.ALIGNMENTS: |
| 131 |
resource = resolver.resolve_alignments(args.job_id) |
| 132 |
elif args.resource_type == ResourceTypes.PREASSEMBLY: |
| 133 |
resource = resolver.resolve_preassembly_stats(args.job_id) |
| 134 |
elif args.resource_type == ResourceTypes.POLISHED_ASSEMBLY: |
| 135 |
resource = resolver.resolve_polished_assembly_stats(args.job_id) |
| 136 |
elif args.resource_type == ResourceTypes.MAPPING_STATS: |
| 137 |
resource = resolver.resolve_mapping_stats(args.job_id) |
| 138 |
elif args.resource_type == ResourceTypes.SUBREADS_ENTRY: |
| 139 |
resource = resolver.resolve_input_subreads(args.job_id) |
| 140 |
else: |
| 141 |
raise NotImplementedError( |
| 142 |
"Can't retrieve resource type '%s'" % args.resource_type) |
| 143 |
return resource |
| 144 |
|
| 145 |
resource = run_client_with_retry(_run_resolver, |
| 146 |
args.host, |
| 147 |
args.port, |
| 148 |
args.user, |
| 149 |
args.password) |
| 150 |
print(resource) |
| 151 |
if args.make_symlink is not None: |
| 152 |
if op.exists(args.make_symlink): |
| 153 |
os.remove(args.make_symlink) |
| 154 |
os.symlink(resource, args.make_symlink) |
| 155 |
return 0 |
| 156 |
|
| 157 |
|
| 158 |
def _get_parser(): |
| 159 |
p = get_default_argparser_with_base_opts( |
| 160 |
__version__, |
| 161 |
__doc__, |
| 162 |
default_level=logging.WARN) |
| 163 |
p.add_argument("job_id", help="SMRT Link analysis job ID") |
| 164 |
p.add_argument("resource_type", nargs="?", |
| 165 |
default=ResourceTypes.JOB_PATH, |
| 166 |
type=ResourceTypes.from_string, |
| 167 |
help="Resource type to resolve (choices: {c})".format( |
| 168 |
c=", ".join(ResourceTypes.ALL))) |
| 169 |
p.add_argument("-u", "--host", dest="host", action="store", |
| 170 |
default=os.environ.get("PB_SERVICE_HOST", "localhost"), |
| 171 |
help="Hostname of SMRT Link server. If this is anything other than 'localhost' you must supply authentication.") |
| 172 |
p.add_argument("-p", "--port", dest="port", action="store", type=int, |
| 173 |
default=int(os.environ.get("PB_SERVICE_PORT", "8081")), |
| 174 |
help="Services port number") |
| 175 |
p.add_argument("--user", dest="user", action="store", |
| 176 |
default=os.environ.get("PB_SERVICE_AUTH_USER", None), |
| 177 |
help="User to authenticate with (if using HTTPS)") |
| 178 |
p.add_argument("--password", dest="password", action="store", |
| 179 |
default=os.environ.get("PB_SERVICE_AUTH_PASSWORD", None), |
| 180 |
help="Password to authenticate with (if using HTTPS)") |
| 181 |
p.add_argument("--symlink", dest="make_symlink", action="store", |
| 182 |
default=None, |
| 183 |
help="If defined, will create a symlink to the retrieved file") |
| 184 |
return p |
| 185 |
|
| 186 |
|
| 187 |
def main(argv): |
| 188 |
return pacbio_args_runner(argv[1:], |
| 189 |
_get_parser(), |
| 190 |
run_args, |
| 191 |
log, |
| 192 |
setup_log_func=setup_log) |
| 193 |
|
| 194 |
|
| 195 |
if __name__ == "__main__": |
| 196 |
sys.exit(main(sys.argv)) |