def test_run_external_command_fail_with_output(self): temp_dir = tempfile.mkdtemp() try: script = os.path.join(temp_dir, 'yo.py') # create a small python script that outputs args passed # in to standard out, writes error to standard error # and exits with 0 exit code f = open(script, 'w') f.write('#! /usr/bin/env python\n\n') f.write('import sys\n') f.write('sys.stdout.write(sys.argv[1])\n') f.write('sys.stdout.write(sys.argv[2])\n') f.write('sys.stderr.write("2error")\n') f.write('sys.exit(2)\n') f.flush() f.close() os.chmod(script, stat.S_IRWXU) ecode, out, err = util.run_external_command(script + ' hi how') self.assertEqual(err, '2error') self.assertEqual(out, 'hihow') self.assertEqual(ecode, 2) finally: shutil.rmtree(temp_dir)
def write_markers(self, markers): """Writes IMOD fiducial file with data from `markers` object passed in :param markers: MarkersList object containing markers to write out :raises UnsetFiducialFileError: If fiducial file set via constructor is None :raises UnsetMarkersListError if markers passed into this method is None :raises Exception: If invocation of point2model binary has non-zero exit code """ if self._fiducialfile is None: raise UnsetFiducialFileError('Fiducial File is set to None') if markers is None: raise UnsetMarkersListError('markers cannot be None') try: temp_dir = tempfile.mkdtemp() tmpfile = os.path.join(temp_dir, 'out.txt') markers.write_markers_to_file(tmpfile) cmd = (self._binary + ' -circle 6 ' + tmpfile + ' ' + self._fiducialfile) (ecode, out, err) = util.run_external_command(cmd) if ecode != 0: raise Exception('Non zero exit code when running : ' + cmd + ' : ' + err) finally: shutil.rmtree(temp_dir)
def get_markers(self): """Gets markers from IMOD fiducial file """ markers = MarkersList() if self._fiducialfile is None: logger.warning('Fiducial file is set to None') return markers if not os.path.isfile(self._fiducialfile): logger.warning('Fiducial file path does not point to file') return markers try: temp_dir = tempfile.mkdtemp() outfile = os.path.join(temp_dir, 'temp.txt') cmd = (self._binary + ' -float -contour ' + self._fiducialfile + ' ' + outfile) (ecode, out, err) = util.run_external_command(cmd) if ecode != 0: raise Exception('Non zero exit code when running : ' + cmd + ' : ' + err) fac = MarkersFrom3DMarkersFileFactory(outfile) return fac.get_markerslist() finally: shutil.rmtree(temp_dir)
def _run_rotatevol(self, rotation): """Rotates mrc volume """ markermrc = os.path.join(self._get_marker_dir(), self._markermrc) tmp_mrc = os.path.join(self._get_marker_dir(), "tmp.mrc") cmd = "rotatevol -angles " + str(rotation) + ",0,0 " + markermrc + " " + tmp_mrc exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run rotatevol : " + err) shutil.move(tmp_mrc, markermrc)
def test_run_external_command_cmd_does_not_exist(self): temp_dir = tempfile.mkdtemp() try: noexist = os.path.join(temp_dir, 'noexist') ecode, out, err = util.run_external_command(noexist) self.assertEqual(ecode, 255) self.assertEqual(out, '') self.assertEqual(err, 'Caught exception trying run command: ' '[Errno 2] No such file or directory') finally: shutil.rmtree(temp_dir)
def _get_mrc_marker_image_dimensions(self): """Gets dimensions of marker mrc file :returns tuple: x, y, z """ markermrc = os.path.join(self._get_marker_dir(), self._markermrc) cmd = "header -s " + markermrc exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run header -s : " + err) list = re.split("\s+", out) if len(list) != 5: raise Exception("Invalid output from header : " + out) return list[1], list[2], list[3]
def _run_point2model(self): """runs point2model for fid file """ two_d_fid = os.path.join(self._workdir, TiltSeriesCreator.TWO_D_MARKERS_ALL_FID) cmd = ( "point2model -circle 6 " + os.path.join(self._get_tracking_dir(), TiltSeriesCreator.TWO_D_MARKERS_ALL_TXT) + " " + two_d_fid ) exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run point2model : " + err)
def _run_point2model_common(self): """runs point2model for fid file """ common_fid = os.path.join( self._workdir, self._mrcname + TiltSeriesCreator.PROJECTION_CLIP + TiltSeriesCreator.FID_EXT ) cmd = ( "point2model -circle 6 " + os.path.join(self._get_tracking_dir(), TiltSeriesCreator.TWO_D_MARKERS_COMMON_TXT) + " " + common_fid ) exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run point2model : " + err)
def _run_clip_projection_mrc(self): """Runs clip resize to get a clipped mrc file """ (x, y, z) = self._get_mrc_marker_image_dimensions() cmd = ( "clip resize -ox " + str(int(int(x) / 3)) + " -oy " + str(int(int(y) / 3)) + " " + os.path.join(self._workdir, self._projectionmrc) + " " + os.path.join(self._workdir, self._projectionclipmrc) ) exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run clip : " + err)
def _run_rotate_3dmarkers(self, rotation): """ Rotates 3Dmarkers.txt file :param rotation: :return: """ (x, y, z) = self._get_mrc_marker_image_dimensions() three_d_markers_file = os.path.join(self._get_marker_dir(), TiltSeriesCreator.THREE_D_MARKERS_TXT) cmd = ( "rotate_3dmarkers.py --angle " + str(rotation) + " --width " + str(x) + " --height " + str(y) + " " + three_d_markers_file ) exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run rotate_3dmarkers.py : " + err)
def _run_shift_fidfilemarkers_common(self): """Runs shift_fidfilemarkers to shift fid file for non clipped projection """ common_fid = os.path.join( self._workdir, self._mrcname + TiltSeriesCreator.PROJECTION_CLIP + TiltSeriesCreator.FID_EXT ) (x, y, z) = self._get_mrc_marker_image_dimensions() cmd = ( "shift_fidfilemarkers.py --xshift " + str(int(int(x) / 3)) + " --yshift " + str(int(int(y) / 3)) + " " + common_fid + " " + os.path.join(self._workdir, self._mrcname + TiltSeriesCreator.PROJECTION + TiltSeriesCreator.FID_EXT) ) exitcode, out, err = util.run_external_command(cmd) if exitcode != 0: raise Exception("Unable to run shif_fidfilemarkers.py : " + err)
def test_run_external_command_command_not_set(self): ecode, out, err = util.run_external_command(None) self.assertEqual(ecode, 255) self.assertEqual(out, '') self.assertEqual(err, 'Command must be set')