Answers.Body: <p>This is not a bioinformatics question, but a basic pandas question, so SO is a better place for this... But your odd looking VCF file can be parsed as follows without even using regex:</p>
  
  <pre><code>import pandas as pd
  
  def splitter(cell, key):
      parts = cell.split('||')
      if key in parts[0]:
           p = parts[0].split('|')
      else:
           p = parts[1].split('|')
      sp = [x.strip() for x in p if key not in x]
      return float(sp[0])
  
  def read():
      splitter_a = lambda v: v.split('||')[0].split('|')[1]) 
      varian=pd.read_csv('file.txt',sep = "\t")
      a_splitter = lambda c: splitter(c, 'a')
      b_splitter = lambda c: splitter(c, 'b')
      varian = varian.assign(father_a=varian.FATHER.apply(a_splitter)\
                .assign(father_a=varian.FATHER.apply(b_splitter)\
                .assign(mother_a=varian.MOTHER.apply(la_splitter)\
  </code></pre>
  
  <p>etc.</p>
  
  <p><code>display(varian)</code> or <code>print(varian.columns)</code> will show that you have new columns called <code>father_a</code> etc.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-16T13:24:13.053Z
Answers.Text: pandas regex might still help, but re isn't needed. The concept might work. ... It's a pandas solution for sure. I like the style. I'd construct a mask and then filter the columns. They are wanting to exclude homozygotes
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-16T13:27:29.900Z
Answers.Text: The immediate issue is the separator can be '/' as well
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-15T19:00:44.843Z
Answers.LastActivityDate: 2019-10-15T19:20:09.827Z
Answers.LastEditDate: 2019-10-15T19:20:09.827Z
Answers.UserDisplayName: Matteo Ferla
Answers.Body: <p>Its easy. You want to remove homozygotes and here is the solution for the FATHER class. Note the MOTHER class will comprise homozygotes in the answer because you are only searching for FATHER. To perform an intersection one approach is just to concatenate the mask.</p>
  
  <pre><code>import pandas as pd
  
  genedict = {\
              'FATHER': ['1/0', '1/1', '0/0', '1/0', '0/1'], \
              'MOTHER': ['1/1', '1/1', '1/0', '1/0', '0/0']}
  
  df = pd.DataFrame(genedict)    
  mask = df.FATHER.str.contains(r'1[|/]0') + df.FATHER.str.contains(r'0[|/]1')
  df[mask]
  </code></pre>
  
  <p><strong>OUTPUT</strong></p>
  
  <pre><code>  FATHER MOTHER
  0    1/0    1/1
  3    1/0    1/0
  4    0/1    0/0
  </code></pre>
  
  <p>Note, this is not your original dataset, its a dictionary to demonstrate the principle and note all your other columns will carry across unless you want to 'drop' them. Finally, pandas has its own regex system, regardless the traditional r'' code is still needed</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-10-18T07:16:37.620Z
Answers.LastActivityDate: 2019-10-18T08:11:39.677Z
Answers.LastEditDate: 2019-10-18T08:11:39.677Z
Answers.UserDisplayName: M__
Body: <p>I have read the content of a text file into pandas and needed some help matching the pattern.</p>
  
  <p>Here is the pattern, where <code>a</code> can any number greater than zero and <code>b</code> can be zero or any number greater than zero:</p>
  
  <pre><code>father=a|0 || 0|a
  mother=a|0 || 0|a
  daughter1=b|0 || 0|b
  daughter2=a|0 || 0|a
  daughter3=b|0 || 0|b
  son1=a|0 || 0|a
  son2=b|0 || 0|b
  </code></pre>
  
  <p>Here is the function that reads the content of text file to pandas</p>
  
  <pre><code>import pandas as pd
  def read():
      varian=pd.read_csv('file.txt',sep = "\t")
  
  father=varian["FATHER"]
  mother=varian["MOTHER"]
  daughter1=varian["DAUGHTER1"]
  daughter2=varian["DAUGHTER2"]
  daughter3=varian["DAUGHTER3"]
  son1=varian["SON1"]
  son2=varian["SON2"]
  </code></pre>
  
  <p>The content of the file looks like this</p>
  
  <pre><code>HROM    POS REF ALT FATHER  MOTHER  DAUGHTER1   DAUGHTER2   DAUGHTER3   SON1    SON2    INFO    FREQUECY
  1   1226852 G   C   1/0 0/0 1|0 1|0 1|0 1|0 0|0 AN=2184;AC=12   0.005494505
  1   1847936 C   T   0/1 1/1 1|1 1|1 1|1 1|1 0|1 AC=1;AN=2184    0.000457875
  1   2428427 C   G   0/1 0/0 0|0 0|0 0|0 0|0 1|0 AC=4;AN=2184    0.001831502
  1   2515616 G   A   1/0 1/0 1/0 1|1 1|1 1/0 1/0 AC=1;AN=2184    0.000457875
  1   3801895 T   C   1/0 1/0 0|0 1/0 1/0 0|0 1|1 AC=10;AN=2184   0.004578755
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-14T08:48:57.400Z
Comments.Text: Is that really what your file looks like? Doesn't the first line start with `#CHROM`? Does it really start with `HROM`? Also, please look at our [formatting tools](https://bioinformatics.stackexchange.com/help/formatting) to see how to format your posts correctly and avoid confusion.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-18T07:19:58.217Z
Comments.Text: Sorry for the delay, busy week, done below
Comments.UserDisplayName: M__
Tags: <python>
Title: read and matching pattern with python
ThreadText: # read and matching pattern with python
  
  (Asked by: username_0 on 2019-10-13)
  
  I have read the content of a text file into pandas and needed some help matching the pattern.
  
  Here is the pattern, where `a` can any number greater than zero and `b` can be zero or any number greater than zero:
  
  ```
  father=a|0 || 0|a
  mother=a|0 || 0|a
  daughter1=b|0 || 0|b
  daughter2=a|0 || 0|a
  daughter3=b|0 || 0|b
  son1=a|0 || 0|a
  son2=b|0 || 0|b
  
  ```
  
  Here is the function that reads the content of text file to pandas
  
  ```
  import pandas as pd
  def read():
      varian=pd.read_csv('file.txt',sep = "\t")
  
  father=varian["FATHER"]
  mother=varian["MOTHER"]
  daughter1=varian["DAUGHTER1"]
  daughter2=varian["DAUGHTER2"]
  daughter3=varian["DAUGHTER3"]
  son1=varian["SON1"]
  son2=varian["SON2"]
  
  ```
  
  The content of the file looks like this
  
  ```
  HROM    POS REF ALT FATHER  MOTHER  DAUGHTER1   DAUGHTER2   DAUGHTER3   SON1    SON2    INFO    FREQUECY
  1   1226852 G   C   1/0 0/0 1|0 1|0 1|0 1|0 0|0 AN=2184;AC=12   0.005494505
  1   1847936 C   T   0/1 1/1 1|1 1|1 1|1 1|1 0|1 AC=1;AN=2184    0.000457875
  1   2428427 C   G   0/1 0/0 0|0 0|0 0|0 0|0 1|0 AC=4;AN=2184    0.001831502
  1   2515616 G   A   1/0 1/0 1/0 1|1 1|1 1/0 1/0 AC=1;AN=2184    0.000457875
  1   3801895 T   C   1/0 1/0 0|0 1/0 1/0 0|0 1|1 AC=10;AN=2184   0.004578755
  
  ```
  
  **Tags:** python
  
  > **Comments:**
  > **username_1**: Is that really what your file looks like? Doesn't the first line start with `#CHROM`? Does it really start with `HROM`? Also, please look at our [formatting tools](https://bioinformatics.stackexchange.com/help/formatting) to see how to format your posts correctly and avoid confusion.  
  > **username_2**: Sorry for the delay, busy week, done below  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2019-10-15. Score: 1 | Accepted answer
  
  This is not a bioinformatics question, but a basic pandas question, so SO is a better place for this... But your odd looking VCF file can be parsed as follows without even using regex:
  
  ```
  import pandas as pd
  
  def splitter(cell, key):
      parts = cell.split('||')
      if key in parts[0]:
           p = parts[0].split('|')
      else:
           p = parts[1].split('|')
      sp = [x.strip() for x in p if key not in x]
      return float(sp[0])
  
  def read():
      splitter_a = lambda v: v.split('||')[0].split('|')[1]) 
      varian=pd.read_csv('file.txt',sep = "\t")
      a_splitter = lambda c: splitter(c, 'a')
      b_splitter = lambda c: splitter(c, 'b')
      varian = varian.assign(father_a=varian.FATHER.apply(a_splitter)\
                .assign(father_a=varian.FATHER.apply(b_splitter)\
                .assign(mother_a=varian.MOTHER.apply(la_splitter)\
  
  ```
  
  etc.
  
  `display(varian)` or `print(varian.columns)` will show that you have new columns called `father_a` etc.
  
  > **Comments:**
  > **username_2**: pandas regex might still help, but re isn't needed. The concept might work. ... It's a pandas solution for sure. I like the style. I'd construct a mask and then filter the columns. They are wanting to exclude homozygotes  
  > **username_2**: The immediate issue is the separator can be '/' as well  
  
  ---
  
  ### Answer by username_2 on 2019-10-18. Score: 1
  
  Its easy. You want to remove homozygotes and here is the solution for the FATHER class. Note the MOTHER class will comprise homozygotes in the answer because you are only searching for FATHER. To perform an intersection one approach is just to concatenate the mask.
  
  ```
  import pandas as pd
  
  genedict = {\
              'FATHER': ['1/0', '1/1', '0/0', '1/0', '0/1'], \
              'MOTHER': ['1/1', '1/1', '1/0', '1/0', '0/0']}
  
  df = pd.DataFrame(genedict)    
  mask = df.FATHER.str.contains(r'1[|/]0') + df.FATHER.str.contains(r'0[|/]1')
  df[mask]
  
  ```
  
  **OUTPUT**
  
  ```
    FATHER MOTHER
  0    1/0    1/1
  3    1/0    1/0
  4    0/1    0/0
  
  ```
  
  Note, this is not your original dataset, its a dictionary to demonstrate the principle and note all your other columns will carry across unless you want to 'drop' them. Finally, pandas has its own regex system, regardless the traditional r'' code is still needed
  
  
  ---
  
  