def test_cit_magic_fail_option4(self): options = {} options['input_dir_path'] = os.path.join(WD, 'data_files', 'Measurement_Import', 'cit_magic', 'PI47') options['magfile'] = 'PI47-.sam' options['samp_con'] = '4' program_ran, error_message = cit_magic.main(False, **options) self.assertFalse(program_ran) self.assertEqual(error_message, "naming convention option [4] must be in form 4-Z where Z is an integer")
def test_cit_magic_with_file(self): options = {} options['input_dir_path'] = os.path.join(WD, 'data_files', 'Measurement_Import', 'cit_magic', 'PI47') options['magfile'] = 'PI47-.sam' program_ran, outfile = cit_magic.main(False, **options) self.assertTrue(program_ran) expected_file = os.path.join('.', 'magic_measurements.txt') self.assertEqual(outfile, expected_file)
def test_cit_magic_succeed_option4(self): options = {} options['input_dir_path'] = os.path.join(WD, 'pmagpy_data_files', 'Measurement_Import', 'cit_magic', 'MP18') options['magfile'] = 'bMP.sam' options['samp_con'] = '4-3' program_ran, outfile = cit_magic.main(False, **options) self.assertTrue(program_ran) expected_file = os.path.join('.', 'magic_measurements.txt') self.assertEqual(outfile, expected_file)
def test_cit_magic_fail_option4(self): options = {} options['input_dir_path'] = os.path.join(WD, 'data_files', 'Measurement_Import', 'cit_magic', 'PI47') options['magfile'] = 'PI47-.sam' options['samp_con'] = '4' program_ran, error_message = cit_magic.main(False, **options) self.assertFalse(program_ran) self.assertEqual( error_message, "naming convention option [4] must be in form 4-Z where Z is an integer" )
def test_cit_magic_with_other_data(self): options = {} options['input_dir_path'] = os.path.join(WD, 'data_files', 'Measurement_Import', 'cit_magic', 'PI47') options['magfile'] = 'PI47-.sam' options['samp_con'] = '1' options['methods'] = ['SO-SM:SO-MAG'] options['locname'] = 'location' options['avg'] = 1 options['specnum'] = 2 program_ran, outfile = cit_magic.main(False, **options) self.assertTrue(program_ran) expected_file = os.path.join('.', 'magic_measurements.txt') self.assertEqual(outfile, expected_file)
def read_inp(WD, inp_file_name, magic_files): inp_file = open(inp_file_name, "r") new_inp_file = "" if type(magic_files) != dict: magic_files = {} if "measurements" not in magic_files.keys(): magic_files["measurements"] = [] if "specimens" not in magic_files.keys(): magic_files["specimens"] = [] if "samples" not in magic_files.keys(): magic_files["samples"] = [] if "sites" not in magic_files.keys(): magic_files["sites"] = [] lines = inp_file.read().split("\n") if len(lines) < 3: print(".inp file improperly formated") return new_inp_file = lines[0] + "\n" + lines[1] + "\n" [lines.remove("") for i in range(lines.count(""))] format = lines[0].strip() header = lines[1].split("\t") update_files = lines[2:] update_data = False for i, update_file in enumerate(update_files): update_lines = update_file.split("\t") if not os.path.isfile(update_lines[0]): print("%s not found searching for location of file" % (update_lines[0])) sam_file_name = os.path.split(update_lines[0])[-1] new_file_path = find_file(sam_file_name, WD) if new_file_path == None or not os.path.isfile(new_file_path): print("%s does not exist in any subdirectory of %s and will be skipped" % (update_lines[0], WD)) new_inp_file += update_file + "\n" continue else: print("new location for file found at %s" % (new_file_path)) update_lines[0] = new_file_path d = os.path.dirname(update_lines[0]) name = os.path.basename(os.path.splitext(update_lines[0])[0]) erspecf = name + "_er_specimens.txt" ersampf = name + "_er_samples.txt" ersitef = name + "_er_sites.txt" f = name + ".magic" if os.path.join(d, f) in magic_files: new_inp_file += update_file + "\n" continue if float(update_lines[-1]) >= os.path.getmtime(update_lines[0]): no_changes = True # check specimen files for changes sam_file = open(update_lines[0]) sam_file_lines = sam_file.readlines() spec_file_paths = map(lambda x: os.path.join(d, x.strip("\r \n")), sam_file_lines[2:]) for spec_file_path in spec_file_paths: if float(update_lines[-1]) < os.path.getmtime(spec_file_path): no_changes = False break if ( no_changes and os.path.isfile(os.path.join(WD, f)) and os.path.isfile(os.path.join(WD, erspecf)) and os.path.isfile(os.path.join(WD, ersampf)) and os.path.isfile(os.path.join(WD, ersitef)) ): magic_files["measurements"].append(os.path.join(WD, f)) magic_files["specimens"].append(os.path.join(WD, erspecf)) magic_files["samples"].append(os.path.join(WD, ersampf)) magic_files["sites"].append(os.path.join(WD, ersitef)) new_inp_file += update_file + "\n" continue if len(header) != len(update_lines): print( "length of header and length of enteries for the file %s are different and will be skipped" % (update_lines[0]) ) new_inp_file += update_file + "\n" continue update_dict = {} for head, entry in zip(header, update_lines): update_dict[head] = entry if format == "CIT": CIT_kwargs = {} CIT_name = os.path.basename(os.path.splitext(update_dict["sam_path"])[0]) CIT_kwargs["dir_path"] = WD # reduce(lambda x,y: x+"/"+y, update_dict["sam_path"].split("/")[:-1]) CIT_kwargs["user"] = "" CIT_kwargs["meas_file"] = CIT_name + ".magic" CIT_kwargs["spec_file"] = CIT_name + "_er_specimens.txt" CIT_kwargs["samp_file"] = CIT_name + "_er_samples.txt" CIT_kwargs["site_file"] = CIT_name + "_er_sites.txt" CIT_kwargs["locname"] = update_dict["location"] CIT_kwargs["methods"] = update_dict["field_magic_codes"] CIT_kwargs["specnum"] = update_dict["num_terminal_char"] CIT_kwargs["avg"] = update_dict["dont_average_replicate_measurements"] CIT_kwargs["samp_con"] = update_dict["naming_convention"] CIT_kwargs["peak_AF"] = update_dict["peak_AF"] CIT_kwargs["magfile"] = os.path.basename(update_dict["sam_path"]) CIT_kwargs["input_dir_path"] = os.path.dirname(update_dict["sam_path"]) program_ran, error_message = cit_magic.main(command_line=False, **CIT_kwargs) measp = os.path.join(CIT_kwargs["dir_path"], CIT_kwargs["meas_file"]) specp = os.path.join(CIT_kwargs["dir_path"], CIT_kwargs["spec_file"]) sampp = os.path.join(CIT_kwargs["dir_path"], CIT_kwargs["samp_file"]) sitep = os.path.join(CIT_kwargs["dir_path"], CIT_kwargs["site_file"]) if program_ran: update_data = True update_lines[-1] = time() new_inp_file += reduce(lambda x, y: str(x) + "\t" + str(y), update_lines) + "\n" magic_files["measurements"].append(measp) magic_files["specimens"].append(specp) magic_files["samples"].append(sampp) magic_files["sites"].append(sitep) else: new_inp_file += update_file if os.path.isfile(measp) and os.path.isfile(specp) and os.path.isfile(sampp) and os.path.isfile(sitep): magic_files["measurements"].append(measp) magic_files["specimens"].append(specp) magic_files["samples"].append(sampp) magic_files["sites"].append(sitep) inp_file.close() out_file = open(inp_file_name, "w") out_file.write(new_inp_file) return update_data
def test_cit_with_no_files(self): program_ran, error_message = cit_magic.main(False) self.assertFalse(program_ran) self.assertEqual(error_message, 'bad sam file name')