def add_aminoacids(rows: pd.Series, seqs: dict):
cpos = rows["codon_pos"]
rpos = rows["relative_pos"]
gene = rows["Gene"]
ref = rows["ref"]
alt = rows["alt"]
try:
nt = seqs[gene][rpos] # locating ref within gene seq
except:
# print(rows)
return None, None
if ref == nt: # confirming we are OK
if cpos == 0:
ref_aa = Seq(nt + seqs[gene][rpos+1] + seqs[gene][rpos+2])
alt_aa = Seq(alt + seqs[gene][rpos+1] + seqs[gene][rpos+2])
elif cpos == 1:
ref_aa = Seq(seqs[gene][rpos-1] + nt + seqs[gene][rpos+1])
alt_aa = Seq(seqs[gene][rpos-1] + alt + seqs[gene][rpos+1])
elif cpos == 2:
ref_aa = Seq(seqs[gene][rpos-2] + seqs[gene][rpos-1] + nt)
alt_aa = Seq(seqs[gene][rpos-2] + seqs[gene][rpos-1] + alt)
ref_trans = ref_aa.translate()
aa_trans = alt_aa.translate()
print(ref_aa, alt_aa, ref_trans, aa_trans)
return ref_aa, alt_aa, ref_trans, aa_trans
seqs = get_seqs_from_fasta()
rs[0][['ref_aa', 'alt_aa','ref_aa_trans','alt_aa_trans']] = rs[0].apply(lambda row: add_aminoacids(row, seqs), axis=1, result_type='expand')ValueError Traceback (most recent call last)
Cell In [76], line 2
1 seqs = get_seqs_from_fasta()
----> 2 rs[0][['ref_aa', 'alt_aa','ref_aa_trans','alt_aa_trans']] = rs[0].apply(lambda row: add_aminoacids(row, seqs), axis=1, result_type='expand')
File /usr/lib64/python3.11/site-packages/pandas/core/frame.py:8740, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs)
8729 from pandas.core.apply import frame_apply
8731 op = frame_apply(
8732 self,
8733 func=func,
(...)
8738 kwargs=kwargs,
8739 )
-> 8740 return op.apply()
File /usr/lib64/python3.11/site-packages/pandas/core/apply.py:688, in FrameApply.apply(self)
685 elif self.raw:
686 return self.apply_raw()
--> 688 return self.apply_standard()
File /usr/lib64/python3.11/site-packages/pandas/core/apply.py:815, in FrameApply.apply_standard(self)
812 results, res_index = self.apply_series_generator()
814 # wrap results
--> 815 return self.wrap_results(results, res_index)
File /usr/lib64/python3.11/site-packages/pandas/core/apply.py:841, in FrameApply.wrap_results(self, results, res_index)
839 # see if we can infer the results
840 if len(results) > 0 and 0 in results and is_sequence(results[0]):
--> 841 return self.wrap_results_for_axis(results, res_index)
843 # dict of scalars
844
845 # the default dtype of an empty Series will be object, but this
846 # code can be hit by df.mean() where the result should have dtype
847 # float64 even if it's an empty Series.
848 constructor_sliced = self.obj._constructor_sliced
File /usr/lib64/python3.11/site-packages/pandas/core/apply.py:979, in FrameColumnApply.wrap_results_for_axis(self, results, res_index)
977 # we have requested to expand
978 if self.result_type == "expand":
--> 979 result = self.infer_to_same_shape(results, res_index)
981 # we have a non-series and don't want inference
982 elif not isinstance(results[0], ABCSeries):
File /usr/lib64/python3.11/site-packages/pandas/core/apply.py:994, in FrameColumnApply.infer_to_same_shape(self, results, res_index)
992 def infer_to_same_shape(self, results: ResType, res_index: Index) -> DataFrame:
993 """infer the results to the same shape as the input object"""
--> 994 result = self.obj._constructor(data=results)
995 result = result.T
997 # set the index
File /usr/lib64/python3.11/site-packages/pandas/core/frame.py:614, in DataFrame.init(self, data, index, columns, dtype, copy)
608 mgr = self._init_mgr(
609 data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
610 )
612 elif isinstance(data, dict):
613 # GH#38939 de facto copy defaults to False only in non-dict cases
--> 614 mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
615 elif isinstance(data, ma.MaskedArray):
616 import numpy.ma.mrecords as mrecords
File /usr/lib64/python3.11/site-packages/pandas/core/internals/construction.py:464, in dict_to_mgr(data, index, columns, dtype, typ, copy)
456 arrays = [
457 x
458 if not hasattr(x, "dtype") or not isinstance(x.dtype, ExtensionDtype)
459 else x.copy()
460 for x in arrays
461 ]
462 # TODO: can we get rid of the dt64tz special case above?
--> 464 return arrays_to_mgr(
465 arrays, data_names, index, columns, dtype=dtype, typ=typ, consolidate=copy
466 )
File /usr/lib64/python3.11/site-packages/pandas/core/internals/construction.py:119, in arrays_to_mgr(arrays, arr_names, index, columns, dtype, verify_integrity, typ, consolidate)
116 if verify_integrity:
117 # figure out the index, if necessary
118 if index is None:
--> 119 index = _extract_index(arrays)
120 else:
121 index = ensure_index(index)
File /usr/lib64/python3.11/site-packages/pandas/core/internals/construction.py:635, in _extract_index(data)
633 lengths = list(set(raw_lengths))
634 if len(lengths) > 1:
--> 635 raise ValueError("All arrays must be of the same length")
637 if have_dicts:
638 raise ValueError(
639 "Mixing dicts with non-Series may lead to ambiguous ordering."
640 )
ValueError: All arrays must be of the same length